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._initiatorHandle = None
31 self._initiatorID = None
32 self._targetHandle = None
34 self._pendingHandles = None
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),
41 self._outstandingRequests.append(self._on_got_all)
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),
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),
56 self._outstandingRequests.append(self._on_got_pending_members)
59 isInbound = self._targetHandle == self._initiatorHandle
64 return self._initiatorID
70 def accept_call(self, on_accepted, on_error):
71 self._chan[telepathy.interfaces.CHANNEL_INTERFACE_GROUP].AddMembers(
74 reply_handler = self._custom_on_accept(on_accepted),
75 error_handler = self._custom_on_accept_error(on_error),
78 def _custom_on_accept(self, callback):
85 def _custom_on_accept_error(self, callback):
92 def _custom_error(self, action):
95 _moduleLogger.error("Failed for %r (%r)" % (action, args))
96 self._outstandingRequests.remove(action)
97 if self._outstandingRequests:
104 def _report_callback_done(self, action):
105 _moduleLogger.debug("Succeded with %r" % (action, ))
106 self._outstandingRequests.remove(action)
107 if self._outstandingRequests:
111 self._initiatorHandle,
115 self._pendingHandles,
118 self._on_success(self)
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"]
127 self._report_callback_done(self._on_got_all)
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
134 self._pendingHandles = [pendingWithInfo[0] for pendingWithInfo in pendings]
136 self._report_callback_done(self._on_got_pending_members)
139 class NewChannelSignaller(object):
141 def __init__(self, on_new_channel):
142 self._sessionBus = dbus.SessionBus()
143 self._on_user_new_channel = on_new_channel
146 self._sessionBus.add_signal_receiver(
147 self._on_new_channel,
149 "org.freedesktop.Telepathy.Connection",
155 self._sessionBus.remove_signal_receiver(
156 self._on_new_channel,
158 "org.freedesktop.Telepathy.Connection",
163 @gtk_toolbox.log_exception(_moduleLogger)
165 self, channelObjectPath, channelType, handleType, handle, supressHandler
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)
174 class Manager(object):
177 self._newChannelSignaller = NewChannelSignaller(self._on_new_channel)
180 self._newChannelSignaller.start()
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:
188 # @bug does not distinguish between preferred CMs
189 attemptPickup = AutoAcceptCall(bus, conn, chan, self._on_inbound_call, self._on_inbound_call_error)
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)
199 "Not an inbound call (initiator=%r, target=%r)" % (autoAcceptCall.initiator, autoAcceptCall.target)
202 @gtk_toolbox.log_exception(_moduleLogger)
203 def _on_inbound_call_error(self, *args):
204 _moduleLogger.info("Inbound call error")
206 @gtk_toolbox.log_exception(_moduleLogger)
207 def _on_call_pickedup(self, autoAcceptCall):
208 _moduleLogger.info("Call picked up")
210 @gtk_toolbox.log_exception(_moduleLogger)
211 def _on_pickup_error(self, autoAcceptCall, *args):
212 _moduleLogger.info("Call failed to pick up (%r)" % (args, ))
214 if __name__ == "__main__":
215 logging.basicConfig(level=logging.DEBUG)
216 l = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
217 autoaccept = Manager()
219 gobject.threads_init()
220 gobject.idle_add(autoaccept.start)
222 mainloop = gobject.MainLoop(is_running=True)