From cf2a4d3cf350913cfb2e93b7d78bc52d63ae0499 Mon Sep 17 00:00:00 2001 From: l0drex Date: Thu, 18 Apr 2024 21:56:42 +0200 Subject: [PATCH] Use dbus for notifications --- yin_yang/NotificationHandler.py | 31 ++++++++++++++++++++++++++++--- yin_yang/plugins/notify.py | 13 +++++++++---- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/yin_yang/NotificationHandler.py b/yin_yang/NotificationHandler.py index 1751a827..e001d768 100644 --- a/yin_yang/NotificationHandler.py +++ b/yin_yang/NotificationHandler.py @@ -1,9 +1,34 @@ -import subprocess from logging import Handler +from PySide6.QtDBus import QDBusMessage, QDBusConnection + + +def create_dbus_message(title: str, body: str): + message = QDBusMessage.createMethodCall( + 'org.freedesktop.portal.Desktop', + '/org/freedesktop/portal/desktop', + 'org.freedesktop.portal.Notification', + 'AddNotification' + ) + + notification = { + 'title': title, + 'body': body, + 'icon': 'yin_yang', + 'priority': 'low', + } + + message.setArguments([ + 'YingYang.ThemeChanged', + notification + ]) + + return message + class NotificationHandler(Handler): """Shows logs as notifications""" def emit(self, record): - subprocess.call(['notify-send', record.levelname, str(record.msg), - '-a', 'Yin & Yang', '-u', 'low', '--icon', 'yin_yang']) + connection = QDBusConnection.sessionBus() + message = create_dbus_message(record.levelname, str(record.msg)) + connection.call(message) diff --git a/yin_yang/plugins/notify.py b/yin_yang/plugins/notify.py index 12707bbb..03b9d377 100644 --- a/yin_yang/plugins/notify.py +++ b/yin_yang/plugins/notify.py @@ -1,9 +1,14 @@ -from ._plugin import PluginCommandline +from PySide6.QtDBus import QDBusMessage +from NotificationHandler import create_dbus_message +from ._plugin import DBusPlugin -class Notification(PluginCommandline): + +class Notification(DBusPlugin): def __init__(self): - super().__init__(['notify-send', 'Theme changed', 'Set the theme to {theme}', - '-a', 'Yin & Yang', '-u', 'low', '--icon', 'yin_yang']) + super().__init__() self.theme_light = 'Day' self.theme_dark = 'Night' + + def create_message(self, theme: str) -> QDBusMessage: + return create_dbus_message('Theme changed', f'Set the theme to {theme}')