GajimDBus: gajim2beryl.py

File gajim2beryl.py, 2.5 kB (added by jan@uos.de, 1 year ago)

Python dbus script to glue gajim and beryl together

Line 
1 #!/usr/bin/python
2
3 # notification about new messages in
4 # gajim using the beryl water ripple effect
5 # Jan Vornberger (jav), xmpp:jav@jabber.de
6
7 import os
8 import gobject
9 import string
10 import re
11 import dbus
12
13 if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
14     import dbus.glib
15
16 GAJIM_SERVICE = 'org.gajim.dbus'
17 GAJIM_OBJ_PATH = '/org/gajim/dbus/RemoteObject'
18 GAJIM_INTERFACE = 'org.gajim.dbus.RemoteInterface'
19
20 BERYL_SERVICE = 'org.freedesktop.beryl'
21 BERYL_OBJ_PATH = '/org/freedesktop/beryl/water/allscreens/point'
22 BERYL_INTERFACE = 'org.freedesktop.beryl.activate'
23
24 # function to find root window id;
25 # we will need to pass that to beryl
26 def get_root_window_id():
27         rootid = 0
28         idline = os.popen('xwininfo -root | grep "id:"').readlines()[0]
29         idmatch = re.search('id: ([^ ]*)', idline)
30         if (idmatch):
31                 rootid = int(idmatch.group(1), 16)
32                 return rootid
33         else:
34                 raise 'Can not find window id of root window'
35
36 # function to find the position of gajim systray icon;
37 # we want to ping that position
38 def get_systray_icon_position():
39         # find systray icon
40         cmd = 'xwininfo  -root -tree | grep gajim'
41         posregex = re.compile('(\d+)x(\d+)\+\d+\+\d+\s*\+(\d+)\+(\d+)') # match window position info
42
43         for line in os.popen(cmd).readlines():
44                 posmatch = posregex.search(line)
45                 if (posmatch):
46                         width = int(posmatch.group(1))
47                         height = int(posmatch.group(2))
48                         x = int(posmatch.group(3))
49                         y = int(posmatch.group(4))
50                         if (width >= 15 and width <= 30 and height >= 15 and height < 30):
51                                 # I guess this is the systray icon
52                                 return x + (width / 2), y
53
54         raise 'Can not find gajim systray icon'
55
56 # callback function to be informed about new messages in gajim
57 def new_message(details):
58         # new message -> ripple notification
59         beryl_dbus_iface.activate('root', rootid,
60                                         'amplitude', 1,
61                                         'x', systray_x,
62                                         'y', systray_y,
63                                         reply_handler=(lambda *args: None),
64                                         error_handler=(lambda *args: None))
65
66 # get root id and systray position for later use
67 rootid = get_root_window_id()
68 systray_x, systray_y = get_systray_icon_position()
69
70 # magic starts here
71 bus = dbus.SessionBus()
72
73 # connect to NewMessage signal from gajim
74 gajim_proxy_obj = bus.get_object(GAJIM_SERVICE, GAJIM_OBJ_PATH)
75 gajim_dbus_iface = dbus.Interface(gajim_proxy_obj, GAJIM_INTERFACE)
76 gajim_dbus_iface.connect_to_signal('NewMessage', new_message)
77
78 # prepare beryl dbus connection
79 beryl_proxy_obj = bus.get_object(BERYL_SERVICE, BERYL_OBJ_PATH)
80 beryl_dbus_iface = dbus.Interface(beryl_proxy_obj, BERYL_INTERFACE)
81
82 # get things going
83 mainloop = gobject.MainLoop()
84 mainloop.run()