| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# This is a part of the Cairo-Dock plug-ins. |
| 4 |
# Copyright : (C) 2011 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 |
# Developped as a part of Cairo-Dock, but usable as a stand-alone systray daemon. |
| 19 |
# The code follows the same logic as the KDE watcher, to ensure a complete compatibility. |
| 20 |
|
| 21 |
import sys |
| 22 |
|
| 23 |
from gi.repository import GLib as glib |
| 24 |
from gi.repository import GObject as gobject |
| 25 |
g_bMainLoopInGObject = False |
| 26 |
|
| 27 |
import dbus, dbus.service |
| 28 |
from dbus.mainloop.glib import DBusGMainLoop |
| 29 |
|
| 30 |
class Registrar(dbus.service.Object): |
| 31 |
bus_name_str = 'com.canonical.AppMenu.Registrar' |
| 32 |
bus_obj_str = '/com/canonical/AppMenu/Registrar' |
| 33 |
bus_iface_str = 'com.canonical.AppMenu.Registrar' |
| 34 |
windows = {} # table of couple (X id, menu object path) |
| 35 |
|
| 36 |
def __init__(self): |
| 37 |
DBusGMainLoop(set_as_default=True) |
| 38 |
try: |
| 39 |
self.bus = dbus.SessionBus() |
| 40 |
bus_name = dbus.service.BusName (self.bus_name_str, self.bus) |
| 41 |
print("registered a registrar:",bus_name) |
| 42 |
dbus.service.Object.__init__(self, bus_name, self.bus_obj_str) |
| 43 |
except dbus.DBusException: |
| 44 |
print('Could not open dbus. Uncaught exception.') |
| 45 |
return |
| 46 |
|
| 47 |
bus_object = self.bus.get_object(dbus.BUS_DAEMON_NAME, dbus.BUS_DAEMON_PATH) |
| 48 |
self.main = dbus.Interface(bus_object, dbus.BUS_DAEMON_IFACE) |
| 49 |
#self.main.connect_to_signal("NameOwnerChanged", self.on_name_owner_changed) |
| 50 |
|
| 51 |
if g_bMainLoopInGObject: |
| 52 |
self.loop = gobject.MainLoop() |
| 53 |
else: |
| 54 |
self.loop = glib.MainLoop() |
| 55 |
self.loop.run() |
| 56 |
|
| 57 |
### methods ### |
| 58 |
|
| 59 |
@dbus.service.method(dbus_interface = bus_iface_str, in_signature = 'uo', out_signature = None, sender_keyword='sender') |
| 60 |
def RegisterWindow(self, Xid, menu, sender=None): |
| 61 |
self.windows[Xid] = (sender, menu) |
| 62 |
|
| 63 |
self.WindowRegistered (Xid, sender, menu) |
| 64 |
|
| 65 |
@dbus.service.method(dbus_interface = bus_iface_str, in_signature = 'u', out_signature = None) |
| 66 |
def UnregisterWindow(self, Xid): |
| 67 |
del self.windows[Xid] |
| 68 |
|
| 69 |
self.WindowUnregistered (Xid) |
| 70 |
|
| 71 |
@dbus.service.method(dbus_interface = bus_iface_str, in_signature = 'u', out_signature = 'so') |
| 72 |
def GetMenuForWindow(self, Xid): |
| 73 |
print("GetMenuForWindow(%d)..." % Xid) |
| 74 |
try: |
| 75 |
service,menu = self.windows[Xid] |
| 76 |
return service,menu # dbus.ObjectPath(menu) |
| 77 |
except: |
| 78 |
return "","/" # return an empty string to avoid ugly warnings; clients should be able to deal with it. |
| 79 |
|
| 80 |
@dbus.service.method(dbus_interface = bus_iface_str, in_signature = None, out_signature = 'a(uso)') |
| 81 |
def GetMenus(self): |
| 82 |
menus=[] |
| 83 |
for xid in self.windows: |
| 84 |
service,menu = self.windows[xid] |
| 85 |
menus.append((xid, service, menu)) |
| 86 |
|
| 87 |
### Signals ### |
| 88 |
|
| 89 |
@dbus.service.signal(dbus_interface=bus_name_str, signature='uso') |
| 90 |
def WindowRegistered(self, Xid, service, menu): |
| 91 |
print("%d registered" % (Xid)) |
| 92 |
sys.stdout.flush() |
| 93 |
|
| 94 |
@dbus.service.signal(dbus_interface=bus_name_str, signature='u') |
| 95 |
def WindowUnregistered(self, Xid): |
| 96 |
print("%d unregistered" % (Xid)) |
| 97 |
sys.stdout.flush() |
| 98 |
|
| 99 |
|
| 100 |
if __name__ == '__main__': |
| 101 |
Registrar() |