annotate frontends/src/primitivus/notify.py @ 587:952322b1d490

Remove trailing whitespaces.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 18 Jan 2013 17:55:34 +0100
parents ca13633d3b6b
children beaf6bec2fcd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
3
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Primitivus: a SAT frontend
572
ca13633d3b6b dates update
Goffi <goffi@goffi.org>
parents: 480
diff changeset
6 Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org)
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
7
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
480
2a072735e459 Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents: 459
diff changeset
9 it under the terms of the GNU Affero General Public License as published by
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
12
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
480
2a072735e459 Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents: 459
diff changeset
16 GNU Affero General Public License for more details.
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
17
480
2a072735e459 Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents: 459
diff changeset
18 You should have received a copy of the GNU Affero General Public License
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
21
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import dbus
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
23
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
24 class Notify:
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
25 """Used to send notification and detect if we have focus"""
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
26
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
27 def __init__(self):
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
28
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
29 #X11 stuff
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
30 self.display = None
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
31 self.X11_id = -1
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
32
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
33 try:
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from Xlib import display as X_display
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
35 self.display = X_display.Display()
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
36 self.X11_id = self.getFocus()
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
37 except:
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
38 pass
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
39
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
40 #Now we try to connect to Freedesktop D-Bus API
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
41 try:
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
42 bus = dbus.SessionBus()
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
43 db_object = bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications')
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
44 self.freedesktop_int = dbus.Interface(db_object, dbus_interface='org.freedesktop.Notifications')
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
45 except:
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
46 self.freedesktop_int = None
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
47
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
48
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
49 def getFocus(self):
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
50 if not self.display:
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
51 return 0
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
52 return self.display.get_input_focus().focus.id
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
53
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def hasFocus(self):
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
55 return (self.getFocus() == self.X11_id) if self.display else True
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
56
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
57 def useX11(self):
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
58 return bool(self.display)
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
59
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
60 def sendNotification(self, summ_mess, body_mess=""):
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
61 """Send notification to the user if possible"""
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
62 #TODO: check options before sending notifications
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
63 if self.freedesktop_int:
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self.sendFDNotification(summ_mess, body_mess)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
65
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
66 def sendFDNotification(self, summ_mess, body_mess=""):
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
67 """Send notification with the FreeDesktop D-Bus API"""
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
68 if self.freedesktop_int:
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
69 app_name = "Primitivus"
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
70 replaces_id = 0
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
71 app_icon = ""
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
72 summary = summ_mess
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
73 body = body_mess
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
74 actions = dbus.Array(signature='s')
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
75 hints = dbus.Dictionary(signature='sv')
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
76 expire_timeout = -1
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
77
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.
Goffi <goffi@goffi.org>
parents:
diff changeset
78 self.freedesktop_int.Notify(app_name, replaces_id, app_icon, summary, body, actions, hints, expire_timeout)