Call-centric work
[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         def handle(self, handleType, handleId):
28                 """
29                 @abstract
30                 """
31                 raise NotImplementedError("Abstract function called")
32
33         def GetSelfHandle(self):
34                 """
35                 @abstract
36                 """
37                 raise NotImplementedError("Abstract function called")
38
39         @gtk_toolbox.log_exception(_moduleLogger)
40         def GetCapabilities(self, handleIds):
41                 """
42                 @todo HACK Remove this once we are building against a fixed version of python-telepathy
43                 """
44                 ret = []
45                 for handleId in handleIds:
46                         h = self.handle(telepathy.HANDLE_TYPE_CONTACT, handleId)
47                         if handleId != 0 and (telepathy.HANDLE_TYPE_CONTACT, handleId) not in self._handles:
48                                 raise telepathy.errors.InvalidHandle
49                         elif h in self._caps:
50                                 types = self._caps[h]
51                                 for type in types:
52                                         for ctype, specs in types.iteritems():
53                                                 ret.append([handleId, type, specs[0], specs[1]])
54                         else:
55                                 # No caps, so just default to the connection's caps
56                                 types = self._caps[self.GetSelfHandle()]
57                                 for type in types:
58                                         for ctype, specs in types.iteritems():
59                                                 ret.append([handleId, type, specs[0], specs[1]])
60                 return ret
61
62         @gtk_toolbox.log_exception(_moduleLogger)
63         def AdvertiseCapabilities(self, add, remove):
64                 """
65                 @todo HACK Remove this once we are building against a fixed version of python-telepathy
66                 """
67                 my_caps = self._caps.setdefault(self.GetSelfHandle(), {})
68
69                 changed = {}
70                 for ctype, spec_caps in add:
71                         changed[ctype] = spec_caps
72                 for ctype in remove:
73                         changed[ctype] = None
74
75                 caps = []
76                 for ctype, spec_caps in changed.iteritems():
77                         gen_old, spec_old = my_caps.get(ctype, (0, 0))
78                         if spec_caps is None:
79                                 # channel type no longer supported (provider has gone away)
80                                 gen_new, spec_new = 0, 0
81                         else:
82                                 # channel type supports new capabilities
83                                 gen_new, spec_new = gen_old, spec_old | spec_caps
84                         if spec_old != spec_new or gen_old != gen_new:
85                                 caps.append((self.GetSelfHandle(), ctype, gen_old, gen_new,
86                                                         spec_old, spec_new))
87
88                 self.CapabilitiesChanged(caps)
89                 _moduleLogger.info("CapsChanged %r" % self._caps)
90
91                 # return all my capabilities
92                 ret = [(ctype, caps[1]) for ctype, caps in my_caps.iteritems()]
93                 return ret