Merge lp:~zeitgeist/zeitgeist/bug727226 into lp:zeitgeist/0.1

Proposed by Siegfried Gevatter
Status: Merged
Merged at revision: 1682
Proposed branch: lp:~zeitgeist/zeitgeist/bug727226
Merge into: lp:zeitgeist/0.1
Diff against target: 104 lines (+33/-17)
1 file modified
zeitgeist/client.py (+33/-17)
To merge this branch: bzr merge lp:~zeitgeist/zeitgeist/bug727226
Reviewer Review Type Date Requested Status
Mikkel Kamstrup Erlandsen Approve
Review via email: mp+53396@code.launchpad.net
To post a comment you must log in.
lp:~zeitgeist/zeitgeist/bug727226 updated
1682. By Siegfried Gevatter

client.py:
 - Reconnect signals when Zeitgeist is restarted by someone else, and
   not just when it's restarted by the same instance (LP: #727226).
 - Cleanup code related to the above task.

Revision history for this message
Mikkel Kamstrup Erlandsen (kamstrup) wrote :

Looks good to me

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'zeitgeist/client.py'
2--- zeitgeist/client.py 2011-03-15 09:20:47 +0000
3+++ zeitgeist/client.py 2011-03-15 10:34:08 +0000
4@@ -2,9 +2,11 @@
5
6 # Zeitgeist
7 #
8-# Copyright © 2009-2011 Siegfried-Angel Gevatter Pujals <rainct@ubuntu.com>
9+# Copyright © 2009-2011 Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
10 # Copyright © 2009 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com>
11 # Copyright © 2009-2011 Markus Korn <thekorn@gmx.de>
12+# Copyright © 2011 Collabora Ltd.
13+# By Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
14 #
15 # This program is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU Lesser General Public License as published by
17@@ -41,7 +43,10 @@
18 class _DBusInterface(object):
19 """Wrapper around dbus.Interface adding convenience methods."""
20
21- reconnect_callbacks = []
22+ # We initialize those as sets in the constructor. Remember that we can't do
23+ # that here because otherwise all instances would share their state.
24+ _disconnect_callbacks = None
25+ _reconnect_callbacks = None
26
27 @staticmethod
28 def get_members(introspection_xml):
29@@ -64,8 +69,6 @@
30 self.__proxy = dbus.SessionBus().get_object(self.__iface.requested_bus_name,
31 self.__object_path)
32 self.__iface = dbus.Interface(self.__proxy, self.__interface_name)
33- for callback in self.reconnect_callbacks:
34- callback()
35
36 def _disconnection_safe(self, meth, *args, **kwargs):
37 """
38@@ -123,16 +126,16 @@
39 **kwargs)
40
41 def connect_exit(self, callback):
42- """Executes callback when the RemoteInterface exits"""
43- bus_obj = dbus.SessionBus().get_object(dbus.BUS_DAEMON_IFACE,
44- dbus.BUS_DAEMON_PATH)
45- bus_obj.connect_to_signal(
46- "NameOwnerChanged",
47- lambda *args: callback(),
48- dbus_interface=dbus.BUS_DAEMON_IFACE,
49- arg0=self.__iface.requested_bus_name, # only match what we want
50- arg2="", # only match services with no new owner
51- )
52+ """Executes callback when the remote interface disappears from the bus"""
53+ self._disconnect_callbacks.add(callback)
54+
55+ def connect_join(self, callback):
56+ """
57+ Executes callback when someone claims the Zeitgeist D-Bus name.
58+ This may be used to perform some action if the daemon is restarted while
59+ it was being used.
60+ """
61+ self._reconnect_callbacks.add(callback)
62
63 @property
64 def proxy(self):
65@@ -144,6 +147,21 @@
66 self.__object_path = object_path
67 self.__iface = dbus.Interface(proxy, interface_name)
68 self.__methods, self.__signals = self.get_members(proxy.Introspect())
69+
70+ self._disconnect_callbacks = set()
71+ self._reconnect_callbacks = set()
72+
73+ # Listen to (dis)connection notifications, for connect_exit and connect_join
74+ def name_owner_changed(connection_name):
75+ if connection_name == "":
76+ callbacks = self._disconnect_callbacks
77+ else:
78+ self.reconnect()
79+ callbacks = self._reconnect_callbacks
80+ for callback in callbacks:
81+ callback()
82+ dbus.SessionBus().watch_name_owner(self.__iface.requested_bus_name,
83+ name_owner_changed)
84
85 class ZeitgeistDBusInterface(object):
86 """ Central DBus interface to the Zeitgeist engine
87@@ -215,8 +233,6 @@
88 self.__shared_state["extension_interfaces"] = {}
89 self.__shared_state["dbus_interface"] = _DBusInterface(proxy,
90 self.INTERFACE_NAME, self.OBJECT_PATH)
91- self.reconnect_callbacks = \
92- self.__shared_state["dbus_interface"].reconnect_callbacks
93
94 class Monitor(dbus.service.Object):
95 """
96@@ -356,7 +372,7 @@
97 reply_handler=self._void_reply_handler,
98 error_handler=lambda err: log.warn(
99 "Error reinstalling monitor: %s" % err))
100- self._iface.reconnect_callbacks.append(reconnect_monitors)
101+ self._iface.connect_join(reconnect_monitors)
102
103 def _safe_error_handler(self, error_handler, *args):
104 if error_handler is not None: