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(linux): Improve detection of Gnome environment #11292

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 17 additions & 8 deletions linux/keyman-config/keyman_config/gnome_keyboards_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from keyman_config.gsettings import GSettings


# pylint: disable=global-statement


class GnomeKeyboardsUtil():
def __init__(self):
self.input_sources = GSettings('org.gnome.desktop.input-sources')
Expand All @@ -21,23 +24,29 @@ def write_input_sources(self, sources):
self.input_sources.set('sources', sources, 'a(ss)')


__is_gnome_shell = None
# pylint: disable=invalid-name
__is_gnome_desktop = None


def is_gnome_shell():
global __is_gnome_shell
def is_gnome_desktop():
'''
Returns true if system is running Gnome shell or another Gnome desktop
that stores keyboard data under `org.gnome.desktop.input-sources`.
'''
global __is_gnome_desktop

if __is_gnome_shell is None:
if __is_gnome_desktop is None:
code = os.system('pidof gnome-shell >/dev/null 2>&1')
__is_gnome_shell = (code == 0)
return __is_gnome_shell
gnome_env = ('XDG_CURRENT_DESKTOP' in os.environ) and ('gnome' in os.environ['XDG_CURRENT_DESKTOP'].lower())
__is_gnome_desktop = (code == 0) or gnome_env
return __is_gnome_desktop


def _reset_gnome_shell():
# used in unit tests
global __is_gnome_shell
global __is_gnome_desktop

__is_gnome_shell = None
__is_gnome_desktop = None


def get_ibus_keyboard_id(keyboard, packageDir, language=None, ignore_language=False):
Expand Down
4 changes: 2 additions & 2 deletions linux/keyman-config/keyman_config/ibus_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from gi.repository import IBus
from pkg_resources import parse_version

from keyman_config.gnome_keyboards_util import is_gnome_shell
from keyman_config.gnome_keyboards_util import is_gnome_desktop
from keyman_config.gsettings import GSettings


Expand Down Expand Up @@ -164,7 +164,7 @@ def _start_ibus_daemon(realuser):
# If IBus is too old we have to start ibus-daemon directly and pass
# what we think are the correct parameters
args = ['ibus-daemon', '-d', '-r', '--xim']
if is_gnome_shell():
if is_gnome_desktop():
# on Ubuntu 21.10 with Gnome the keyboards don't show in dropdown
# list if we don't disable the panel
args.extend(['--panel', 'disable'])
Expand Down
4 changes: 2 additions & 2 deletions linux/keyman-config/keyman_config/install_kmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
get_keyman_font_dir)
from keyman_config.gnome_keyboards_util import (GnomeKeyboardsUtil,
get_ibus_keyboard_id,
is_gnome_shell)
is_gnome_desktop)
from keyman_config.ibus_util import get_ibus_bus, install_to_ibus, restart_ibus
from keyman_config.kmpmetadata import KMFileTypes, get_metadata
from keyman_config.kvk2ldml import convert_kvk_to_ldml, output_ldml
Expand Down Expand Up @@ -261,7 +261,7 @@ def _install_keyboards(self, keyboards, packageDir, requested_language=None):
return self._install_keyboards_to_fcitx()

restart_ibus()
if is_gnome_shell():
if is_gnome_desktop():
return self._install_keyboards_to_gnome(keyboards, packageDir, language)
else:
return self._install_keyboards_to_ibus(keyboards, packageDir, language)
Expand Down
4 changes: 2 additions & 2 deletions linux/keyman-config/keyman_config/uninstall_kmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
get_keyman_doc_dir, get_keyman_font_dir)
from keyman_config.gnome_keyboards_util import (GnomeKeyboardsUtil,
get_ibus_keyboard_id,
is_gnome_shell)
is_gnome_desktop)
from keyman_config.gsettings import GSettings
from keyman_config.ibus_util import IbusUtil, get_ibus_bus, restart_ibus
from keyman_config.kmpmetadata import get_metadata
Expand Down Expand Up @@ -56,7 +56,7 @@ def _uninstall_kmp_common(location, packageID, removeLanguages):
if keyboards:
if is_fcitx_running():
_uninstall_keyboards_from_fcitx5()
elif is_gnome_shell():
elif is_gnome_desktop():
_uninstall_keyboards_from_gnome(keyboards, kbdir)
else:
_uninstall_keyboards_from_ibus(keyboards, kbdir)
Expand Down
22 changes: 17 additions & 5 deletions linux/keyman-config/tests/test_gnome_keyboards_util.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
#!/usr/bin/python3
import os
import unittest
from unittest import mock
from unittest.mock import patch

from keyman_config.gnome_keyboards_util import is_gnome_shell, _reset_gnome_shell
from keyman_config.gnome_keyboards_util import is_gnome_desktop, _reset_gnome_shell


class GnomeKeyboardsUtilTests(unittest.TestCase):
def setUp(self):
_reset_gnome_shell()

@patch('os.system')
def test_IsGnomeShell_RunningGnomeShell(self, mockSystem):
@mock.patch.dict(os.environ, {'XDG_CURRENT_DESKTOP': 'ubuntu:GNOME'})
def test_IsGnomeDesktop_RunningGnomeShell(self, mockSystem):
# Setup
mockSystem.return_value = 0
# Execute/Verify
self.assertEqual(is_gnome_shell(), True)
self.assertEqual(is_gnome_desktop(), True)

@patch('os.system')
def test_IsGnomeShell_NotRunningGnomeShell(self, mockSystem):
@mock.patch.dict(os.environ, {'XDG_CURRENT_DESKTOP': 'X-Cinnamon'})
def test_IsGnomeDesktop_NotRunningGnomeShell(self, mockSystem):
# Setup
mockSystem.return_value = 1
# Execute/Verify
self.assertEqual(is_gnome_shell(), False)
self.assertEqual(is_gnome_desktop(), False)

@patch('os.system')
@mock.patch.dict(os.environ, {'XDG_CURRENT_DESKTOP': 'budgie:GNOME'})
def test_IsGnomeDesktop_RunningBudgie(self, mockSystem):
# Setup
mockSystem.return_value = 1
# Execute/Verify
self.assertEqual(is_gnome_desktop(), True)


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion linux/keyman-config/tests/test_install_kmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def setUp(self):
patcher7 = patch('keyman_config.install_kmp.extractico')
self.mockExtractIco = patcher7.start()
self.addCleanup(patcher7.stop)
patcher8 = patch('keyman_config.install_kmp.is_gnome_shell')
patcher8 = patch('keyman_config.install_kmp.is_gnome_desktop')
self.mockIsGnomeShell = patcher8.start()
self.addCleanup(patcher8.stop)
self.mockIsGnomeShell.return_value = False
Expand Down
2 changes: 1 addition & 1 deletion linux/keyman-config/tests/test_uninstall_kmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def setUp(self):
patcher3 = patch('keyman_config.uninstall_kmp.get_keyman_font_dir')
self.mockGetKeymanFontDir = patcher3.start()
self.addCleanup(patcher3.stop)
patcher4 = patch('keyman_config.uninstall_kmp.is_gnome_shell')
patcher4 = patch('keyman_config.uninstall_kmp.is_gnome_desktop')
self.mockIsGnomeShell = patcher4.start()
self.addCleanup(patcher4.stop)
self.mockIsGnomeShell.return_value = False
Expand Down
Loading