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.
This commit is contained in:
Gustavo Iñiguez Goia 2023-04-22 01:35:58 +02:00
parent 19890062ff
commit e8ff8349f2
Failed to generate hash of commit
2 changed files with 23 additions and 6 deletions

View file

@ -35,7 +35,13 @@ class DesktopNotifications():
URGENCY_NORMAL = 1 URGENCY_NORMAL = 1
URGENCY_CRITICAL = 2 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): def __init__(self):
self.ACTION_OPEN = QC.translate("popups", "Open")
self.ACTION_ALLOW = QC.translate("popups", "Allow") self.ACTION_ALLOW = QC.translate("popups", "Allow")
self.ACTION_DENY = QC.translate("popups", "Deny") self.ACTION_DENY = QC.translate("popups", "Deny")
self.IS_LIBNOTIFY_AVAILABLE = True self.IS_LIBNOTIFY_AVAILABLE = True
@ -85,7 +91,7 @@ class DesktopNotifications():
""" """
return self.DOES_SUPPORT_ACTIONS 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: try:
ntf = self.ntf2.Notification(title, body, icon) ntf = self.ntf2.Notification(title, body, icon)
@ -99,8 +105,12 @@ class DesktopNotifications():
ntf.set_urgency(urgency) ntf.set_urgency(urgency)
ntf.set_category(self.CATEGORY_NETWORK) ntf.set_category(self.CATEGORY_NETWORK)
# used to display our app icon an name. # used to display our app icon and name.
ntf.set_hint(self.HINT_DESKTOP_ENTRY, "opensnitch_ui") # 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() ntf.show()
except Exception as e: except Exception as e:
print("[notifications] show() exception:", e) print("[notifications] show() exception:", e)
@ -124,8 +134,8 @@ class DesktopNotifications():
ntf.timeout = timeout * 1000 ntf.timeout = timeout * 1000
if self.DOES_SUPPORT_ACTIONS: if self.DOES_SUPPORT_ACTIONS:
ntf.set_urgency(self.ntf2.URGENCY_CRITICAL) ntf.set_urgency(self.ntf2.URGENCY_CRITICAL)
ntf.add_action("allow", self.ACTION_ALLOW, callback, connection) ntf.add_action(self.ACTION_ID_ALLOW, self.ACTION_ALLOW, callback, connection)
ntf.add_action("deny", self.ACTION_DENY, 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.add_action("open-gui", QC.translate("popups", "View"), callback, connection)
ntf.set_category(self.CATEGORY_NETWORK) ntf.set_category(self.CATEGORY_NETWORK)
ntf.set_hint(self.HINT_DESKTOP_ENTRY, "opensnitch_ui") ntf.set_hint(self.HINT_DESKTOP_ENTRY, "opensnitch_ui")

View file

@ -330,6 +330,12 @@ class UIService(ui_pb2_grpc.UIServicer, QtWidgets.QGraphicsObject):
@QtCore.pyqtSlot(str, str, int, int) @QtCore.pyqtSlot(str, str, int, int)
def _show_systray_message(self, title, body, icon, urgency): 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(): if self._desktop_notifications.are_enabled():
timeout = self._cfg.getInt(Config.DEFAULT_TIMEOUT_KEY, 15) 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( self._desktop_notifications.show(
title, title,
body, body,
os.path.join(self._path, "res/icon-white.svg") os.path.join(self._path, "res/icon-white.svg"),
callback=callback_open_clicked
) )
except: except:
self._tray.showMessage(title, body, icon, timeout * 1000) self._tray.showMessage(title, body, icon, timeout * 1000)