X-Git-Url: http://git.maemo.org/git/?p=theonering;a=blobdiff_plain;f=src%2Fhandle.py;h=5fc4e98ef6e98131d3232f5d6d6286f270f84b13;hp=702e35a25010f55415a5c4ab16cd94c591ec2a5b;hb=be0ad376e3eb189f99ad93fda6c276168cf5c834;hpb=7912b2aa8daf492f3933d975fab97e59937981e1 diff --git a/src/handle.py b/src/handle.py index 702e35a..5fc4e98 100644 --- a/src/handle.py +++ b/src/handle.py @@ -3,43 +3,20 @@ import weakref import telepathy - -_moduleLogger = logging.getLogger("handle") +import tp +import util.misc as util_misc -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): +class TheOneRingHandle(tp.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) + tp.Handle.__init__(self, id, handleType, name) self._conn = weakref.proxy(connection) def __repr__(self): @@ -47,15 +24,13 @@ class TheOneRingHandle(telepathy.server.Handle): type(self).__name__, self.id, self.name ) - id = property(telepathy.server.Handle.get_id) - type = property(telepathy.server.Handle.get_type) - name = property(telepathy.server.Handle.get_name) + id = property(tp.Handle.get_id) + type = property(tp.Handle.get_type) + name = property(tp.Handle.get_name) class ConnectionHandle(TheOneRingHandle): - instance = None - def __init__(self, connection, id): handleType = telepathy.HANDLE_TYPE_CONTACT handleName = connection.username @@ -66,25 +41,16 @@ class ConnectionHandle(TheOneRingHandle): class ContactHandle(TheOneRingHandle): - def __init__(self, connection, id, contactId, contactAccount): + def __init__(self, connection, id, phoneNumber): + self._phoneNumber = util_misc.normalize_number(phoneNumber) + handleType = telepathy.HANDLE_TYPE_CONTACT - handleName = contactId + handleName = self._phoneNumber TheOneRingHandle.__init__(self, connection, id, handleType, handleName) - self._account = contactAccount - self._id = contactId - @property - def contactID(self): - return self._id - - @property - def contactName(self): - return self._account - - @property - def contactDetails(self): - return self._conn.gvoice_client.get_contact_details(self._id) + def phoneNumber(self): + return self._phoneNumber class ListHandle(TheOneRingHandle): @@ -95,23 +61,34 @@ class ListHandle(TheOneRingHandle): TheOneRingHandle.__init__(self, connection, id, handleType, handleName) -class GroupHandle(TheOneRingHandle): - - def __init__(self, connection, id, groupName): - handleType = telepathy.HANDLE_TYPE_GROUP - handleName = groupName - TheOneRingHandle.__init__(self, connection, id, handleType, handleName) - - _HANDLE_TYPE_MAPPING = { 'connection': ConnectionHandle, 'contact': ContactHandle, 'list': ListHandle, - 'group': GroupHandle, } -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 + handleStatus = "Is New!" if isNewHandle else "From Cache" + _moduleLogger.debug("Created Handle: %r (%s)" % (handle, handleStatus)) + return handle + + return create_handle + + +create_handle = create_handle_factory()