KGuiAddons

kcolorschemewatcher_xdg.cpp
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
15KColorSchemeWatcherXDG::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 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
32
33 if (reply.isValid()) {
34 const uint result = reply.value().variant().toUInt();
35 m_preference = fdoToInternal(result);
36 }
37}
38
39KColorSchemeWatcher::ColorPreference KColorSchemeWatcherXDG::systemPreference() const
40{
41 return m_preference;
42}
43
44void 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(result);
49
50 if (m_preference != newValue) {
51 m_preference = fdoToInternal(result);
52 Q_EMIT systemPreferenceChanged();
53 }
54 }
55}
56
57KColorSchemeWatcher::ColorPreference KColorSchemeWatcherXDG::fdoToInternal(uint value) const
58{
59 if (value == 0) {
60 return KColorSchemeWatcher::NoPreference;
61 } else if (value == 1) {
63 } else if (value == 2) {
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"
@ PreferLight
The user prefers a dark color scheme.
@ PreferDark
No preference available.
QDBusMessage call(const QDBusMessage &message, QDBus::CallMode mode, int timeout) const const
bool connect(const QString &service, const QString &path, const QString &interface, const QString &name, QObject *receiver, const char *slot)
QDBusConnection sessionBus()
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
void setArguments(const QList< QVariant > &arguments)
bool isValid() const const
QVariant variant() const const
Q_EMITQ_EMIT
uint toUInt(bool *ok) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:02:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.