Mass load of bug fixes
[theonering] / src / capabilities.py
1 import logging
2
3 import telepathy
4
5 import gtk_toolbox
6
7
8 _moduleLogger = logging.getLogger('capabilities')
9
10
11 class CapabilitiesMixin(telepathy.server.ConnectionInterfaceCapabilities):
12
13         def __init__(self):
14                 telepathy.server.ConnectionInterfaceCapabilities.__init__(self)
15                 self._implement_property_get(
16                         telepathy.interfaces.CONN_INTERFACE_CAPABILITIES,
17                         {"caps": self.GetCapabilities},
18                 )
19
20         @property
21         def session(self):
22                 """
23                 @abstract
24                 """
25                 raise NotImplementedError("Abstract property called")
26
27         @property
28         def handle(self):
29                 """
30                 @abstract
31                 """
32                 raise NotImplementedError("Abstract property called")
33
34         @gtk_toolbox.log_exception(_moduleLogger)
35         def GetCapabilities(self, handles):
36                 """
37                 @todo HACK Remove this once we are building against a fixed version of python-telepathy
38                 """
39                 ret = []
40                 for handle in handles:
41                         if handle != 0 and (telepathy.HANDLE_TYPE_CONTACT, handle) not in self._handles:
42                                 raise telepathy.errors.InvalidHandle
43                         elif handle in self._caps:
44                                 theirs = self._caps[handle]
45                                 for type in theirs:
46                                         ret.append([handle, type, theirs[0], theirs[1]])
47                 _moduleLogger.info("GetCaps %r" % ret)
48                 return ret
49
50         @gtk_toolbox.log_exception(_moduleLogger)
51         def AdvertiseCapabilities(self, add, remove):
52                 """
53                 @todo HACK Remove this once we are building against a fixed version of python-telepathy
54                 """
55                 my_caps = self._caps.setdefault(self._self_handle, {})
56
57                 changed = {}
58                 for ctype, spec_caps in add:
59                         changed[ctype] = spec_caps
60                 for ctype in remove:
61                         changed[ctype] = None
62
63                 caps = []
64                 for ctype, spec_caps in changed.iteritems():
65                         gen_old, spec_old = my_caps.get(ctype, (0, 0))
66                         if spec_caps is None:
67                                 # channel type no longer supported (provider has gone away)
68                                 gen_new, spec_new = 0, 0
69                         else:
70                                 # channel type supports new capabilities
71                                 gen_new, spec_new = gen_old, spec_old | spec_caps
72                         if spec_old != spec_new or gen_old != gen_new:
73                                 caps.append((self._self_handle, ctype, gen_old, gen_new,
74                                                         spec_old, spec_new))
75
76                 _moduleLogger.info("CapsChanged %r" % caps)
77                 self.CapabilitiesChanged(caps)
78
79                 # return all my capabilities
80                 ret = [(ctype, caps[1]) for ctype, caps in my_caps.iteritems()]
81                 _moduleLogger.info("Adv %r" % ret)
82                 return ret