Mercurial > libervia-backend
comparison frontends/src/primitivus/notify.py @ 380:ede26abf6ca1
primitivus: freedesktop notifications (if available) when somebody is talking to us and we have not focus, or our nick is pinged and we have not focus.
This need python-Xlib to work, if it's not present, notification are silently ignored.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 27 Aug 2011 17:15:41 +0200 |
parents | |
children | cf005701624b |
comparison
equal
deleted
inserted
replaced
379:adcc41e4d6ea | 380:ede26abf6ca1 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 Primitivus: a SAT frontend | |
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 import dbus | |
23 | |
24 class Notify: | |
25 """Used to send notification and detect if we have focus""" | |
26 | |
27 def __init__(self): | |
28 | |
29 #X11 stuff | |
30 self.display = None | |
31 self.X11_id = -1 | |
32 | |
33 try: | |
34 from Xlib import display as X_display | |
35 self.display = X_display.Display() | |
36 self.X11_id = self.getFocus() | |
37 except: | |
38 pass | |
39 | |
40 #Now we try to connect to Freedesktop D-Bus API | |
41 try: | |
42 bus = dbus.SessionBus() | |
43 db_object = bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications') | |
44 self.freedesktop_int = dbus.Interface(db_object, dbus_interface='org.freedesktop.Notifications') | |
45 except: | |
46 self.freedesktop_int = None | |
47 | |
48 | |
49 def getFocus(self): | |
50 if not self.display: | |
51 return 0 | |
52 return self.display.get_input_focus().focus.id | |
53 | |
54 def hasFocus(self): | |
55 return (self.getFocus() == self.X11_id) if self.display else True | |
56 | |
57 def useX11(self): | |
58 return bool(self.display) | |
59 | |
60 def sendNotification(self, summ_mess, body_mess=""): | |
61 """Send notification to the user if possible""" | |
62 #TODO: check options before sending notifications | |
63 if self.freedesktop_int: | |
64 self.sendFDNotification(summ_mess, body_mess) | |
65 | |
66 def sendFDNotification(self, summ_mess, body_mess=""): | |
67 """Send notification with the FreeDesktop D-Bus API""" | |
68 if self.freedesktop_int: | |
69 app_name = "Primitivus" | |
70 replaces_id = 0 | |
71 app_icon = "" | |
72 summary = summ_mess | |
73 body = body_mess | |
74 actions = dbus.Array(signature='s') | |
75 hints = dbus.Dictionary(signature='sv') | |
76 expire_timeout = -1 | |
77 | |
78 self.freedesktop_int.Notify(app_name, replaces_id, app_icon, summary, body, actions, hints, expire_timeout) |