Welcome to mirror list, hosted at ThFree Co, Russian Federation.

system_style.py « dbus « common « gajim - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8a8995f0ce1349211628605ebf30c65743a8c049 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# This file is part of Gajim.
#
# Gajim is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# Gajim is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.

from __future__ import annotations

from typing import Any
from typing import Callable

import logging
import sys

from gi.repository import Gio
from gi.repository import GLib

from gajim.common import app
from gajim.common.events import StyleChanged

log = logging.getLogger('gajim.c.dbus.system_style')


class SystemStyleListener:
    def __init__(self, callback: Callable[..., None]) -> None:
        self._prefer_dark: bool | None = None
        self._callback = callback

        if sys.platform in ('win32', 'darwin'):
            return

        try:
            self.dbus_proxy = Gio.DBusProxy.new_for_bus_sync(
                Gio.BusType.SESSION,
                Gio.DBusProxyFlags.NONE,
                None,
                'org.freedesktop.portal.Desktop',
                '/org/freedesktop/portal/desktop',
                'org.freedesktop.portal.Settings',
                None
            )
        except GLib.Error as error:
            log.info('Settings portal not found: %s', error)
            return

        self.dbus_proxy.connect('g-signal', self._signal_setting_changed)
        self.read_color_scheme()

    def read_color_scheme(self) -> None:
        try:
            result = self.dbus_proxy.call_sync(
                'Read',
                GLib.Variant('(ss)', ('org.freedesktop.appearance',
                                      'color-scheme')),
                Gio.DBusCallFlags.NO_AUTO_START,
                -1,
                None)
            self._prefer_dark = result[0] == 1
        except GLib.Error as error:
            log.error('Couldn’t read the color-scheme setting: %s',
                      error.message)
            return

    def _signal_setting_changed(self,
                                _proxy: Gio.DBusProxy,
                                _sender_name: str | None,
                                signal_name: str,
                                parameters: GLib.Variant,
                                *_user_data: Any
                                ) -> None:
        if signal_name != 'SettingChanged':
            return

        namespace, name, value = parameters
        if (namespace == 'org.freedesktop.appearance' and
                name == 'color-scheme'):
            self._prefer_dark = value == 1
            self._callback()
            app.ged.raise_event(StyleChanged())

    @property
    def prefer_dark(self) -> bool | None:
        return self._prefer_dark