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 <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 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(m);
31
32 const uint result = reply.arguments().first().value<QDBusVariant>().variant().value<QDBusVariant>().variant().toUInt();
33 m_preference = fdoToInternal(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(result);
46
47 if (m_preference != newValue) {
48 m_preference = fdoToInternal(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) {
60 } else if (value == 2) {
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"
@ 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()
QList< QVariant > arguments() const const
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
void setArguments(const QList< QVariant > &arguments)
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 Tue Mar 26 2024 11:14:40 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.