File indexing completed on 2024-12-08 12:18:37

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Nicolas Fella <nicolas.fella@gmx.de>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "kcolorschemewatcher_xdg.h"
0008 
0009 #include <QDBusConnection>
0010 #include <QDBusMessage>
0011 #include <QDBusVariant>
0012 #include <QDebug>
0013 
0014 KColorSchemeWatcherXDG::KColorSchemeWatcherXDG()
0015     : KColorSchemeWatcherBackend()
0016 {
0017     QDBusConnection::sessionBus().connect(QStringLiteral("org.freedesktop.portal.Desktop"),
0018                                           QStringLiteral("/org/freedesktop/portal/desktop"),
0019                                           QStringLiteral("org.freedesktop.portal.Settings"),
0020                                           QStringLiteral("SettingChanged"),
0021                                           this,
0022                                           SLOT(slotSettingChanged(QString, QString, QDBusVariant)));
0023 
0024     QDBusMessage m = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.portal.Desktop"),
0025                                                     QStringLiteral("/org/freedesktop/portal/desktop"),
0026                                                     QStringLiteral("org.freedesktop.portal.Settings"),
0027                                                     QStringLiteral("Read"));
0028     m.setArguments({QStringLiteral("org.freedesktop.appearance"), QStringLiteral("color-scheme")});
0029 
0030     auto reply = QDBusConnection::sessionBus().call(m);
0031 
0032     const uint result = reply.arguments().first().value<QDBusVariant>().variant().value<QDBusVariant>().variant().toUInt();
0033     m_preference = fdoToInternal(result);
0034 }
0035 
0036 KColorSchemeWatcher::ColorPreference KColorSchemeWatcherXDG::systemPreference() const
0037 {
0038     return m_preference;
0039 }
0040 
0041 void KColorSchemeWatcherXDG::slotSettingChanged(QString nameSpace, QString key, QDBusVariant value)
0042 {
0043     if (nameSpace == QLatin1String("org.freedesktop.appearance") && key == QLatin1String("color-scheme")) {
0044         const uint result = value.variant().toUInt();
0045         auto newValue = fdoToInternal(result);
0046 
0047         if (m_preference != newValue) {
0048             m_preference = fdoToInternal(result);
0049             Q_EMIT systemPreferenceChanged();
0050         }
0051     }
0052 }
0053 
0054 KColorSchemeWatcher::ColorPreference KColorSchemeWatcherXDG::fdoToInternal(uint value) const
0055 {
0056     if (value == 0) {
0057         return KColorSchemeWatcher::NoPreference;
0058     } else if (value == 1) {
0059         return KColorSchemeWatcher::PreferDark;
0060     } else if (value == 2) {
0061         return KColorSchemeWatcher::PreferLight;
0062     } else {
0063         qWarning() << "Unhandled org.freedesktop.appearance color-scheme value" << value;
0064         return KColorSchemeWatcher::NoPreference;
0065     }
0066 }
0067 
0068 #include "moc_kcolorschemewatcher_xdg.cpp"