6 import util.misc as util_misc
9 _moduleLogger = logging.getLogger("handle")
12 class TheOneRingHandle(telepathy.server.Handle):
14 Instances are memoized
17 def __init__(self, connection, id, handleType, name):
18 telepathy.server.Handle.__init__(self, id, handleType, name)
19 self._conn = weakref.proxy(connection)
22 return "<%s id=%u name='%s'>" % (
23 type(self).__name__, self.id, self.name
26 id = property(telepathy.server.Handle.get_id)
27 type = property(telepathy.server.Handle.get_type)
28 name = property(telepathy.server.Handle.get_name)
31 class ConnectionHandle(TheOneRingHandle):
33 def __init__(self, connection, id):
34 handleType = telepathy.HANDLE_TYPE_CONTACT
35 handleName = connection.username
36 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
38 self.profile = connection.username
41 class ContactHandle(TheOneRingHandle):
43 def __init__(self, connection, id, contactId, phoneNumber):
44 handleType = telepathy.HANDLE_TYPE_CONTACT
45 handleName = self.to_handle_name(contactId, phoneNumber)
46 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
48 self._contactId = contactId
49 self._phoneNumber = util_misc.strip_number(phoneNumber)
52 def from_handle_name(handleName):
53 parts = handleName.split("#", 1)
55 contactId, contactNumber = parts[0:2]
57 contactId, contactNumber = "", handleName
59 raise RuntimeError("Invalid handle: %s" % handleName)
61 contactNumber = util_misc.strip_number(contactNumber)
62 return contactId, contactNumber
65 def to_handle_name(contactId, contactNumber):
66 handleName = "#".join((contactId, util_misc.strip_number(contactNumber)))
71 return self._contactId
74 def phoneNumber(self):
75 return self._phoneNumber
78 def contactDetails(self):
79 return self._conn.addressbook.get_contact_details(self._id)
82 class ListHandle(TheOneRingHandle):
84 def __init__(self, connection, id, listName):
85 handleType = telepathy.HANDLE_TYPE_LIST
87 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
90 _HANDLE_TYPE_MAPPING = {
91 'connection': ConnectionHandle,
92 'contact': ContactHandle,
97 def create_handle_factory():
99 cache = weakref.WeakValueDictionary()
101 def create_handle(connection, type, *args):
102 Handle = _HANDLE_TYPE_MAPPING[type]
103 key = Handle, connection.username, args
108 # The misnamed get_handle_id requests a new handle id
109 handle = Handle(connection, connection.get_handle_id(), *args)
112 connection._handles[handle.get_type(), handle.get_id()] = handle
114 handleStatus = "Is New!" if isNewHandle else "From Cache"
115 _moduleLogger.info("Created Handle: %r (%s)" % (handle, handleStatus))
121 create_handle = create_handle_factory()