From 110936a5e8744bfbb8ccb06d14457b920c24fc3e Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 21 Jun 2022 10:18:50 +0200 Subject: notifications: Refactor selecting candidates --- qutebrowser/browser/webengine/notification.py | 54 ++++++++++++++------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/qutebrowser/browser/webengine/notification.py b/qutebrowser/browser/webengine/notification.py index 692a85376..444465bf2 100644 --- a/qutebrowser/browser/webengine/notification.py +++ b/qutebrowser/browser/webengine/notification.py @@ -48,7 +48,7 @@ import dataclasses import itertools import functools import subprocess -from typing import Any, List, Dict, Optional, Iterator, TYPE_CHECKING +from typing import Any, List, Dict, Optional, Iterator, Type, TYPE_CHECKING from PyQt5.QtCore import (Qt, QObject, QVariant, QMetaType, QByteArray, pyqtSlot, pyqtSignal, QTimer, QProcess, QUrl) @@ -229,31 +229,7 @@ class NotificationBridgePresenter(QObject): message.error("Can't switch to qt notification presenter at runtime.") setting = "auto" - if setting in ["auto", "libnotify"]: - candidates = [ - DBusNotificationAdapter, - SystrayNotificationAdapter, - MessagesNotificationAdapter, - ] - elif setting == "systray": - candidates = [ - SystrayNotificationAdapter, - DBusNotificationAdapter, - MessagesNotificationAdapter, - ] - elif setting == "herbe": - candidates = [ - HerbeNotificationAdapter, - DBusNotificationAdapter, - SystrayNotificationAdapter, - MessagesNotificationAdapter, - ] - elif setting == "messages": - candidates = [MessagesNotificationAdapter] # always succeeds - else: - raise utils.Unreachable(setting) - - for candidate in candidates: + for candidate in self._get_adapter_candidates(setting): try: self._adapter = candidate() except Error as e: @@ -272,6 +248,32 @@ class NotificationBridgePresenter(QObject): self._adapter.error.connect(self._on_adapter_error) self._adapter.clear_all.connect(self._on_adapter_clear_all) + def _get_adapter_candidates( + self, + setting: str, + ) -> List[Type[AbstractNotificationAdapter]]: + candidates: Dict[str, List[Type[AbstractNotificationAdapter]]] = { + "libnotify": [ + DBusNotificationAdapter, + SystrayNotificationAdapter, + MessagesNotificationAdapter, + ], + "systray": [ + SystrayNotificationAdapter, + DBusNotificationAdapter, + MessagesNotificationAdapter, + ], + "herbe": [ + HerbeNotificationAdapter, + DBusNotificationAdapter, + SystrayNotificationAdapter, + MessagesNotificationAdapter, + ], + "messages": [MessagesNotificationAdapter], # always succeeds + } + candidates["auto"] = candidates["libnotify"] + return candidates[setting] + def install(self, profile: "QWebEngineProfile") -> None: """Set the profile to use this bridge as the presenter.""" # WORKAROUND for -- cgit v1.2.3-54-g00ecf