More imitation of Butterfly
[theonering] / src / channel / call.py
1 import logging
2
3 import dbus
4 import gobject
5 import telepathy
6
7 import tp
8 import gtk_toolbox
9
10
11 _moduleLogger = logging.getLogger("channel.call")
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.__cancelId = None
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._implement_property_get(
49                         telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
50                         {
51                                 "InitialAudio": self.initial_audio,
52                                 "InitialVideo": self.initial_video,
53                         },
54                 )
55                 self._add_immutables({
56                         'InitialAudio': telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
57                         'InitialVideo': telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
58                 })
59                 self._implement_property_get(
60                         telepathy.interfaces.CHANNEL_INTERFACE,
61                         {
62                                 'InitiatorHandle': lambda: dbus.UInt32(self._initiator.id),
63                                 'InitiatorID': lambda: self._initiator.name,
64                         },
65                 )
66                 self._add_immutables({
67                         'InitiatorHandle': telepathy.interfaces.CHANNEL_INTERFACE,
68                         'InitiatorID': telepathy.interfaces.CHANNEL_INTERFACE,
69                 })
70
71                 self.GroupFlagsChanged(0, 0)
72                 self.MembersChanged(
73                         '', [self._conn.GetSelfHandle()], [], [], [contactHandle],
74                         0, telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE
75                 )
76
77         def initial_audio(self):
78                 return False
79
80         def initial_video(self):
81                 return False
82
83         @gtk_toolbox.log_exception(_moduleLogger)
84         def Close(self):
85                 self.close()
86
87         def close(self):
88                 _moduleLogger.debug("Closing call")
89                 tp.ChannelTypeStreamedMedia.Close(self)
90                 self.remove_from_connection()
91                 if self.__cancelId is not None:
92                         gobject.source_remove(self.__cancelId)
93                         self.__cancelId = None
94
95         @gtk_toolbox.log_exception(_moduleLogger)
96         def GetLocalPendingMembersWithInfo(self):
97                 return []
98
99         @gtk_toolbox.log_exception(_moduleLogger)
100         def ListStreams(self):
101                 """
102                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
103                 """
104                 return ()
105
106         @gtk_toolbox.log_exception(_moduleLogger)
107         def RemoveStreams(self, streams):
108                 """
109                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
110                 """
111                 raise telepathy.errors.NotImplemented("Cannot remove a stream")
112
113         @gtk_toolbox.log_exception(_moduleLogger)
114         def RequestStreamDirection(self, stream, streamDirection):
115                 """
116                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
117
118                 @note Since streams are short lived, not bothering to implement this
119                 """
120                 _moduleLogger.info("A request was made to change the stream direction")
121                 raise telepathy.errors.NotImplemented("Cannot change directions")
122
123         @gtk_toolbox.log_exception(_moduleLogger)
124         def RequestStreams(self, contactId, streamTypes):
125                 """
126                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
127
128                 @returns [(Stream ID, contact, stream type, stream state, stream direction, pending send flags)]
129                 """
130                 contact = self._conn.get_handle_by_id(telepathy.constants.HANDLE_TYPE_CONTACT, contactId)
131                 assert self.__contactHandle == contact, "%r != %r" % (self.__contactHandle, contact)
132                 contactNumber = contact.phoneNumber
133
134                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_RINGING)
135                 self.__cancelId = gobject.idle_add(self._on_cancel)
136                 self._conn.session.backend.call(contactNumber)
137
138                 streamId = 0
139                 streamState = telepathy.constants.MEDIA_STREAM_STATE_DISCONNECTED
140                 streamDirection = telepathy.constants.MEDIA_STREAM_DIRECTION_BIDIRECTIONAL
141                 pendingSendFlags = telepathy.constants.MEDIA_STREAM_PENDING_REMOTE_SEND
142                 return [(streamId, contact, streamTypes[0], streamState, streamDirection, pendingSendFlags)]
143
144         @gtk_toolbox.log_exception(_moduleLogger)
145         def GetCallStates(self):
146                 """
147                 For org.freedesktop.Telepathy.Channel.Interface.CallState
148
149                 Get the current call states for all contacts involved in this call. 
150                 @returns {Contact: telepathy.constants.CHANNEL_CALL_STATE_*}
151                 """
152                 return {self.__contactHandle: telepathy.constants.CHANNEL_CALL_STATE_FORWARDED}
153
154         @gtk_toolbox.log_exception(_moduleLogger)
155         def _on_cancel(self, *args):
156                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_FORWARDED)
157                 self.close()
158                 self.__cancelId = None
159                 return False