Async connect/disconnect. Moved everything to a new Async and Timeout which cleans...
[theonering] / src / channel / call.py
1 import logging
2
3 import dbus
4 import gobject
5 import telepathy
6
7 import tp
8 import util.go_utils as gobject_utils
9 import util.misc as misc_utils
10
11
12 _moduleLogger = logging.getLogger("channel.call")
13
14
15 class CallChannel(
16                 tp.ChannelTypeStreamedMedia,
17                 tp.ChannelInterfaceCallState,
18                 tp.ChannelInterfaceGroup,
19         ):
20
21         def __init__(self, connection, manager, props, contactHandle):
22                 self.__manager = manager
23                 self.__props = props
24                 self._delayedCancel = gobject_utils.Async(self._on_cancel)
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.ChannelInterfaceCallState.__init__(self)
47                 tp.ChannelInterfaceGroup.__init__(self)
48                 self.__contactHandle = contactHandle
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                 self._delayedCancel.cancel()
100
101         @misc_utils.log_exception(_moduleLogger)
102         def GetLocalPendingMembersWithInfo(self):
103                 info = dbus.Array([], signature="(uuus)")
104                 for member in self._local_pending:
105                         info.append((member, self._handle, 0, ''))
106                 return info
107
108         @misc_utils.log_exception(_moduleLogger)
109         def AddMembers(self, handles, message):
110                 _moduleLogger.info("Add members %r: %s" % (handles, message))
111                 for handle in handles:
112                         if handle == int(self.GetSelfHandle()) and self.GetSelfHandle() in self._local_pending:
113                                 _moduleLogger.info("Technically the user just accepted the call")
114
115         @misc_utils.log_exception(_moduleLogger)
116         def RemoveMembers(self, handles, message):
117                 _moduleLogger.info("Remove members (no-op) %r: %s" % (handles, message))
118
119         @misc_utils.log_exception(_moduleLogger)
120         def RemoveMembersWithReason(self, handles, message, reason):
121                 _moduleLogger.info("Remove members (no-op) %r: %s (%i)" % (handles, message, reason))
122
123         @misc_utils.log_exception(_moduleLogger)
124         def ListStreams(self):
125                 """
126                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
127                 """
128                 return ()
129
130         @misc_utils.log_exception(_moduleLogger)
131         def RemoveStreams(self, streams):
132                 """
133                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
134                 """
135                 raise telepathy.errors.NotImplemented("Cannot remove a stream")
136
137         @misc_utils.log_exception(_moduleLogger)
138         def RequestStreamDirection(self, stream, streamDirection):
139                 """
140                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
141
142                 @note Since streams are short lived, not bothering to implement this
143                 """
144                 _moduleLogger.info("A request was made to change the stream direction")
145                 raise telepathy.errors.NotImplemented("Cannot change directions")
146
147         @misc_utils.log_exception(_moduleLogger)
148         def RequestStreams(self, contactId, streamTypes):
149                 """
150                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
151
152                 @returns [(Stream ID, contact, stream type, stream state, stream direction, pending send flags)]
153                 """
154                 contact = self._conn.get_handle_by_id(telepathy.constants.HANDLE_TYPE_CONTACT, contactId)
155                 assert self.__contactHandle == contact, "%r != %r" % (self.__contactHandle, contact)
156                 contactNumber = contact.phoneNumber
157
158                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_RINGING)
159                 self._delayedCancel.start()
160                 self._conn.session.backend.call(contactNumber)
161
162                 streamId = 0
163                 streamState = telepathy.constants.MEDIA_STREAM_STATE_CONNECTED
164                 streamDirection = telepathy.constants.MEDIA_STREAM_DIRECTION_BIDIRECTIONAL
165                 pendingSendFlags = telepathy.constants.MEDIA_STREAM_PENDING_REMOTE_SEND
166                 return [(streamId, contact, streamTypes[0], streamState, streamDirection, pendingSendFlags)]
167
168         @misc_utils.log_exception(_moduleLogger)
169         def GetCallStates(self):
170                 """
171                 For org.freedesktop.Telepathy.Channel.Interface.CallState
172
173                 Get the current call states for all contacts involved in this call. 
174                 @returns {Contact: telepathy.constants.CHANNEL_CALL_STATE_*}
175                 """
176                 return {self.__contactHandle: telepathy.constants.CHANNEL_CALL_STATE_FORWARDED}
177
178         @misc_utils.log_exception(_moduleLogger)
179         def _on_cancel(self, *args):
180                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_FORWARDED)
181                 self.close()