Making the code slightly easier to maintain
[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._delayedCancel = gobject_utils.Async(self._on_cancel)
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         @misc_utils.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                 self._delayedCancel.cancel()
99
100         @misc_utils.log_exception(_moduleLogger)
101         def GetLocalPendingMembersWithInfo(self):
102                 info = dbus.Array([], signature="(uuus)")
103                 for member in self._local_pending:
104                         info.append((member, self._handle, 0, ''))
105                 return info
106
107         @misc_utils.log_exception(_moduleLogger)
108         def AddMembers(self, handles, message):
109                 _moduleLogger.info("Add members %r: %s" % (handles, message))
110                 for handle in handles:
111                         if handle == int(self.GetSelfHandle()) and self.GetSelfHandle() in self._local_pending:
112                                 _moduleLogger.info("Technically the user just accepted the call")
113
114         @misc_utils.log_exception(_moduleLogger)
115         def RemoveMembers(self, handles, message):
116                 _moduleLogger.info("Remove members (no-op) %r: %s" % (handles, message))
117
118         @misc_utils.log_exception(_moduleLogger)
119         def RemoveMembersWithReason(self, handles, message, reason):
120                 _moduleLogger.info("Remove members (no-op) %r: %s (%i)" % (handles, message, reason))
121
122         @misc_utils.log_exception(_moduleLogger)
123         def ListStreams(self):
124                 """
125                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
126                 """
127                 return ()
128
129         @misc_utils.log_exception(_moduleLogger)
130         def RemoveStreams(self, streams):
131                 """
132                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
133                 """
134                 raise telepathy.errors.NotImplemented("Cannot remove a stream")
135
136         @misc_utils.log_exception(_moduleLogger)
137         def RequestStreamDirection(self, stream, streamDirection):
138                 """
139                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
140
141                 @note Since streams are short lived, not bothering to implement this
142                 """
143                 _moduleLogger.info("A request was made to change the stream direction")
144                 raise telepathy.errors.NotImplemented("Cannot change directions")
145
146         @misc_utils.log_exception(_moduleLogger)
147         def RequestStreams(self, contactId, streamTypes):
148                 """
149                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
150
151                 @returns [(Stream ID, contact, stream type, stream state, stream direction, pending send flags)]
152                 """
153                 contact = self._conn.get_handle_by_id(telepathy.constants.HANDLE_TYPE_CONTACT, contactId)
154                 assert self.__contactHandle == contact, "%r != %r" % (self.__contactHandle, contact)
155                 contactNumber = contact.phoneNumber
156
157                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_RINGING)
158                 self._delayedCancel.start()
159                 self._conn.session.backend.call(contactNumber)
160
161                 streamId = 0
162                 streamState = telepathy.constants.MEDIA_STREAM_STATE_CONNECTED
163                 streamDirection = telepathy.constants.MEDIA_STREAM_DIRECTION_BIDIRECTIONAL
164                 pendingSendFlags = telepathy.constants.MEDIA_STREAM_PENDING_REMOTE_SEND
165                 return [(streamId, contact, streamTypes[0], streamState, streamDirection, pendingSendFlags)]
166
167         @misc_utils.log_exception(_moduleLogger)
168         def GetCallStates(self):
169                 """
170                 For org.freedesktop.Telepathy.Channel.Interface.CallState
171
172                 Get the current call states for all contacts involved in this call. 
173                 @returns {Contact: telepathy.constants.CHANNEL_CALL_STATE_*}
174                 """
175                 return {self.__contactHandle: telepathy.constants.CHANNEL_CALL_STATE_FORWARDED}
176
177         @misc_utils.log_exception(_moduleLogger)
178         def _on_cancel(self, *args):
179                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_FORWARDED)
180                 self.close()