4 sys.path.insert(0,"../src")
10 import dbus.mainloop.glib
13 import util.go_utils as gobject_utils
17 _moduleLogger = logging.getLogger("receptionist")
18 DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
21 class AutoAcceptCall(object):
23 def __init__(self, bus, conn, chan, on_success, on_error):
24 self._sessionBus = bus
27 self._outstandingRequests = []
28 self._on_success = on_success
29 self._on_error = on_error
31 self._initiatorHandle = None
32 self._initiatorID = None
33 self._targetHandle = None
35 self._requested = None
36 self._pendingHandles = None
38 self._chan[DBUS_PROPERTIES].GetAll(
39 telepathy.interfaces.CHANNEL_INTERFACE,
40 reply_handler = self._on_got_all,
41 error_handler = self._custom_error(self._on_got_all),
43 self._outstandingRequests.append(self._on_got_all)
46 # @bug Unsure why this isn't working
47 self._chan[DBUS_PROPERTIES].Get(
48 telepathy.interfaces.CHANNEL_INTERFACE_GROUP,
49 'LocalPendingMembers',
50 reply_handler = self._on_got_pending_members,
51 error_handler = self._custom_error(self._on_got_pending_members),
54 self._chan[telepathy.interfaces.CHANNEL_INTERFACE_GROUP].GetLocalPendingMembersWithInfo(
55 reply_handler = self._on_got_pending_members,
56 error_handler = self._custom_error(self._on_got_pending_members),
58 self._outstandingRequests.append(self._on_got_pending_members)
61 return not self._requested
62 isInbound = self._targetHandle == self._initiatorHandle
67 return self._initiatorID
73 def accept_call(self, on_accepted, on_error):
74 self._chan[telepathy.interfaces.CHANNEL_INTERFACE_GROUP].AddMembers(
77 reply_handler = self._custom_on_accept(on_accepted),
78 error_handler = self._custom_on_accept_error(on_error),
81 def _custom_on_accept(self, callback):
88 def _custom_on_accept_error(self, callback):
95 def _custom_error(self, action):
98 _moduleLogger.error("Failed for %r (%r)" % (action, args))
99 self._outstandingRequests.remove(action)
100 if self._outstandingRequests:
107 def _report_callback_done(self, action):
108 _moduleLogger.debug("Succeded with %r" % (action, ))
109 self._outstandingRequests.remove(action)
110 if self._outstandingRequests:
114 self._initiatorHandle,
118 self._pendingHandles,
122 self._on_success(self)
124 @gtk_toolbox.log_exception(_moduleLogger)
125 def _on_got_all(self, properties):
126 self._initiatorID = properties["InitiatorID"]
127 self._initiatorHandle = properties["InitiatorHandle"]
128 self._targetID = properties["InitiatorID"]
129 self._targetHandle = properties["InitiatorHandle"]
130 self._requested = properties["Requested"]
132 self._report_callback_done(self._on_got_all)
134 @gtk_toolbox.log_exception(_moduleLogger)
135 def _on_got_pending_members(self, pendings):
136 for pendingHandle, instigatorHandle, reason, message in pendings:
137 print pendingHandle, instigatorHandle, reason, message
139 self._pendingHandles = [pendingWithInfo[0] for pendingWithInfo in pendings]
141 self._report_callback_done(self._on_got_pending_members)
144 class WasMissedCall(object):
146 def __init__(self, bus, conn, chan, on_success, on_error):
147 self._on_success = on_success
148 self._on_error = on_error
150 self._requested = None
151 self._didMembersChange = False
152 self._didClose = False
153 self._didReport = False
155 self._timeoutId = gobject_utils.timeout_add_seconds(10, self._on_timeout)
157 chan[telepathy.interfaces.CHANNEL_INTERFACE_GROUP].connect_to_signal(
159 self._on_members_changed,
162 chan[telepathy.interfaces.CHANNEL].connect_to_signal(
167 chan[DBUS_PROPERTIES].GetAll(
168 telepathy.interfaces.CHANNEL_INTERFACE,
169 reply_handler = self._on_got_all,
170 error_handler = self._on_got_all,
174 self._report_error("by request")
176 def _report_missed_if_ready(self):
179 elif self._requested is not None and (self._didMembersChange or self._didClose):
181 self._report_error("wrong direction")
183 self._report_success()
185 self._report_error("members added")
188 self._report_error("closed too early")
190 def _report_success(self):
191 assert not self._didReport
192 self._didReport = True
194 gobject.source_remove(self._timeoutId)
195 self._timeoutId = None
196 self._on_success(self)
198 def _report_error(self, reason):
199 assert not self._didReport
200 self._didReport = True
202 gobject.source_remove(self._timeoutId)
203 self._timeoutId = None
204 self._on_error(self, reason)
206 @gtk_toolbox.log_exception(_moduleLogger)
207 def _on_got_all(self, properties):
208 self._requested = properties["Requested"]
209 self._report_missed_if_ready()
211 @gtk_toolbox.log_exception(_moduleLogger)
212 def _on_members_changed(self, message, added, removed, lp, rp, actor, reason):
213 pprint.pprint((message, added, removed, lp, rp, actor, reason))
215 self._didMembersChange = True
216 self._report_missed_if_ready()
218 @gtk_toolbox.log_exception(_moduleLogger)
219 def _on_closed(self):
220 self._didClose = True
221 self._report_missed_if_ready()
223 @gtk_toolbox.log_exception(_moduleLogger)
224 def _on_error(self, *args):
225 self._report_error(args)
227 @gtk_toolbox.log_exception(_moduleLogger)
228 def _on_timeout(self):
229 self._report_error("timeout")
233 class NewChannelSignaller(object):
235 def __init__(self, on_new_channel):
236 self._sessionBus = dbus.SessionBus()
237 self._on_user_new_channel = on_new_channel
240 self._sessionBus.add_signal_receiver(
241 self._on_new_channel,
243 "org.freedesktop.Telepathy.Connection",
249 self._sessionBus.remove_signal_receiver(
250 self._on_new_channel,
252 "org.freedesktop.Telepathy.Connection",
257 @gtk_toolbox.log_exception(_moduleLogger)
259 self, channelObjectPath, channelType, handleType, handle, supressHandler
261 connObjectPath = channelObjectPath.rsplit("/", 1)[0]
262 serviceName = connObjectPath[1:].replace("/", ".")
263 conn = telepathy.client.Connection(serviceName, connObjectPath)
264 chan = telepathy.client.Channel(serviceName, channelObjectPath)
265 self._on_user_new_channel(self._sessionBus, conn, chan, channelType)
268 class AutoAcceptManager(object):
271 self._newChannelSignaller = NewChannelSignaller(self._on_new_channel)
274 self._newChannelSignaller.start()
276 @gtk_toolbox.log_exception(_moduleLogger)
277 def _on_new_channel(self, bus, conn, chan, channelType):
278 pprint.pprint((bus, conn, chan, channelType))
279 if channelType != telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA:
282 # @todo distinguish between preferred CMs
283 attemptPickup = AutoAcceptCall(bus, conn, chan, self._on_inbound_call, self._on_inbound_call_error)
285 @gtk_toolbox.log_exception(_moduleLogger)
286 def _on_inbound_call(self, autoAcceptCall):
287 # @todo Add a comparison for picking up for only certain contacts
288 print autoAcceptCall.initiator, autoAcceptCall.target
289 if autoAcceptCall.is_inbound():
290 autoAcceptCall.accept_call(self._on_call_pickedup, self._on_pickup_error)
293 "Not an inbound call (initiator=%r, target=%r)" % (autoAcceptCall.initiator, autoAcceptCall.target)
296 @gtk_toolbox.log_exception(_moduleLogger)
297 def _on_inbound_call_error(self, *args):
298 _moduleLogger.info("Inbound call error")
300 @gtk_toolbox.log_exception(_moduleLogger)
301 def _on_call_pickedup(self, autoAcceptCall):
302 _moduleLogger.info("Call picked up")
304 @gtk_toolbox.log_exception(_moduleLogger)
305 def _on_pickup_error(self, autoAcceptCall, *args):
306 _moduleLogger.info("Call failed to pick up (%r)" % (args, ))
309 class MissedManager(object):
312 self._newChannelSignaller = NewChannelSignaller(self._on_new_channel)
315 self._newChannelSignaller.start()
317 @gtk_toolbox.log_exception(_moduleLogger)
318 def _on_new_channel(self, bus, conn, chan, channelType):
319 pprint.pprint((bus, conn, chan, channelType))
320 if channelType != telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA:
323 missDetection = WasMissedCall(
324 bus, conn, chan, self._on_missed_call, self._on_error_for_missed
327 @gtk_toolbox.log_exception(_moduleLogger)
328 def _on_missed_call(self, missDetection):
329 _moduleLogger.info("Missed a call")
331 @gtk_toolbox.log_exception(_moduleLogger)
332 def _on_error_for_missed(self, missDetection, reason):
333 _moduleLogger.info("Error: %r claims %r" % (missDetection, reason))
336 if __name__ == "__main__":
337 logging.basicConfig(level=logging.DEBUG)
338 l = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
340 manager = AutoAcceptManager()
342 manager = MissedManager()
344 gobject.threads_init()
345 gobject.idle_add(manager.start)
347 mainloop = gobject.MainLoop(is_running=True)