Reducing log noise
[theonering] / src / capabilities.py
index 31227d4..a64c38b 100644 (file)
 import logging
 
+import dbus
 import telepathy
 
-import gtk_toolbox
+import tp
+from tp._generated import Connection_Interface_Contact_Capabilities
+import util.misc as misc_utils
 
 
-_moduleLogger = logging.getLogger('capabilities')
+_moduleLogger = logging.getLogger(__name__)
 
 
-class CapabilitiesMixin(telepathy.server.ConnectionInterfaceCapabilities):
+class CapabilitiesMixin(
+       tp.ConnectionInterfaceCapabilities,
+       Connection_Interface_Contact_Capabilities.ConnectionInterfaceContactCapabilities,
+):
+
+       _CAPABILITIES = {
+               telepathy.CHANNEL_TYPE_TEXT: (
+                       telepathy.CONNECTION_CAPABILITY_FLAG_CREATE,
+                       0,
+               ),
+               telepathy.CHANNEL_TYPE_STREAMED_MEDIA: (
+                       telepathy.CONNECTION_CAPABILITY_FLAG_CREATE |
+                               telepathy.CONNECTION_CAPABILITY_FLAG_INVITE,
+                       telepathy.CHANNEL_MEDIA_CAPABILITY_AUDIO,
+               ),
+       }
+
+       text_chat_class = (
+               {
+                       telepathy.CHANNEL_INTERFACE + '.ChannelType': telepathy.CHANNEL_TYPE_TEXT,
+                       telepathy.CHANNEL_INTERFACE + '.TargetHandleType': dbus.UInt32(telepathy.HANDLE_TYPE_CONTACT),
+               },
+               [
+                       telepathy.CHANNEL_INTERFACE + '.TargetHandle',
+                       telepathy.CHANNEL_INTERFACE + '.TargetID',
+               ],
+       )
+
+       audio_chat_class = (
+               {
+                       telepathy.CHANNEL_INTERFACE + '.ChannelType': telepathy.CHANNEL_TYPE_STREAMED_MEDIA,
+                       telepathy.CHANNEL_INTERFACE + '.TargetHandleType': dbus.UInt32(telepathy.HANDLE_TYPE_CONTACT),
+               },
+               [
+                       telepathy.CHANNEL_INTERFACE + '.TargetHandle',
+                       telepathy.CHANNEL_INTERFACE + '.TargetID',
+                       telepathy.CHANNEL_TYPE_STREAMED_MEDIA + '.InitialAudio',
+               ],
+       )
+
+       av_chat_class = (
+               {
+                       telepathy.CHANNEL_INTERFACE + '.ChannelType': telepathy.CHANNEL_TYPE_STREAMED_MEDIA,
+                       telepathy.CHANNEL_INTERFACE + '.TargetHandleType': dbus.UInt32(telepathy.HANDLE_TYPE_CONTACT),
+               },
+               [
+                       telepathy.CHANNEL_INTERFACE + '.TargetHandle',
+                       telepathy.CHANNEL_INTERFACE + '.TargetID',
+                       telepathy.CHANNEL_TYPE_STREAMED_MEDIA + '.InitialAudio',
+                       telepathy.CHANNEL_TYPE_STREAMED_MEDIA + '.InitialVideo',
+               ],
+       )
 
        def __init__(self):
-               telepathy.server.ConnectionInterfaceCapabilities.__init__(self)
-               self._implement_property_get(
-                       telepathy.interfaces.CONN_INTERFACE_CAPABILITIES,
-                       {"caps": self.GetCapabilities},
-               )
-
-       @property
-       def session(self):
-               """
-               @abstract
-               """
-               raise NotImplementedError("Abstract property called")
+               tp.ConnectionInterfaceCapabilities.__init__(self)
+               Connection_Interface_Contact_Capabilities.ConnectionInterfaceContactCapabilities.__init__(self)
 
-       @property
-       def handle(self):
+       def get_handle_by_id(self, handleType, handleId):
                """
                @abstract
                """
-               raise NotImplementedError("Abstract property called")
+               raise NotImplementedError("Abstract function called")
 
-       @gtk_toolbox.log_exception(_moduleLogger)
-       def GetCapabilities(self, handles):
-               """
-               @todo HACK Remove this once we are building against a fixed version of python-telepathy
-               """
+       @misc_utils.log_exception(_moduleLogger)
+       def GetCapabilities(self, handleIds):
                ret = []
-               for handle in handles:
-                       if handle != 0 and (telepathy.HANDLE_TYPE_CONTACT, handle) not in self._handles:
+               for handleId in handleIds:
+                       if handleId != 0 and (telepathy.HANDLE_TYPE_CONTACT, handleId) not in self._handles:
                                raise telepathy.errors.InvalidHandle
-                       elif handle in self._caps:
-                               theirs = self._caps[handle]
-                               for type in theirs:
-                                       ret.append([handle, type, theirs[0], theirs[1]])
-               _moduleLogger.info("GetCaps %r" % ret)
+
+                       h = self.get_handle_by_id(telepathy.HANDLE_TYPE_CONTACT, handleId)
+                       for type, (gen, spec) in self._CAPABILITIES.iteritems():
+                               ret.append([handleId, type, gen, spec])
                return ret
 
-       @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._self_handle, {})
-
-               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._self_handle, ctype, gen_old, gen_new,
-                                                       spec_old, spec_new))
-
-               _moduleLogger.info("CapsChanged %r" % caps)
-               self.CapabilitiesChanged(caps)
-
-               # return all my capabilities
-               ret = [(ctype, caps[1]) for ctype, caps in my_caps.iteritems()]
-               _moduleLogger.info("Adv %r" % ret)
+       def GetContactCapabilities(self, handles):
+               if 0 in handles:
+                       raise telepathy.InvalidHandle('Contact handle list contains zero')
+
+               ret = dbus.Dictionary({}, signature='ua(a{sv}as)')
+               for i in handles:
+                       handle = self.get_handle_by_id(telepathy.HANDLE_TYPE_CONTACT, i)
+                       contactCapabilities = dbus.Dictionary(
+                               (self.text_chat_class, self.audio_chat_class),
+                               signature="sv",
+                       )
+                       ret[handle] = dbus.Array(contactCapabilities, signature='(a{sv}as)')
+
                return ret
+
+       def UpdateCapabilities(self, caps):
+               _moduleLogger.info("Ignoring updating contact capabilities")