From 7e5d809683e01df6b740e6ff5c0a4cff7f1020da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20I=C3=B1iguez=20Goia?= Date: Sat, 8 Jan 2022 01:00:04 +0100 Subject: [PATCH] ui, prefs, notifications: fixes - Fixed preferences dialog status error messages. - Improved notifications --- ui/opensnitch/dialogs/preferences.py | 26 +++++++++++++++++++---- ui/opensnitch/notifications.py | 31 ++++++++++------------------ ui/opensnitch/res/preferences.ui | 3 --- ui/opensnitch/service.py | 9 ++++---- 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/ui/opensnitch/dialogs/preferences.py b/ui/opensnitch/dialogs/preferences.py index 1d2b37c3..227739e0 100644 --- a/ui/opensnitch/dialogs/preferences.py +++ b/ui/opensnitch/dialogs/preferences.py @@ -38,6 +38,7 @@ class PreferencesDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]): self._notification_callback.connect(self._cb_notification_callback) self._notifications_sent = {} + self._desktop_notifications = DesktopNotifications() self.setupUi(self) @@ -59,6 +60,7 @@ class PreferencesDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]): self.cmdDBPurgesUp.clicked.connect(lambda: self._cb_cmd_spin_clicked(self.spinDBPurgeInterval, self.SUM)) self.cmdDBPurgesDown.clicked.connect(lambda: self._cb_cmd_spin_clicked(self.spinDBPurgeInterval, self.REST)) self.cmdTestNotifs.clicked.connect(self._cb_test_notifs_clicked) + self.radioSysNotifs.clicked.connect(self._cb_radio_system_notifications) self.helpButton.setToolTipDuration(30 * 1000) if QtGui.QIcon.hasThemeIcon("emblem-default") == False: @@ -147,12 +149,13 @@ class PreferencesDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]): self.dstPortCheck.setChecked(self._cfg.getBool(self._cfg.DEFAULT_POPUP_ADVANCED_DSTPORT)) self.uidCheck.setChecked(self._cfg.getBool(self._cfg.DEFAULT_POPUP_ADVANCED_UID)) - self.groupNotifs.setChecked(self._cfg.getBool(Config.NOTIFICATIONS_ENABLED)) + # by default, if no configuration exists, enable notifications. + self.groupNotifs.setChecked(self._cfg.getBool(Config.NOTIFICATIONS_ENABLED, True)) self.radioSysNotifs.setChecked( - True if self._cfg.getInt(Config.NOTIFICATIONS_TYPE) == Config.NOTIFICATION_TYPE_SYSTEM else False + True if self._cfg.getInt(Config.NOTIFICATIONS_TYPE) == Config.NOTIFICATION_TYPE_SYSTEM and self._desktop_notifications.is_available() == True else False ) self.radioQtNotifs.setChecked( - True if self._cfg.getInt(Config.NOTIFICATIONS_TYPE) == Config.NOTIFICATION_TYPE_QT else False + True if self._cfg.getInt(Config.NOTIFICATIONS_TYPE) == Config.NOTIFICATION_TYPE_QT or self._desktop_notifications.is_available() == False else False ) self.dbType = self._cfg.getInt(self._cfg.DEFAULT_DB_TYPE_KEY) @@ -408,19 +411,23 @@ class PreferencesDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]): self.statusLabel.show() def _set_status_error(self, msg): + self._show_status_label() self.statusLabel.setStyleSheet('color: red') self.statusLabel.setText(msg) def _set_status_successful(self, msg): + self._show_status_label() self.statusLabel.setStyleSheet('color: green') self.statusLabel.setText(msg) def _set_status_message(self, msg): + self._show_status_label() self.statusLabel.setStyleSheet('color: darkorange') self.statusLabel.setText(msg) def _reset_status_message(self): self.statusLabel.setText("") + self._hide_status_label() def _enable_db_cleaner_options(self, enable, db_max_days): self.checkDBMaxDays.setChecked(enable) @@ -500,8 +507,19 @@ class PreferencesDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]): else: spinWidget.setValue(spinWidget.value() - 1) + def _cb_radio_system_notifications(self): + if self._desktop_notifications.is_available() == False: + self.radioSysNotifs.setChecked(False) + self.radioQtNotifs.setChecked(True) + self._set_status_error(QC.translate("notifications", "System notifications are not available, you need to install python3-notify2.")) + return + def _cb_test_notifs_clicked(self): + if self._desktop_notifications.is_available() == False: + self._set_status_error(QC.translate("notifications", "System notifications are not available, you need to install python3-notify2.")) + return + if self.radioSysNotifs.isChecked(): - DesktopNotifications().show("title", "body") + self._desktop_notifications.show("title", "body") else: pass diff --git a/ui/opensnitch/notifications.py b/ui/opensnitch/notifications.py index 8fc46434..1759097a 100644 --- a/ui/opensnitch/notifications.py +++ b/ui/opensnitch/notifications.py @@ -68,7 +68,10 @@ class DesktopNotifications(): self.IS_LIBNOTIFY_AVAILABLE = False def is_available(self): - return self.IS_LIBNOTIFY_AVAILABLE and self._cfg.getBool(Config.NOTIFICATIONS_ENABLED) + return self.IS_LIBNOTIFY_AVAILABLE + + def are_enabled(self): + return self._cfg.getBool(Config.NOTIFICATIONS_ENABLED, True) def support_actions(self): """Returns true if the notifications daemon support actions(buttons). @@ -86,8 +89,8 @@ class DesktopNotifications(): # -1 and 0 are special values if timeout > 0: timeout = timeout * 1000 - ntf.set_timeout(timeout) - ntf.timeout = timeout + ntf.set_timeout(timeout * 1000) + ntf.timeout = timeout * 1000 ntf.set_category(self.CATEGORY_NETWORK) # used to display our app icon an name. @@ -100,16 +103,12 @@ class DesktopNotifications(): def ask(self, connection, timeout, callback): c = connection title = QC.translate("popups", "New outgoing connection") - body = """ - {0} + body = c.process_path + "\n" + body = body + QC.translate("popups", "is connecting to %s on %s port %d") % ( \ + c.dst_host or c.dst_ip, + c.protocol.upper(), + c.dst_port ) - {1} {2}:{3} -> {4}:{5} - UID: {6} PID: {7} - """.format(c.process_path, c.protocol.upper(), - c.src_port, c.src_ip, - c.dst_host if c.dst_host != "" else c.dst_ip, c.dst_port, - c.user_id, c.process_id - ) ntf = self.ntf2.Notification(title, body, "dialog-warning") timeout = self._cfg.getInt(Config.DEFAULT_TIMEOUT_KEY, 15) ntf.set_timeout(timeout * 1000) @@ -122,11 +121,3 @@ class DesktopNotifications(): ntf.set_category(self.CATEGORY_NETWORK) ntf.set_hint(self.HINT_DESKTOP_ENTRY, "opensnitch_ui") ntf.show() - - @staticmethod - def areEnabled(): - """Return if notifications are enabled. - - Default: True - """ - return DesktopNotifications._cfg.getBool(DesktopNotifications._cfg.NOTIFICATIONS_ENABLED, True) diff --git a/ui/opensnitch/res/preferences.ui b/ui/opensnitch/res/preferences.ui index cb04a653..edce03be 100644 --- a/ui/opensnitch/res/preferences.ui +++ b/ui/opensnitch/res/preferences.ui @@ -1376,9 +1376,6 @@ - - - true diff --git a/ui/opensnitch/service.py b/ui/opensnitch/service.py index 63cb8816..f955b3a3 100644 --- a/ui/opensnitch/service.py +++ b/ui/opensnitch/service.py @@ -264,12 +264,13 @@ class UIService(ui_pb2_grpc.UIServicer, QtWidgets.QGraphicsObject): @QtCore.pyqtSlot(str, str, int) def _show_systray_message(self, title, body, icon): - if DesktopNotifications.areEnabled(): - if self._desktop_notifications.is_available() and self._cfg.getInt(Config.NOTIFICATIONS_TYPE) == Config.NOTIFICATION_TYPE_SYSTEM: + if self._desktop_notifications.are_enabled(): + timeout = self._cfg.getInt(Config.DEFAULT_TIMEOUT_KEY, 15) + + if self._desktop_notifications.is_available() and self._cfg.getInt(Config.NOTIFICATIONS_TYPE, 1) == Config.NOTIFICATION_TYPE_SYSTEM: self._desktop_notifications.show(title, body, os.path.join(self._path, "res/icon-white.svg")) else: - timeout = self._cfg.getInt(Config.DEFAULT_TIMEOUT_KEY, 15) - self._tray.showMessage(title, body, icon, timeout) + self._tray.showMessage(title, body, icon, timeout * 1000) if icon == QtWidgets.QSystemTrayIcon.NoIcon: self._tray.setIcon(self.alert_icon)