5 import dbus.mainloop.glib
9 DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
12 class AutoAcceptCall(object):
14 def __init__(self, bus, conn, chan):
15 self._sessionBus = bus
19 self._selfHandle = None
20 self._initiatorHandle = None
21 self._initiatorID = None
22 self._targetHandle = None
24 self._pendingHandles = None
27 # @bug Unsure why this isn't working
28 self._conn[DBUS_PROPERTIES].Get(
29 telepathy.interfaces.CONNECTION_INTERFACE,
31 reply_handler = self._on_got_self_handle,
32 error_handler = self._on_nothing,
35 self._conn[telepathy.interfaces.CHANNEL_INTERFACE_GROUP].GetSelfHandle(
36 reply_handler = self._on_got_self_handle,
37 error_handler = self._on_nothing,
40 self._chan[DBUS_PROPERTIES].GetAll(
41 telepathy.interfaces.CHANNEL_INTERFACE,
42 reply_handler = self._on_got_all,
43 error_handler = self._on_nothing,
47 # @bug Unsure why this isn't working
48 self._chan[DBUS_PROPERTIES].Get(
49 telepathy.interfaces.CHANNEL_INTERFACE_GROUP,
50 'LocalPendingMembers',
51 reply_handler = self._on_got_pending_members,
52 error_handler = self._on_nothing,
55 self._chan[telepathy.interfaces.CHANNEL_INTERFACE_GROUP].GetLocalPendingMembersWithInfo(
56 reply_handler = self._on_got_pending_members,
57 error_handler = self._on_nothing,
60 def _pickup_if_ready(self):
63 self._initiatorHandle,
69 # Note ready yet, still some outstanding requests
72 if self._selfHandle != self._targetHandle:
73 # Turns out it was an inbound call
76 # @bug does not distinguish between who the call is from for use for TOR auto-pickup
77 self._chan[telepathy.interfaces.CHANNEL_INTERFACE_GROUP].AddMembers(
78 reply_handler = self._on_members_added,
79 error_handler = self._on_nothing,
82 def _on_got_self_handle(self, selfHandle):
83 self._pickup_if_ready()
85 def _on_got_all(self, properties):
86 self._initiatorID = properties["InitiatorID"]
87 self._initiatorHandle = properties["InitiatorHandle"]
88 self._targetID = properties["InitiatorID"]
89 self._targetHandle = properties["InitiatorHandle"]
91 self._pickup_if_ready()
93 def _on_got_pending_members(self, pendings):
94 for pendingHandle, instigatorHandle, reason, message in pendings:
95 print pendingHandle, instigatorHandle, reason, message
97 self._pendingHandles = [pendingWithInfo[0] for pendingWithInfo in pendings]
98 self._pickup_if_ready()
100 def _on_members_added(self):
101 print "Should be picked up now"
103 def _on_nothing(*args):
107 class NewChannelSignaller(object):
109 def __init__(self, on_new_channel):
110 self._sessionBus = dbus.SessionBus()
111 self._on_new_channel = on_new_channel
114 self._sessionBus.add_signal_receiver(
115 self._on_new_channel,
117 "org.freedesktop.Telepathy.Connection",
123 self._sessionBus.remove_signal_receiver(
124 self._on_new_channel,
126 "org.freedesktop.Telepathy.Connection",
132 self, channelObjectPath, channelType, handleType, handle, supressHandler
134 connObjectPath = channelObjectPath.rsplit("/", 1)[0][1:]
135 serviceName = connObjectPath.replace("/", ".")
136 conn = telepathy.client.Channel(serviceName, connObjectPath)
137 chan = telepathy.client.Channel(serviceName, channelObjectPath)
138 self._on_new_channel(self._sessionBus, conn, chan, channelType)
141 class Manager(object):
144 self._newChannelSignaller = NewChannelSignaller(self._on_new_channel)
145 self._activeAttempts = []
147 def _on_new_channel(self, bus, conn, chan, channelType):
148 if channelType != telepathy.interfaces.CHANNEL_TYPE_STREAMED_MEDIA:
151 # @bug does not distinguish between preferred CMs
152 # @todo Need a way to be notified on error, ignored, or if picked up
153 attemptPickup = AutoAcceptCall(bus, conn, chan)
154 self._activeAttempts.append(attemptPickup)
157 if __name__ == "__main__":
158 l = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
159 autoaccept = Manager()
161 gobject.threads_init()
162 gobject.idle_add(autoaccept.start)
164 mainloop = gobject.MainLoop(is_running=True)