Removing some debug stuff
[theonering] / hand_tests / dbus_signals.py
1 #!/usr/bin/env python
2
3 import sys
4 sys.path.insert(0,"../src")
5 import logging
6 import pprint
7
8 import gobject
9 import dbus
10 import dbus.mainloop.glib
11 import telepathy
12
13 import gtk_toolbox
14
15
16 _moduleLogger = logging.getLogger("receptionist")
17 DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
18
19
20 class AutoAcceptCall(object):
21
22         def __init__(self, bus, conn, chan, on_success, on_error):
23                 self._sessionBus = bus
24                 self._conn = conn
25                 self._chan = chan
26                 self._outstandingRequests = []
27                 self._on_success = on_success
28                 self._on_error = on_error
29
30                 self._initiatorHandle = None
31                 self._initiatorID = None
32                 self._targetHandle = None
33                 self._targetID = None
34                 self._pendingHandles = None
35
36                 self._chan[DBUS_PROPERTIES].GetAll(
37                         telepathy.interfaces.CHANNEL_INTERFACE,
38                         reply_handler = self._on_got_all,
39                         error_handler = self._custom_error(self._on_got_all),
40                 )
41                 self._outstandingRequests.append(self._on_got_all)
42
43                 if False:
44                         # @bug Unsure why this isn't working
45                         self._chan[DBUS_PROPERTIES].Get(
46                                 telepathy.interfaces.CHANNEL_INTERFACE_GROUP,
47                                 'LocalPendingMembers',
48                                 reply_handler = self._on_got_pending_members,
49                                 error_handler = self._custom_error(self._on_got_pending_members),
50                         )
51                 else:
52                         self._chan[telepathy.interfaces.CHANNEL_INTERFACE_GROUP].GetLocalPendingMembersWithInfo(
53                                 reply_handler = self._on_got_pending_members,
54                                 error_handler = self._custom_error(self._on_got_pending_members),
55                         )
56                 self._outstandingRequests.append(self._on_got_pending_members)
57
58         def is_inbound(self):
59                 isInbound = self._targetHandle == self._initiatorHandle
60                 return isInbound
61
62         @property
63         def initiator(self):
64                 return self._initiatorID
65
66         @property
67         def target(self):
68                 return self._targetID
69
70         def accept_call(self, on_accepted, on_error):
71                 self._chan[telepathy.interfaces.CHANNEL_INTERFACE_GROUP].AddMembers(
72                         self._pendingHandles,
73                         "",
74                         reply_handler = self._custom_on_accept(on_accepted),
75                         error_handler = self._custom_on_accept_error(on_error),
76                 )
77
78         def _custom_on_accept(self, callback):
79
80                 def on_accept(*args):
81                         callback(self)
82
83                 return on_accept
84
85         def _custom_on_accept_error(self, callback):
86
87                 def on_error(*args):
88                         callback(self, *args)
89
90                 return on_error
91
92         def _custom_error(self, action):
93
94                 def _on_error(*args):
95                         _moduleLogger.error("Failed for %r (%r)" % (action, args))
96                         self._outstandingRequests.remove(action)
97                         if self._outstandingRequests:
98                                 return
99
100                         self._on_error(self)
101
102                 return _on_error
103
104         def _report_callback_done(self, action):
105                 _moduleLogger.debug("Succeded with %r" % (action, ))
106                 self._outstandingRequests.remove(action)
107                 if self._outstandingRequests:
108                         return
109
110                 assert None not in (
111                         self._initiatorHandle,
112                         self._initiatorID,
113                         self._targetHandle,
114                         self._targetID,
115                         self._pendingHandles,
116                 )
117
118                 self._on_success(self)
119
120         @gtk_toolbox.log_exception(_moduleLogger)
121         def _on_got_all(self, properties):
122                 self._initiatorID = properties["InitiatorID"]
123                 self._initiatorHandle = properties["InitiatorHandle"]
124                 self._targetID = properties["InitiatorID"]
125                 self._targetHandle = properties["InitiatorHandle"]
126
127                 self._report_callback_done(self._on_got_all)
128
129         @gtk_toolbox.log_exception(_moduleLogger)
130         def _on_got_pending_members(self, pendings):
131                 for pendingHandle, instigatorHandle, reason, message in pendings:
132                         print pendingHandle, instigatorHandle, reason, message
133
134                 self._pendingHandles = [pendingWithInfo[0] for pendingWithInfo in pendings]
135
136                 self._report_callback_done(self._on_got_pending_members)
137
138
139 class NewChannelSignaller(object):
140
141         def __init__(self, on_new_channel):
142                 self._sessionBus = dbus.SessionBus()
143                 self._on_user_new_channel = on_new_channel
144
145         def start(self):
146                 self._sessionBus.add_signal_receiver(
147                         self._on_new_channel,
148                         "NewChannel",
149                         "org.freedesktop.Telepathy.Connection",
150                         None,
151                         None
152                 )
153
154         def stop(self):
155                 self._sessionBus.remove_signal_receiver(
156                         self._on_new_channel,
157                         "NewChannel",
158                         "org.freedesktop.Telepathy.Connection",
159                         None,
160                         None
161                 )
162
163         @gtk_toolbox.log_exception(_moduleLogger)
164         def _on_new_channel(
165                 self, channelObjectPath, channelType, handleType, handle, supressHandler
166         ):
167                 connObjectPath = channelObjectPath.rsplit("/", 1)[0]
168                 serviceName = connObjectPath[1:].replace("/", ".")
169                 conn = telepathy.client.Connection(serviceName, connObjectPath)
170                 chan = telepathy.client.Channel(serviceName, channelObjectPath)
171                 self._on_user_new_channel(self._sessionBus, conn, chan, channelType)
172
173
174 class Manager(object):
175
176         def __init__(self):
177                 self._newChannelSignaller = NewChannelSignaller(self._on_new_channel)
178
179         def start(self):
180                 self._newChannelSignaller.start()
181
182         @gtk_toolbox.log_exception(_moduleLogger)
183         def _on_new_channel(self, bus, conn, chan, channelType):
184                 pprint.pprint((bus, conn, chan, channelType))
185                 if channelType != telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA:
186                         return
187
188                 # @bug does not distinguish between preferred CMs
189                 attemptPickup = AutoAcceptCall(bus, conn, chan, self._on_inbound_call, self._on_inbound_call_error)
190
191         @gtk_toolbox.log_exception(_moduleLogger)
192         def _on_inbound_call(self, autoAcceptCall):
193                 # @todo Add a comparison for picking up for only certain contacts
194                 print autoAcceptCall.initiator, autoAcceptCall.target
195                 if autoAcceptCall.is_inbound():
196                         autoAcceptCall.accept_call(self._on_call_pickedup, self._on_pickup_error)
197                 else:
198                         _moduleLogger.debug(
199                                 "Not an inbound call (initiator=%r, target=%r)" % (autoAcceptCall.initiator, autoAcceptCall.target)
200                         )
201
202         @gtk_toolbox.log_exception(_moduleLogger)
203         def _on_inbound_call_error(self, *args):
204                 _moduleLogger.info("Inbound call error")
205
206         @gtk_toolbox.log_exception(_moduleLogger)
207         def _on_call_pickedup(self, autoAcceptCall):
208                 _moduleLogger.info("Call picked up")
209
210         @gtk_toolbox.log_exception(_moduleLogger)
211         def _on_pickup_error(self, autoAcceptCall, *args):
212                 _moduleLogger.info("Call failed to pick up (%r)" % (args, ))
213
214 if __name__ == "__main__":
215         logging.basicConfig(level=logging.DEBUG)
216         l = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
217         autoaccept = Manager()
218
219         gobject.threads_init()
220         gobject.idle_add(autoaccept.start)
221
222         mainloop = gobject.MainLoop(is_running=True)
223         mainloop.run()