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