Adding a debug prompt.
[theonering] / src / handle.py
1 import logging
2 import weakref
3
4 import telepathy
5
6 import util.misc as util_misc
7
8
9 _moduleLogger = logging.getLogger("handle")
10
11
12 class TheOneRingHandle(telepathy.server.Handle):
13         """
14         Instances are memoized
15         """
16
17         def __init__(self, connection, id, handleType, name):
18                 telepathy.server.Handle.__init__(self, id, handleType, name)
19                 self._conn = weakref.proxy(connection)
20
21         def __repr__(self):
22                 return "<%s id=%u name='%s'>" % (
23                         type(self).__name__, self.id, self.name
24                 )
25
26         id = property(telepathy.server.Handle.get_id)
27         type = property(telepathy.server.Handle.get_type)
28         name = property(telepathy.server.Handle.get_name)
29
30
31 class ConnectionHandle(TheOneRingHandle):
32
33         def __init__(self, connection, id):
34                 handleType = telepathy.HANDLE_TYPE_CONTACT
35                 handleName = connection.username
36                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
37
38                 self.profile = connection.username
39
40
41 class ContactHandle(TheOneRingHandle):
42
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)
47
48                 self._contactId = contactId
49                 self._phoneNumber = util_misc.strip_number(phoneNumber)
50
51         @staticmethod
52         def from_handle_name(handleName):
53                 parts = handleName.split("#", 1)
54                 if len(parts) == 2:
55                         contactId, contactNumber = parts[0:2]
56                 elif len(parts) == 1:
57                         contactId, contactNumber = "", handleName
58                 else:
59                         raise RuntimeError("Invalid handle: %s" % handleName)
60
61                 contactNumber = util_misc.strip_number(contactNumber)
62                 return contactId, contactNumber
63
64         @staticmethod
65         def to_handle_name(contactId, contactNumber):
66                 handleName = "#".join((contactId, util_misc.strip_number(contactNumber)))
67                 return handleName
68
69         @property
70         def contactID(self):
71                 return self._contactId
72
73         @property
74         def phoneNumber(self):
75                 return self._phoneNumber
76
77         @property
78         def contactDetails(self):
79                 return self._conn.addressbook.get_contact_details(self._id)
80
81
82 class ListHandle(TheOneRingHandle):
83
84         def __init__(self, connection, id, listName):
85                 handleType = telepathy.HANDLE_TYPE_LIST
86                 handleName = listName
87                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
88
89
90 _HANDLE_TYPE_MAPPING = {
91         'connection': ConnectionHandle,
92         'contact': ContactHandle,
93         'list': ListHandle,
94 }
95
96
97 def create_handle_factory():
98
99         cache = weakref.WeakValueDictionary()
100
101         def create_handle(connection, type, *args):
102                 Handle = _HANDLE_TYPE_MAPPING[type]
103                 key = Handle, connection.username, args
104                 try:
105                         handle = cache[key]
106                         isNewHandle = False
107                 except KeyError:
108                         # The misnamed get_handle_id requests a new handle id
109                         handle = Handle(connection, connection.get_handle_id(), *args)
110                         cache[key] = handle
111                         isNewHandle = True
112                 connection._handles[handle.get_type(), handle.get_id()] = handle
113                 if False:
114                         handleStatus = "Is New!" if isNewHandle else "From Cache"
115                         _moduleLogger.info("Created Handle: %r (%s)" % (handle, handleStatus))
116                 return handle
117
118         return create_handle
119
120
121 create_handle = create_handle_factory()