Reducing duplicate error reports and making note of why an error may occur
[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                 for interface in interfaces:
43                         if interface not in self.ATTRIBUTES:
44                                 raise telepathy.errors.InvalidArgument(
45                                         'Interface %s is not supported by GetContactAttributes' % (interface)
46                                 )
47
48                 handle_type = telepathy.HANDLE_TYPE_CONTACT
49                 ret = {}
50                 for handle in handles:
51                         ret[handle] = {}
52
53                 functions = {
54                         telepathy.CONNECTION:
55                                 lambda x: zip(x, self.InspectHandles(handle_type, x)),
56                         telepathy.CONNECTION_INTERFACE_SIMPLE_PRESENCE:
57                                 lambda x: self.GetPresences(x).items(),
58                         telepathy.CONNECTION_INTERFACE_ALIASING:
59                                 lambda x: self.GetAliases(x).items(),
60                         telepathy.CONNECTION_INTERFACE_CAPABILITIES:
61                                 lambda x: self.GetCapabilities(x).items(),
62                 }
63
64                 #Hold handles if needed
65                 if hold:
66                         self.HoldHandles(handle_type, handles, sender)
67
68                 # Attributes from the interface org.freedesktop.Telepathy.Connection
69                 # are always returned, and need not be requested explicitly.
70                 interfaces = set(interfaces + [telepathy.CONNECTION])
71                 for interface in interfaces:
72                         interface_attribute = interface + '/' + self.ATTRIBUTES[interface]
73                         results = functions[interface](handles)
74                         for handle, value in results:
75                                 ret[int(handle)][interface_attribute] = value
76                 return ret
77
78         def get_contact_attribute_interfaces(self):
79                 return self.ATTRIBUTES.keys()