Making phone numbers as consistent as possible so user started conversations don...
[theonering] / src / channel / call.py
1 import logging
2
3 import gobject
4 import telepathy
5
6 import tp
7 import gtk_toolbox
8
9
10 _moduleLogger = logging.getLogger("channel.call")
11
12
13 class CallChannel(
14                 tp.ChannelTypeStreamedMedia,
15                 tp.ChannelInterfaceCallState,
16                 tp.ChannelInterfaceGroup,
17         ):
18         # @bug On Maemo 5 this is having some kind of "General" error, possibly due to an issue with "GetAll" DBusProperties stuff
19
20         def __init__(self, connection, manager, props, contactHandle):
21                 self.__manager = manager
22                 self.__props = props
23                 self.__cancelId = None
24
25                 tp.ChannelTypeStreamedMedia.__init__(self, connection, manager, props)
26                 tp.ChannelInterfaceCallState.__init__(self)
27                 tp.ChannelInterfaceGroup.__init__(self)
28                 self.__contactHandle = contactHandle
29                 self._implement_property_get(
30                         telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
31                         {
32                                 "InitialAudio": self.initial_audio,
33                                 "InitialVideo": self.initial_video,
34                         },
35                 )
36
37                 self.GroupFlagsChanged(0, 0)
38                 self.MembersChanged(
39                         '', [self._conn.GetSelfHandle()], [], [], [contactHandle],
40                         0, telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE
41                 )
42
43         def initial_audio(self):
44                 return False
45
46         def initial_video(self):
47                 return False
48
49         @gtk_toolbox.log_exception(_moduleLogger)
50         def Close(self):
51                 self.close()
52
53         def close(self):
54                 tp.ChannelTypeStreamedMedia.Close(self)
55                 self.remove_from_connection()
56                 if self.__cancelId is not None:
57                         gobject.source_remove(self.__cancelId)
58                         self.__cancelId = None
59
60         @gtk_toolbox.log_exception(_moduleLogger)
61         def ListStreams(self):
62                 """
63                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
64                 """
65                 return ()
66
67         @gtk_toolbox.log_exception(_moduleLogger)
68         def RemoveStreams(self, streams):
69                 """
70                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
71                 """
72                 raise telepathy.errors.NotImplemented("Cannot remove a stream")
73
74         @gtk_toolbox.log_exception(_moduleLogger)
75         def RequestStreamDirection(self, stream, streamDirection):
76                 """
77                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
78
79                 @note Since streams are short lived, not bothering to implement this
80                 """
81                 _moduleLogger.info("A request was made to change the stream direction")
82                 raise telepathy.errors.NotImplemented("Cannot change directions")
83
84         @gtk_toolbox.log_exception(_moduleLogger)
85         def RequestStreams(self, contactId, streamTypes):
86                 """
87                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
88
89                 @returns [(Stream ID, contact, stream type, stream state, stream direction, pending send flags)]
90                 """
91                 contact = self._conn.get_handle_by_id(telepathy.constants.HANDLE_TYPE_CONTACT, contactId)
92                 assert self.__contactHandle == contact, "%r != %r" % (self.__contactHandle, contact)
93                 contactNumber = contact.phoneNumber
94
95                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_RINGING)
96                 self.__cancelId = gobject.idle_add(self._on_cancel)
97                 self._conn.session.backend.call(contactNumber)
98
99                 streamId = 0
100                 streamState = telepathy.constants.MEDIA_STREAM_STATE_DISCONNECTED
101                 streamDirection = telepathy.constants.MEDIA_STREAM_DIRECTION_BIDIRECTIONAL
102                 pendingSendFlags = telepathy.constants.MEDIA_STREAM_PENDING_REMOTE_SEND
103                 return [(streamId, contact, streamTypes[0], streamState, streamDirection, pendingSendFlags)]
104
105         @gtk_toolbox.log_exception(_moduleLogger)
106         def GetCallStates(self):
107                 """
108                 For org.freedesktop.Telepathy.Channel.Interface.CallState
109
110                 Get the current call states for all contacts involved in this call. 
111                 @returns {Contact: telepathy.constants.CHANNEL_CALL_STATE_*}
112                 """
113                 return {self.__contactHandle: telepathy.constants.CHANNEL_CALL_STATE_FORWARDED}
114
115         @gtk_toolbox.log_exception(_moduleLogger)
116         def _on_cancel(self, *args):
117                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_FORWARDED)
118                 self.close()
119                 self.__cancelId = None
120                 return False