b21a310395e638a05fbfc3c6bc969d6a52a15f3f
[theonering] / src / channel / call.py
1 import logging
2
3 import telepathy
4
5 import gtk_toolbox
6 import handle
7
8
9 _moduleLogger = logging.getLogger("channel.call")
10
11
12 class CallChannel(
13                 telepathy.server.ChannelTypeStreamedMedia,
14                 telepathy.server.ChannelInterfaceCallState,
15                 telepathy.server.ChannelInterfaceGroup,
16         ):
17
18         def __init__(self, connection, manager, props, contactHandle):
19                 self.__manager = manager
20                 self.__props = props
21
22                 try:
23                         # HACK Older python-telepathy way
24                         telepathy.server.ChannelTypeStreamedMedia.__init__(self, connection, contactHandle)
25                         self._requested = props[telepathy.interfaces.CHANNEL_INTERFACE + '.Requested']
26                         self._implement_property_get(
27                                 telepathy.interfaces.CHANNEL_INTERFACE,
28                                 {"Requested": lambda: self._requested}
29                         )
30                 except TypeError:
31                         # HACK Newer python-telepathy way
32                         telepathy.server.ChannelTypeStreamedMedia.__init__(self, connection, manager, props)
33                 telepathy.server.ChannelInterfaceCallState.__init__(self)
34                 telepathy.server.ChannelInterfaceGroup.__init__(self)
35                 self.__contactHandle = contactHandle
36                 self._implement_property_get(
37                         telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA,
38                         {
39                                 "InitialAudio": self.initial_audio,
40                                 "InitialVideo": self.initial_video,
41                         },
42                 )
43
44                 # HACK Older python-telepathy doesn't provide this
45                 self._immutable_properties = {
46                         'ChannelType': telepathy.server.interfaces.CHANNEL_INTERFACE,
47                         'TargetHandle': telepathy.server.interfaces.CHANNEL_INTERFACE,
48                         'Interfaces': telepathy.server.interfaces.CHANNEL_INTERFACE,
49                         'TargetHandleType': telepathy.server.interfaces.CHANNEL_INTERFACE,
50                         'TargetID': telepathy.server.interfaces.CHANNEL_INTERFACE,
51                         'Requested': telepathy.server.interfaces.CHANNEL_INTERFACE
52                 }
53
54                 self.GroupFlagsChanged(0, 0)
55                 self.MembersChanged(
56                         '', [self._conn.GetSelfHandle()], [], [], [contactHandle],
57                         0, telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE
58                 )
59
60         def initial_audio(self):
61                 return False
62
63         def initial_video(self):
64                 return False
65
66         def get_props(self):
67                 # HACK Older python-telepathy doesn't provide this
68                 props = dict()
69                 for prop, iface in self._immutable_properties.items():
70                         props[iface + '.' + prop] = \
71                                 self._prop_getters[iface][prop]()
72                 return props
73
74         @gtk_toolbox.log_exception(_moduleLogger)
75         def Close(self):
76                 self.close()
77
78         def close(self):
79                 telepathy.server.ChannelTypeStreamedMedia.Close(self)
80                 if self.__manager.channel_exists(self.__props):
81                         # HACK Older python-telepathy requires doing this manually
82                         self.__manager.remove_channel(self)
83                 self.remove_from_connection()
84
85         @gtk_toolbox.log_exception(_moduleLogger)
86         def ListStreams(self):
87                 """
88                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
89                 """
90                 return ()
91
92         @gtk_toolbox.log_exception(_moduleLogger)
93         def RemoveStreams(self, streams):
94                 """
95                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
96                 """
97                 raise telepathy.errors.NotImplemented("Cannot remove a stream")
98
99         @gtk_toolbox.log_exception(_moduleLogger)
100         def RequestStreamDirection(self, stream, streamDirection):
101                 """
102                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
103
104                 @note Since streams are short lived, not bothering to implement this
105                 """
106                 _moduleLogger.info("A request was made to change the stream direction")
107                 raise telepathy.errors.NotImplemented("Cannot change directions")
108
109         @gtk_toolbox.log_exception(_moduleLogger)
110         def RequestStreams(self, contactId, streamTypes):
111                 """
112                 For org.freedesktop.Telepathy.Channel.Type.StreamedMedia
113
114                 @returns [(Stream ID, contact, stream type, stream state, stream direction, pending send flags)]
115                 """
116                 contact = self._conn.handle(telepathy.constants.HANDLE_TYPE_CONTACT, contactId)
117                 assert self.__contactHandle == contact, "%r != %r" % (self.__contactHandle, contact)
118                 contactId, contactNumber = handle.ContactHandle.from_handle_name(contact.name)
119
120                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_RINGING)
121                 self._conn.session.backend.call(contactNumber)
122                 self.CallStateChanged(self.__contactHandle, telepathy.constants.CHANNEL_CALL_STATE_FORWARDED)
123
124                 streamId = 0
125                 streamState = telepathy.constants.MEDIA_STREAM_STATE_DISCONNECTED
126                 streamDirection = telepathy.constants.MEDIA_STREAM_DIRECTION_BIDIRECTIONAL
127                 pendingSendFlags = telepathy.constants.MEDIA_STREAM_PENDING_REMOTE_SEND
128                 return [(streamId, contact, streamTypes[0], streamState, streamDirection, pendingSendFlags)]
129
130         @gtk_toolbox.log_exception(_moduleLogger)
131         def GetCallStates(self):
132                 """
133                 For org.freedesktop.Telepathy.Channel.Interface.CallState
134
135                 Get the current call states for all contacts involved in this call. 
136                 @returns {Contact: telepathy.constants.CHANNEL_CALL_STATE_*}
137                 """
138                 return {self.__contactHandle: telepathy.constants.CHANNEL_CALL_STATE_FORWARDED}