Mercurial > libervia-backend
comparison libervia/tui/notify.py @ 4076:b620a8e882e1
refactoring: rename `libervia.frontends.primitivus` to `libervia.tui`
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 16:25:25 +0200 |
parents | libervia/frontends/primitivus/notify.py@26b7ed2817da |
children |
comparison
equal
deleted
inserted
replaced
4075:47401850dec6 | 4076:b620a8e882e1 |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 # Libervia TUI | |
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (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 Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 import dbus | |
21 | |
22 | |
23 class Notify(object): | |
24 """Used to send notification and detect if we have focus""" | |
25 | |
26 def __init__(self): | |
27 | |
28 # X11 stuff | |
29 self.display = None | |
30 self.X11_id = -1 | |
31 | |
32 try: | |
33 from Xlib import display as X_display | |
34 | |
35 self.display = X_display.Display() | |
36 self.X11_id = self.get_focus() | |
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( | |
44 "org.freedesktop.Notifications", | |
45 "/org/freedesktop/Notifications", | |
46 follow_name_owner_changes=True, | |
47 ) | |
48 self.freedesktop_int = dbus.Interface( | |
49 db_object, dbus_interface="org.freedesktop.Notifications" | |
50 ) | |
51 except: | |
52 self.freedesktop_int = None | |
53 | |
54 def get_focus(self): | |
55 if not self.display: | |
56 return 0 | |
57 return self.display.get_input_focus().focus.id | |
58 | |
59 def has_focus(self): | |
60 return (self.get_focus() == self.X11_id) if self.display else True | |
61 | |
62 def use_x11(self): | |
63 return bool(self.display) | |
64 | |
65 def send_notification(self, summ_mess, body_mess=""): | |
66 """Send notification to the user if possible""" | |
67 # TODO: check options before sending notifications | |
68 if self.freedesktop_int: | |
69 self.send_fd_notification(summ_mess, body_mess) | |
70 | |
71 def send_fd_notification(self, summ_mess, body_mess=""): | |
72 """Send notification with the FreeDesktop D-Bus API""" | |
73 if self.freedesktop_int: | |
74 app_name = "LiberviaTUI" | |
75 replaces_id = 0 | |
76 app_icon = "" | |
77 summary = summ_mess | |
78 body = body_mess | |
79 actions = dbus.Array(signature="s") | |
80 hints = dbus.Dictionary(signature="sv") | |
81 expire_timeout = -1 | |
82 | |
83 self.freedesktop_int.Notify( | |
84 app_name, | |
85 replaces_id, | |
86 app_icon, | |
87 summary, | |
88 body, | |
89 actions, | |
90 hints, | |
91 expire_timeout, | |
92 ) |