spiegel-keyman/linux/keyman-config/keyman_config/custom_keyboards.py
Eberhard Beilharz 23a558d88e
feat(linux): Deal with temporary dbus session
When we launch dbus if it is not running, we're probably (un-)installing
keyboards for a different user. This is an unusual environment where not
everything is available, e.g. dconf-service won't be running and can't
be started because DISPLAY variable is not set. This means we can't
write gsettings values through the API. However, we can read them, and we
can write a keyfile and update the database. This is what this change
implements for the cases where we started dbus.
2024-03-22 12:54:50 +01:00

52 lines
1.7 KiB
Python

#!/usr/bin/python3
from keyman_config.gsettings import GSettings
GSETTINGS_ENGINE_BASE = 'com.keyman.engine'
GSETTINGS_ADDITIONAL_KEYBOARDS_KEY = 'additional-keyboards'
class CustomKeyboards():
def __init__(self):
self.gsettings = GSettings(GSETTINGS_ENGINE_BASE)
def add(self, keyboard):
if not keyboard:
return
custom_keyboards = self.gsettings.get(GSETTINGS_ADDITIONAL_KEYBOARDS_KEY)
if isinstance(custom_keyboards, type([])):
duplicate = False
i = 0
while i < len(custom_keyboards):
kb = custom_keyboards[i]
if kb == keyboard:
duplicate = True
elif not len(kb.split(':')) > 1:
custom_keyboards.remove(kb)
i -= 1
i += 1
if duplicate:
return
custom_keyboards.append(keyboard)
else:
custom_keyboards = [keyboard]
self.gsettings.set(GSETTINGS_ADDITIONAL_KEYBOARDS_KEY, custom_keyboards, 'as')
def remove(self, keyboard_path):
if not keyboard_path:
return
custom_keyboards = self.gsettings.get(GSETTINGS_ADDITIONAL_KEYBOARDS_KEY)
if isinstance(custom_keyboards, type([])):
i = 0
while i < len(custom_keyboards):
kb = custom_keyboards[i]
if kb.endswith(keyboard_path) or not len(kb.split(':')) > 1:
custom_keyboards.remove(kb)
else:
i += 1
else:
custom_keyboards = []
self.gsettings.set(GSETTINGS_ADDITIONAL_KEYBOARDS_KEY, custom_keyboards, 'as')