Attempting to fix a bug in code I stole from python-telepathy in which they deviated...
authorEd Page <eopage@byu.net>
Thu, 14 Jan 2010 02:15:17 +0000 (20:15 -0600)
committerEd Page <eopage@byu.net>
Thu, 14 Jan 2010 02:15:17 +0000 (20:15 -0600)
src/connection.py
src/handle.py
src/requests.py

index 48aaffa..7ff7675 100644 (file)
@@ -127,6 +127,22 @@ class TheOneRingConnection(
                self.check_handle(handleType, handleId)
                return self._handles[handleType, handleId]
 
+       def handle_by_name(self, handleType, handleName):
+               requestedHandleName = handleName.encode('utf-8')
+               if handleType == telepathy.HANDLE_TYPE_CONTACT:
+                       _moduleLogger.info("RequestHandles Contact: %s" % requestedHandleName)
+                       requestedContactId, requestedContactNumber = handle.ContactHandle.from_handle_name(
+                               requestedHandleName
+                       )
+                       h = handle.create_handle(self, 'contact', requestedContactId, requestedContactNumber)
+               elif handleType == telepathy.HANDLE_TYPE_LIST:
+                       # Support only server side (immutable) lists
+                       _moduleLogger.info("RequestHandles List: %s" % requestedHandleName)
+                       h = handle.create_handle(self, 'list', requestedHandleName)
+               else:
+                       raise telepathy.errors.NotAvailable('Handle type unsupported %d' % handleType)
+               return h
+
        @property
        def _channel_manager(self):
                return self.__channelManager
@@ -185,7 +201,6 @@ class TheOneRingConnection(
        def Disconnect(self):
                """
                For org.freedesktop.telepathy.Connection
-               @bug Not properly logging out.  Cookie files need to be per connection and removed
                """
                self.StatusChanged(
                        telepathy.CONNECTION_STATUS_DISCONNECTED,
@@ -231,20 +246,8 @@ class TheOneRingConnection(
 
                handles = []
                for name in names:
-                       requestedHandleName = name.encode('utf-8')
-                       if handleType == telepathy.HANDLE_TYPE_CONTACT:
-                               _moduleLogger.info("RequestHandles Contact: %s" % requestedHandleName)
-                               requestedContactId, requestedContactNumber = handle.ContactHandle.from_handle_name(
-                                       requestedHandleName
-                               )
-                               h = handle.create_handle(self, 'contact', requestedContactId, requestedContactNumber)
-                       elif handleType == telepathy.HANDLE_TYPE_LIST:
-                               # Support only server side (immutable) lists
-                               _moduleLogger.info("RequestHandles List: %s" % requestedHandleName)
-                               h = handle.create_handle(self, 'list', requestedHandleName)
-                       else:
-                               raise telepathy.errors.NotAvailable('Handle type unsupported %d' % handleType)
-                       handles.append(h.id)
+                       h = self.handle_by_name(handleType, name)
+                       handles.append(h)
                        self.add_client_handle(h, sender)
                return handles
 
index 9c34ca5..d063a77 100644 (file)
@@ -23,6 +23,9 @@ class TheOneRingHandle(telepathy.server.Handle):
                        type(self).__name__, self.id, self.name
                )
 
+       def is_same(self, handleType, handleName):
+               return self.get_name() == handleName and self.get_type() == handleType
+
        id = property(telepathy.server.Handle.get_id)
        type = property(telepathy.server.Handle.get_type)
        name = property(telepathy.server.Handle.get_name)
@@ -66,6 +69,20 @@ class ContactHandle(TheOneRingHandle):
                handleName = "#".join((contactId, util_misc.strip_number(contactNumber)))
                return handleName
 
+       @classmethod
+       def normalize_handle_name(cls, name):
+               if "#" in name:
+                       # Already a properly formatted name, run through the ringer just in case
+                       return cls.to_handle_name(*cls.from_handle_name(name))
+                       return name
+               else:
+                       return cls.to_handle_name("", name)
+
+       def is_same(self, handleType, handleName):
+               handleName = self.normalize_handle_name(handleName)
+               _moduleLogger.info("%r == %r %r?" % (self, handleType, handleName))
+               return self.get_name() == handleName and self.get_type() == handleType
+
        @property
        def contactID(self):
                return self._contactId
index 95ba7d1..51da1f2 100644 (file)
@@ -34,6 +34,13 @@ class RequestsMixin(
                """
                raise NotImplementedError("Abstract property called")
 
+       @property
+       def handle_by_name(self, handleType, handleName):
+               """
+               @abstract
+               """
+               raise NotImplementedError("Abstract property called")
+
        def _get_channels(self):
                return [(c._object_path, c.get_props()) for c in self._channels]
 
@@ -114,13 +121,7 @@ class RequestsMixin(
                if target_handle_type != telepathy.constants.HANDLE_TYPE_NONE:
                        if target_handle == None:
                                # Turn TargetID into TargetHandle.
-                               for handle in self._handles.itervalues():
-                                       if handle.get_name() == target_id and handle.get_type() == target_handle_type:
-                                               target_handle = handle.get_id()
-                               if not target_handle:
-                                       raise telepathy.errors.InvalidHandle('TargetID %s not valid for type %d' %
-                                               (target_id, target_handle_type))
-
+                               target_handle = self.handle_by_name(target_handle_type, target_id)
                                altered_properties[telepathy.interfaces.CHANNEL_INTERFACE + '.TargetHandle'] = \
                                        target_handle
                        else: