Reducing the timeout for closing the channel and cleaning up some code
[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.ChannelInterfaceGroup,
17                 tp.ChannelInterfaceCallState,
18                 tp.ChannelInterfaceHold,
19         ):
20
21         def __init__(self, connection, manager, props, contactHandle):
22                 self.__manager = manager
23                 self.__props = props
24                 self._delayedClose = gobject_utils.Timeout(self._on_close_requested)
25
26                 if telepathy.interfaces.CHANNEL_INTERFACE + '.InitiatorHandle' in props:
27                         self._initiator = connection.get_handle_by_id(
28                                 telepathy.HANDLE_TYPE_CONTACT,
29                                 props[telepathy.interfaces.CHANNEL_INTERFACE + '.InitiatorHandle'],
30                         )
31                 elif telepathy.interfaces.CHANNEL_INTERFACE + '.InitiatorID' in props:
32                         self._initiator = connection.get_handle_by_name(
33                                 telepathy.HANDLE_TYPE_CONTACT,
34                                 props[telepathy.interfaces.CHANNEL_INTERFACE + '.InitiatorHandle'],
35                         )
36                 else:
37                         # Maemo 5 seems to require InitiatorHandle/InitiatorID to be set
38                         # even though I can't find them in the dbus spec.  I think its
39                         # generally safe to assume that its locally initiated if not
40                         # specified.  Specially for The One Ring, its always locally
41                         # initiated
42                         _moduleLogger.warning('InitiatorID or InitiatorHandle not set on new channel, assuming locally initiated')
43                         self._initiator = connection.GetSelfHandle()
44
45                 tp.ChannelTypeStreamedMedia.__init__(self, connection, manager, props)
46                 tp.ChannelInterfaceGroup.__init__(self)
47                 tp.ChannelInterfaceCallState.__init__(self)
48                 tp.ChannelInterfaceHold.__init__(self)
49                 self.__contactHandle = contactHandle
50                 self.__calledNumer = None
51
52                 self._implement_property_get(
53                         telepathy.interfaces.CHANNEL_INTERFACE,
54                         {
55                                 'InitiatorHandle': lambda: dbus.UInt32(self._initiator.id),
56                                 'InitiatorID': lambda: self._initiator.name,
57                         },
58                 )
59                 self._add_immutables({
60                         'InitiatorHandle': telepathy.interfaces.CHANNEL_INTERFACE,
61                         'InitiatorID': telepathy.interfaces.CHANNEL_INTERFACE,
62                 })
63                 self._implement_property_get(
64                         telepathy.interfaces.CHANNEL_INTERFACE_GROUP,
65                         {
66                                 'LocalPendingMembers': lambda: self.GetLocalPendingMembersWithInfo()
67                         },
68                 )
69                 self._implement_property_get(
70                         telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
71                         {
72                                 "InitialAudio": self.initial_audio,
73                                 "InitialVideo": self.initial_video,
74                         },
75                 )
76                 self._add_immutables({
77                         'InitialAudio': telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
78                         'InitialVideo': telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
79                 })
80
81                 self.GroupFlagsChanged(0, 0)
82                 added, removed = [self._conn.GetSelfHandle()], []
83                 localPending, remotePending = [], [contactHandle]
84                 self.MembersChanged(
85                         '', added, removed, localPending, remotePending,
86                         0, telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE
87                 )
88
89         def initial_audio(self):
90                 return False
91
92         def initial_video(self):
93                 return False
94
95         @misc_utils.log_exception(_moduleLogger)
96         def Close(self):
97                 self.close()
98
99         def close(self):
100                 _moduleLogger.debug("Closing call")
101                 tp.ChannelTypeStreamedMedia.Close(self)
102                 self.remove_from_connection()
103                 if self.__calledNumer is not None:
104                         self._conn.session.backend.cancel(self.__calledNumer)
105                 self._delayedClose.cancel()
106
107         @misc_utils.log_exception(_moduleLogger)
108         def GetLocalPendingMembersWithInfo(self):
109                 info = dbus.Array([], signature="(uuus)")
110                 for member in self._local_pending:
111                         info.append((member, self._handle, 0, ''))
112                 return info
113
114         @misc_utils.log_exception(_moduleLogger)
115         def AddMembers(self, handles, message):
116                 _moduleLogger.info("Add members %r: %s" % (handles, message))
117                 for handle in handles:
118                         if handle == int(self.GetSelfHandle()) and self.GetSelfHandle() in self._local_pending:
119                                 _moduleLogger.info("Technically the user just accepted the call")
120
121         @misc_utils.log_exception(_moduleLogger)
122         def RemoveMembers(self, handles, message):
123                 _moduleLogger.info("Remove members (no-op) %r: %s" % (handles, message))
124
125         @misc_utils.log_exception(_moduleLogger)
126         def RemoveMembersWithReason(self, handles, message, reason):
127                 _moduleLogger.info("Remove members (no-op) %r: %s (%i)" % (handles, message, reason))
128
129         @misc_utils.log_exception(_moduleLogger)
130         def ListStreams(self):
131                 """
132                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
133                 """
134                 return ()
135
136         @misc_utils.log_exception(_moduleLogger)
137         def RemoveStreams(self, streams):
138                 """
139                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
140                 """
141                 raise telepathy.errors.NotImplemented("Cannot remove a stream")
142
143         @misc_utils.log_exception(_moduleLogger)
144         def RequestStreamDirection(self, stream, streamDirection):
145                 """
146                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
147
148                 @note Since streams are short lived, not bothering to implement this
149                 """
150                 _moduleLogger.info("A request was made to change the stream direction")
151                 raise telepathy.errors.NotImplemented("Cannot change directions")
152
153         @misc_utils.log_exception(_moduleLogger)
154         def RequestStreams(self, contactId, streamTypes):
155                 """
156                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
157
158                 @returns [(Stream ID, contact, stream type, stream state, stream direction, pending send flags)]
159                 """
160                 contact = self._conn.get_handle_by_id(telepathy.constants.HANDLE_TYPE_CONTACT, contactId)
161                 assert self.__contactHandle == contact, "%r != %r" % (self.__contactHandle, contact)
162                 contactNumber = contact.phoneNumber
163
164                 self.__calledNumer = contactNumber
165                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_RINGING)
166                 self._conn.session.backend.call(contactNumber)
167                 self._delayedClose.start(seconds=0)
168                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_FORWARDED)
169
170                 streamId = 0
171                 streamState = telepathy.constants.MEDIA_STREAM_STATE_CONNECTED
172                 streamDirection = telepathy.constants.MEDIA_STREAM_DIRECTION_BIDIRECTIONAL
173                 pendingSendFlags = telepathy.constants.MEDIA_STREAM_PENDING_REMOTE_SEND
174                 return [(streamId, contact, streamTypes[0], streamState, streamDirection, pendingSendFlags)]
175
176         @misc_utils.log_exception(_moduleLogger)
177         def GetCallStates(self):
178                 """
179                 For org.freedesktop.Telepathy.Channel.Interface.CallState
180
181                 Get the current call states for all contacts involved in this call. 
182                 @returns {Contact: telepathy.constants.CHANNEL_CALL_STATE_*}
183                 """
184                 return {self.__contactHandle: telepathy.constants.CHANNEL_CALL_STATE_FORWARDED}
185
186         @misc_utils.log_exception(_moduleLogger)
187         def GetHoldState(self):
188                 """
189                 For org.freedesktop.Telepathy.Channel.Interface.Hold
190
191                 Get the current hold state
192                 @returns (HoldState, Reason)
193                 """
194                 return (
195                         telepathy.constants.LOCAL_HOLD_STATE_UNHELD,
196                         telepathy.constants.LOCAL_HOLD_STATE_REASON_NONE,
197                 )
198
199         @misc_utils.log_exception(_moduleLogger)
200         def RequestHold(self, Hold):
201                 """
202                 For org.freedesktop.Telepathy.Channel.Interface.Hold
203                 """
204                 if not Hold:
205                         return
206                 _moduleLogger.debug("Closing without cancel to get out of users way")
207                 self.__calledNumer = None
208                 self.close()
209
210         @misc_utils.log_exception(_moduleLogger)
211         def _on_close_requested(self, *args):
212                 _moduleLogger.debug("Cancel now disallowed")
213                 self.__calledNumer = None
214                 self.close()