Bump to 0.8.24
[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.__calledNumber = 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                 self._delayedClose.cancel()
102
103                 tp.ChannelTypeStreamedMedia.Close(self)
104                 self.remove_from_connection()
105
106         @misc_utils.log_exception(_moduleLogger)
107         def GetLocalPendingMembersWithInfo(self):
108                 info = dbus.Array([], signature="(uuus)")
109                 for member in self._local_pending:
110                         info.append((member, self._handle, 0, ''))
111                 return info
112
113         @misc_utils.log_exception(_moduleLogger)
114         def AddMembers(self, handles, message):
115                 _moduleLogger.info("Add members %r: %s" % (handles, message))
116                 for handle in handles:
117                         if handle == int(self.GetSelfHandle()) and self.GetSelfHandle() in self._local_pending:
118                                 _moduleLogger.info("Technically the user just accepted the call")
119
120         @misc_utils.log_exception(_moduleLogger)
121         def RemoveMembers(self, handles, message):
122                 _moduleLogger.info("Remove members (no-op) %r: %s" % (handles, message))
123
124         @misc_utils.log_exception(_moduleLogger)
125         def RemoveMembersWithReason(self, handles, message, reason):
126                 _moduleLogger.info("Remove members (no-op) %r: %s (%i)" % (handles, message, reason))
127
128         @misc_utils.log_exception(_moduleLogger)
129         def ListStreams(self):
130                 """
131                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
132                 """
133                 return ()
134
135         @misc_utils.log_exception(_moduleLogger)
136         def RemoveStreams(self, streams):
137                 """
138                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
139                 """
140                 raise telepathy.errors.NotImplemented("Cannot remove a stream")
141
142         @misc_utils.log_exception(_moduleLogger)
143         def RequestStreamDirection(self, stream, streamDirection):
144                 """
145                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
146
147                 @note Since streams are short lived, not bothering to implement this
148                 """
149                 _moduleLogger.info("A request was made to change the stream direction")
150                 raise telepathy.errors.NotImplemented("Cannot change directions")
151
152         @misc_utils.log_exception(_moduleLogger)
153         def RequestStreams(self, contactId, streamTypes):
154                 """
155                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
156
157                 @returns [(Stream ID, contact, stream type, stream state, stream direction, pending send flags)]
158                 """
159                 contact = self._conn.get_handle_by_id(telepathy.constants.HANDLE_TYPE_CONTACT, contactId)
160                 assert self.__contactHandle == contact, "%r != %r" % (self.__contactHandle, contact)
161
162                 le = gobject_utils.AsyncLinearExecution(self._conn.session.pool, self._call)
163                 le.start(contact)
164
165                 streamId = 0
166                 streamState = telepathy.constants.MEDIA_STREAM_STATE_CONNECTED
167                 streamDirection = telepathy.constants.MEDIA_STREAM_DIRECTION_BIDIRECTIONAL
168                 pendingSendFlags = telepathy.constants.MEDIA_STREAM_PENDING_REMOTE_SEND
169                 return [(streamId, contact, streamTypes[0], streamState, streamDirection, pendingSendFlags)]
170
171         @misc_utils.log_exception(_moduleLogger)
172         def _call(self, contact):
173                 contactNumber = contact.phoneNumber
174
175                 self.__calledNumber = contactNumber
176                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_RINGING)
177
178                 self._delayedClose.start(seconds=0)
179                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_FORWARDED)
180
181                 try:
182                         result = yield (
183                                 self._conn.session.backend.call,
184                                 (contactNumber, ),
185                                 {},
186                         )
187                 except Exception, e:
188                         _moduleLogger.exception("While placing call to %s" % (self.__calledNumber, ))
189                         self._conn.force_log_display()
190                         accountNumber = misc_utils.normalize_number(self._conn.session.backend.get_account_number())
191                         self._conn.log_to_user(
192                                 __name__,
193                                 "Error while placing call from %s to %s:\n%s" % (
194                                         accountNumber, self.__calledNumber, str(e)
195                                 )
196                         )
197                         return
198
199         @misc_utils.log_exception(_moduleLogger)
200         def GetCallStates(self):
201                 """
202                 For org.freedesktop.Telepathy.Channel.Interface.CallState
203
204                 Get the current call states for all contacts involved in this call. 
205                 @returns {Contact: telepathy.constants.CHANNEL_CALL_STATE_*}
206                 """
207                 return {self.__contactHandle: telepathy.constants.CHANNEL_CALL_STATE_FORWARDED}
208
209         @misc_utils.log_exception(_moduleLogger)
210         def GetHoldState(self):
211                 """
212                 For org.freedesktop.Telepathy.Channel.Interface.Hold
213
214                 Get the current hold state
215                 @returns (HoldState, Reason)
216                 """
217                 return (
218                         telepathy.constants.LOCAL_HOLD_STATE_UNHELD,
219                         telepathy.constants.LOCAL_HOLD_STATE_REASON_NONE,
220                 )
221
222         @misc_utils.log_exception(_moduleLogger)
223         def RequestHold(self, Hold):
224                 """
225                 For org.freedesktop.Telepathy.Channel.Interface.Hold
226                 """
227                 if not Hold:
228                         return
229                 _moduleLogger.debug("Closing without cancel to get out of users way")
230                 self.close()
231
232         @misc_utils.log_exception(_moduleLogger)
233         def _on_close_requested(self, *args):
234                 _moduleLogger.debug("Cancel now disallowed")
235                 self.close()