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 <QDBusReply> |
12 | #include <QDBusVariant> |
13 | #include <QDebug> |
14 | |
15 | KColorSchemeWatcherXDG::KColorSchemeWatcherXDG() |
16 | : KColorSchemeWatcherBackend() |
17 | { |
18 | QDBusConnection::sessionBus().connect(QStringLiteral("org.freedesktop.portal.Desktop" ), |
19 | QStringLiteral("/org/freedesktop/portal/desktop" ), |
20 | QStringLiteral("org.freedesktop.portal.Settings" ), |
21 | QStringLiteral("SettingChanged" ), |
22 | receiver: this, |
23 | SLOT(slotSettingChanged(QString, QString, QDBusVariant))); |
24 | |
25 | QDBusMessage m = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.portal.Desktop" ), |
26 | QStringLiteral("/org/freedesktop/portal/desktop" ), |
27 | QStringLiteral("org.freedesktop.portal.Settings" ), |
28 | QStringLiteral("Read" )); |
29 | m.setArguments({QStringLiteral("org.freedesktop.appearance" ), QStringLiteral("color-scheme" )}); |
30 | |
31 | QDBusReply<QDBusVariant> reply = QDBusConnection::sessionBus().call(message: m); |
32 | |
33 | if (reply.isValid()) { |
34 | const uint result = reply.value().variant().value<QDBusVariant>().variant().toUInt(); |
35 | m_preference = fdoToInternal(value: result); |
36 | } |
37 | } |
38 | |
39 | KColorSchemeWatcher::ColorPreference KColorSchemeWatcherXDG::systemPreference() const |
40 | { |
41 | return m_preference; |
42 | } |
43 | |
44 | void KColorSchemeWatcherXDG::slotSettingChanged(QString nameSpace, QString key, QDBusVariant value) |
45 | { |
46 | if (nameSpace == QLatin1String("org.freedesktop.appearance" ) && key == QLatin1String("color-scheme" )) { |
47 | const uint result = value.variant().toUInt(); |
48 | auto newValue = fdoToInternal(value: result); |
49 | |
50 | if (m_preference != newValue) { |
51 | m_preference = fdoToInternal(value: result); |
52 | Q_EMIT systemPreferenceChanged(); |
53 | } |
54 | } |
55 | } |
56 | |
57 | KColorSchemeWatcher::ColorPreference KColorSchemeWatcherXDG::fdoToInternal(uint value) const |
58 | { |
59 | if (value == 0) { |
60 | return KColorSchemeWatcher::NoPreference; |
61 | } else if (value == 1) { |
62 | return KColorSchemeWatcher::PreferDark; |
63 | } else if (value == 2) { |
64 | return KColorSchemeWatcher::PreferLight; |
65 | } else { |
66 | qWarning() << "Unhandled org.freedesktop.appearance color-scheme value" << value; |
67 | return KColorSchemeWatcher::NoPreference; |
68 | } |
69 | } |
70 | |
71 | #include "moc_kcolorschemewatcher_xdg.cpp" |
72 | |