7#include <private/qdbusplatformmenu_p.h>
8#include <QtDBus/QDBusMessage>
9#include <QtDBus/QDBusPendingCall>
10#include <QtDBus/QDBusReply>
11#include <QtDBus/QDBusVariant>
16Q_STATIC_LOGGING_CATEGORY(lcQpaThemeGnome,
"qt.qpa.theme.gnome")
19using namespace Qt::StringLiterals;
21QGnomePortalInterface::QGnomePortalInterface(QObject *parent)
22 : QObject(parent), m_dbus{
new QDBusListener }
24 QObject::connect(m_dbus.get(), &QDBusListener::settingChanged,
this,
25 &QGnomePortalInterface::dbusSettingChanged);
31
32
33
34
35
36
37
38Qt::ColorScheme QGnomePortalInterface::colorScheme(Qt::ColorScheme fallback)
const
40 if (!m_colorScheme.has_value())
42 return m_colorScheme.value();
46
47
48
49
50
51
52
54QGnomePortalInterface::contrastPreference(Qt::ContrastPreference fallback)
const
56 if (!m_contrast.has_value())
58 return m_contrast.value();
61void QGnomePortalInterface::querySettings()
63 QDBusConnection dbus = QDBusConnection::sessionBus();
64 if (!dbus.isConnected()) {
65 qCWarning(lcQpaThemeGnome) <<
"dbus connection failed. Last error: " << dbus.lastError();
69 constexpr auto method =
"ReadAll"_L1;
70 auto message = QDBusMessage::createMethodCall(s_service, s_path, s_interface, method);
72 message << QStringList{ QDBusSettings::XdgSettings::AppearanceNamespace,
73 QDBusSettings::GnomeSettings::AllyNamespace };
75 QDBusPendingCall pendingCall = dbus.asyncCall(message);
76 QDBusPendingCallWatcher *watcher =
new QDBusPendingCallWatcher(pendingCall,
this);
78 auto onWatcherFinished = [&](QDBusPendingCallWatcher *watcher) {
79 const QDBusMessage reply = watcher->reply();
80 if (QDBusMessage::ErrorMessage == reply.type()) {
81 qCWarning(lcQpaThemeGnome)
82 <<
"dbus reply error: [" << reply.errorName() <<
"]" << reply.errorMessage();
83 watcher->deleteLater();
87 const auto convertXdgColorScheme = [](
const QVariant &value) {
88 using namespace QDBusSettings::XdgSettings;
89 return QVariant::fromValue(convertColorScheme(value));
92 constexpr auto XdgContrastKey = QDBusSettings::XdgSettings::ContrastKey;
93 const auto convertXdgContrast = [](
const QVariant &value) {
94 using namespace QDBusSettings::XdgSettings;
95 return QVariant::fromValue(convertContrastPreference(value));
98 constexpr auto GnomeContrastKey = QDBusSettings::GnomeSettings::ContrastKey;
99 const auto convrtGnomeContrast = [](
const QVariant &value) {
100 using namespace QDBusSettings::GnomeSettings;
101 return QVariant::fromValue(convertContrastPreference(value));
104 const QVariantList &args = reply.arguments();
105 for (
const QVariant &arg_ : args) {
106 const QMap<QString, QVariantMap> arg = qdbus_cast<QMap<QString, QVariantMap>>(arg_);
107 for (
auto aIt = arg.begin(); aIt != arg.end(); ++aIt) {
108 const QString &namespace_ = aIt.key();
109 const QVariantMap &settings = aIt.value();
110 for (
auto sIt = settings.begin(); sIt != settings.end(); ++sIt) {
111 const QString &key = sIt.key();
112 const QVariant &value = sIt.value();
113 if ((key == QDBusSettings::XdgSettings::ColorSchemeKey)
114 && (namespace_ == QDBusSettings::XdgSettings::AppearanceNamespace))
115 dbusSettingChanged(QDBusListener::Provider::Gnome,
116 QDBusListener::Setting::ColorScheme,
117 convertXdgColorScheme(value));
118 else if ((key == XdgContrastKey)
119 && (namespace_ == QDBusSettings::XdgSettings::AppearanceNamespace))
120 dbusSettingChanged(QDBusListener::Provider::Gnome,
121 QDBusListener::Setting::Contrast,
122 convertXdgContrast(value));
123 else if ((key == GnomeContrastKey)
124 && (namespace_ == QDBusSettings::GnomeSettings::AllyNamespace))
125 dbusSettingChanged(QDBusListener::Provider::Gnome,
126 QDBusListener::Setting::Contrast,
127 convrtGnomeContrast(value));
131 watcher->deleteLater();
133 connect(watcher, &QDBusPendingCallWatcher::finished,
this, onWatcherFinished);
136void QGnomePortalInterface::updateColorScheme(Qt::ColorScheme colorScheme)
138 if (m_colorScheme.has_value() && (m_colorScheme.value() == colorScheme))
140 m_colorScheme = colorScheme;
141 emit colorSchemeChanged(colorScheme);
144void QGnomePortalInterface::updateContrast(Qt::ContrastPreference contrast)
146 if (m_contrast.has_value() && (m_contrast.value() == contrast))
148 m_contrast = contrast;
149 emit contrastChanged(contrast);
152void QGnomePortalInterface::dbusSettingChanged(QDBusListener::Provider provider,
153 QDBusListener::Setting setting,
154 const QVariant &value)
156 if (provider != QDBusListener::Provider::Gnome && provider != QDBusListener::Provider::Gtk)
160 case QDBusListener::Setting::ColorScheme:
161 updateColorScheme(value.value<Qt::ColorScheme>());
163 case QDBusListener::Setting::Contrast:
164 updateContrast(value.value<Qt::ContrastPreference>());
166 case QDBusListener::Setting::Theme:
167 emit themeNameChanged(value.toString());
176#include "moc_qgnomeportalinterface_p.cpp"