Switching away from using contact ids
[theonering] / src / handle.py
1 import logging
2 import weakref
3
4 import telepathy
5
6 import tp
7 import util.misc as util_misc
8
9
10 _moduleLogger = logging.getLogger("handle")
11
12
13 class TheOneRingHandle(tp.Handle):
14         """
15         Instances are memoized
16         """
17
18         def __init__(self, connection, id, handleType, name):
19                 tp.Handle.__init__(self, id, handleType, name)
20                 self._conn = weakref.proxy(connection)
21
22         def __repr__(self):
23                 return "<%s id=%u name='%s'>" % (
24                         type(self).__name__, self.id, self.name
25                 )
26
27         id = property(tp.Handle.get_id)
28         type = property(tp.Handle.get_type)
29         name = property(tp.Handle.get_name)
30
31
32 class ConnectionHandle(TheOneRingHandle):
33
34         def __init__(self, connection, id):
35                 handleType = telepathy.HANDLE_TYPE_CONTACT
36                 handleName = connection.username
37                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
38
39                 self.profile = connection.username
40
41
42 class ContactHandle(TheOneRingHandle):
43
44         def __init__(self, connection, id, phoneNumber):
45                 self._phoneNumber = util_misc.normalize_number(phoneNumber)
46
47                 handleType = telepathy.HANDLE_TYPE_CONTACT
48                 handleName = self._phoneNumber
49                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
50
51         @property
52         def phoneNumber(self):
53                 return self._phoneNumber
54
55         @property
56         def contact_name(self):
57                 return self._conn.session.addressbook.get_contact_name(self.phoneNumber)
58
59         @property
60         def contactDetails(self):
61                 return self._conn.session.addressbook.get_phone_type(self.phoneNumber)
62
63
64 class ListHandle(TheOneRingHandle):
65
66         def __init__(self, connection, id, listName):
67                 handleType = telepathy.HANDLE_TYPE_LIST
68                 handleName = listName
69                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
70
71
72 _HANDLE_TYPE_MAPPING = {
73         'connection': ConnectionHandle,
74         'contact': ContactHandle,
75         'list': ListHandle,
76 }
77
78
79 def create_handle_factory():
80
81         cache = weakref.WeakValueDictionary()
82
83         def create_handle(connection, type, *args):
84                 Handle = _HANDLE_TYPE_MAPPING[type]
85                 key = Handle, connection.username, args
86                 try:
87                         handle = cache[key]
88                         isNewHandle = False
89                 except KeyError:
90                         # The misnamed get_handle_id requests a new handle id
91                         handle = Handle(connection, connection.get_handle_id(), *args)
92                         cache[key] = handle
93                         isNewHandle = True
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))
97                 return handle
98
99         return create_handle
100
101
102 create_handle = create_handle_factory()