Debian Codegraph @

Dbus/data/cairo-dock-launcher-API-daemon.py

raw
1 #!/usr/bin/env python3
2 #
3 # This is a part of the Cairo-Dock plug-ins.
4 # Copyright : (C) 2012 by Fabrice Rey
5 # E-mail : [email protected]
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 3
10 # of the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 # http://www.gnu.org/licenses/licenses.html#GPL
17 #
18
19 import sys
20
21 from gi.repository import GLib as glib
22 from gi.repository import GObject as gobject
23 g_bMainLoopInGObject = False
24
25 import re
26 from os import popen, getpid
27 import dbus, dbus.service
28 from dbus.mainloop.glib import DBusGMainLoop
29 from CairoDock import CairoDock
30
31 class Launcher:
32 has_count = False
33 count = 0
34 count_visible = False
35 has_progress = False
36 progress = 0.
37 progress_visible = False
38 has_emblem = False
39 emblem = 0
40 emblem_visible = False
41 menu_path = ''
42 urgent = False
43
44 def __init__(self, app_uri=None, dock=None, dbus_name=None):
45 ## initialize the properties of the launcher
46 self.app_uri = app_uri
47 self.dock = dock
48 self.dbus_name = dbus_name
49 desktop_file = re.sub ('application://', '', app_uri)
50 app_name = re.sub ('.desktop', '', desktop_file)
51 self.id = 'class='+app_name
52
53 ## watch for the deconnection of the distant app (we don't want to let an icon demanding attention or with an invalid menu).
54 bus = dbus.SessionBus()
55 bus.watch_name_owner(self.dbus_name, self.on_name_owner_changed)
56
57 def update_dbus_name(self, dbus_name):
58 if dbus_name != self.dbus_name:
59 self.dbus_name = dbus_name
60 bus = dbus.SessionBus()
61 bus.watch_name_owner(self.dbus_name, self.on_name_owner_changed)
62
63 def on_name_owner_changed(self,connection_name):
64 print("Launcher-API-Daemon: launcher bus name changed:",connection_name)
65 if len(connection_name) == 0: # no more distant app -> reset the launcher and its icon.
66 print("Launcher-API-Daemon: -> the launcher "+ str (self.dbus_name) +" is no longer connected to its app.")
67 if self.has_count and self.count_visible:
68 self.dock.iface.SetQuickInfo('', self.id)
69 self.has_count = False
70 self.count_visible = False
71 if self.has_progress and self.progress_visible:
72 self.dock.iface.SetProgress (-1, self.id)
73 self.has_progress = False
74 self.progress_visible = False
75 if self.has_emblem and self.emblem_visible:
76 self.dock.iface.SetEmblem('', CairoDock.EMBLEM_TOP_RIGHT, self.id)
77 self.has_emblem = False
78 self.emblem_visible = False
79 if self.urgent:
80 self.dock.iface.DemandsAttention(False, 'default', self.id)
81 self.urgent = False
82 if self.menu_path != '':
83 self.dock.iface.SetMenu(self.dbus_name, '', self.id)
84 self.menu_path = ''
85 self.dbus_name = None
86
87 def set_count(self, x):
88 print('Launcher-API-Daemon: set_count (%d)'%x)
89 self.count = x
90 self.has_count = True
91 if self.count_visible:
92 self.dock.iface.SetQuickInfo(str(self.count), self.id)
93
94 def set_count_visible(self, x):
95 print('Launcher-API-Daemon: set_count_visible (%d)'%x)
96 self.count_visible = x
97 if self.has_count:
98 if x:
99 self.dock.iface.SetQuickInfo(str(self.count), self.id)
100 else:
101 self.dock.iface.SetQuickInfo('', self.id)
102
103 def set_progress(self, x):
104 print('Launcher-API-Daemon: set_progress (%.2f)'%x)
105 self.progress = x
106 self.has_progress = True
107 self.dock.iface.SetProgress (x, self.id)
108
109 def set_progress_visible(self, x):
110 print('Launcher-API-Daemon: set_progress_visible (%d)'%x)
111 self.progress_visible = x
112 if not x:
113 self.dock.iface.SetProgress (-1, self.id)
114 else:
115 self.dock.iface.SetProgress (self.progress, self.id)
116
117 def set_urgent(self, x):
118 print('Launcher-API-Daemon: set_urgent (%d)'%x)
119 self.urgent = x
120 self.dock.iface.DemandsAttention(x, 'default', self.id)
121
122 def set_menu(self, menu_path):
123 print('Launcher-API-Daemon: set_menu (%s)'%menu_path)
124 self.menu_path = menu_path
125 self.dock.iface.SetMenu(self.dbus_name, menu_path, self.id)
126
127 def set_emblem(self, x):
128 print('Launcher-API-Daemon: set_emblem (%s)'%x)
129 self.emblem = x
130 self.has_emblem = True
131 if self.emblem_visible:
132 self.dock.iface.SetEmblem(self.emblem, CairoDock.EMBLEM_TOP_RIGHT, self.id)
133
134 def set_emblem_visible(self, x):
135 print('Launcher-API-Daemon: set_emblem_visible (%d)'%x)
136 self.emblem_visible = x
137 if self.has_emblem:
138 if x:
139 self.dock.iface.SetEmblem(self.emblem, CairoDock.EMBLEM_TOP_RIGHT, self.id)
140 else:
141 self.dock.iface.SetEmblem('', CairoDock.EMBLEM_TOP_RIGHT, self.id)
142
143
144 class ULWatcher:
145 bus_iface_str = 'com.canonical.Unity.LauncherEntry' # bus name to watch
146 bus_name_str = 'com.canonical.Unity' # bus name to claim, to signal our presence on the bus.
147 launchers = {} # list of launchers that have been seen on the bus at least once.
148
149 def __init__(self):
150 DBusGMainLoop(set_as_default=True)
151
152 try:
153 self.bus = dbus.SessionBus()
154 except Exception as exception:
155 print('Launcher-API-Daemon: Could not open dbus. Uncaught exception.')
156 return
157
158 bus_name = dbus.service.BusName (self.bus_name_str, self.bus, allow_replacement=True) # allow Unity shell to take ownership of the bus name; we only want to register on the bus so that other applications know they can send message on the LauncherEntry interface
159 print("Launcher-API-Daemon: registered as Unity:",bus_name)
160
161 self.bus.add_signal_receiver (self.on_launcher_entry_signal, dbus_interface=self.bus_iface_str, member_keyword='member', sender_keyword='sender')
162
163 self.dock = CairoDock()
164
165 if g_bMainLoopInGObject:
166 self.loop = gobject.MainLoop()
167 else:
168 self.loop = glib.MainLoop()
169 self.loop.run()
170
171 def on_launcher_entry_signal(self, val1=None, val2=None, member=None, sender=None): # application://evolution.desktop, dictionnary (sv)
172 if (member == 'Update'):
173 print('Launcher-API-Daemon: Update',val1,'with',val2)
174
175 launcher = None
176 if val1 in self.launchers:
177 launcher = self.launchers[val1]
178 launcher.update_dbus_name(sender)
179 else:
180 launcher = Launcher(val1, self.dock, sender)
181 self.launchers[val1] = launcher
182
183 if 'count' in val2:
184 count = val2['count']
185 launcher.set_count (count)
186
187 if 'progress' in val2:
188 progress = val2['progress']
189 launcher.set_progress (progress)
190
191 if 'emblem' in val2:
192 emblem = val2['emblem']
193 launcher.set_emblem (emblem)
194
195 if 'urgent' in val2:
196 urgent = val2['urgent']
197 launcher.set_urgent (urgent)
198
199 if 'quicklist' in val2:
200 menu_path = val2['quicklist']
201 launcher.set_menu (menu_path)
202
203 if 'count-visible' in val2:
204 count_visible = val2['count-visible']
205 launcher.set_count_visible (count_visible)
206
207 if 'progress-visible' in val2:
208 progress_visible = val2['progress-visible']
209 launcher.set_progress_visible (progress_visible)
210
211 if 'emblem-visible' in val2:
212 emblem_visible = val2['emblem-visible']
213 launcher.set_emblem_visible (emblem_visible)
214
215 elif (member == 'Query'):
216 print('Launcher-API-Daemon: query',val2,'for',val1)
217 else:
218 print('Launcher-API-Daemon: unknown signal')
219
220 if __name__ == '__main__':
221 # check that we don't run twice (ex.: from a 2nd cairo-dock instance)
222 # we could detect it from the bus, but then we couldn't detect the following case: Unity launcher + Cairo-Dock
223 # we want: ps ux | grep " + filename + " | grep python | grep -v " + pid_str)
224 ps_str = popen("ps -u $USER -wwo pid,cmd").read().splitlines() # only users' processes with all lines
225 filename = sys.argv[0] # /usr/lib/cairo-dock/cairo-dock-launcher-API-daemon
226 pid_str = str (getpid()) # we check if previous processes are running, we exclude this process
227 for line in ps_str:
228 line_split = line.split()
229 line_cmd = ' '.join(line_split[1:])
230 line_pid = line_split[0]
231 if "python" in line_cmd and filename in line_cmd and not pid_str in line_pid:
232 print('Cairo-Dock - Launcher API Daemon is already running (' + line_pid + ')')
233 sys.exit(1)
234
235 ULWatcher()