7 import util.misc as misc_utils
10 _moduleLogger = logging.getLogger(__name__)
13 class TheOneRingHandle(tp.Handle):
15 Instances are memoized
18 def __init__(self, connection, id, handleType, name):
19 tp.Handle.__init__(self, id, handleType, name)
20 self._conn = weakref.proxy(connection)
23 return "<%s id=%u name='%s'>" % (
24 type(self).__name__, self.id, self.name
27 id = property(tp.Handle.get_id)
28 type = property(tp.Handle.get_type)
29 name = property(tp.Handle.get_name)
32 class ConnectionHandle(TheOneRingHandle):
34 def __init__(self, connection, id):
35 handleType = telepathy.HANDLE_TYPE_CONTACT
36 handleName = connection.username
37 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
39 self.profile = connection.username
42 class ContactHandle(TheOneRingHandle):
44 def __init__(self, connection, id, phoneNumber):
45 self._phoneNumber = misc_utils.normalize_number(phoneNumber)
47 handleType = telepathy.HANDLE_TYPE_CONTACT
48 handleName = self._phoneNumber
49 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
52 def phoneNumber(self):
53 return self._phoneNumber
56 class ListHandle(TheOneRingHandle):
58 def __init__(self, connection, id, listName):
59 handleType = telepathy.HANDLE_TYPE_LIST
61 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
64 _HANDLE_TYPE_MAPPING = {
65 'connection': ConnectionHandle,
66 'contact': ContactHandle,
71 def create_handle_factory():
73 cache = weakref.WeakValueDictionary()
75 def _create_handle(connection, type, *args):
76 Handle = _HANDLE_TYPE_MAPPING[type]
77 key = Handle, connection.username, args
82 # The misnamed get_handle_id requests a new handle id
83 handle = Handle(connection, connection.get_handle_id(), *args)
86 connection._handles[handle.get_type(), handle.get_id()] = handle
88 handleStatus = "Is New!" if isNewHandle else "From Cache"
89 _moduleLogger.debug("Created Handle: %r (%s)" % (handle, handleStatus))
95 create_handle = create_handle_factory()