1/*
2 * SPDX-FileCopyrightText: 2021 Nicolas Fella <nicolas.fella@gmx.de>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "kcolorschemewatcher_xdg.h"
8
9#include <QDBusConnection>
10#include <QDBusMessage>
11#include <QDBusVariant>
12#include <QDebug>
13
14KColorSchemeWatcherXDG::KColorSchemeWatcherXDG()
15 : KColorSchemeWatcherBackend()
16{
17 QDBusConnection::sessionBus().connect(QStringLiteral("org.freedesktop.portal.Desktop"),
18 QStringLiteral("/org/freedesktop/portal/desktop"),
19 QStringLiteral("org.freedesktop.portal.Settings"),
20 QStringLiteral("SettingChanged"),
21 receiver: this,
22 SLOT(slotSettingChanged(QString, QString, QDBusVariant)));
23
24 QDBusMessage m = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.portal.Desktop"),
25 QStringLiteral("/org/freedesktop/portal/desktop"),
26 QStringLiteral("org.freedesktop.portal.Settings"),
27 QStringLiteral("Read"));
28 m.setArguments({QStringLiteral("org.freedesktop.appearance"), QStringLiteral("color-scheme")});
29
30 auto reply = QDBusConnection::sessionBus().call(message: m);
31
32 const uint result = reply.arguments().first().value<QDBusVariant>().variant().value<QDBusVariant>().variant().toUInt();
33 m_preference = fdoToInternal(value: result);
34}
35
36KColorSchemeWatcher::ColorPreference KColorSchemeWatcherXDG::systemPreference() const
37{
38 return m_preference;
39}
40
41void KColorSchemeWatcherXDG::slotSettingChanged(QString nameSpace, QString key, QDBusVariant value)
42{
43 if (nameSpace == QLatin1String("org.freedesktop.appearance") && key == QLatin1String("color-scheme")) {
44 const uint result = value.variant().toUInt();
45 auto newValue = fdoToInternal(value: result);
46
47 if (m_preference != newValue) {
48 m_preference = fdoToInternal(value: result);
49 Q_EMIT systemPreferenceChanged();
50 }
51 }
52}
53
54KColorSchemeWatcher::ColorPreference KColorSchemeWatcherXDG::fdoToInternal(uint value) const
55{
56 if (value == 0) {
57 return KColorSchemeWatcher::NoPreference;
58 } else if (value == 1) {
59 return KColorSchemeWatcher::PreferDark;
60 } else if (value == 2) {
61 return KColorSchemeWatcher::PreferLight;
62 } else {
63 qWarning() << "Unhandled org.freedesktop.appearance color-scheme value" << value;
64 return KColorSchemeWatcher::NoPreference;
65 }
66}
67
68#include "moc_kcolorschemewatcher_xdg.cpp"
69

source code of kguiaddons/src/colors/kcolorschemewatcher_xdg.cpp