Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix konsole dbus call and modify xsettingsd on kde as a fallback method #260

Merged
merged 5 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion tests/test_plugins.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions yin_yang/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions yin_yang/plugins/_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
"""
Expand Down Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion yin_yang/plugins/gtk.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from os import scandir, path
from pathlib import Path
import subprocess

from PySide6.QtDBus import QDBusConnection, QDBusMessage

Expand Down Expand Up @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions yin_yang/plugins/konsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions yin_yang/plugins/kvantum.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from operator import sub
import os
from pathlib import Path
import subprocess

from ._plugin import PluginCommandline

Expand All @@ -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()
Expand Down