Starting to add access to the debug log
[theonering] / src / channel / call.py
index 9de629e..5808d4e 100644 (file)
 import logging
 
+import dbus
+import gobject
 import telepathy
 
+import tp
 import gtk_toolbox
-import handle
 
 
 _moduleLogger = logging.getLogger("channel.call")
 
 
-# @todo Test Calls
 class CallChannel(
-               telepathy.server.ChannelTypeStreamedMedia,
-               telepathy.server.ChannelInterfaceCallState,
+               tp.ChannelTypeStreamedMedia,
+               tp.ChannelInterfaceCallState,
+               tp.ChannelInterfaceGroup,
        ):
 
-       def __init__(self, connection, contactHandle):
-               telepathy.server.ChannelTypeStreamedMedia.__init__(self, connection, None)
-               telepathy.server.ChannelInterfaceCallState.__init__(self)
-               self._contactHandle = contactHandle
+       def __init__(self, connection, manager, props, contactHandle):
+               self.__manager = manager
+               self.__props = props
+               self.__cancelId = None
+
+               if telepathy.interfaces.CHANNEL_INTERFACE + '.InitiatorHandle' in props:
+                       self._initiator = connection.get_handle_by_id(
+                               telepathy.HANDLE_TYPE_CONTACT,
+                               props[telepathy.interfaces.CHANNEL_INTERFACE + '.InitiatorHandle'],
+                       )
+               elif telepathy.interfaces.CHANNEL_INTERFACE + '.InitiatorID' in props:
+                       self._initiator = connection.get_handle_by_name(
+                               telepathy.HANDLE_TYPE_CONTACT,
+                               props[telepathy.interfaces.CHANNEL_INTERFACE + '.InitiatorHandle'],
+                       )
+               else:
+                       # Maemo 5 seems to require InitiatorHandle/InitiatorID to be set
+                       # even though I can't find them in the dbus spec.  I think its
+                       # generally safe to assume that its locally initiated if not
+                       # specified.  Specially for The One Ring, its always locally
+                       # initiated
+                       _moduleLogger.warning('InitiatorID or InitiatorHandle not set on new channel, assuming locally initiated')
+                       self._initiator = connection.GetSelfHandle()
+
+               tp.ChannelTypeStreamedMedia.__init__(self, connection, manager, props)
+               tp.ChannelInterfaceCallState.__init__(self)
+               tp.ChannelInterfaceGroup.__init__(self)
+               self.__contactHandle = contactHandle
+               self._implement_property_get(
+                       telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
+                       {
+                               "InitialAudio": self.initial_audio,
+                               "InitialVideo": self.initial_video,
+                       },
+               )
+               self._add_immutables({
+                       'InitialAudio': telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
+                       'InitialVideo': telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
+               })
+               self._implement_property_get(
+                       telepathy.interfaces.CHANNEL_INTERFACE,
+                       {
+                               'InitiatorHandle': lambda: dbus.UInt32(self._initiator.id),
+                               'InitiatorID': lambda: self._initiator.name,
+                       },
+               )
+               self._add_immutables({
+                       'InitiatorHandle': telepathy.interfaces.CHANNEL_INTERFACE,
+                       'InitiatorID': telepathy.interfaces.CHANNEL_INTERFACE,
+               })
+
+               self.GroupFlagsChanged(0, 0)
+               self.MembersChanged(
+                       '', [self._conn.GetSelfHandle()], [], [], [contactHandle],
+                       0, telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE
+               )
+
+       def initial_audio(self):
+               return False
+
+       def initial_video(self):
+               return False
 
        @gtk_toolbox.log_exception(_moduleLogger)
        def Close(self):
                self.close()
 
        def close(self):
-               telepathy.server.ChannelTypeStreamedMedia.Close(self)
+               _moduleLogger.debug("Closing call")
+               tp.ChannelTypeStreamedMedia.Close(self)
                self.remove_from_connection()
-               self._prop_getters = None # HACK to get around python-telepathy memory leaks
+               if self.__cancelId is not None:
+                       gobject.source_remove(self.__cancelId)
+                       self.__cancelId = None
+
+       @gtk_toolbox.log_exception(_moduleLogger)
+       def GetLocalPendingMembersWithInfo(self):
+               return []
 
        @gtk_toolbox.log_exception(_moduleLogger)
        def ListStreams(self):
@@ -60,18 +127,16 @@ class CallChannel(
 
                @returns [(Stream ID, contact, stream type, stream state, stream direction, pending send flags)]
                """
-               for streamType in streamTypes:
-                       if streamType != telepathy.constants.MEDIA_STREAM_TYPE_AUDIO:
-                               raise telepathy.errors.NotImplemented("Audio is the only stream type supported")
-
-               contact = self._conn.handle(telepathy.constants.HANDLE_TYPE_CONTACT, contactId)
-               assert self._contactHandle == contact, "%r != %r" % (self._contactHandle, contact)
-               contactId, contactNumber = handle.ContactHandle.from_handle_name(contact.name)
+               contact = self._conn.get_handle_by_id(telepathy.constants.HANDLE_TYPE_CONTACT, contactId)
+               assert self.__contactHandle == contact, "%r != %r" % (self.__contactHandle, contact)
+               contactNumber = contact.phoneNumber
 
+               self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_RINGING)
+               self.__cancelId = gobject.idle_add(self._on_cancel)
                self._conn.session.backend.call(contactNumber)
 
                streamId = 0
-               streamState = telepathy.constants.MEDIA_STREAM_STATE_DISCONNECTED
+               streamState = telepathy.constants.MEDIA_STREAM_STATE_CONNECTED
                streamDirection = telepathy.constants.MEDIA_STREAM_DIRECTION_BIDIRECTIONAL
                pendingSendFlags = telepathy.constants.MEDIA_STREAM_PENDING_REMOTE_SEND
                return [(streamId, contact, streamTypes[0], streamState, streamDirection, pendingSendFlags)]
@@ -84,4 +149,11 @@ class CallChannel(
                Get the current call states for all contacts involved in this call. 
                @returns {Contact: telepathy.constants.CHANNEL_CALL_STATE_*}
                """
-               return {}
+               return {self.__contactHandle: telepathy.constants.CHANNEL_CALL_STATE_FORWARDED}
+
+       @gtk_toolbox.log_exception(_moduleLogger)
+       def _on_cancel(self, *args):
+               self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_FORWARDED)
+               self.close()
+               self.__cancelId = None
+               return False