Updating to new connection style
[theonering] / src / connection.py
index 3b17095..6ff64d8 100644 (file)
@@ -4,17 +4,20 @@ import logging
 import telepathy
 
 import constants
-import gv_backend
+import gvoice
 import handle
 import channel_manager
-import simple_presence
 
 
-class TheOneRingConnection(telepathy.server.Connection, simple_presence.SimplePresenceMixin):
+_moduleLogger = logging.getLogger("connection")
+
+
+class TheOneRingConnection(telepathy.server.Connection):
 
        MANDATORY_PARAMETERS = {
                'account' : 's',
-               'password' : 's'
+               'password' : 's',
+               'forward' : 's',
        }
        OPTIONAL_PARAMETERS = {
        }
@@ -38,16 +41,17 @@ class TheOneRingConnection(telepathy.server.Connection, simple_presence.SimplePr
                                parameters['account'].encode('utf-8'),
                                parameters['password'].encode('utf-8'),
                        )
+                       self._callbackNumber = parameters['forward'].encode('utf-8')
                        self._channelManager = channel_manager.ChannelManager(self)
 
                        cookieFilePath = "%s/cookies.txt" % constants._data_path_
-                       self._backend = gv_backend.GVDialer(cookieFilePath)
+                       self._session = gvoice.session.Session(cookieFilePath)
 
                        self.set_self_handle(handle.create_handle(self, 'connection'))
 
-                       logging.info("Connection to the account %s created" % account)
+                       _moduleLogger.info("Connection to the account %s created" % account)
                except Exception, e:
-                       logging.exception("Failed to create Connection")
+                       _moduleLogger.exception("Failed to create Connection")
                        raise
 
        @property
@@ -55,8 +59,8 @@ class TheOneRingConnection(telepathy.server.Connection, simple_presence.SimplePr
                return self._manager
 
        @property
-       def gvoice_backend(self):
-               return self._backend
+       def session(self):
+               return self._session
 
        @property
        def username(self):
@@ -70,29 +74,42 @@ class TheOneRingConnection(telepathy.server.Connection, simple_presence.SimplePr
                """
                For org.freedesktop.telepathy.Connection
                """
+               self.StatusChanged(
+                       telepathy.CONNECTION_STATUS_CONNECTING,
+                       telepathy.CONNECTION_STATUS_REASON_REQUESTED
+               )
                try:
-                       self._backend.login(*self._credentials)
+                       self.session.login(*self._credentials)
+                       self.session.backend.set_callback_number(self._callbackNumber)
+               except gvoice.backend.NetworkError:
                        self.StatusChanged(
-                               telepathy.CONNECTION_STATUS_CONNECTED,
-                               telepathy.CONNECTION_STATUS_REASON_REQUESTED
+                               telepathy.CONNECTION_STATUS_DISCONNECTED,
+                               telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR
                        )
-                       logging.info("Connected")
                except Exception:
                        self.StatusChanged(
                                telepathy.CONNECTION_STATUS_DISCONNECTED,
                                telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
                        )
-                       logging.exception("Connecting Failed")
+               else:
+                       self.StatusChanged(
+                               telepathy.CONNECTION_STATUS_CONNECTED,
+                               telepathy.CONNECTION_STATUS_REASON_REQUESTED
+                       )
 
        def Disconnect(self):
                """
                For org.freedesktop.telepathy.Connection
                """
                try:
-                       self._backend.logout()
-                       logging.info("Disconnected")
+                       self.session.logout()
+                       _moduleLogger.info("Disconnected")
                except Exception:
-                       logging.exception("Disconnecting Failed")
+                       _moduleLogger.exception("Disconnecting Failed")
+               self.StatusChanged(
+                       telepathy.CONNECTION_STATUS_DISCONNECTED,
+                       telepathy.CONNECTION_STATUS_REASON_REQUESTED
+               )
 
        def RequestChannel(self, type, handleType, handleId, suppressHandler):
                """
@@ -114,7 +131,10 @@ class TheOneRingConnection(telepathy.server.Connection, simple_presence.SimplePr
                elif type == telepathy.CHANNEL_TYPE_TEXT:
                        if handleType != telepathy.HANDLE_TYPE_CONTACT:
                                raise telepathy.NotImplemented("Only Contacts are allowed")
-                       contact = handle.contact
+                       channel = channelManager.channel_for_text(handle, None, suppressHandler)
+               elif type == telepathy.CHANNEL_TYPE_STREAMED_MEDIA:
+                       if handleType != telepathy.HANDLE_TYPE_CONTACT:
+                               raise telepathy.NotImplemented("Only Contacts are allowed")
                        channel = channelManager.channel_for_text(handle, None, suppressHandler)
                else:
                        raise telepathy.NotImplemented("unknown channel type %s" % type)
@@ -126,7 +146,7 @@ class TheOneRingConnection(telepathy.server.Connection, simple_presence.SimplePr
                For org.freedesktop.telepathy.Connection
                """
                self.check_connected()
-               self.check_handleType(handleType)
+               self.check_handle_type(handleType)
 
                handles = []
                for name in names:
@@ -134,9 +154,8 @@ class TheOneRingConnection(telepathy.server.Connection, simple_presence.SimplePr
                        if handleType == telepathy.HANDLE_TYPE_CONTACT:
                                h = self._create_contact_handle(name)
                        elif handleType == telepathy.HANDLE_TYPE_LIST:
+                               # Support only server side (immutable) lists
                                h = handle.create_handle(self, 'list', name)
-                       elif handleType == telepathy.HANDLE_TYPE_GROUP:
-                               h = handle.create_handle(self, 'group', name)
                        else:
                                raise telepathy.NotAvailable('Handle type unsupported %d' % handleType)
                        handles.append(h.id)
@@ -144,16 +163,18 @@ class TheOneRingConnection(telepathy.server.Connection, simple_presence.SimplePr
                return handles
 
        def _create_contact_handle(self, name):
-               requestedContactId, requestedContactName = handle.field_split(name)
+               requestedContactId = name
 
-               contacts = self._backend.get_contacts()
+               contacts = self.session.addressbook.get_contacts()
                contactsFound = [
-                       (contactId, contactName) for (contactId, contactName) in contacts
-                       if contactName == name
+                       contactId for contactId in contacts
+                       if contactId == requestedContactId
                ]
 
                if 0 < len(contactsFound):
-                       contactId, contactName = contactsFound[0]
-                       h = handle.create_handle(self, 'contact', contactId, contactName)
+                       contactId = contactsFound[0]
+                       if len(contactsFound) != 1:
+                               _moduleLogger.error("Contact ID was not unique: %s for %s" % (contactId, ))
                else:
-                       h = handle.create_handle(self, 'contact', requestedContactId, requestedContactName)
+                       contactId = requestedContactId
+               h = handle.create_handle(self, 'contact', contactId)