583851751fed9ed964cf75908d3dbcb5acfb3daf
[theonering] / src / handle.py
1 import logging
2 import weakref
3
4 import telepathy
5
6
7 _moduleLogger = logging.getLogger("handle")
8
9
10 class TheOneRingHandle(telepathy.server.Handle):
11         """
12         Instances are memoized
13         """
14
15         def __init__(self, connection, id, handleType, name):
16                 telepathy.server.Handle.__init__(self, id, handleType, name)
17                 self._conn = weakref.proxy(connection)
18
19         def __repr__(self):
20                 return "<%s id=%u name='%s'>" % (
21                         type(self).__name__, self.id, self.name
22                 )
23
24         id = property(telepathy.server.Handle.get_id)
25         type = property(telepathy.server.Handle.get_type)
26         name = property(telepathy.server.Handle.get_name)
27
28
29 class ConnectionHandle(TheOneRingHandle):
30
31         def __init__(self, connection, id):
32                 handleType = telepathy.HANDLE_TYPE_CONTACT
33                 handleName = connection.username
34                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
35
36                 self.profile = connection.username
37
38
39 class ContactHandle(TheOneRingHandle):
40
41         def __init__(self, connection, id, contactId):
42                 handleType = telepathy.HANDLE_TYPE_CONTACT
43                 handleName = contactId
44                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
45
46                 self._contactId = contactId
47
48         @property
49         def contactID(self):
50                 return self._contactId
51
52         @property
53         def contactDetails(self):
54                 return self._conn.addressbook.get_contact_details(self._id)
55
56
57 class ListHandle(TheOneRingHandle):
58
59         def __init__(self, connection, id, listName):
60                 handleType = telepathy.HANDLE_TYPE_LIST
61                 handleName = listName
62                 TheOneRingHandle.__init__(self, connection, id, handleType, handleName)
63
64
65 _HANDLE_TYPE_MAPPING = {
66         'connection': ConnectionHandle,
67         'contact': ContactHandle,
68         'list': ListHandle,
69 }
70
71
72 def create_handle_factory():
73
74         cache = weakref.WeakValueDictionary()
75
76         def create_handle(connection, type, *args):
77                 Handle = _HANDLE_TYPE_MAPPING[type]
78                 key = Handle, connection.username, args
79                 try:
80                         handle = cache[key]
81                         isNewHandle = False
82                 except KeyError:
83                         # The misnamed get_handle_id requests a new handle id
84                         handle = Handle(connection, connection.get_handle_id(), *args)
85                         cache[key] = handle
86                         isNewHandle = True
87                 connection._handles[handle.get_type(), handle.get_id()] = handle
88                 handleStatus = "Is New!" if isNewHandle else "From Cache"
89                 _moduleLogger.info("Created Handle: %r (%s)" % (handle, handleStatus))
90                 return handle
91
92         return create_handle
93
94
95 create_handle = create_handle_factory()