Adding some logging
[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_AVATARS : 'token',
19                 telepathy.CONNECTION_INTERFACE_CAPABILITIES : 'caps',
20                 telepathy.CONNECTION_INTERFACE_CONTACT_CAPABILITIES : 'capabilities'
21         }
22
23         def __init__(self):
24                 telepathy.server.ConnectionInterfaceContacts.__init__(self)
25
26                 dbus_interface = telepathy.CONNECTION_INTERFACE_CONTACTS
27                 self._implement_property_get(
28                         dbus_interface,
29                         {'ContactAttributeInterfaces' : self.get_contact_attribute_interfaces}
30                 )
31
32         def HoldHandles(self, *args):
33                 """
34                 @abstract
35                 """
36                 raise NotImplementedError("Abstract function called")
37
38         # Overwrite the dbus attribute to get the sender argument
39         @misc_utils.log_exception(_moduleLogger)
40         @dbus.service.method(telepathy.CONNECTION_INTERFACE_CONTACTS, in_signature='auasb',
41                                                         out_signature='a{ua{sv}}', sender_keyword='sender')
42         def GetContactAttributes(self, handles, interfaces, hold, sender):
43                 #InspectHandle already checks we're connected, the handles and handle type.
44                 supportedInterfaces = set()
45                 for interface in interfaces:
46                         if interface in self.ATTRIBUTES:
47                                 supportedInterfaces.add(interface)
48                         else:
49                                 _moduleLogger.debug("Ignoring unsupported interface %s" % interface)
50
51                 handle_type = telepathy.HANDLE_TYPE_CONTACT
52                 ret = dbus.Dictionary(signature='ua{sv}')
53                 for handle in handles:
54                         ret[handle] = dbus.Dictionary(signature='sv')
55
56                 functions = {
57                         telepathy.CONNECTION:
58                                 lambda x: zip(x, self.InspectHandles(handle_type, x)),
59                         telepathy.CONNECTION_INTERFACE_SIMPLE_PRESENCE:
60                                 lambda x: self.GetPresences(x).items(),
61                         telepathy.CONNECTION_INTERFACE_ALIASING:
62                                 lambda x: self.GetAliases(x).items(),
63                         telepathy.CONNECTION_INTERFACE_AVATARS :
64                                 lambda x: self.GetKnownAvatarTokens(x).items(),
65                         telepathy.CONNECTION_INTERFACE_CAPABILITIES:
66                                 lambda x: self.GetCapabilities(x).items(),
67                         telepathy.CONNECTION_INTERFACE_CONTACT_CAPABILITIES :
68                                 lambda x: self.GetContactCapabilities(x).items()
69                 }
70
71                 #Hold handles if needed
72                 if hold:
73                         self.HoldHandles(handle_type, handles, sender)
74
75                 # Attributes from the interface org.freedesktop.Telepathy.Connection
76                 # are always returned, and need not be requested explicitly.
77                 supportedInterfaces.add(telepathy.CONNECTION)
78
79                 for interface in supportedInterfaces:
80                         interface_attribute = interface + '/' + self.ATTRIBUTES[interface]
81                         results = functions[interface](handles)
82                         for handle, value in results:
83                                 ret[int(handle)][interface_attribute] = value
84                 return ret
85
86         def get_contact_attribute_interfaces(self):
87                 return self.ATTRIBUTES.keys()