From 5ae3e292ca9beddd8a13c3dffe8751839d23e09b Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Fri, 23 Feb 2007 14:15:51 +0100 Subject: [PATCH] create buddy when connecting --- services/presence2/buddy.py | 26 ++++++++++------- services/presence2/presenceservice.py | 42 ++++++++++++++++++--------- services/presence2/telepathyclient.py | 22 ++++++++++---- 3 files changed, 60 insertions(+), 30 deletions(-) diff --git a/services/presence2/buddy.py b/services/presence2/buddy.py index 984bf949..035a1303 100644 --- a/services/presence2/buddy.py +++ b/services/presence2/buddy.py @@ -155,6 +155,19 @@ class Buddy(dbus.service.Object): self._icon = icon self.IconChanged(icon) + def _set_name(self, name): + self._nick_name = name + + def _set_color(self, color): + self._color = color + + def set_properties(self, prop): + if "name" in properties.keys(): + self._set_name(properties["name"]) + if "color" in properties.keys(): + self._set_color(properties["color"]) + self.PropertyChanged(properties) + def is_owner(self): return False @@ -180,12 +193,8 @@ class Owner(Buddy): @dbus.service.method(_OWNER_INTERFACE, in_signature="a{sv}", out_signature="") - def SetProperties(self, properties): - if "name" in properties.keys(): - self.set_name(properties["name"]) - if "color" in properties.keys(): - self.set_color(properties["color"]) - self.PropertyChanged(properties) + def SetProperties(self, prop): + self.set_properties(self, prop) # methods def is_owner(self): @@ -194,8 +203,3 @@ class Owner(Buddy): def set_icon(self, icon): self._icon = icon - def set_name(self, name): - self._nick_name = name - - def set_color(self, color): - self._color = color diff --git a/services/presence2/presenceservice.py b/services/presence2/presenceservice.py index 659c389f..fffb7c52 100644 --- a/services/presence2/presenceservice.py +++ b/services/presence2/presenceservice.py @@ -42,6 +42,7 @@ class PresenceService(dbus.service.Object): self._next_object_id = 0 self._buddies = {} # key -> Buddy + self._buddies_handle = {} # tp handle -> Buddy self._activities = {} # activity id -> Activity self._icon_cache = buddyiconcache.BuddyIconCache() @@ -56,21 +57,8 @@ class PresenceService(dbus.service.Object): self._registry = ManagerRegistry() self._registry.LoadManagers() + # Telepathy connection to the server - self._server_client = None - # Telepathy link local connection - self._ll_client = None - - self._connect_server () - - dbus.service.Object.__init__(self, self._bus_name, _PRESENCE_PATH) - - def _get_next_object_id(self): - """Increment and return the object ID counter.""" - self._next_object_id = self._next_object_id + 1 - return self._next_object_id - - def _connect_server (self): mgr = self._registry.GetManager('gabble') protocol = 'jabber' account = { @@ -83,6 +71,32 @@ class PresenceService(dbus.service.Object): conn = Connection(conn_bus_name, conn_object_path) self._server_client = telepathyclient.TelepathyClient(conn) + # Telepathy link local connection + self._ll_client = None + + self._server_client.connect('contact-appeared', self._contact_appeared) + self._server_client.run() + + dbus.service.Object.__init__(self, self._bus_name, _PRESENCE_PATH) + + def _contact_appeared(self, tp, handle, key): + if self._buddies.has_key(key): + # We already know this buddy + return + + objid = self._get_next_object_id() + new_buddy = buddy.Buddy(self._bus_name, objid, self._icon_cache) + self._buddies[key] = new_buddy + self._buddies_handle[handle] = new_buddy + + self.BuddyAppeared(new_buddy.object_path()) + + + def _get_next_object_id(self): + """Increment and return the object ID counter.""" + self._next_object_id = self._next_object_id + 1 + return self._next_object_id + @dbus.service.signal(_PRESENCE_INTERFACE, signature="o") def ActivityAppeared(self, activity): pass diff --git a/services/presence2/telepathyclient.py b/services/presence2/telepathyclient.py index 8a7a71de..4ade31e5 100644 --- a/services/presence2/telepathyclient.py +++ b/services/presence2/telepathyclient.py @@ -15,14 +15,19 @@ loop = None import buddy -class TelepathyClient: +class TelepathyClient(gobject.GObject): + __gsignals__ = { + 'contact-appeared':(gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, + ([gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT])), + } + def __init__(self, conn): + gobject.GObject.__init__(self) + conn[CONN_INTERFACE].connect_to_signal('StatusChanged', self._status_changed_cb) - conn[CONN_INTERFACE].Connect() self.conn = conn - self.buddies = {} def _request_list_channel(self, name): handle = self.conn[CONN_INTERFACE].RequestHandles( @@ -59,8 +64,8 @@ class TelepathyClient: # hack self.conn._valid_interfaces.add(CONN_INTERFACE_ALIASING) - #for handle in subscribe_handles: - # self.buddies[handle] = buddy.Buddy() + for handle in subscribe_handles: + self._contact_appeared(handle); if CONN_INTERFACE_ALIASING in self.conn: aliases = self.conn[CONN_INTERFACE_ALIASING].RequestAliases(subscribe_handles) @@ -104,9 +109,16 @@ class TelepathyClient: print 'disconnected' loop.quit() + def run(self): + self.conn[CONN_INTERFACE].Connect() + def disconnect(self): self.conn[CONN_INTERFACE].Disconnect() + def _contact_appeared(self, handle): + key = "1111111" + self.emit("contact-appeared", handle, key) + if __name__ == '__main__': import logging logging.basicConfig()