When removing the async calls I ended up wiping information on a callback, so moving...
[theonering] / src / handle.py
index 057ddf3..9c34ca5 100644 (file)
@@ -3,21 +3,10 @@ import weakref
 
 import telepathy
 
+import util.misc as util_misc
 
-_moduleLogger = logging.getLogger("handle")
-
-
-class MetaMemoize(type):
-       """
-       Allows a class to cache off instances for reuse
-       """
 
-       def __call__(cls, connection, *args):
-               obj, newlyCreated = cls.__new__(cls, connection, *args)
-               if newlyCreated:
-                       obj.__init__(connection, connection.get_handle_id(), *args)
-                       _moduleLogger.info("New Handle %r" % obj)
-               return obj
+_moduleLogger = logging.getLogger("handle")
 
 
 class TheOneRingHandle(telepathy.server.Handle):
@@ -25,19 +14,6 @@ class TheOneRingHandle(telepathy.server.Handle):
        Instances are memoized
        """
 
-       __metaclass__ = MetaMemoize
-
-       _instances = weakref.WeakValueDictionary()
-
-       def __new__(cls, connection, *args):
-               key = cls, connection.username, args
-               if key in cls._instances.keys():
-                       return cls._instances[key], False
-               else:
-                       instance = object.__new__(cls, connection, *args)
-                       cls._instances[key] = instance # TRICKY: instances is a weakdict
-                       return instance, True
-
        def __init__(self, connection, id, handleType, name):
                telepathy.server.Handle.__init__(self, id, handleType, name)
                self._conn = weakref.proxy(connection)
@@ -54,8 +30,6 @@ class TheOneRingHandle(telepathy.server.Handle):
 
 class ConnectionHandle(TheOneRingHandle):
 
-       instance = None
-
        def __init__(self, connection, id):
                handleType = telepathy.HANDLE_TYPE_CONTACT
                handleName = connection.username
@@ -66,16 +40,39 @@ class ConnectionHandle(TheOneRingHandle):
 
 class ContactHandle(TheOneRingHandle):
 
-       def __init__(self, connection, id, contactId):
+       def __init__(self, connection, id, contactId, phoneNumber):
                handleType = telepathy.HANDLE_TYPE_CONTACT
-               handleName = contactId
+               handleName = self.to_handle_name(contactId, phoneNumber)
                TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
 
-               self._id = contactId
+               self._contactId = contactId
+               self._phoneNumber = util_misc.strip_number(phoneNumber)
+
+       @staticmethod
+       def from_handle_name(handleName):
+               parts = handleName.split("#", 1)
+               if len(parts) == 2:
+                       contactId, contactNumber = parts[0:2]
+               elif len(parts) == 1:
+                       contactId, contactNumber = "", handleName
+               else:
+                       raise RuntimeError("Invalid handle: %s" % handleName)
+
+               contactNumber = util_misc.strip_number(contactNumber)
+               return contactId, contactNumber
+
+       @staticmethod
+       def to_handle_name(contactId, contactNumber):
+               handleName = "#".join((contactId, util_misc.strip_number(contactNumber)))
+               return handleName
 
        @property
        def contactID(self):
-               return self._id
+               return self._contactId
+
+       @property
+       def phoneNumber(self):
+               return self._phoneNumber
 
        @property
        def contactDetails(self):
@@ -97,7 +94,28 @@ _HANDLE_TYPE_MAPPING = {
 }
 
 
-def create_handle(connection, type, *args):
-       handle = _HANDLE_TYPE_MAPPING[type](connection, *args)
-       connection._handles[handle.get_type(), handle.get_id()] = handle
-       return handle
+def create_handle_factory():
+
+       cache = weakref.WeakValueDictionary()
+
+       def create_handle(connection, type, *args):
+               Handle = _HANDLE_TYPE_MAPPING[type]
+               key = Handle, connection.username, args
+               try:
+                       handle = cache[key]
+                       isNewHandle = False
+               except KeyError:
+                       # The misnamed get_handle_id requests a new handle id
+                       handle = Handle(connection, connection.get_handle_id(), *args)
+                       cache[key] = handle
+                       isNewHandle = True
+               connection._handles[handle.get_type(), handle.get_id()] = handle
+               if False:
+                       handleStatus = "Is New!" if isNewHandle else "From Cache"
+                       _moduleLogger.info("Created Handle: %r (%s)" % (handle, handleStatus))
+               return handle
+
+       return create_handle
+
+
+create_handle = create_handle_factory()