| 1 |
# Nagstamon - Nagios status monitor for your desktop |
| 2 |
# Copyright (C) 2008-2026 Henri Wahl <[email protected]> et al. |
| 3 |
# |
| 4 |
# This program is free software; you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation; either version 2 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 17 |
|
| 18 |
# DBus connection for Qt |
| 19 |
|
| 20 |
from os import sep |
| 21 |
from random import random |
| 22 |
from sys import (modules, |
| 23 |
stdout) |
| 24 |
from traceback import print_exc |
| 25 |
|
| 26 |
from Nagstamon.config import (AppInfo, |
| 27 |
OS, |
| 28 |
OS_NON_LINUX, |
| 29 |
RESOURCES) |
| 30 |
from Nagstamon.qui.qt import (QObject, |
| 31 |
Signal) |
| 32 |
|
| 33 |
# DBus only interesting for Linux too |
| 34 |
if OS not in OS_NON_LINUX: |
| 35 |
# get DBUS availability - still possible it does not work due to missing |
| 36 |
# .service file on certain distributions |
| 37 |
try: |
| 38 |
from dbus import (Interface, |
| 39 |
SessionBus) |
| 40 |
# no DBusQtMainLoop available for Qt6 |
| 41 |
from dbus.mainloop.glib import DBusGMainLoop as DBusMainLoop |
| 42 |
|
| 43 |
# flag to check later if DBus is available |
| 44 |
DBUS_AVAILABLE = True |
| 45 |
|
| 46 |
except ImportError as error: |
| 47 |
print(error) |
| 48 |
print('No DBus for desktop notification available.') |
| 49 |
DBUS_AVAILABLE = False |
| 50 |
|
| 51 |
|
| 52 |
class DBus(QObject): |
| 53 |
""" |
| 54 |
Create connection to DBus for desktop notification for Linux/Unix |
| 55 |
""" |
| 56 |
|
| 57 |
open_statuswindow = Signal() |
| 58 |
|
| 59 |
# random ID needed because otherwise all instances of Nagstamon |
| 60 |
# will get commands by clicking on notification bubble via DBUS |
| 61 |
random_id = str(int(random() * 100000)) |
| 62 |
|
| 63 |
def __init__(self): |
| 64 |
QObject.__init__(self) |
| 65 |
|
| 66 |
self.id = 0 |
| 67 |
self.actions = [('open' + self.random_id), 'Open status window'] |
| 68 |
self.timeout = 0 |
| 69 |
# use icon from resources in hints, not the package icon - doesn't work either |
| 70 |
self.icon = '' |
| 71 |
# use Nagstamon image if icon is not available from the system |
| 72 |
# see https://developer.gnome.org/notification-spec/#icons-and-images |
| 73 |
self.hints = {'image-path': f'{RESOURCES}{sep}nagstamon.svg'} |
| 74 |
|
| 75 |
if not OS in OS_NON_LINUX and DBUS_AVAILABLE: |
| 76 |
if 'dbus' in modules: |
| 77 |
# try/except needed because of partly occuring problems with DBUS |
| 78 |
# see https://github.com/HenriWahl/Nagstamon/issues/320 |
| 79 |
try: |
| 80 |
# import dbus # never used |
| 81 |
dbus_mainloop = DBusMainLoop(set_as_default=True) |
| 82 |
dbus_sessionbus = SessionBus(dbus_mainloop) |
| 83 |
dbus_object = dbus_sessionbus.get_object('org.freedesktop.Notifications', |
| 84 |
'/org/freedesktop/Notifications') |
| 85 |
self.dbus_interface = Interface(dbus_object, |
| 86 |
dbus_interface='org.freedesktop.Notifications') |
| 87 |
# connect button to action |
| 88 |
self.dbus_interface.connect_to_signal('ActionInvoked', self.action_callback) |
| 89 |
self.connected = True |
| 90 |
|
| 91 |
except Exception: |
| 92 |
print_exc(file=stdout) |
| 93 |
self.connected = False |
| 94 |
else: |
| 95 |
self.connected = False |
| 96 |
|
| 97 |
def show(self, summary, message): |
| 98 |
""" |
| 99 |
simply show a message |
| 100 |
""" |
| 101 |
if self.connected: |
| 102 |
notification_id = self.dbus_interface.Notify(AppInfo.NAME, |
| 103 |
self.id, |
| 104 |
self.icon, |
| 105 |
summary, |
| 106 |
message, |
| 107 |
self.actions, |
| 108 |
self.hints, |
| 109 |
self.timeout) |
| 110 |
# reuse ID |
| 111 |
self.id = int(notification_id) |
| 112 |
|
| 113 |
def action_callback(self, dummy, action): |
| 114 |
""" |
| 115 |
react to clicked action button in notification bubble |
| 116 |
""" |
| 117 |
if action == 'open' + self.random_id: |
| 118 |
self.open_statuswindow.emit() |