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