diff --git a/services/presence2/presenceservice.py b/services/presence2/presenceservice.py index af709695..109fd0fd 100644 --- a/services/presence2/presenceservice.py +++ b/services/presence2/presenceservice.py @@ -17,7 +17,9 @@ import gobject import dbus, dbus.service, dbus.glib from telepathy.client import ManagerRegistry, Connection -from telepathy.interfaces import CONN_MGR_INTERFACE +from telepathy.interfaces import (CONN_MGR_INTERFACE, CONN_INTERFACE) +from telepathy.constants import (CONNECTION_STATUS_CONNECTING, CONNECTION_STATUS_CONNECTED, + CONNECTION_STATUS_DISCONNECTED, CONNECTION_HANDLE_TYPE_CONTACT) import telepathyclient from buddy import Buddy, Owner @@ -58,19 +60,7 @@ class PresenceService(dbus.service.Object): self._registry = ManagerRegistry() self._registry.LoadManagers() - # Telepathy connection to the server - mgr = self._registry.GetManager('gabble') - protocol = 'jabber' - account = { - 'account': 'olpc@collabora.co.uk', - 'password': 'learn', - 'server': 'light.bluelinux.co.uk' - } - conn_bus_name, conn_object_path = \ - mgr[CONN_MGR_INTERFACE].RequestConnection(protocol, account) - conn = Connection(conn_bus_name, conn_object_path) - self._server_client = telepathyclient.TelepathyClient(conn) - + self._server_client = self._connect_to_server() self._handles[self._server_client] = {} # Telepathy link local connection @@ -82,6 +72,38 @@ class PresenceService(dbus.service.Object): dbus.service.Object.__init__(self, self._bus_name, _PRESENCE_PATH) + def _connect_to_server(self): + protocol = 'jabber' + account = { + 'account': 'olpc@collabora.co.uk', + 'password': 'learn', + 'server': 'light.bluelinux.co.uk' + } + + mgr = self._registry.GetManager('gabble') + conn = None + + # Search existing connections, if any, that we might be able to use + connections = Connection.get_connections() + for item in connections: + if item[CONN_INTERFACE].GetProtocol() != protocol: + continue + if not item.object_path.startswith("/org/freedesktop/Telepathy/Connection/gabble/jabber/"): + continue + if item[CONN_INTERFACE].GetStatus() == CONNECTION_STATUS_CONNECTED: + self_name = account['account'] + test_handle = item[CONN_INTERFACE].RequestHandles(CONNECTION_HANDLE_TYPE_CONTACT, [self_name])[0] + if item[CONN_INTERFACE].GetSelfHandle() != test_handle: + continue + conn = item + + if not conn: + conn_bus_name, conn_object_path = \ + mgr[CONN_MGR_INTERFACE].RequestConnection(protocol, account) + conn = Connection(conn_bus_name, conn_object_path) + + return telepathyclient.TelepathyClient(conn) + def _contact_online(self, tp, handle, key): buddy = self._buddies.get(key)