diff options
author | Florian Bruhin <me@the-compiler.org> | 2022-06-21 10:45:24 +0200 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2022-06-21 12:26:40 +0200 |
commit | 1d816ef5e21d07e92ff9f6feb1b1661464b770cd (patch) | |
tree | 24cc537dd16cc7ce3f4eab19cff6ae86f5ed1a17 | |
parent | 110936a5e8744bfbb8ccb06d14457b920c24fc3e (diff) | |
download | qutebrowser-1d816ef5e21d07e92ff9f6feb1b1661464b770cd.tar.gz qutebrowser-1d816ef5e21d07e92ff9f6feb1b1661464b770cd.zip |
notification: Split up present()
-rw-r--r-- | qutebrowser/browser/webengine/notification.py | 118 |
1 files changed, 80 insertions, 38 deletions
diff --git a/qutebrowser/browser/webengine/notification.py b/qutebrowser/browser/webengine/notification.py index 444465bf2..72b18fd96 100644 --- a/qutebrowser/browser/webengine/notification.py +++ b/qutebrowser/browser/webengine/notification.py @@ -965,22 +965,42 @@ class DBusNotificationAdapter(AbstractNotificationAdapter): f"Got a message of type {type_str} but expected {expected_type_str}" f"(args: {msg.arguments()})") - def present( + def _verify_notification_id( self, - qt_notification: "QWebEngineNotification", - *, - replaces_id: Optional[int], - ) -> int: - """Shows a notification over DBus.""" - if replaces_id is None: - replaces_id = 0 # 0 is never a valid ID according to the spec + notification_id: int, *, + replaces_id: int, + ) -> None: + """Ensure the returned notification id is valid.""" + if replaces_id not in [0, notification_id]: + msg = ( + f"Wanted to replace notification {replaces_id} but got new id " + f"{notification_id}." + ) + if self._quirks.wrong_replaces_id: + log.misc.debug(msg) + else: + log.misc.error(msg) + + if notification_id <= 0: + self.error.emit(f"Got invalid notification id {notification_id}") + def _get_title_arg(self, title: str) -> str: + """Get the title argument for present().""" + # Titles don't support markup (except with broken servers) + if self._quirks.escape_title: + return html.escape(title, quote=False) + return title + + def _get_actions_arg(self) -> QDBusArgument: + """Get the actions argument for present().""" actions = [] if self._capabilities.actions: actions = ['default', 'Activate'] # key, name - actions_arg = QDBusArgument(actions, QMetaType.QStringList) + return QDBusArgument(actions, QMetaType.QStringList) - origin_url_str = qt_notification.origin().toDisplayString() + def _get_hints_arg(self, *, origin_url: QUrl, icon: QImage) -> Dict[str, Any]: + """Get the hints argument for present().""" + origin_url_str = origin_url.toDisplayString() hints: Dict[str, Any] = { # Include the origin in case the user wants to do different things # with different origin's notifications. @@ -988,11 +1008,10 @@ class DBusNotificationAdapter(AbstractNotificationAdapter): "desktop-entry": "org.qutebrowser.qutebrowser", } - is_useful_origin = self._should_include_origin(qt_notification.origin()) + is_useful_origin = self._should_include_origin(origin_url) if self._capabilities.kde_origin_name and is_useful_origin: hints["x-kde-origin-name"] = origin_url_str - icon = qt_notification.icon() if icon.isNull(): filename = 'icons/qutebrowser-64x64.png' icon = QImage.fromData(resources.read_file_binary(filename)) @@ -1002,22 +1021,58 @@ class DBusNotificationAdapter(AbstractNotificationAdapter): if data is not None: hints[key] = data - # Titles don't support markup (except with broken servers) - title = qt_notification.title() - if self._quirks.escape_title: - title = html.escape(title, quote=False) - - reply = self.interface.call( + return hints + + def _call_notify_wrapper( + self, *, + appname: str, + replaces_id: QVariant, + icon: str, + title: str, + body: str, + actions: QDBusArgument, + hints: Dict[str, Any], + timeout: int, + ) -> Any: + """Wrapper around DBus call to use keyword args.""" + return self.interface.call( QDBus.BlockWithGui, "Notify", - "qutebrowser", # application name - _as_uint32(replaces_id), # replaces notification id - "", # icon name/file URL, we use image-data and friends instead. + appname, + replaces_id, + icon, title, - self._format_body(qt_notification.message(), qt_notification.origin()), - actions_arg, + body, + actions, hints, - -1, # timeout; -1 means 'use default' + timeout, + ) + + def present( + self, + qt_notification: "QWebEngineNotification", + *, + replaces_id: Optional[int], + ) -> int: + """Shows a notification over DBus.""" + if replaces_id is None: + replaces_id = 0 # 0 is never a valid ID according to the spec + + reply = self._call_notify_wrapper( + appname="qutebrowser", + replaces_id=_as_uint32(replaces_id), + icon="", # we use image-data and friends instead + title=self._get_title_arg(qt_notification.title()), + body=self._format_body( + body=qt_notification.message(), + origin_url=qt_notification.origin(), + ), + actions=self._get_actions_arg(), + hints=self._get_hints_arg( + origin_url=qt_notification.origin(), + icon=qt_notification.icon(), + ), + timeout=-1, # use default ) try: @@ -1030,20 +1085,7 @@ class DBusNotificationAdapter(AbstractNotificationAdapter): return -1 notification_id = reply.arguments()[0] - - if replaces_id not in [0, notification_id]: - msg = ( - f"Wanted to replace notification {replaces_id} but got new id " - f"{notification_id}." - ) - if self._quirks.wrong_replaces_id: - log.misc.debug(msg) - else: - log.misc.error(msg) - - if notification_id <= 0: - self.error.emit(f"Got invalid notification id {notification_id}") - + self._verify_notification_id(notification_id, replaces_id=replaces_id) return notification_id def _convert_image(self, qimage: QImage) -> Optional[QDBusArgument]: |