annotate sat_frontends/primitivus/notify.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents 524856bd7b19
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
1 #!/usr/bin/env python3
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
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
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
4 # Primitivus: a SAT frontend
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
5 # Copyright (C) 2009-2021 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
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
10 # (at your option) any later version.
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
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
15 # 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
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 588
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
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
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 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
21
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
22
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
23 class Notify(object):
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
24 """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
25
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
26 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
27
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
28 # X11 stuff
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
29 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
30 self.X11_id = -1
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
31
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
32 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
33 from Xlib import display as X_display
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
34
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
35 self.display = X_display.Display()
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
36 self.X11_id = self.get_focus()
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
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
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
40 # Now we try to connect to Freedesktop D-Bus API
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
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()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
43 db_object = bus.get_object(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
44 "org.freedesktop.Notifications",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
45 "/org/freedesktop/Notifications",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
46 follow_name_owner_changes=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
47 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
48 self.freedesktop_int = dbus.Interface(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
49 db_object, dbus_interface="org.freedesktop.Notifications"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
50 )
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
51 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
52 self.freedesktop_int = None
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
53
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
54 def get_focus(self):
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
55 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
56 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
57 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
58
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
59 def has_focus(self):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
60 return (self.get_focus() == self.X11_id) if self.display else True
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
61
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
62 def use_x11(self):
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
63 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
64
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
65 def send_notification(self, summ_mess, 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
66 """Send notification to the user if possible"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 # TODO: check options before sending notifications
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
68 if self.freedesktop_int:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
69 self.send_fd_notification(summ_mess, body_mess)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
70
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
71 def send_fd_notification(self, summ_mess, 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
72 """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
73 if self.freedesktop_int:
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
74 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
75 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
76 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
77 summary = summ_mess
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
78 body = body_mess
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
79 actions = dbus.Array(signature="s")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
80 hints = dbus.Dictionary(signature="sv")
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
81 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
82
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
83 self.freedesktop_int.Notify(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
84 app_name,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 replaces_id,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
86 app_icon,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
87 summary,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
88 body,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
89 actions,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
90 hints,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
91 expire_timeout,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
92 )