Trying to improve behavior on n900
[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
49                 self._implement_property_get(
50                         telepathy.interfaces.CHANNEL_INTERFACE,
51                         {
52                                 'InitiatorHandle': lambda: dbus.UInt32(self._initiator.id),
53                                 'InitiatorID': lambda: self._initiator.name,
54                         },
55                 )
56                 self._add_immutables({
57                         'InitiatorHandle': telepathy.interfaces.CHANNEL_INTERFACE,
58                         'InitiatorID': telepathy.interfaces.CHANNEL_INTERFACE,
59                 })
60                 self._implement_property_get(
61                         telepathy.interfaces.CHANNEL_INTERFACE_GROUP,
62                         {
63                                 'LocalPendingMembers': lambda: self.GetLocalPendingMembersWithInfo()
64                         },
65                 )
66                 self._implement_property_get(
67                         telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
68                         {
69                                 "InitialAudio": self.initial_audio,
70                                 "InitialVideo": self.initial_video,
71                         },
72                 )
73                 self._add_immutables({
74                         'InitialAudio': telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
75                         'InitialVideo': telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
76                 })
77
78                 self.GroupFlagsChanged(0, 0)
79                 self.MembersChanged(
80                         '', [self._conn.GetSelfHandle()], [], [], [contactHandle],
81                         0, telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE
82                 )
83
84         def initial_audio(self):
85                 return False
86
87         def initial_video(self):
88                 return False
89
90         @gtk_toolbox.log_exception(_moduleLogger)
91         def Close(self):
92                 self.close()
93
94         def close(self):
95                 _moduleLogger.debug("Closing call")
96                 tp.ChannelTypeStreamedMedia.Close(self)
97                 self.remove_from_connection()
98                 if self.__cancelId is not None:
99                         gobject.source_remove(self.__cancelId)
100                         self.__cancelId = None
101
102         @gtk_toolbox.log_exception(_moduleLogger)
103         def GetLocalPendingMembersWithInfo(self):
104                 info = dbus.Array([], signature="(uuus)")
105                 for member in self._local_pending:
106                         info.append((member, self._handle, 0, ''))
107                 return info
108
109         @gtk_toolbox.log_exception(_moduleLogger)
110         def AddMembers(self, handles, message):
111                 _moduleLogger.info("Add members %r: %s" % (handles, message))
112                 for handle in handles:
113                         if handle == int(self.GetSelfHandle()) and self.GetSelfHandle() in self._local_pending:
114                                 _moduleLogger.info("Technically the user just accepted the call")
115
116         @gtk_toolbox.log_exception(_moduleLogger)
117         def RemoveMembers(self, handles, message):
118                 _moduleLogger.info("Remove members (no-op) %r: %s" % (handles, message))
119
120         @gtk_toolbox.log_exception(_moduleLogger)
121         def RemoveMembersWithReason(self, handles, message, reason):
122                 _moduleLogger.info("Remove members (no-op) %r: %s (%i)" % (handles, message, reason))
123
124         @gtk_toolbox.log_exception(_moduleLogger)
125         def ListStreams(self):
126                 """
127                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
128                 """
129                 return ()
130
131         @gtk_toolbox.log_exception(_moduleLogger)
132         def RemoveStreams(self, streams):
133                 """
134                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
135                 """
136                 raise telepathy.errors.NotImplemented("Cannot remove a stream")
137
138         @gtk_toolbox.log_exception(_moduleLogger)
139         def RequestStreamDirection(self, stream, streamDirection):
140                 """
141                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
142
143                 @note Since streams are short lived, not bothering to implement this
144                 """
145                 _moduleLogger.info("A request was made to change the stream direction")
146                 raise telepathy.errors.NotImplemented("Cannot change directions")
147
148         @gtk_toolbox.log_exception(_moduleLogger)
149         def RequestStreams(self, contactId, streamTypes):
150                 """
151                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
152
153                 @returns [(Stream ID, contact, stream type, stream state, stream direction, pending send flags)]
154                 """
155                 contact = self._conn.get_handle_by_id(telepathy.constants.HANDLE_TYPE_CONTACT, contactId)
156                 assert self.__contactHandle == contact, "%r != %r" % (self.__contactHandle, contact)
157                 contactNumber = contact.phoneNumber
158
159                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_RINGING)
160                 self.__cancelId = gobject.idle_add(self._on_cancel)
161                 self._conn.session.backend.call(contactNumber)
162
163                 streamId = 0
164                 streamState = telepathy.constants.MEDIA_STREAM_STATE_CONNECTED
165                 streamDirection = telepathy.constants.MEDIA_STREAM_DIRECTION_BIDIRECTIONAL
166                 pendingSendFlags = telepathy.constants.MEDIA_STREAM_PENDING_REMOTE_SEND
167                 return [(streamId, contact, streamTypes[0], streamState, streamDirection, pendingSendFlags)]
168
169         @gtk_toolbox.log_exception(_moduleLogger)
170         def GetCallStates(self):
171                 """
172                 For org.freedesktop.Telepathy.Channel.Interface.CallState
173
174                 Get the current call states for all contacts involved in this call. 
175                 @returns {Contact: telepathy.constants.CHANNEL_CALL_STATE_*}
176                 """
177                 return {self.__contactHandle: telepathy.constants.CHANNEL_CALL_STATE_FORWARDED}
178
179         @gtk_toolbox.log_exception(_moduleLogger)
180         def _on_cancel(self, *args):
181                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_FORWARDED)
182                 self.close()
183                 self.__cancelId = None
184                 return False