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):
46 def __init__(self, connection, id, contactId, phoneNumber):
47 handleType = telepathy.HANDLE_TYPE_CONTACT
48 handleName = self.to_handle_name(contactId, phoneNumber)
49 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
51 self._contactId = contactId
52 self._phoneNumber = util_misc.normalize_number(phoneNumber)
55 def from_handle_name(cls, handleName):
57 >>> ContactHandle.from_handle_name("+1 555 123-1234")
59 >>> ContactHandle.from_handle_name("+15551231234")
61 >>> ContactHandle.from_handle_name("123456|+15551231234")
62 ('123456', '+15551231234')
64 parts = handleName.split(cls._DELIMETER, 1)
66 contactId, contactNumber = parts[0:2]
68 contactId, contactNumber = "", handleName
70 raise RuntimeError("Invalid handle: %s" % handleName)
72 contactNumber = util_misc.normalize_number(contactNumber)
73 return contactId, contactNumber
76 def to_handle_name(cls, contactId, contactNumber):
78 >>> ContactHandle.to_handle_name('', "+1 555 123-1234")
80 >>> ContactHandle.to_handle_name('', "+15551231234")
82 >>> ContactHandle.to_handle_name('123456', "+15551231234")
85 contactNumber = util_misc.normalize_number(contactNumber)
87 handleName = cls._DELIMETER.join((contactId, contactNumber))
89 handleName = contactNumber
94 return self._contactId
97 def phoneNumber(self):
98 return self._phoneNumber
101 def contactDetails(self):
102 return self._conn.addressbook.get_contact_details(self._id)
105 class ListHandle(TheOneRingHandle):
107 def __init__(self, connection, id, listName):
108 handleType = telepathy.HANDLE_TYPE_LIST
109 handleName = listName
110 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
113 _HANDLE_TYPE_MAPPING = {
114 'connection': ConnectionHandle,
115 'contact': ContactHandle,
120 def create_handle_factory():
122 cache = weakref.WeakValueDictionary()
124 def create_handle(connection, type, *args):
125 Handle = _HANDLE_TYPE_MAPPING[type]
126 key = Handle, connection.username, args
131 # The misnamed get_handle_id requests a new handle id
132 handle = Handle(connection, connection.get_handle_id(), *args)
135 connection._handles[handle.get_type(), handle.get_id()] = handle
136 handleStatus = "Is New!" if isNewHandle else "From Cache"
137 _moduleLogger.debug("Created Handle: %r (%s)" % (handle, handleStatus))
143 create_handle = create_handle_factory()