4 sys.path.insert(0,"../src")
10 import dbus.mainloop.glib
16 _moduleLogger = logging.getLogger("receptionist")
17 DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
20 class AutoAcceptCall(object):
22 def __init__(self, bus, conn, chan, on_success, on_error):
23 self._sessionBus = bus
26 self._outstandingRequests = []
27 self._on_success = on_success
28 self._on_error = on_error
30 self._selfHandle = None
31 self._initiatorHandle = None
32 self._initiatorID = None
33 self._targetHandle = None
35 self._pendingHandles = None
38 # @bug Unsure why this isn't working
39 self._conn[DBUS_PROPERTIES].Get(
40 telepathy.interfaces.CONNECTION_INTERFACE,
42 reply_handler = self._on_got_self_handle,
43 error_handler = self._custom_error(self._on_got_self_handle),
46 self._conn[telepathy.interfaces.CONNECTION].GetSelfHandle(
47 reply_handler = self._on_got_self_handle,
48 error_handler = self._custom_error(self._on_got_self_handle),
50 self._outstandingRequests.append(self._on_got_self_handle)
53 self._chan[DBUS_PROPERTIES].GetAll(
54 telepathy.interfaces.CHANNEL_INTERFACE,
55 reply_handler = self._on_got_all,
56 error_handler = self._custom_error(self._on_got_all),
58 self._outstandingRequests.append(self._on_got_all)
61 # @bug Unsure why this isn't working
62 self._chan[DBUS_PROPERTIES].Get(
63 telepathy.interfaces.CHANNEL_INTERFACE_GROUP,
64 'LocalPendingMembers',
65 reply_handler = self._on_got_pending_members,
66 error_handler = self._custom_error(self._on_got_pending_members),
69 self._chan[telepathy.interfaces.CHANNEL_INTERFACE_GROUP].GetLocalPendingMembersWithInfo(
70 reply_handler = self._on_got_pending_members,
71 error_handler = self._custom_error(self._on_got_pending_members),
73 self._outstandingRequests.append(self._on_got_pending_members)
76 isInbound = self._selfHandle == self._targetHandle and self._selfHandle != self._initiatorHandle
77 print "is_inbound", self._selfHandle, self._targetHandle, self._initiatorHandle
78 print "is_inbound", self._targetID, self._initiatorID
83 return self._initiatorID
89 def accept_call(self, on_accepted, on_error):
90 self._chan[telepathy.interfaces.CHANNEL_INTERFACE_GROUP].AddMembers(
93 reply_handler = self._custom_on_accept(on_accepted),
94 error_handler = self._custom_on_accept_error(on_error),
97 def _custom_on_accept(self, callback):
104 def _custom_on_accept_error(self, callback):
106 def on_error(self, *args):
107 callback(self, *args)
111 def _custom_error(self, action):
113 def _on_error(self, *args):
114 _moduleLogger.error("Failed for %r (%r)" % (action, args))
115 self._outstandingRequests.remove(action)
116 if self._outstandingRequests:
123 def _report_callback_done(self, action):
124 _moduleLogger.debug("Succeded with %r" % (action, ))
125 self._outstandingRequests.remove(action)
126 if self._outstandingRequests:
131 self._initiatorHandle,
135 self._pendingHandles,
138 self._on_success(self)
140 @gtk_toolbox.log_exception(_moduleLogger)
141 def _on_got_self_handle(self, selfHandle):
142 self._selfHandle = selfHandle
144 self._report_callback_done(self._on_got_self_handle)
146 @gtk_toolbox.log_exception(_moduleLogger)
147 def _on_got_all(self, properties):
148 self._initiatorID = properties["InitiatorID"]
149 self._initiatorHandle = properties["InitiatorHandle"]
150 self._targetID = properties["InitiatorID"]
151 self._targetHandle = properties["InitiatorHandle"]
153 self._report_callback_done(self._on_got_all)
155 @gtk_toolbox.log_exception(_moduleLogger)
156 def _on_got_pending_members(self, pendings):
157 for pendingHandle, instigatorHandle, reason, message in pendings:
158 print pendingHandle, instigatorHandle, reason, message
160 self._pendingHandles = [pendingWithInfo[0] for pendingWithInfo in pendings]
162 self._report_callback_done(self._on_got_pending_members)
165 class NewChannelSignaller(object):
167 def __init__(self, on_new_channel):
168 self._sessionBus = dbus.SessionBus()
169 self._on_user_new_channel = on_new_channel
172 self._sessionBus.add_signal_receiver(
173 self._on_new_channel,
175 "org.freedesktop.Telepathy.Connection",
181 self._sessionBus.remove_signal_receiver(
182 self._on_new_channel,
184 "org.freedesktop.Telepathy.Connection",
189 @gtk_toolbox.log_exception(_moduleLogger)
191 self, channelObjectPath, channelType, handleType, handle, supressHandler
193 connObjectPath = channelObjectPath.rsplit("/", 1)[0]
194 serviceName = connObjectPath[1:].replace("/", ".")
195 conn = telepathy.client.Connection(serviceName, connObjectPath)
196 chan = telepathy.client.Channel(serviceName, channelObjectPath)
197 self._on_user_new_channel(self._sessionBus, conn, chan, channelType)
200 class Manager(object):
203 self._newChannelSignaller = NewChannelSignaller(self._on_new_channel)
206 self._newChannelSignaller.start()
208 @gtk_toolbox.log_exception(_moduleLogger)
209 def _on_new_channel(self, bus, conn, chan, channelType):
210 pprint.pprint((bus, conn, chan, channelType))
211 if channelType != telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA:
214 # @bug does not distinguish between preferred CMs
215 attemptPickup = AutoAcceptCall(bus, conn, chan, self._on_inbound_call, self._on_inbound_call_error)
217 @gtk_toolbox.log_exception(_moduleLogger)
218 def _on_inbound_call(self, autoAcceptCall):
219 # @todo Add a comparison for picking up for only certain contacts
220 print autoAcceptCall.initiator, autoAcceptCall.target
221 if autoAcceptCall.is_inbound():
222 autoAcceptCall.accept_call(self._on_call_pickedup, self._on_pickup_error)
225 "Not an inbound call (initiator=%r, target=%r)" % (autoAcceptCall.initiator, autoAcceptCall.target)
228 @gtk_toolbox.log_exception(_moduleLogger)
229 def _on_inbound_call_error(self, *args):
230 _moduleLogger.info("Inbound call error")
232 @gtk_toolbox.log_exception(_moduleLogger)
233 def _on_call_pickedup(self, autoAcceptCall):
234 _moduleLogger.info("Call picked up")
236 @gtk_toolbox.log_exception(_moduleLogger)
237 def _on_pickup_error(self, autoAcceptCall, *args):
238 _moduleLogger.info("Call failed to pick up (%r)" % (args, ))
240 if __name__ == "__main__":
241 logging.basicConfig(level=logging.DEBUG)
242 l = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
243 autoaccept = Manager()
245 gobject.threads_init()
246 gobject.idle_add(autoaccept.start)
248 mainloop = gobject.MainLoop(is_running=True)