7 import util.misc as util_misc
10 _moduleLogger = logging.getLogger("handle")
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 = util_misc.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 def contact_name(self):
57 return self._conn.session.addressbook.get_contact_name(self.phoneNumber)
60 def contactDetails(self):
61 return self._conn.session.addressbook.get_phone_type(self.phoneNumber)
64 class ListHandle(TheOneRingHandle):
66 def __init__(self, connection, id, listName):
67 handleType = telepathy.HANDLE_TYPE_LIST
69 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
72 _HANDLE_TYPE_MAPPING = {
73 'connection': ConnectionHandle,
74 'contact': ContactHandle,
79 def create_handle_factory():
81 cache = weakref.WeakValueDictionary()
83 def create_handle(connection, type, *args):
84 Handle = _HANDLE_TYPE_MAPPING[type]
85 key = Handle, connection.username, args
90 # The misnamed get_handle_id requests a new handle id
91 handle = Handle(connection, connection.get_handle_id(), *args)
94 connection._handles[handle.get_type(), handle.get_id()] = handle
95 handleStatus = "Is New!" if isNewHandle else "From Cache"
96 _moduleLogger.debug("Created Handle: %r (%s)" % (handle, handleStatus))
102 create_handle = create_handle_factory()