From e8ff8349f2111275eda8ff6dc55812097dbfcf9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20I=C3=B1iguez=20Goia?= Date: Sat, 22 Apr 2023 01:35:58 +0200 Subject: [PATCH] ui: improved notifications behaviour Up until now, clicking on a system notification caused several behaviours depending on the Desktop Environment: - On Gnome it executed opensnitch_ui.desktop, which as of today kills the running opensnitch-ui process, and relaunches it. - On other DEs the notifications was just dismissed. To improve the first behaviour, the Hint "desktop-entry" has been removed. Now clicking on a notification on Gnome will bring the main window to the front if it's opened. On the other hand a new button has been added to the notifications, to open the main window if it's not opened, or bring it to the front otherwise. --- ui/opensnitch/notifications.py | 20 +++++++++++++++----- ui/opensnitch/service.py | 9 ++++++++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/ui/opensnitch/notifications.py b/ui/opensnitch/notifications.py index 9e8db3f5..daa400b7 100644 --- a/ui/opensnitch/notifications.py +++ b/ui/opensnitch/notifications.py @@ -35,7 +35,13 @@ class DesktopNotifications(): URGENCY_NORMAL = 1 URGENCY_CRITICAL = 2 + # must be a string + ACTION_ID_OPEN = "action-open" + ACTION_ID_ALLOW = "action-allow" + ACTION_ID_DENY = "action-deny" + def __init__(self): + self.ACTION_OPEN = QC.translate("popups", "Open") self.ACTION_ALLOW = QC.translate("popups", "Allow") self.ACTION_DENY = QC.translate("popups", "Deny") self.IS_LIBNOTIFY_AVAILABLE = True @@ -85,7 +91,7 @@ class DesktopNotifications(): """ return self.DOES_SUPPORT_ACTIONS - def show(self, title, body, icon="dialog-information", urgency=URGENCY_NORMAL): + def show(self, title, body, icon="dialog-information", urgency=URGENCY_NORMAL, callback=None): try: ntf = self.ntf2.Notification(title, body, icon) @@ -99,8 +105,12 @@ class DesktopNotifications(): ntf.set_urgency(urgency) ntf.set_category(self.CATEGORY_NETWORK) - # used to display our app icon an name. - ntf.set_hint(self.HINT_DESKTOP_ENTRY, "opensnitch_ui") + # used to display our app icon and name. + # Note: setting this Hint causes some DEs to call opensnitch_ui.desktop file, + # that as of today, kills and relaunches the current opensnitch-ui process. + #ntf.set_hint(self.HINT_DESKTOP_ENTRY, "opensnitch_ui") + if self.DOES_SUPPORT_ACTIONS and callback != None: + ntf.add_action(self.ACTION_ID_OPEN, self.ACTION_OPEN, callback) ntf.show() except Exception as e: print("[notifications] show() exception:", e) @@ -124,8 +134,8 @@ class DesktopNotifications(): ntf.timeout = timeout * 1000 if self.DOES_SUPPORT_ACTIONS: ntf.set_urgency(self.ntf2.URGENCY_CRITICAL) - ntf.add_action("allow", self.ACTION_ALLOW, callback, connection) - ntf.add_action("deny", self.ACTION_DENY, callback, connection) + ntf.add_action(self.ACTION_ID_ALLOW, self.ACTION_ALLOW, callback, connection) + ntf.add_action(self.ACTION_ID_DENY, self.ACTION_DENY, callback, connection) #ntf.add_action("open-gui", QC.translate("popups", "View"), callback, connection) ntf.set_category(self.CATEGORY_NETWORK) ntf.set_hint(self.HINT_DESKTOP_ENTRY, "opensnitch_ui") diff --git a/ui/opensnitch/service.py b/ui/opensnitch/service.py index 74a9d934..e4d0ec7b 100644 --- a/ui/opensnitch/service.py +++ b/ui/opensnitch/service.py @@ -330,6 +330,12 @@ class UIService(ui_pb2_grpc.UIServicer, QtWidgets.QGraphicsObject): @QtCore.pyqtSlot(str, str, int, int) def _show_systray_message(self, title, body, icon, urgency): + def callback_open_clicked(notifObject, action): + if action == DesktopNotifications.ACTION_ID_OPEN: + self._stats_dialog.show() + #self._stats_dialog.raise() + self._stats_dialog.activateWindow() + if self._desktop_notifications.are_enabled(): timeout = self._cfg.getInt(Config.DEFAULT_TIMEOUT_KEY, 15) @@ -338,7 +344,8 @@ class UIService(ui_pb2_grpc.UIServicer, QtWidgets.QGraphicsObject): self._desktop_notifications.show( title, body, - os.path.join(self._path, "res/icon-white.svg") + os.path.join(self._path, "res/icon-white.svg"), + callback=callback_open_clicked ) except: self._tray.showMessage(title, body, icon, timeout * 1000)