Imitating butterfly in being forgiving
[theonering] / src / contacts.py
1 import logging
2
3 import dbus
4 import telepathy
5
6 import util.misc as misc_utils
7
8
9 _moduleLogger = logging.getLogger(__name__)
10
11
12 class ContactsMixin(telepathy.server.ConnectionInterfaceContacts):
13
14         ATTRIBUTES = {
15                 telepathy.CONNECTION : 'contact-id',
16                 telepathy.CONNECTION_INTERFACE_SIMPLE_PRESENCE : 'presence',
17                 telepathy.CONNECTION_INTERFACE_ALIASING : 'alias',
18                 telepathy.CONNECTION_INTERFACE_CAPABILITIES : 'caps',
19         }
20
21         def __init__(self):
22                 telepathy.server.ConnectionInterfaceContacts.__init__(self)
23
24                 dbus_interface = telepathy.CONNECTION_INTERFACE_CONTACTS
25                 self._implement_property_get(
26                         dbus_interface,
27                         {'ContactAttributeInterfaces' : self.get_contact_attribute_interfaces}
28                 )
29
30         def HoldHandles(self, *args):
31                 """
32                 @abstract
33                 """
34                 raise NotImplementedError("Abstract function called")
35
36         # Overwrite the dbus attribute to get the sender argument
37         @misc_utils.log_exception(_moduleLogger)
38         @dbus.service.method(telepathy.CONNECTION_INTERFACE_CONTACTS, in_signature='auasb',
39                                                         out_signature='a{ua{sv}}', sender_keyword='sender')
40         def GetContactAttributes(self, handles, interfaces, hold, sender):
41                 #InspectHandle already checks we're connected, the handles and handle type.
42                 supportedInterfaces = set()
43                 for interface in interfaces:
44                         if interface in self.ATTRIBUTES:
45                                 supportedInterfaces.add(interface)
46                         else:
47                                 _moduleLogger.debug("Ignoring unsupported interface %s" % interface)
48
49                 handle_type = telepathy.HANDLE_TYPE_CONTACT
50                 ret = dbus.Dictionary(signature='ua{sv}')
51                 for handle in handles:
52                         ret[handle] = dbus.Dictionary(signature='sv')
53
54                 functions = {
55                         telepathy.CONNECTION:
56                                 lambda x: zip(x, self.InspectHandles(handle_type, x)),
57                         telepathy.CONNECTION_INTERFACE_SIMPLE_PRESENCE:
58                                 lambda x: self.GetPresences(x).items(),
59                         telepathy.CONNECTION_INTERFACE_ALIASING:
60                                 lambda x: self.GetAliases(x).items(),
61                         telepathy.CONNECTION_INTERFACE_CAPABILITIES:
62                                 lambda x: self.GetCapabilities(x).items(),
63                 }
64
65                 #Hold handles if needed
66                 if hold:
67                         self.HoldHandles(handle_type, handles, sender)
68
69                 # Attributes from the interface org.freedesktop.Telepathy.Connection
70                 # are always returned, and need not be requested explicitly.
71                 supportedInterfaces.add(telepathy.CONNECTION)
72
73                 for interface in supportedInterfaces:
74                         interface_attribute = interface + '/' + self.ATTRIBUTES[interface]
75                         results = functions[interface](handles)
76                         for handle, value in results:
77                                 ret[int(handle)][interface_attribute] = value
78                 return ret
79
80         def get_contact_attribute_interfaces(self):
81                 return self.ATTRIBUTES.keys()