7 _moduleLogger = logging.getLogger("handle")
10 class MetaMemoize(type):
12 Allows a class to cache off instances for reuse
15 def __call__(cls, connection, *args):
16 obj, newlyCreated = cls.__new__(cls, connection, *args)
18 obj.__init__(connection, connection.get_handle_id(), *args)
19 _moduleLogger.info("New Handle %r" % obj)
23 class TheOneRingHandle(telepathy.server.Handle):
25 Instances are memoized
28 __metaclass__ = MetaMemoize
30 _instances = weakref.WeakValueDictionary()
32 def __new__(cls, connection, *args):
33 key = cls, connection.username, args
34 if key in cls._instances.keys():
35 return cls._instances[key], False
37 instance = object.__new__(cls, connection, *args)
38 cls._instances[key] = instance # TRICKY: instances is a weakdict
41 def __init__(self, connection, id, handleType, name):
42 telepathy.server.Handle.__init__(self, id, handleType, name)
43 self._conn = weakref.proxy(connection)
46 return "<%s id=%u name='%s'>" % (
47 type(self).__name__, self.id, self.name
50 id = property(telepathy.server.Handle.get_id)
51 type = property(telepathy.server.Handle.get_type)
52 name = property(telepathy.server.Handle.get_name)
55 class ConnectionHandle(TheOneRingHandle):
59 def __init__(self, connection, id):
60 handleType = telepathy.HANDLE_TYPE_CONTACT
61 handleName = connection.username
62 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
64 self.profile = connection.username
67 class ContactHandle(TheOneRingHandle):
69 def __init__(self, connection, id, contactId, contactAccount):
70 handleType = telepathy.HANDLE_TYPE_CONTACT
71 handleName = contactId
72 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
74 self._account = contactAccount
82 def contactName(self):
86 def contactDetails(self):
87 return self._conn.gvoice_client.get_contact_details(self._id)
90 class ListHandle(TheOneRingHandle):
92 def __init__(self, connection, id, listName):
93 handleType = telepathy.HANDLE_TYPE_LIST
95 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
98 class GroupHandle(TheOneRingHandle):
100 def __init__(self, connection, id, groupName):
101 handleType = telepathy.HANDLE_TYPE_GROUP
102 handleName = groupName
103 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
106 _HANDLE_TYPE_MAPPING = {
107 'connection': ConnectionHandle,
108 'contact': ContactHandle,
110 'group': GroupHandle,
114 def create_handle(connection, type, *args):
115 handle = _HANDLE_TYPE_MAPPING[type](connection, *args)
116 connection._handles[handle.get_type(), handle.get_id()] = handle