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 def is_same(self, handleType, handleName):
28 return self.get_name() == handleName and self.get_type() == handleType
30 id = property(tp.Handle.get_id)
31 type = property(tp.Handle.get_type)
32 name = property(tp.Handle.get_name)
35 class ConnectionHandle(TheOneRingHandle):
37 def __init__(self, connection, id):
38 handleType = telepathy.HANDLE_TYPE_CONTACT
39 handleName = connection.username
40 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
42 self.profile = connection.username
45 class ContactHandle(TheOneRingHandle):
47 def __init__(self, connection, id, contactId, phoneNumber):
48 handleType = telepathy.HANDLE_TYPE_CONTACT
49 handleName = self.to_handle_name(contactId, phoneNumber)
50 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
52 self._contactId = contactId
53 self._phoneNumber = util_misc.strip_number(phoneNumber)
56 def from_handle_name(handleName):
57 parts = handleName.split("#", 1)
59 contactId, contactNumber = parts[0:2]
61 contactId, contactNumber = "", handleName
63 raise RuntimeError("Invalid handle: %s" % handleName)
65 contactNumber = util_misc.strip_number(contactNumber)
66 return contactId, contactNumber
69 def to_handle_name(contactId, contactNumber):
70 handleName = "#".join((contactId, util_misc.strip_number(contactNumber)))
74 def normalize_handle_name(cls, name):
76 # Already a properly formatted name, run through the ringer just in case
77 return cls.to_handle_name(*cls.from_handle_name(name))
80 return cls.to_handle_name("", name)
82 def is_same(self, handleType, handleName):
83 handleName = self.normalize_handle_name(handleName)
84 _moduleLogger.info("%r == %r %r?" % (self, handleType, handleName))
85 return self.get_name() == handleName and self.get_type() == handleType
89 return self._contactId
92 def phoneNumber(self):
93 return self._phoneNumber
96 def contactDetails(self):
97 return self._conn.addressbook.get_contact_details(self._id)
100 class ListHandle(TheOneRingHandle):
102 def __init__(self, connection, id, listName):
103 handleType = telepathy.HANDLE_TYPE_LIST
104 handleName = listName
105 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
108 _HANDLE_TYPE_MAPPING = {
109 'connection': ConnectionHandle,
110 'contact': ContactHandle,
115 def create_handle_factory():
117 cache = weakref.WeakValueDictionary()
119 def create_handle(connection, type, *args):
120 Handle = _HANDLE_TYPE_MAPPING[type]
121 key = Handle, connection.username, args
126 # The misnamed get_handle_id requests a new handle id
127 handle = Handle(connection, connection.get_handle_id(), *args)
130 connection._handles[handle.get_type(), handle.get_id()] = handle
131 handleStatus = "Is New!" if isNewHandle else "From Cache"
132 _moduleLogger.debug("Created Handle: %r (%s)" % (handle, handleStatus))
138 create_handle = create_handle_factory()