Switching back to my hacked capabilities to get calls working
authorEd Page <eopage@byu.net>
Tue, 19 Jan 2010 04:14:13 +0000 (22:14 -0600)
committerEd Page <eopage@byu.net>
Tue, 19 Jan 2010 04:14:13 +0000 (22:14 -0600)
src/capabilities.py

index 7a4c8c5..11a7f0c 100644 (file)
@@ -1,11 +1,9 @@
-import itertools
 import logging
 
 import telepathy
 
 import tp
 import gtk_toolbox
-import util.coroutines as coroutines
 
 
 _moduleLogger = logging.getLogger('capabilities')
@@ -15,16 +13,9 @@ class CapabilitiesMixin(tp.ConnectionInterfaceCapabilities):
 
        def __init__(self):
                tp.ConnectionInterfaceCapabilities.__init__(self)
-               self._caps[telepathy.CHANNEL_TYPE_STREAMED_MEDIA] = self._get_capabilities(
-                       None, telepathy.CHANNEL_TYPE_STREAMED_MEDIA
-               )
-               self._callback = coroutines.func_sink(
-                       coroutines.expand_positional(
-                               self._on_contacts_refreshed
-                       )
-               )
-               self.session.addressbook.updateSignalHandler.register_sink(
-                       self._callback
+               self._implement_property_get(
+                       telepathy.interfaces.CONN_INTERFACE_CAPABILITIES,
+                       {"caps": self.GetCapabilities},
                )
 
        @property
@@ -34,55 +25,70 @@ class CapabilitiesMixin(tp.ConnectionInterfaceCapabilities):
                """
                raise NotImplementedError("Abstract property called")
 
-       def GetSelfHandle():
+       def get_handle_by_id(self, handleType, handleId):
                """
                @abstract
                """
                raise NotImplementedError("Abstract function called")
 
-       def get_handle_by_name(self, handleType, handleName):
+       def GetSelfHandle(self):
                """
                @abstract
                """
                raise NotImplementedError("Abstract function called")
 
        @gtk_toolbox.log_exception(_moduleLogger)
-       def _on_contacts_refreshed(self, addressbook, added, removed, changed):
-               capabilityDifferences = []
-               for isAdded, phoneNumber in itertools.chain(
-                       itertools.izip(itertools.repeat(True), added),
-                       itertools.izip(itertools.repeat(False), removed),
-               ):
-                       handle = self.get_handle_by_name(telepathy.HANDLE_TYPE_CONTACT, phoneNumber)
-                       ctype = telepathy.CHANNEL_TYPE_STREAMED_MEDIA
-
-                       if isAdded:
-                               new_gen, new_spec = self._get_capabilities(handle, ctype)
+       def GetCapabilities(self, handleIds):
+               """
+               @todo HACK Remove this once we are building against a fixed version of python-telepathy
+               """
+               ret = []
+               for handleId in handleIds:
+                       h = self.get_handle_by_id(telepathy.HANDLE_TYPE_CONTACT, handleId)
+                       if handleId != 0 and (telepathy.HANDLE_TYPE_CONTACT, handleId) not in self._handles:
+                               raise telepathy.errors.InvalidHandle
+                       elif h in self._caps:
+                               types = self._caps[h]
+                               for type in types:
+                                       for ctype, specs in types.iteritems():
+                                               ret.append([handleId, type, specs[0], specs[1]])
                        else:
-                               new_gen, new_spec = 0, 0
-                       old_gen, old_spec = self._get_old_capabilities(handle, ctype)
-
-                       if old_gen == new_gen and old_spec == new_spec:
-                               continue
-
-                       diff = (int(handle), ctype, old_gen, new_gen, old_spec, new_spec)
-                       capabilityDifferences.append(diff)
-               self.CapabilitiesChanged(capabilityDifferences)
-
-       def _get_old_capabilities(self, handle, ctype):
-               if handle in self._caps:
-                       old_gen, old_spec = self._caps[handle][ctype]
-               else:
-                       old_gen = 0
-                       old_spec = 0
-               return old_gen, old_spec
+                               # No caps, so just default to the connection's caps
+                               types = self._caps[self.GetSelfHandle()]
+                               for type in types:
+                                       for ctype, specs in types.iteritems():
+                                               ret.append([handleId, type, specs[0], specs[1]])
+               return ret
 
-       def _get_capabilities(self, handle, ctype):
-               gen_caps = 0
-               spec_caps = 0
-
-               gen_caps |= telepathy.CONNECTION_CAPABILITY_FLAG_CREATE
-               gen_caps |= telepathy.CONNECTION_CAPABILITY_FLAG_INVITE
-               spec_caps |= telepathy.CHANNEL_MEDIA_CAPABILITY_AUDIO
-
-               return gen_caps, spec_caps
+       @gtk_toolbox.log_exception(_moduleLogger)
+       def AdvertiseCapabilities(self, add, remove):
+               """
+               @todo HACK Remove this once we are building against a fixed version of python-telepathy
+               """
+               my_caps = self._caps.setdefault(self.GetSelfHandle(), {})
+
+               changed = {}
+               for ctype, spec_caps in add:
+                       changed[ctype] = spec_caps
+               for ctype in remove:
+                       changed[ctype] = None
+
+               caps = []
+               for ctype, spec_caps in changed.iteritems():
+                       gen_old, spec_old = my_caps.get(ctype, (0, 0))
+                       if spec_caps is None:
+                               # channel type no longer supported (provider has gone away)
+                               gen_new, spec_new = 0, 0
+                       else:
+                               # channel type supports new capabilities
+                               gen_new, spec_new = gen_old, spec_old | spec_caps
+                       if spec_old != spec_new or gen_old != gen_new:
+                               caps.append((self.GetSelfHandle(), ctype, gen_old, gen_new,
+                                                       spec_old, spec_new))
+
+               self.CapabilitiesChanged(caps)
+               _moduleLogger.info("CapsChanged %r" % self._caps)
+
+               # return all my capabilities
+               ret = [(ctype, caps[1]) for ctype, caps in my_caps.iteritems()]
+               return ret