From 42baf6e1b64c464fe39a014b9cf412bb6f1383b1 Mon Sep 17 00:00:00 2001 From: Heddxh Date: Mon, 15 Apr 2024 20:10:14 +0800 Subject: [PATCH 01/14] Specify module name to logger --- yin_yang/__main__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/yin_yang/__main__.py b/yin_yang/__main__.py index a4474c7..1521234 100755 --- a/yin_yang/__main__.py +++ b/yin_yang/__main__.py @@ -19,7 +19,7 @@ from yin_yang.config import config, Modes from yin_yang.ui import main_window_connector -logger = logging.getLogger() +logger = logging.getLogger(__name__) def setup_logger(use_systemd_journal: bool): @@ -97,7 +97,7 @@ def systray_icon_clicked(reason: QSystemTrayIcon.ActivationReason): logger.debug(f'Using language {lang}') # system translations - path = QLibraryInfo.path(QLibraryInfo.TranslationsPath) + path = QLibraryInfo.path(QLibraryInfo.LibraryPath.TranslationsPath) translator = QTranslator(app) if translator.load(QLocale.system(), 'qtbase', '_', path): app.installTranslator(translator) From 9832e73abd2efd12ea4edbfd0f816ea71fea0ac1 Mon Sep 17 00:00:00 2001 From: Heddxh Date: Tue, 16 Apr 2024 21:32:21 +0800 Subject: [PATCH 02/14] Refactor DBusPlugin --- yin_yang/plugins/_plugin.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/yin_yang/plugins/_plugin.py b/yin_yang/plugins/_plugin.py index de7abd7..e33822c 100644 --- a/yin_yang/plugins/_plugin.py +++ b/yin_yang/plugins/_plugin.py @@ -4,13 +4,13 @@ from abc import ABC, abstractmethod from configparser import ConfigParser from pathlib import Path -from typing import Optional, List +from typing import List, Optional from PySide6.QtDBus import QDBusConnection, QDBusMessage from PySide6.QtGui import QColor, QRgba64 -from PySide6.QtWidgets import QGroupBox, QHBoxLayout, QLineEdit, QComboBox +from PySide6.QtWidgets import QComboBox, QGroupBox, QHBoxLayout, QLineEdit -from ..meta import UnsupportedDesktopError, FileFormat +from ..meta import FileFormat, UnsupportedDesktopError logger = logging.getLogger(__name__) @@ -253,25 +253,30 @@ def set_theme(self, theme: str): class DBusPlugin(Plugin): + """A class for plugins that mainly switching theme via DBus""" def __init__(self): super().__init__() self.connection = QDBusConnection.sessionBus() + self.message = QDBusMessage() + self.message_data: List[str] = ['' for _ in range(4)] # Store DBusMessage data(destination, path, interface, method) def set_theme(self, theme: str): + """Check arguments, create DBus message and then call""" if not (self.available and self.enabled): return if not theme: raise ValueError(f'Theme \"{theme}\" is invalid') - self.call(self.create_message(theme)) + self.create_message(theme) + self.call() @abstractmethod - def create_message(self, theme: str) -> QDBusMessage: + def create_message(self, theme: str) -> None: raise NotImplementedError(f'Plugin {self.name} did not implement create_message()') - def call(self, message) -> QDBusMessage: - return self.connection.call(message) + def call(self) -> QDBusMessage: + return self.connection.call(self.message) class ConfigFilePlugin(Plugin): From 88993096f5acd01779530a2240a1926ef5bc3305 Mon Sep 17 00:00:00 2001 From: Heddxh Date: Wed, 17 Apr 2024 11:16:10 +0800 Subject: [PATCH 03/14] Port wallpaper plugin to new dbus plugin --- yin_yang/plugins/wallpaper.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/yin_yang/plugins/wallpaper.py b/yin_yang/plugins/wallpaper.py index bc5d058..7589663 100755 --- a/yin_yang/plugins/wallpaper.py +++ b/yin_yang/plugins/wallpaper.py @@ -6,7 +6,7 @@ from PySide6.QtDBus import QDBusMessage from ..meta import Desktop -from ._plugin import PluginDesktopDependent, PluginCommandline, DBusPlugin +from yin_yang.plugins._plugin import PluginDesktopDependent, PluginCommandline, DBusPlugin from .system import test_gnome_availability logger = logging.getLogger(__name__) @@ -87,15 +87,18 @@ def check_theme(theme: str) -> bool: class _Kde(DBusPlugin): - name = 'Wallpaper' + @property + def name(self): + return 'Wallpaper' def __init__(self): super().__init__() self._theme_light = None self._theme_dark = None + self.message_data = ['org.kde.plasmashell', '/PlasmaShell', 'org.kde.PlasmaShell', 'evaluateScript'] @property - def theme_light(self) -> str: + def theme_light(self): return self._theme_light @theme_light.setter @@ -104,7 +107,7 @@ def theme_light(self, value: str): self._theme_light = value @property - def theme_dark(self) -> str: + def theme_dark(self): return self._theme_dark @theme_dark.setter @@ -116,13 +119,8 @@ def theme_dark(self, value: str): def available(self) -> bool: return True - def create_message(self, theme: str) -> QDBusMessage: - message = QDBusMessage.createMethodCall( - 'org.kde.plasmashell', - '/PlasmaShell', - 'org.kde.PlasmaShell', - 'evaluateScript', - ) + def create_message(self, theme: str): + message = QDBusMessage.createMethodCall(*self.message_data) message.setArguments([ 'string:' 'var Desktops = desktops();' @@ -133,7 +131,6 @@ def create_message(self, theme: str) -> QDBusMessage: f' d.writeConfig("Image", "file:{theme}");' '}' ]) - return message class _Xfce(PluginCommandline): From 5079883a5e7c10c239b7afcf0f380c36d859bb02 Mon Sep 17 00:00:00 2001 From: Heddxh Date: Wed, 17 Apr 2024 17:13:10 +0800 Subject: [PATCH 04/14] Port gtk(_kde) plugin to new dbus plugin Add dconf/gsettings as fallback method --- yin_yang/plugins/gtk.py | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/yin_yang/plugins/gtk.py b/yin_yang/plugins/gtk.py index 89ba084..4c927b7 100755 --- a/yin_yang/plugins/gtk.py +++ b/yin_yang/plugins/gtk.py @@ -68,48 +68,52 @@ def available(self) -> bool: class _Kde(DBusPlugin): - name = 'GTK' + @property + def name(self): + return 'GTK' def __init__(self): super().__init__() self.theme_light = 'Breeze' self.theme_dark = 'Breeze' + self.message_data = ['org.kde.GtkConfig', '/GtkConfig', 'org.kde.GtkConfig', 'setGtkTheme'] - def create_message(self, theme: str) -> QDBusMessage: - message = QDBusMessage.createMethodCall( - 'org.kde.GtkConfig', - '/GtkConfig', - 'org.kde.GtkConfig', - 'setGtkTheme' - ) - message.setArguments([theme]) - return message + def create_message(self, theme: str): + self.message = QDBusMessage.createMethodCall(*self.message_data) + self.message.setArguments([theme]) def set_theme(self, theme: str): - response = self.call(self.create_message(theme)) + """Call DBus interface of kde-ftk-config if installed. + Otherwise try changing xsettingsd's conf and dconf""" - if response.type() != QDBusMessage.MessageType.ErrorMessage: + if self.connection.interface().isServiceRegistered('org.kde.GtkConfig'): + super().set_theme(theme) return - logger.warning('kde-gtk-config not available, trying xsettingsd') + # User don't have kde-gtk-config installed, try xsettingsd and dconf + logger.warning('kde-gtk-config not available, trying xsettingsd and dconf') + + # xsettingsd xsettingsd_conf_path = Path.home() / '.config/xsettingsd/xsettingsd.conf' if not xsettingsd_conf_path.exists(): logger.warning('xsettingsd not available') return - with open(xsettingsd_conf_path, 'r') as f: lines = f.readlines() for i, line in enumerate(lines): if line.startswith('Net/ThemeName'): lines[i] = f'Net/ThemeName "{theme}"\n' break - with open(xsettingsd_conf_path, 'w') as f: f.writelines(lines) - # send signal to read new config subprocess.run(['killall', '-HUP', 'xsettingsd']) + # change dconf db. since dconf sending data as GVariant, use gsettings instead + subprocess.run(['gsettings', 'set', 'org.gnome.desktop.interface', 'gtk-theme', '{theme}']) + color_scheme = 'prefer-dark' if theme == self.theme_dark else 'prefer-light' + subprocess.run(['gsettings', 'set', 'org.gnome.desktop.interface', 'color-scheme', f'{color_scheme}']) + class _Xfce(PluginCommandline): def __init__(self): From 4b517300f5cff5dcd3c90ab4ed6af2a2a05bdf19 Mon Sep 17 00:00:00 2001 From: Heddxh Date: Mon, 22 Apr 2024 21:21:30 +0800 Subject: [PATCH 05/14] Port wallpaper(_kde) plugin to use plasma-apply-wallpaperimage --- yin_yang/plugins/wallpaper.py | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/yin_yang/plugins/wallpaper.py b/yin_yang/plugins/wallpaper.py index 7589663..162982c 100755 --- a/yin_yang/plugins/wallpaper.py +++ b/yin_yang/plugins/wallpaper.py @@ -2,11 +2,11 @@ import subprocess from pathlib import Path -from PySide6.QtWidgets import QDialogButtonBox, QVBoxLayout, QWidget, QLineEdit -from PySide6.QtDBus import QDBusMessage +from PySide6.QtWidgets import QDialogButtonBox, QLineEdit, QVBoxLayout, QWidget + +from yin_yang.plugins._plugin import PluginCommandline, PluginDesktopDependent from ..meta import Desktop -from yin_yang.plugins._plugin import PluginDesktopDependent, PluginCommandline, DBusPlugin from .system import test_gnome_availability logger = logging.getLogger(__name__) @@ -86,16 +86,15 @@ def check_theme(theme: str) -> bool: return True -class _Kde(DBusPlugin): +class _Kde(PluginCommandline): @property def name(self): return 'Wallpaper' def __init__(self): - super().__init__() + super().__init__(['/usr/bin/plasma-apply-wallpaperimage', '{theme}']) self._theme_light = None self._theme_dark = None - self.message_data = ['org.kde.plasmashell', '/PlasmaShell', 'org.kde.PlasmaShell', 'evaluateScript'] @property def theme_light(self): @@ -119,19 +118,6 @@ def theme_dark(self, value: str): def available(self) -> bool: return True - def create_message(self, theme: str): - message = QDBusMessage.createMethodCall(*self.message_data) - message.setArguments([ - 'string:' - 'var Desktops = desktops();' - 'for (let i = 0; i < Desktops.length; i++) {' - ' let d = Desktops[i];' - ' d.wallpaperPlugin = "org.kde.image";' - ' d.currentConfigGroup = Array("Wallpaper", "org.kde.image", "General");' - f' d.writeConfig("Image", "file:{theme}");' - '}' - ]) - class _Xfce(PluginCommandline): def __init__(self): From 4980cae02e33c81038259e52f1cd67f9471820c0 Mon Sep 17 00:00:00 2001 From: Heddxh Date: Mon, 22 Apr 2024 21:22:05 +0800 Subject: [PATCH 06/14] Fix typos in gtk(_kde) plugin --- yin_yang/plugins/gtk.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/yin_yang/plugins/gtk.py b/yin_yang/plugins/gtk.py index 4c927b7..a66cddc 100755 --- a/yin_yang/plugins/gtk.py +++ b/yin_yang/plugins/gtk.py @@ -52,8 +52,8 @@ def __init__(self): @property def available(self) -> bool: return test_gnome_availability(self.command) - - + + class _Budgie(PluginCommandline): name = 'GTK' @@ -83,10 +83,11 @@ def create_message(self, theme: str): self.message.setArguments([theme]) def set_theme(self, theme: str): - """Call DBus interface of kde-ftk-config if installed. + """Call DBus interface of kde-gtk-config if installed. Otherwise try changing xsettingsd's conf and dconf""" - if self.connection.interface().isServiceRegistered('org.kde.GtkConfig'): + if self.connection.interface().isServiceRegistered('org.kde.GtkConfig').value(): + logger.debug("Detected kde-gtk-config, use it") super().set_theme(theme) return @@ -110,7 +111,7 @@ def set_theme(self, theme: str): subprocess.run(['killall', '-HUP', 'xsettingsd']) # change dconf db. since dconf sending data as GVariant, use gsettings instead - subprocess.run(['gsettings', 'set', 'org.gnome.desktop.interface', 'gtk-theme', '{theme}']) + subprocess.run(['gsettings', 'set', 'org.gnome.desktop.interface', 'gtk-theme', f'{theme}']) color_scheme = 'prefer-dark' if theme == self.theme_dark else 'prefer-light' subprocess.run(['gsettings', 'set', 'org.gnome.desktop.interface', 'color-scheme', f'{color_scheme}']) From 9700d6b3e9aff3f124d40dd5a95d505f247ba4a7 Mon Sep 17 00:00:00 2001 From: Heddxh Date: Wed, 24 Apr 2024 22:57:51 +0800 Subject: [PATCH 07/14] Revert "Port wallpaper(_kde) plugin to use plasma-apply-wallpaperimage" This reverts commit fee0c2e0d5638d8351cd7120a66ee39c0c8a33d4. --- yin_yang/plugins/wallpaper.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/yin_yang/plugins/wallpaper.py b/yin_yang/plugins/wallpaper.py index 162982c..7589663 100755 --- a/yin_yang/plugins/wallpaper.py +++ b/yin_yang/plugins/wallpaper.py @@ -2,11 +2,11 @@ import subprocess from pathlib import Path -from PySide6.QtWidgets import QDialogButtonBox, QLineEdit, QVBoxLayout, QWidget - -from yin_yang.plugins._plugin import PluginCommandline, PluginDesktopDependent +from PySide6.QtWidgets import QDialogButtonBox, QVBoxLayout, QWidget, QLineEdit +from PySide6.QtDBus import QDBusMessage from ..meta import Desktop +from yin_yang.plugins._plugin import PluginDesktopDependent, PluginCommandline, DBusPlugin from .system import test_gnome_availability logger = logging.getLogger(__name__) @@ -86,15 +86,16 @@ def check_theme(theme: str) -> bool: return True -class _Kde(PluginCommandline): +class _Kde(DBusPlugin): @property def name(self): return 'Wallpaper' def __init__(self): - super().__init__(['/usr/bin/plasma-apply-wallpaperimage', '{theme}']) + super().__init__() self._theme_light = None self._theme_dark = None + self.message_data = ['org.kde.plasmashell', '/PlasmaShell', 'org.kde.PlasmaShell', 'evaluateScript'] @property def theme_light(self): @@ -118,6 +119,19 @@ def theme_dark(self, value: str): def available(self) -> bool: return True + def create_message(self, theme: str): + message = QDBusMessage.createMethodCall(*self.message_data) + message.setArguments([ + 'string:' + 'var Desktops = desktops();' + 'for (let i = 0; i < Desktops.length; i++) {' + ' let d = Desktops[i];' + ' d.wallpaperPlugin = "org.kde.image";' + ' d.currentConfigGroup = Array("Wallpaper", "org.kde.image", "General");' + f' d.writeConfig("Image", "file:{theme}");' + '}' + ]) + class _Xfce(PluginCommandline): def __init__(self): From d2b548499a229a518cef0409e0c2c6880f176b38 Mon Sep 17 00:00:00 2001 From: Heddxh Date: Wed, 24 Apr 2024 23:02:07 +0800 Subject: [PATCH 08/14] Update DBus plugin --- yin_yang/plugins/_plugin.py | 4 ++-- yin_yang/plugins/gtk.py | 3 +-- yin_yang/plugins/wallpaper.py | 3 +-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/yin_yang/plugins/_plugin.py b/yin_yang/plugins/_plugin.py index e33822c..6d7dfce 100644 --- a/yin_yang/plugins/_plugin.py +++ b/yin_yang/plugins/_plugin.py @@ -254,11 +254,11 @@ def set_theme(self, theme: str): class DBusPlugin(Plugin): """A class for plugins that mainly switching theme via DBus""" - def __init__(self): + def __init__(self, message_data: List[str]): super().__init__() self.connection = QDBusConnection.sessionBus() self.message = QDBusMessage() - self.message_data: List[str] = ['' for _ in range(4)] # Store DBusMessage data(destination, path, interface, method) + self.message_data: List[str] = message_data # Store DBusMessage data(destination, path, interface, method) def set_theme(self, theme: str): """Check arguments, create DBus message and then call""" diff --git a/yin_yang/plugins/gtk.py b/yin_yang/plugins/gtk.py index a66cddc..bb0f7f6 100755 --- a/yin_yang/plugins/gtk.py +++ b/yin_yang/plugins/gtk.py @@ -73,10 +73,9 @@ def name(self): return 'GTK' def __init__(self): - super().__init__() + super().__init__(['org.kde.GtkConfig', '/GtkConfig', 'org.kde.GtkConfig', 'setGtkTheme']) self.theme_light = 'Breeze' self.theme_dark = 'Breeze' - self.message_data = ['org.kde.GtkConfig', '/GtkConfig', 'org.kde.GtkConfig', 'setGtkTheme'] def create_message(self, theme: str): self.message = QDBusMessage.createMethodCall(*self.message_data) diff --git a/yin_yang/plugins/wallpaper.py b/yin_yang/plugins/wallpaper.py index 7589663..fcab9c3 100755 --- a/yin_yang/plugins/wallpaper.py +++ b/yin_yang/plugins/wallpaper.py @@ -92,10 +92,9 @@ def name(self): return 'Wallpaper' def __init__(self): - super().__init__() + super().__init__(['org.kde.plasmashell', '/PlasmaShell', 'org.kde.PlasmaShell', 'evaluateScript']) self._theme_light = None self._theme_dark = None - self.message_data = ['org.kde.plasmashell', '/PlasmaShell', 'org.kde.PlasmaShell', 'evaluateScript'] @property def theme_light(self): From 462c9ae53d9be3834574631d7a4a22f3ec05890c Mon Sep 17 00:00:00 2001 From: Heddxh Date: Thu, 25 Apr 2024 19:54:29 +0800 Subject: [PATCH 09/14] Add list_paths method for dbus plugin to list sub paths --- yin_yang/plugins/_plugin.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/yin_yang/plugins/_plugin.py b/yin_yang/plugins/_plugin.py index 6d7dfce..1a0007e 100644 --- a/yin_yang/plugins/_plugin.py +++ b/yin_yang/plugins/_plugin.py @@ -278,6 +278,21 @@ def create_message(self, theme: str) -> None: def call(self) -> QDBusMessage: return self.connection.call(self.message) + def list_paths(self, service: str, path: str) -> List[str]: + """ Get all subpath under given pth of service + :path: should start with / but without / on its end + """ + assert path.startswith('/') and not path.endswith('/'), "list_paths wrong, :path: should start with / but without / on its end" + msg = QDBusMessage.createMethodCall(service, path, "org.freedesktop.DBus.Introspectable", "Introspect") + reply = self.connection.call(msg) + if reply.errorName(): + logger.debug(f"No subpath available under {service} {path}") + return [] + + xml = reply.arguments()[0] + sub_path_names = [line.split('"')[1] for line in xml.split("\n") if line.startswith(" Date: Thu, 25 Apr 2024 19:54:50 +0800 Subject: [PATCH 10/14] Port konsole plugin --- yin_yang/plugins/konsole.py | 136 +++++++++++++----------------------- 1 file changed, 49 insertions(+), 87 deletions(-) diff --git a/yin_yang/plugins/konsole.py b/yin_yang/plugins/konsole.py index 194f3c6..dd52d16 100644 --- a/yin_yang/plugins/konsole.py +++ b/yin_yang/plugins/konsole.py @@ -1,21 +1,19 @@ import logging import os import re -import subprocess from configparser import ConfigParser from itertools import chain from pathlib import Path from shutil import copyfile -import psutil -from PySide6.QtDBus import QDBusConnection, QDBusMessage +from PySide6.QtDBus import QDBusMessage -from ._plugin import Plugin +from ._plugin import DBusPlugin logger = logging.getLogger(__name__) -class Konsole(Plugin): +class Konsole(DBusPlugin): """ Themes are profiles. To use a color scheme, create a new profile or edit one to use the desired color scheme. @@ -24,12 +22,15 @@ class Konsole(Plugin): global_path = Path('/usr/share/konsole') config_path = Path.home() / '.config/konsolerc' + apps_have_konsole = ["org.kde.konsole", "org.kde.yakuake", "org.kde.dolphin", "org.kde.kate"] + """ All apps that can using kobnsole, need expand """ + @property def user_path(self) -> Path: return Path.home() / '.local/share/konsole' def __init__(self): - super().__init__() + super().__init__(['org.kde.konsole', 'Sessions/', 'org.kde.konsole.Session', 'setProfile']) self._theme_light = 'BlackOnWhite' self._theme_dark = 'Breeze' @@ -51,6 +52,10 @@ def theme_dark(self, value): self.update_profile(True, value) self._theme_dark = value + def create_message(self, theme: str): + message = QDBusMessage.createMethodCall(*self.message_data) + message.setArguments([theme]) + def set_mode(self, dark: bool) -> bool: # run checks if not super().set_mode(dark): @@ -61,41 +66,17 @@ def set_mode(self, dark: bool) -> bool: # update default profile, if application is started afterward self.default_profile = profile + '.profile' - # Set Konsole profile for all sessions - - # Get the process IDs of all running Konsole instances owned by the current user - process_ids = [ - proc.pid for proc in psutil.process_iter() - if proc.name() == 'konsole' and proc.username() == os.getlogin() - ] - - # loop: console processes - for proc_id in process_ids: - logger.debug(f'Changing profile in konsole session {proc_id}') - set_profile(f'org.kde.konsole-{proc_id}', profile) - set_profile(f'org.kde.konsole-{proc_id}', profile, set_default_profile=True) - - # konsole may don't have session dbus like above - set_profile('org.kde.konsole', profile) - set_profile('org.kde.yakuake', profile) - set_profile('org.kde.konsole', profile, set_default_profile=True) - set_profile('org.kde.yakuake', profile, set_default_profile=True) - - process_ids = [ - proc.pid for proc in psutil.process_iter() - if proc.name() == 'dolphin' and proc.username() == os.getlogin() - ] - - # loop: dolphin processes - for proc_id in process_ids: - logger.debug(f'Changing profile in dolphin session {proc_id}') - set_profile(f'org.kde.dolphin-{proc_id}', profile) - set_profile(f'org.kde.dolphin-{proc_id}', - profile, set_default_profile=True) + # Find available konsole sessions, including dolphin, yakuake, kate, etc + services = [str(n) for n in self.connection.interface().registeredServiceNames().value() + if n.split('-')[0] in self.apps_have_konsole] + for service in services: + logger.debug(f'Changing profile in konsole session {service}') + self.set_profile(service, profile=profile) + self.set_profile(service, profile, set_default_profile=True) return True - def set_theme(self, theme: str): + def set_theme(self, theme: str): # type: ignore # everything is done in set_mode (above) pass @@ -175,6 +156,7 @@ def default_profile(self, value: str): with self.config_path.open('r') as file: lines = file.readlines() + match: re.Match[str] | None = None for i, line in enumerate(lines): # Search for the pattern "DefaultProfile=*" match = re.search(r'DefaultProfile=(.*)', line) @@ -184,10 +166,12 @@ def default_profile(self, value: str): logger.debug(f'Changing default profile to {value}') lines[i] = f'DefaultProfile={value}\n' break - else: - logger.debug('No default profile found') - with self.config_path.open('w') as file: - file.writelines(lines) + + if match is None: + logger.error(f'No DefaultProfile field found in {self.config_path}') + else: + with self.config_path.open('w') as file: + file.writelines(lines) def update_profile(self, dark: bool, theme: str): if not self.available or theme == '': @@ -202,7 +186,7 @@ def update_profile(self, dark: bool, theme: str): self.create_profiles() profile_config = ConfigParser() - profile_config.optionxform = str + profile_config.optionxform = lambda optionstr: optionstr profile_config.read(file_path) try: @@ -225,7 +209,7 @@ def create_profiles(self): # Change name in file profile_config = ConfigParser() - profile_config.optionxform = str + profile_config.optionxform = lambda optionstr: optionstr profile_config.read(light_profile) profile_config['General']['Name'] = light_profile.stem @@ -239,47 +223,25 @@ def create_profiles(self): with open(dark_profile, 'w') as file: profile_config.write(file) - -def set_profile(service: str, profile: str, set_default_profile: bool = False): - # connect to the session bus - connection = QDBusConnection.sessionBus() - if set_default_profile: - path = 'Sessions/' - interface = 'org.kde.konsole.Session' - method = 'setProfile' - else: - path = 'Windows/' - interface = 'org.kde.konsole.Window' - method = 'setDefaultProfile' - - # maybe it's possible with pyside6 dbus packages, but this was simpler and worked - try: - sessions = subprocess.check_output( - f'qdbus {service} | grep "{path}"', shell=True) - except subprocess.CalledProcessError: - try: - sessions = subprocess.check_output( - f'qdbus org.kde.konsole | grep "{path}"', shell=True - ) - logger.debug(f'Found org.kde.konsole, use that instead') - service = "org.kde.konsole" - except subprocess.CalledProcessError: - # happens when dolphins konsole is not opened + def set_profile(self, service: str, profile: str, set_default_profile: bool = False): + if set_default_profile: + path = '/Sessions' + interface = 'org.kde.konsole.Session' + method = 'setProfile' + else: + path = '/Windows' + interface = 'org.kde.konsole.Window' + method = 'setDefaultProfile' + + for path in self.list_paths(service, path): logger.debug( - f'No Konsole sessions available in service {service}, skipping') - return - sessions = sessions.decode('utf-8').removesuffix('\n').split('\n') - - # loop: process sessions - for session in sessions: - logger.debug( - f'Changing {"default" if set_default_profile else ""} profile of session {session} to {profile}') - # set profile - message = QDBusMessage.createMethodCall( - service, - session, - interface, - method - ) - message.setArguments([profile]) - connection.call(message) + f'Changing {"default" if set_default_profile else ""} profile of {path} to {profile}') + # set profile + message = QDBusMessage.createMethodCall( + service, + path, + interface, + method + ) + message.setArguments([profile]) + self.connection.call(message) From 60bd132b9ee510d4051df46de34cfe21acbf048a Mon Sep 17 00:00:00 2001 From: Heddxh Date: Thu, 25 Apr 2024 19:55:40 +0800 Subject: [PATCH 11/14] Shut pylance up in plugins.__init__ --- yin_yang/plugins/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/yin_yang/plugins/__init__.py b/yin_yang/plugins/__init__.py index f81da3f..50a5209 100755 --- a/yin_yang/plugins/__init__.py +++ b/yin_yang/plugins/__init__.py @@ -1,3 +1,4 @@ +from typing import List from ..meta import Desktop from . import system, colors, gtk, icons, kvantum, wallpaper, custom from . import firefox, only_office, okular @@ -9,7 +10,7 @@ from yin_yang.plugins._plugin import Plugin, ExternalPlugin -def get_plugins(desktop: Desktop) -> [Plugin]: +def get_plugins(desktop: Desktop) -> List[Plugin]: return [ system.System(desktop), colors.Colors(desktop), From ccac5c58103a3b6b28e59a08ed42a9a9058d64fe Mon Sep 17 00:00:00 2001 From: Heddxh Date: Sat, 27 Apr 2024 22:13:15 +0800 Subject: [PATCH 12/14] Port notify plugin --- yin_yang/plugins/_plugin.py | 2 +- yin_yang/plugins/notify.py | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/yin_yang/plugins/_plugin.py b/yin_yang/plugins/_plugin.py index 1a0007e..a8b98d5 100644 --- a/yin_yang/plugins/_plugin.py +++ b/yin_yang/plugins/_plugin.py @@ -257,7 +257,7 @@ class DBusPlugin(Plugin): def __init__(self, message_data: List[str]): super().__init__() self.connection = QDBusConnection.sessionBus() - self.message = QDBusMessage() + self.message: QDBusMessage self.message_data: List[str] = message_data # Store DBusMessage data(destination, path, interface, method) def set_theme(self, theme: str): diff --git a/yin_yang/plugins/notify.py b/yin_yang/plugins/notify.py index 6184eeb..adaff67 100644 --- a/yin_yang/plugins/notify.py +++ b/yin_yang/plugins/notify.py @@ -1,14 +1,12 @@ -from PySide6.QtDBus import QDBusMessage - from ..NotificationHandler import create_dbus_message from ._plugin import DBusPlugin class Notification(DBusPlugin): def __init__(self): - super().__init__() - self.theme_light = 'Day' - self.theme_dark = 'Night' + super().__init__([]) # We do everything in [create_dbus_message] + 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}') + def create_message(self, theme: str): + self.message = create_dbus_message("Theme changed", f"Set the theme to {theme}") From b6bdd4e896256b5fe61cb755389c6bcac1aecb2d Mon Sep 17 00:00:00 2001 From: heddxh <63106571+heddxh@users.noreply.github.com> Date: Mon, 29 Apr 2024 08:59:42 +0800 Subject: [PATCH 13/14] Fix typo in _plugin.py --- yin_yang/plugins/_plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yin_yang/plugins/_plugin.py b/yin_yang/plugins/_plugin.py index a8b98d5..9960842 100644 --- a/yin_yang/plugins/_plugin.py +++ b/yin_yang/plugins/_plugin.py @@ -279,7 +279,7 @@ def call(self) -> QDBusMessage: return self.connection.call(self.message) def list_paths(self, service: str, path: str) -> List[str]: - """ Get all subpath under given pth of service + """ Get all subpath under a given pth of service :path: should start with / but without / on its end """ assert path.startswith('/') and not path.endswith('/'), "list_paths wrong, :path: should start with / but without / on its end" From 828a1ab3a51ac9b1d103b4f48b1720f4590b1d4f Mon Sep 17 00:00:00 2001 From: Heddxh Date: Fri, 24 May 2024 22:03:48 +0800 Subject: [PATCH 14/14] Revert change on create_message method in dbus plugin --- yin_yang/plugins/_plugin.py | 4 ++-- yin_yang/plugins/gtk.py | 7 ++++--- yin_yang/plugins/konsole.py | 3 ++- yin_yang/plugins/notify.py | 6 ++++-- yin_yang/plugins/wallpaper.py | 3 ++- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/yin_yang/plugins/_plugin.py b/yin_yang/plugins/_plugin.py index 9960842..4607947 100644 --- a/yin_yang/plugins/_plugin.py +++ b/yin_yang/plugins/_plugin.py @@ -268,11 +268,11 @@ def set_theme(self, theme: str): if not theme: raise ValueError(f'Theme \"{theme}\" is invalid') - self.create_message(theme) + self.message = self.create_message(theme) self.call() @abstractmethod - def create_message(self, theme: str) -> None: + def create_message(self, theme: str) -> QDBusMessage: raise NotImplementedError(f'Plugin {self.name} did not implement create_message()') def call(self) -> QDBusMessage: diff --git a/yin_yang/plugins/gtk.py b/yin_yang/plugins/gtk.py index bb0f7f6..9fb95b6 100755 --- a/yin_yang/plugins/gtk.py +++ b/yin_yang/plugins/gtk.py @@ -77,9 +77,10 @@ def __init__(self): self.theme_light = 'Breeze' self.theme_dark = 'Breeze' - def create_message(self, theme: str): - self.message = QDBusMessage.createMethodCall(*self.message_data) - self.message.setArguments([theme]) + def create_message(self, theme: str) -> QDBusMessage: + message = QDBusMessage.createMethodCall(*self.message_data) + message.setArguments([theme]) + return message def set_theme(self, theme: str): """Call DBus interface of kde-gtk-config if installed. diff --git a/yin_yang/plugins/konsole.py b/yin_yang/plugins/konsole.py index dd52d16..d9c393d 100644 --- a/yin_yang/plugins/konsole.py +++ b/yin_yang/plugins/konsole.py @@ -52,9 +52,10 @@ def theme_dark(self, value): self.update_profile(True, value) self._theme_dark = value - def create_message(self, theme: str): + def create_message(self, theme: str) -> QDBusMessage: message = QDBusMessage.createMethodCall(*self.message_data) message.setArguments([theme]) + return message def set_mode(self, dark: bool) -> bool: # run checks diff --git a/yin_yang/plugins/notify.py b/yin_yang/plugins/notify.py index adaff67..ba06e37 100644 --- a/yin_yang/plugins/notify.py +++ b/yin_yang/plugins/notify.py @@ -1,3 +1,5 @@ +from PySide6.QtDBus import QDBusMessage + from ..NotificationHandler import create_dbus_message from ._plugin import DBusPlugin @@ -8,5 +10,5 @@ def __init__(self): self.theme_light = "Day" self.theme_dark = "Night" - def create_message(self, theme: str): - self.message = create_dbus_message("Theme changed", f"Set the theme to {theme}") + def create_message(self, theme: str) -> QDBusMessage: + return create_dbus_message("Theme changed", f"Set the theme to {theme}") diff --git a/yin_yang/plugins/wallpaper.py b/yin_yang/plugins/wallpaper.py index fcab9c3..3b135a2 100755 --- a/yin_yang/plugins/wallpaper.py +++ b/yin_yang/plugins/wallpaper.py @@ -118,7 +118,7 @@ def theme_dark(self, value: str): def available(self) -> bool: return True - def create_message(self, theme: str): + def create_message(self, theme: str) -> QDBusMessage: message = QDBusMessage.createMethodCall(*self.message_data) message.setArguments([ 'string:' @@ -130,6 +130,7 @@ def create_message(self, theme: str): f' d.writeConfig("Image", "file:{theme}");' '}' ]) + return message class _Xfce(PluginCommandline):