From 0a305004b08cab01c1ae42a069d77b1517491f99 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Thu, 22 Jun 2006 15:59:38 -0400 Subject: [PATCH] Some work getting the one-to-one chat back --- activities/chat/ChatActivity.py | 84 +++++++++++++++++++++++++++++++ activities/chat/chat.activity | 2 + shell/PresenceWindow.py | 17 ++++--- sugar/chat/BuddyChat.py | 15 ++++++ sugar/presence/Buddy.py | 1 + sugar/presence/PresenceService.py | 7 +-- 6 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 activities/chat/ChatActivity.py create mode 100644 activities/chat/chat.activity create mode 100644 sugar/chat/BuddyChat.py diff --git a/activities/chat/ChatActivity.py b/activities/chat/ChatActivity.py new file mode 100644 index 00000000..cb3ada89 --- /dev/null +++ b/activities/chat/ChatActivity.py @@ -0,0 +1,84 @@ +import dbus +import random + +import pygtk +pygtk.require('2.0') +import gtk + +from sugar.activity.Activity import Activity +from sugar.LogWriter import LogWriter +from sugar.presence.Service import Service +from sugar.chat.BuddyChat import BuddyChat +from sugar.p2p.Stream import Stream +from sugar.presence.PresenceService import PresenceService +import sugar.env + +_CHAT_ACTIVITY_TYPE = "_chat_activity_type._tcp" + +class ChatActivity(Activity): + def __init__(self, service): + Activity.__init__(self, _GMAIL_ACTIVITY_TYPE) + self._service = service + + def on_connected_to_shell(self): + self.set_can_close(True) + self.set_tab_icon(icon_name="im") + self.set_show_tab_icon(True) + + plug = self.gtk_plug() + + chat = BuddyChat(self._service) + plug.add(chat) + chat.show() + + plug.show() + +class ChatShellDbusService(dbus.service.Object): + def __init__(self, parent): + self._parent = parent + session_bus = dbus.SessionBus() + bus_name = dbus.service.BusName('com.redhat.Sugar.Chat', bus=session_bus) + object_path = '/com/redhat/Sugar/Chat' + dbus.service.Object.__init__(self, bus_name, object_path) + + @dbus.service.method('com.redhat.Sugar.ChatShell') + def open_chat(self, message): + self._parent.send_text_message(message) + +class ChatShell: + instance = None + + def get_instance(): + if not ChatShell.instance: + ChatShell.instance = ChatShell() + return ChatShell.instance + get_instance = staticmethod(get_instance) + + def __init__(self): + self._pservice = PresenceService.get_instance() + self._pservice.start() + self._pservice.track_service_type(BuddyChat.SERVICE_TYPE) + + def start(self): + port = random.randint(5000, 65535) + service = Service(sugar.env.get_nick_name(), BuddyChat.SERVICE_TYPE, + 'local', '', port) + self._buddy_stream = Stream.new_from_service(service) + self._buddy_stream.set_data_listener(getattr(self, "_recv_message")) + self._pservice.register_service(service) + + def _recv_message(self, address, msg): + print msg + + def open_chat(self, serialized_service): + service = Service.deserialize(serialized_service) + self._chat = ChatActivity(service) + self._chat.connect_to_shell() + +log_writer = LogWriter("Chat") +log_writer.start() + +chat_shell = ChatShell.get_instance() +chat_shell.start() + +gtk.main() diff --git a/activities/chat/chat.activity b/activities/chat/chat.activity new file mode 100644 index 00000000..0910654f --- /dev/null +++ b/activities/chat/chat.activity @@ -0,0 +1,2 @@ +[Activity] +python_class = ChatActivity diff --git a/shell/PresenceWindow.py b/shell/PresenceWindow.py index c9ec5585..6b529e66 100644 --- a/shell/PresenceWindow.py +++ b/shell/PresenceWindow.py @@ -4,6 +4,7 @@ import gtk import gobject from sugar.presence.PresenceService import PresenceService +from sugar.chat.BuddyChat import BuddyChat class PresenceWindow(gtk.Window): _MODEL_COL_NICK = 0 @@ -20,7 +21,6 @@ class PresenceWindow(gtk.Window): self._pservice = PresenceService.get_instance() self._pservice.connect("buddy-appeared", self._on_buddy_appeared_cb) self._pservice.connect("buddy-disappeared", self._on_buddy_disappeared_cb) - self._pservice.set_debug(True) self._pservice.start() self._setup_ui() @@ -110,11 +110,16 @@ class PresenceWindow(gtk.Window): (model, aniter) = view.get_selection().get_selected() chat = None buddy = view.get_model().get_value(aniter, self._MODEL_COL_BUDDY) - if buddy and not self._chats.has_key(buddy): - #chat = BuddyChat(self, buddy) - #self._chats[buddy] = chat - #chat.connect_to_shell() - pass + if buddy: + chat_service = buddy.get_service_of_type(BuddyChat.SERVICE_TYPE) + if chat_service: + bus = dbus.SessionBus() + proxy_obj = bus.get_object('com.redhat.Sugar.Chat', '/com/redhat/Sugar/Chat') + chat_shell = dbus.Interface(proxy_obj, 'com.redhat.Sugar.ChatShell') + serialized_service = Service.serialize(chat_service) + chat_shell.open_chat(serialized_service) + else: + print 'Could not find buddy chat' def __buddy_icon_changed_cb(self, buddy): it = self._get_iter_for_buddy(buddy) diff --git a/sugar/chat/BuddyChat.py b/sugar/chat/BuddyChat.py new file mode 100644 index 00000000..6d4633e5 --- /dev/null +++ b/sugar/chat/BuddyChat.py @@ -0,0 +1,15 @@ +from sugar.activity.Activity import Activity + +class BuddyChat(Activity): + SERVICE_TYPE = "_olpc_buddy_chat._tcp" + + def __init__(self, service): + Chat.__init__(self) + + self._stream = Stream.new_from_service(service) + self._stream.set_data_listener(self._recv_message) + self._stream_writer = self._group_stream.new_writer() + + def recv_message(self, address, msg): + print msg +# Chat.recv_message(self, self._buddy, msg) diff --git a/sugar/presence/Buddy.py b/sugar/presence/Buddy.py index 65405e88..c3dfae4d 100644 --- a/sugar/presence/Buddy.py +++ b/sugar/presence/Buddy.py @@ -149,6 +149,7 @@ class Buddy(gobject.GObject): for service in self._services.values(): if service.get_type() == short_stype and service.get_activity_uid() == uid: return service + print self._services.keys() if self._services.has_key(short_stype): return self._services[short_stype] return None diff --git a/sugar/presence/PresenceService.py b/sugar/presence/PresenceService.py index 276bd69c..6db6e805 100644 --- a/sugar/presence/PresenceService.py +++ b/sugar/presence/PresenceService.py @@ -90,10 +90,9 @@ class PresenceService(gobject.GObject): return PresenceService.__instance get_instance = staticmethod(get_instance) - def __init__(self, debug=True): + def __init__(self): gobject.GObject.__init__(self) - self._debug = debug self._lock = threading.Lock() self._started = False @@ -163,9 +162,6 @@ class PresenceService(gobject.GObject): db = dbus.Interface(self._bus.get_object(avahi.DBUS_NAME, domain_browser), avahi.DBUS_INTERFACE_DOMAIN_BROWSER) db.connect_to_signal('ItemNew', self._new_domain_cb_glue) - def set_debug(self, debug): - self._debug = debug - def get_owner(self): """Return the owner of this machine/instance, if we've recognized them yet.""" return self._owner @@ -648,7 +644,6 @@ def main(): import pygtk, gtk global ps ps = PresenceService.get_instance() - ps.set_debug(True) ps.start() gobject.timeout_add(4000, runTests) gtk.main()