X-Git-Url: http://git.maemo.org/git/?p=theonering;a=blobdiff_plain;f=src%2Fconnection.py;h=2fd3632d153acaaadf2160cf8cb0937b8e438c88;hp=2dce28abd85d4a845a2e0442a7a3f30af2aa8522;hb=b5c08bc1a4affc251daae5caf943589b61abd3f6;hpb=ab4da214c520d763bbbddeb828c8e7c838374fe3 diff --git a/src/connection.py b/src/connection.py index 2dce28a..2fd3632 100644 --- a/src/connection.py +++ b/src/connection.py @@ -4,16 +4,25 @@ import logging import telepathy import constants -import gv_backend +import gvoice import handle import channel_manager +import simple_presence -class TheOneRingConnection(telepathy.server.Connection): +_moduleLogger = logging.getLogger("connection") - _mandatory_parameters = { + +class TheOneRingConnection(telepathy.server.Connection, simple_presence.SimplePresenceMixin): + + MANDATORY_PARAMETERS = { 'account' : 's', - 'password' : 's' + 'password' : 's', + 'forward' : 's', + } + OPTIONAL_PARAMETERS = { + } + PARAMETER_DEFAULTS = { } def __init__(self, manager, parameters): @@ -21,27 +30,30 @@ class TheOneRingConnection(telepathy.server.Connection): self.check_parameters(parameters) account = unicode(parameters['account']) - telepathy.server.Connection.__init__(self, 'gvoice', account, 'theonering') + telepathy.server.Connection.__init__( + self, + constants._telepathy_protocol_name_, + account, + constants._telepathy_implementation_name_ + ) self._manager = weakref.proxy(manager) self._credentials = ( 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._backend = gvoice.dialer.GVDialer(cookieFilePath) + self._addressbook = gvoice.addressbook.Addressbook(self._backend) - self.set_self_handle(handle.create_handle(self, 'self')) + self.set_self_handle(handle.create_handle(self, 'connection')) - self.__disconnect_reason = telepathy.CONNECTION_STATUS_REASON_NONE_SPECIFIED - self._initial_presence = None - self._initial_personal_message = None - - 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 @@ -53,6 +65,10 @@ class TheOneRingConnection(telepathy.server.Connection): return self._backend @property + def addressbook(self): + return self._addressbook + + @property def username(self): self._credentials[0] @@ -62,23 +78,49 @@ class TheOneRingConnection(telepathy.server.Connection): def Connect(self): """ - org.freedesktop.telepathy.Connection + For org.freedesktop.telepathy.Connection """ - logging.info("Connecting") - self.__disconnect_reason = telepathy.CONNECTION_STATUS_REASON_NONE_SPECIFIED - self._backend.login(*self._credentials) + self.StatusChanged( + telepathy.CONNECTION_STATUS_CONNECTING, + telepathy.CONNECTION_STATUS_REASON_REQUESTED + ) + try: + self._backend.login(*self._credentials) + self._backend.set_callback_number(self._callbackNumber) + except gvoice.dialer.NetworkError: + self.StatusChanged( + telepathy.CONNECTION_STATUS_DISCONNECTED, + telepathy.CONNECTION_STATUS_REASON_NETWORK_ERROR + ) + except Exception: + self.StatusChanged( + telepathy.CONNECTION_STATUS_DISCONNECTED, + telepathy.CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED + ) + else: + self.StatusChanged( + telepathy.CONNECTION_STATUS_CONNECTED, + telepathy.CONNECTION_STATUS_REASON_REQUESTED + ) def Disconnect(self): """ - org.freedesktop.telepathy.Connection + For org.freedesktop.telepathy.Connection """ - logging.info("Disconnecting") - self.__disconnect_reason = telepathy.CONNECTION_STATUS_REASON_REQUESTED - self._backend.logout() + try: + self._backend.logout() + _moduleLogger.info("Disconnected") + except Exception: + _moduleLogger.exception("Disconnecting Failed") + self.StatusChanged( + telepathy.CONNECTION_STATUS_DISCONNECTED, + telepathy.CONNECTION_STATUS_REASON_REQUESTED + ) def RequestChannel(self, type, handleType, handleId, suppressHandler): """ - org.freedesktop.telepathy.Connection + For org.freedesktop.telepathy.Connection + @param type DBus interface name for base channel type @param handleId represents a contact, list, etc according to handleType @@ -95,7 +137,10 @@ class TheOneRingConnection(telepathy.server.Connection): 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) @@ -104,10 +149,10 @@ class TheOneRingConnection(telepathy.server.Connection): def RequestHandles(self, handleType, names, sender): """ - org.freedesktop.telepathy.Connection + For org.freedesktop.telepathy.Connection """ self.check_connected() - self.check_handleType(handleType) + self.check_handle_type(handleType) handles = [] for name in names: @@ -115,9 +160,8 @@ class TheOneRingConnection(telepathy.server.Connection): 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) @@ -125,16 +169,18 @@ class TheOneRingConnection(telepathy.server.Connection): return handles def _create_contact_handle(self, name): - requestedContactId, requestedContactName = handle.field_split(name) + requestedContactId = name - contacts = self._backend.get_contacts() + contacts = self._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)