Package dtk ::
Package ui ::
Module unique_service
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 from dbus.mainloop.glib import DBusGMainLoop
24 import dbus
25 import dbus.service
26
28 """
29 This class implement a dbus interface, which is used to ensure that the program or service is unique in the system.
30 """
31 - def __init__(self,
32 bus_name,
33 app_dbus_name,
34 app_object_name,
35 unique_callback=None):
36 """
37 Initialise the class.
38
39 @param bus_name: the public service name of the service.
40 @param app_dbus_name: the public service name of the service.
41 @param app_object_name: the public service path of the service.
42 @param unique_callback: the callback which is invoked when the service is found already start. By default, it's None.
43 """
44 dbus.service.Object.__init__(self, bus_name, app_object_name)
45 self.unique_callback = unique_callback
46
47
48 def unique(self):
49 if self.unique_callback:
50 self.unique_callback()
51
52
53
54 setattr(UniqueService, 'unique', dbus.service.method(app_dbus_name)(unique))
55
56 -def is_exists(app_dbus_name, app_object_name):
57 """
58 Check the program or service is already started by its app_dbus_name and app_object_name.
59
60 @param app_dbus_name: the public service name of the service.
61 @param app_object_name: the public service path of the service.
62 @return: If the service is already on, True is returned. Otherwise return False.
63 """
64 DBusGMainLoop(set_as_default=True)
65
66
67 bus = dbus.SessionBus()
68 if bus.request_name(app_dbus_name) != dbus.bus.REQUEST_NAME_REPLY_PRIMARY_OWNER:
69 method = bus.get_object(app_dbus_name, app_object_name).get_dbus_method("unique")
70 method()
71
72 return True
73 else:
74 return False
75