For historical reasons, a broken attempt at capabilities
[theonering] / src / capabilities.py
1 import itertools
2 import logging
3
4 import telepathy
5
6 import tp
7 import gtk_toolbox
8 import util.coroutines as coroutines
9
10
11 _moduleLogger = logging.getLogger('capabilities')
12
13
14 class CapabilitiesMixin(tp.ConnectionInterfaceCapabilities):
15
16         def __init__(self):
17                 tp.ConnectionInterfaceCapabilities.__init__(self)
18                 self._caps[telepathy.CHANNEL_TYPE_STREAMED_MEDIA] = self._get_capabilities(
19                         None, telepathy.CHANNEL_TYPE_STREAMED_MEDIA
20                 )
21                 self._callback = coroutines.func_sink(
22                         coroutines.expand_positional(
23                                 self._on_contacts_refreshed
24                         )
25                 )
26                 self.session.addressbook.updateSignalHandler.register_sink(
27                         self._callback
28                 )
29
30         @property
31         def session(self):
32                 """
33                 @abstract
34                 """
35                 raise NotImplementedError("Abstract property called")
36
37         def GetSelfHandle():
38                 """
39                 @abstract
40                 """
41                 raise NotImplementedError("Abstract function called")
42
43         def get_handle_by_name(self, handleType, handleName):
44                 """
45                 @abstract
46                 """
47                 raise NotImplementedError("Abstract function called")
48
49         @gtk_toolbox.log_exception(_moduleLogger)
50         def _on_contacts_refreshed(self, addressbook, added, removed, changed):
51                 capabilityDifferences = []
52                 for isAdded, phoneNumber in itertools.chain(
53                         itertools.izip(itertools.repeat(True), added),
54                         itertools.izip(itertools.repeat(False), removed),
55                 ):
56                         handle = self.get_handle_by_name(telepathy.HANDLE_TYPE_CONTACT, phoneNumber)
57                         ctype = telepathy.CHANNEL_TYPE_STREAMED_MEDIA
58
59                         if isAdded:
60                                 new_gen, new_spec = self._get_capabilities(handle, ctype)
61                         else:
62                                 new_gen, new_spec = 0, 0
63                         old_gen, old_spec = self._get_old_capabilities(handle, ctype)
64
65                         if old_gen == new_gen and old_spec == new_spec:
66                                 continue
67
68                         diff = (int(handle), ctype, old_gen, new_gen, old_spec, new_spec)
69                         capabilityDifferences.append(diff)
70                 self.CapabilitiesChanged(capabilityDifferences)
71
72         def _get_old_capabilities(self, handle, ctype):
73                 if handle in self._caps:
74                         old_gen, old_spec = self._caps[handle][ctype]
75                 else:
76                         old_gen = 0
77                         old_spec = 0
78                 return old_gen, old_spec
79
80         def _get_capabilities(self, handle, ctype):
81                 gen_caps = 0
82                 spec_caps = 0
83
84                 gen_caps |= telepathy.CONNECTION_CAPABILITY_FLAG_CREATE
85                 gen_caps |= telepathy.CONNECTION_CAPABILITY_FLAG_INVITE
86                 spec_caps |= telepathy.CHANNEL_MEDIA_CAPABILITY_AUDIO
87
88                 return gen_caps, spec_caps