Prepping for the Maemo 5 Beta
[theonering] / src / channel / call.py
1 import logging
2
3 import dbus
4 import gobject
5 import telepathy
6
7 import tp
8 import gtk_toolbox
9
10
11 _moduleLogger = logging.getLogger("channel.call")
12
13
14 class CallChannel(
15                 tp.ChannelTypeStreamedMedia,
16                 tp.ChannelInterfaceCallState,
17                 tp.ChannelInterfaceGroup,
18         ):
19
20         def __init__(self, connection, manager, props, contactHandle):
21                 self.__manager = manager
22                 self.__props = props
23                 self.__cancelId = None
24
25                 if telepathy.interfaces.CHANNEL_INTERFACE + '.InitiatorHandle' in props:
26                         self._initiator = connection.get_handle_by_id(
27                                 telepathy.HANDLE_TYPE_CONTACT,
28                                 props[telepathy.interfaces.CHANNEL_INTERFACE + '.InitiatorHandle'],
29                         )
30                 elif telepathy.interfaces.CHANNEL_INTERFACE + '.InitiatorID' in props:
31                         self._initiator = connection.get_handle_by_name(
32                                 telepathy.HANDLE_TYPE_CONTACT,
33                                 props[telepathy.interfaces.CHANNEL_INTERFACE + '.InitiatorHandle'],
34                         )
35                 else:
36                         # Maemo 5 seems to require InitiatorHandle/InitiatorID to be set
37                         # even though I can't find them in the dbus spec.  I think its
38                         # generally safe to assume that its locally initiated if not
39                         # specified.  Specially for The One Ring, its always locally
40                         # initiated
41                         _moduleLogger.warning('InitiatorID or InitiatorHandle not set on new channel, assuming locally initiated')
42                         self._initiator = connection.GetSelfHandle()
43
44                 tp.ChannelTypeStreamedMedia.__init__(self, connection, manager, props)
45                 tp.ChannelInterfaceCallState.__init__(self)
46                 tp.ChannelInterfaceGroup.__init__(self)
47                 self.__contactHandle = contactHandle
48                 self._implement_property_get(
49                         telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
50                         {
51                                 "InitialAudio": self.initial_audio,
52                                 "InitialVideo": self.initial_video,
53                         },
54                 )
55                 self._implement_property_get(
56                         telepathy.interfaces.CHANNEL_INTERFACE,
57                         {
58                                 'InitiatorHandle': lambda: dbus.UInt32(self._initiator.id),
59                                 'InitiatorID': lambda: self._initiator.name,
60                         },
61                 )
62
63                 self.GroupFlagsChanged(0, 0)
64                 self.MembersChanged(
65                         '', [self._conn.GetSelfHandle()], [], [], [contactHandle],
66                         0, telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE
67                 )
68
69         def initial_audio(self):
70                 return False
71
72         def initial_video(self):
73                 return False
74
75         @gtk_toolbox.log_exception(_moduleLogger)
76         def Close(self):
77                 self.close()
78
79         def close(self):
80                 _moduleLogger.debug("Closing call")
81                 tp.ChannelTypeStreamedMedia.Close(self)
82                 self.remove_from_connection()
83                 if self.__cancelId is not None:
84                         gobject.source_remove(self.__cancelId)
85                         self.__cancelId = None
86
87         @gtk_toolbox.log_exception(_moduleLogger)
88         def GetLocalPendingMembersWithInfo(self):
89                 return []
90
91         @gtk_toolbox.log_exception(_moduleLogger)
92         def ListStreams(self):
93                 """
94                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
95                 """
96                 return ()
97
98         @gtk_toolbox.log_exception(_moduleLogger)
99         def RemoveStreams(self, streams):
100                 """
101                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
102                 """
103                 raise telepathy.errors.NotImplemented("Cannot remove a stream")
104
105         @gtk_toolbox.log_exception(_moduleLogger)
106         def RequestStreamDirection(self, stream, streamDirection):
107                 """
108                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
109
110                 @note Since streams are short lived, not bothering to implement this
111                 """
112                 _moduleLogger.info("A request was made to change the stream direction")
113                 raise telepathy.errors.NotImplemented("Cannot change directions")
114
115         @gtk_toolbox.log_exception(_moduleLogger)
116         def RequestStreams(self, contactId, streamTypes):
117                 """
118                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
119
120                 @returns [(Stream ID, contact, stream type, stream state, stream direction, pending send flags)]
121                 """
122                 contact = self._conn.get_handle_by_id(telepathy.constants.HANDLE_TYPE_CONTACT, contactId)
123                 assert self.__contactHandle == contact, "%r != %r" % (self.__contactHandle, contact)
124                 contactNumber = contact.phoneNumber
125
126                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_RINGING)
127                 self.__cancelId = gobject.idle_add(self._on_cancel)
128                 self._conn.session.backend.call(contactNumber)
129
130                 streamId = 0
131                 streamState = telepathy.constants.MEDIA_STREAM_STATE_DISCONNECTED
132                 streamDirection = telepathy.constants.MEDIA_STREAM_DIRECTION_BIDIRECTIONAL
133                 pendingSendFlags = telepathy.constants.MEDIA_STREAM_PENDING_REMOTE_SEND
134                 return [(streamId, contact, streamTypes[0], streamState, streamDirection, pendingSendFlags)]
135
136         @gtk_toolbox.log_exception(_moduleLogger)
137         def GetCallStates(self):
138                 """
139                 For org.freedesktop.Telepathy.Channel.Interface.CallState
140
141                 Get the current call states for all contacts involved in this call. 
142                 @returns {Contact: telepathy.constants.CHANNEL_CALL_STATE_*}
143                 """
144                 return {self.__contactHandle: telepathy.constants.CHANNEL_CALL_STATE_FORWARDED}
145
146         @gtk_toolbox.log_exception(_moduleLogger)
147         def _on_cancel(self, *args):
148                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_FORWARDED)
149                 self.close()
150                 self.__cancelId = None
151                 return False