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 def is_same(self, handleType, handleName):
27 return self.get_name() == handleName and self.get_type() == handleType
29 id = property(telepathy.server.Handle.get_id)
30 type = property(telepathy.server.Handle.get_type)
31 name = property(telepathy.server.Handle.get_name)
34 class ConnectionHandle(TheOneRingHandle):
36 def __init__(self, connection, id):
37 handleType = telepathy.HANDLE_TYPE_CONTACT
38 handleName = connection.username
39 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
41 self.profile = connection.username
44 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.strip_number(phoneNumber)
55 def from_handle_name(handleName):
56 parts = handleName.split("#", 1)
58 contactId, contactNumber = parts[0:2]
60 contactId, contactNumber = "", handleName
62 raise RuntimeError("Invalid handle: %s" % handleName)
64 contactNumber = util_misc.strip_number(contactNumber)
65 return contactId, contactNumber
68 def to_handle_name(contactId, contactNumber):
69 handleName = "#".join((contactId, util_misc.strip_number(contactNumber)))
73 def normalize_handle_name(cls, name):
75 # Already a properly formatted name, run through the ringer just in case
76 return cls.to_handle_name(*cls.from_handle_name(name))
79 return cls.to_handle_name("", name)
81 def is_same(self, handleType, handleName):
82 handleName = self.normalize_handle_name(handleName)
83 _moduleLogger.info("%r == %r %r?" % (self, handleType, handleName))
84 return self.get_name() == handleName and self.get_type() == handleType
88 return self._contactId
91 def phoneNumber(self):
92 return self._phoneNumber
95 def contactDetails(self):
96 return self._conn.addressbook.get_contact_details(self._id)
99 class ListHandle(TheOneRingHandle):
101 def __init__(self, connection, id, listName):
102 handleType = telepathy.HANDLE_TYPE_LIST
103 handleName = listName
104 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
107 _HANDLE_TYPE_MAPPING = {
108 'connection': ConnectionHandle,
109 'contact': ContactHandle,
114 def create_handle_factory():
116 cache = weakref.WeakValueDictionary()
118 def create_handle(connection, type, *args):
119 Handle = _HANDLE_TYPE_MAPPING[type]
120 key = Handle, connection.username, args
125 # The misnamed get_handle_id requests a new handle id
126 handle = Handle(connection, connection.get_handle_id(), *args)
129 connection._handles[handle.get_type(), handle.get_id()] = handle
131 handleStatus = "Is New!" if isNewHandle else "From Cache"
132 _moduleLogger.info("Created Handle: %r (%s)" % (handle, handleStatus))
138 create_handle = create_handle_factory()