8 import util.go_utils as gobject_utils
12 _moduleLogger = logging.getLogger("tp_utils")
13 DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
16 class WasMissedCall(object):
18 def __init__(self, bus, conn, chan, on_success, on_error):
19 self.__on_success = on_success
20 self.__on_error = on_error
22 self._requested = None
23 self._didMembersChange = False
24 self._didClose = False
25 self._didReport = False
27 self._onTimeout = gobject_utils.Timeout(self._on_timeout)
28 self._onTimeout.start(seconds=10)
30 chan[telepathy.interfaces.CHANNEL_INTERFACE_GROUP].connect_to_signal(
32 self._on_members_changed,
35 chan[telepathy.interfaces.CHANNEL].connect_to_signal(
40 chan[DBUS_PROPERTIES].GetAll(
41 telepathy.interfaces.CHANNEL_INTERFACE,
42 reply_handler = self._on_got_all,
43 error_handler = self._on_error,
47 self._report_error("by request")
49 def _report_missed_if_ready(self):
52 elif self._requested is not None and (self._didMembersChange or self._didClose):
54 self._report_error("wrong direction")
56 self._report_success()
58 self._report_error("members added")
61 self._report_error("closed too early")
63 def _report_success(self):
64 assert not self._didReport
65 self._didReport = True
66 self._onTimeout.cancel()
67 self.__on_success(self)
69 def _report_error(self, reason):
70 assert not self._didReport
71 self._didReport = True
72 self._onTimeout.cancel()
73 self.__on_error(self, reason)
75 @misc.log_exception(_moduleLogger)
76 def _on_got_all(self, properties):
77 self._requested = properties["Requested"]
78 self._report_missed_if_ready()
80 @misc.log_exception(_moduleLogger)
81 def _on_members_changed(self, message, added, removed, lp, rp, actor, reason):
83 self._didMembersChange = True
84 self._report_missed_if_ready()
86 @misc.log_exception(_moduleLogger)
89 self._report_missed_if_ready()
91 @misc.log_exception(_moduleLogger)
92 def _on_error(self, *args):
93 self._report_error(args)
95 @misc.log_exception(_moduleLogger)
96 def _on_timeout(self):
97 self._report_error("timeout")
101 class NewChannelSignaller(object):
103 def __init__(self, on_new_channel):
104 self._sessionBus = dbus.SessionBus()
105 self._on_user_new_channel = on_new_channel
108 self._sessionBus.add_signal_receiver(
109 self._on_new_channel,
111 "org.freedesktop.Telepathy.Connection",
117 self._sessionBus.remove_signal_receiver(
118 self._on_new_channel,
120 "org.freedesktop.Telepathy.Connection",
125 @misc.log_exception(_moduleLogger)
127 self, channelObjectPath, channelType, handleType, handle, supressHandler
129 connObjectPath = channel_path_to_conn_path(channelObjectPath)
130 serviceName = path_to_service_name(channelObjectPath)
132 self._on_user_new_channel(
133 self._sessionBus, serviceName, connObjectPath, channelObjectPath, channelType
136 _moduleLogger.exception("Blocking exception from being passed up")
139 def channel_path_to_conn_path(channelObjectPath):
141 >>> channel_path_to_conn_path("/org/freedesktop/Telepathy/ConnectionManager/theonering/gv/USERNAME/Channel1")
142 '/org/freedesktop/Telepathy/ConnectionManager/theonering/gv/USERNAME'
144 return channelObjectPath.rsplit("/", 1)[0]
147 def path_to_service_name(path):
149 >>> path_to_service_name("/org/freedesktop/Telepathy/ConnectionManager/theonering/gv/USERNAME/Channel1")
150 'org.freedesktop.Telepathy.ConnectionManager.theonering.gv.USERNAME'
152 return ".".join(path[1:].split("/")[0:7])
155 def cm_from_path(path):
157 >>> cm_from_path("/org/freedesktop/Telepathy/ConnectionManager/theonering/gv/USERNAME/Channel1")
160 return path[1:].split("/")[4]