From b391fafcb712c1ed4ba857279b924e5ef44ded98 Mon Sep 17 00:00:00 2001 From: Heddxh Date: Sun, 24 Dec 2023 23:34:04 +0800 Subject: [PATCH 1/5] Fix konsole dbus call --- yin_yang/plugins/konsole.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/yin_yang/plugins/konsole.py b/yin_yang/plugins/konsole.py index 8f10d5d1..a1e45a2d 100644 --- a/yin_yang/plugins/konsole.py +++ b/yin_yang/plugins/konsole.py @@ -74,6 +74,7 @@ def set_mode(self, dark: bool) -> bool: logger.debug(f'Changing profile in konsole session {proc_id}') set_profile(f'org.kde.konsole-{proc_id}', profile) + set_profile('org.kde.konsole', profile) # konsole may don't have session dbus like above set_profile('org.kde.yakuake', profile) process_ids = [ @@ -174,8 +175,11 @@ def default_profile(self, value: str): # If a match is found, return the content of the wildcard '*' if match: + 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) From 8fd4fe07c6cb517e47bca54572942a973f864ab7 Mon Sep 17 00:00:00 2001 From: Heddxh Date: Sun, 24 Dec 2023 23:54:39 +0800 Subject: [PATCH 2/5] Modify xsettingsd on kde when needed --- yin_yang/plugins/gtk.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/yin_yang/plugins/gtk.py b/yin_yang/plugins/gtk.py index 7dad089c..153e552e 100755 --- a/yin_yang/plugins/gtk.py +++ b/yin_yang/plugins/gtk.py @@ -1,6 +1,7 @@ import logging from os import scandir, path from pathlib import Path +import subprocess from PySide6.QtDBus import QDBusConnection, QDBusMessage @@ -79,7 +80,23 @@ def set_theme(self, theme: str): 'setGtkTheme' ) message.setArguments([theme]) - connection.call(message) + response = connection.call(message) + if response.type() == QDBusMessage.MessageType.ErrorMessage: + logger.warning('kde-gtk-config not available, try xsettingsd') + xsettingsd_conf_path = Path.home() / '.config' / 'xsettingsd' / 'xsettingsd.conf' + if not xsettingsd_conf_path.exists(): + logger.warning('xsettingsd not available') + 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) + subprocess.run(['killall', '-HUP', 'xsettingsd']) + else: + logger.debug('Success by kde-gtk-config') class _Xfce(PluginCommandline): From dca556d1a0f5e9ccec294b977adc0fe161b50e52 Mon Sep 17 00:00:00 2001 From: Heddxh Date: Sun, 14 Jan 2024 10:01:33 +0800 Subject: [PATCH 3/5] Notify KDE reload theme after kvantum change theme --- tests/test_plugins.py | 6 +++++- yin_yang/__main__.py | 1 + yin_yang/plugins/_plugin.py | 4 ++-- yin_yang/plugins/kvantum.py | 16 ++++++++++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/tests/test_plugins.py b/tests/test_plugins.py index f6fe3a5e..dcec6fe0 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -1,5 +1,9 @@ import unittest +# import os +# import sys +# sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) + from yin_yang.config import config from yin_yang.config import plugins from yin_yang.plugins._plugin import Plugin, ExternalPlugin @@ -41,7 +45,7 @@ def test_set_theme_invalid_state(self): 'set_theme() should not be successful if the plugin is disabled') # NOTE if you want to test that your theme changes, set this value to true - @unittest.skipUnless(False, 'test_theme_changes is disabled') + @unittest.skipUnless(True, 'test_theme_changes is disabled') def test_set_theme_works(self): for pl in filter(lambda p: not isinstance(p, ExternalPlugin) and p.enabled, plugins): with self.subTest('Changing the theme should be successful', plugin=pl.name): diff --git a/yin_yang/__main__.py b/yin_yang/__main__.py index c5d248f4..fc49278e 100755 --- a/yin_yang/__main__.py +++ b/yin_yang/__main__.py @@ -12,6 +12,7 @@ from PySide6.QtWidgets import QSystemTrayIcon, QMenu from systemd import journal +sys.path.append(str(Path(__file__).parent.parent)) from yin_yang.NotificationHandler import NotificationHandler from yin_yang import daemon_handler from yin_yang.meta import ConfigEvent diff --git a/yin_yang/plugins/_plugin.py b/yin_yang/plugins/_plugin.py index 0568d08f..05d43e59 100644 --- a/yin_yang/plugins/_plugin.py +++ b/yin_yang/plugins/_plugin.py @@ -118,7 +118,7 @@ def __str__(self): class PluginCommandline(Plugin): - def __init__(self, command: [str]): + def __init__(self, command: list[str]): """ :param command: list of arguments as passed to @subprocess.run, with the theme being inserted as {theme} """ @@ -174,7 +174,7 @@ def __init__(self, strategy_instance: Optional[Plugin]): logger.warning(f'Plugin {self.name} has no support for your desktop environment yet!') @property - def strategy(self) -> Plugin: + def strategy(self) -> Plugin | None: return self._strategy_instance @property diff --git a/yin_yang/plugins/kvantum.py b/yin_yang/plugins/kvantum.py index 82854a49..c240e94e 100755 --- a/yin_yang/plugins/kvantum.py +++ b/yin_yang/plugins/kvantum.py @@ -1,5 +1,7 @@ +from operator import sub import os from pathlib import Path +import subprocess from ._plugin import PluginCommandline @@ -10,6 +12,20 @@ def __init__(self): self.theme_light = 'KvFlatLight' self.theme_dark = 'KvFlat' + def set_theme(self, theme: str): + if not theme: + raise ValueError(f'Theme \"{theme}\" is invalid') + if not (self.available and self.enabled): + return + # insert theme in command and run it + command = self.insert_theme(theme) + subprocess.check_call(command) + subprocess.check_call( + ['dbus-send', '--session', '--type=signal', + '/KGlobalSettings', 'org.kde.KGlobalSettings.notifyChange', + 'int32:2', 'int32:0'] + ) + @classmethod def get_kvantum_theme_from_dir(cls, dir): result = set() From 70e85d803f2da6e46708557e6b48410cc3f67f5f Mon Sep 17 00:00:00 2001 From: Heddxh Date: Mon, 15 Jan 2024 20:13:50 +0800 Subject: [PATCH 4/5] Clean code --- tests/test_plugins.py | 6 +----- yin_yang/__main__.py | 1 - yin_yang/plugins/kvantum.py | 16 ---------------- 3 files changed, 1 insertion(+), 22 deletions(-) diff --git a/tests/test_plugins.py b/tests/test_plugins.py index dcec6fe0..f6fe3a5e 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -1,9 +1,5 @@ import unittest -# import os -# import sys -# sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) - from yin_yang.config import config from yin_yang.config import plugins from yin_yang.plugins._plugin import Plugin, ExternalPlugin @@ -45,7 +41,7 @@ def test_set_theme_invalid_state(self): 'set_theme() should not be successful if the plugin is disabled') # NOTE if you want to test that your theme changes, set this value to true - @unittest.skipUnless(True, 'test_theme_changes is disabled') + @unittest.skipUnless(False, 'test_theme_changes is disabled') def test_set_theme_works(self): for pl in filter(lambda p: not isinstance(p, ExternalPlugin) and p.enabled, plugins): with self.subTest('Changing the theme should be successful', plugin=pl.name): diff --git a/yin_yang/__main__.py b/yin_yang/__main__.py index fc49278e..c5d248f4 100755 --- a/yin_yang/__main__.py +++ b/yin_yang/__main__.py @@ -12,7 +12,6 @@ from PySide6.QtWidgets import QSystemTrayIcon, QMenu from systemd import journal -sys.path.append(str(Path(__file__).parent.parent)) from yin_yang.NotificationHandler import NotificationHandler from yin_yang import daemon_handler from yin_yang.meta import ConfigEvent diff --git a/yin_yang/plugins/kvantum.py b/yin_yang/plugins/kvantum.py index c240e94e..82854a49 100755 --- a/yin_yang/plugins/kvantum.py +++ b/yin_yang/plugins/kvantum.py @@ -1,7 +1,5 @@ -from operator import sub import os from pathlib import Path -import subprocess from ._plugin import PluginCommandline @@ -12,20 +10,6 @@ def __init__(self): self.theme_light = 'KvFlatLight' self.theme_dark = 'KvFlat' - def set_theme(self, theme: str): - if not theme: - raise ValueError(f'Theme \"{theme}\" is invalid') - if not (self.available and self.enabled): - return - # insert theme in command and run it - command = self.insert_theme(theme) - subprocess.check_call(command) - subprocess.check_call( - ['dbus-send', '--session', '--type=signal', - '/KGlobalSettings', 'org.kde.KGlobalSettings.notifyChange', - 'int32:2', 'int32:0'] - ) - @classmethod def get_kvantum_theme_from_dir(cls, dir): result = set() From ebb016bfa28ffbd007a1fb742e4e3f658b2f5aa4 Mon Sep 17 00:00:00 2001 From: Heddxh Date: Mon, 15 Jan 2024 23:38:54 +0800 Subject: [PATCH 5/5] Basic fallback method for konsole profile switch --- yin_yang/plugins/konsole.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/yin_yang/plugins/konsole.py b/yin_yang/plugins/konsole.py index a1e45a2d..ebe8a561 100644 --- a/yin_yang/plugins/konsole.py +++ b/yin_yang/plugins/konsole.py @@ -242,9 +242,16 @@ def set_profile(service: str, profile: str): try: sessions = subprocess.check_output(f'qdbus {service} | grep "Sessions/"', shell=True) except subprocess.CalledProcessError: - # happens when dolphins konsole is not opened - logger.debug(f'No Konsole sessions available in service {service}, skipping') - return + try: + sessions = subprocess.check_output( + f'qdbus org.kde.konsole | grep "Sessions/"', 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 + logger.debug(f'No Konsole sessions available in service {service}, skipping') + return sessions = sessions.decode('utf-8').removesuffix('\n').split('\n') # loop: process sessions