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();
61Qt::MotionPreference QGnomePortalInterface::motionPreference(Qt::MotionPreference fallback)
const
63 if (!m_motion.has_value())
65 return m_motion.value();
69void QGnomePortalInterface::querySettings()
71 QDBusConnection dbus = QDBusConnection::sessionBus();
72 if (!dbus.isConnected()) {
73 qCWarning(lcQpaThemeGnome) <<
"dbus connection failed. Last error: " << dbus.lastError();
77 constexpr auto method =
"ReadAll"_L1;
78 auto message = QDBusMessage::createMethodCall(s_service, s_path, s_interface, method);
80 message << QStringList{ QDBusSettings::XdgSettings::AppearanceNamespace,
81 QDBusSettings::GnomeSettings::AllyNamespace,
82 QDBusSettings::GnomeSettings::DesktopNamespace };
84 QDBusPendingCall pendingCall = dbus.asyncCall(message);
85 QDBusPendingCallWatcher *watcher =
new QDBusPendingCallWatcher(pendingCall,
this);
87 auto onWatcherFinished = [&](QDBusPendingCallWatcher *watcher) {
88 const QDBusMessage reply = watcher->reply();
89 if (QDBusMessage::ErrorMessage == reply.type()) {
90 qCWarning(lcQpaThemeGnome)
91 <<
"dbus reply error: [" << reply.errorName() <<
"]" << reply.errorMessage();
92 watcher->deleteLater();
96 const auto convertXdgColorScheme = [](
const QVariant &value) {
97 using namespace QDBusSettings::XdgSettings;
98 return QVariant::fromValue(convertColorScheme(value));
101 constexpr auto XdgContrastKey = QDBusSettings::XdgSettings::ContrastKey;
102 const auto convertXdgContrast = [](
const QVariant &value) {
103 using namespace QDBusSettings::XdgSettings;
104 return QVariant::fromValue(convertContrastPreference(value));
107 constexpr auto XdgMotionKey = QDBusSettings::XdgSettings::MotionKey;
108 const auto convertXdgMotion = [](
const QVariant &value) {
109 return QVariant::fromValue(QDBusSettings::XdgSettings::convertMotionPreference(value));
112 constexpr auto GnomeContrastKey = QDBusSettings::GnomeSettings::ContrastKey;
113 const auto convertGnomeContrast = [](
const QVariant &value) {
114 using namespace QDBusSettings::GnomeSettings;
115 return QVariant::fromValue(convertContrastPreference(value));
118 constexpr auto GnomeMotionKey = QDBusSettings::GnomeSettings::MotionKey;
119 const auto convertGnomeMotion = [](
const QVariant &value) {
120 using namespace QDBusSettings::GnomeSettings;
121 return QVariant::fromValue(convertMotionPreference(value));
124 const QVariantList &args = reply.arguments();
125 for (
const QVariant &arg_ : args) {
126 const QMap<QString, QVariantMap> arg = qdbus_cast<QMap<QString, QVariantMap>>(arg_);
127 for (
auto aIt = arg.begin(); aIt != arg.end(); ++aIt) {
128 const QString &namespace_ = aIt.key();
129 const QVariantMap &settings = aIt.value();
130 for (
auto sIt = settings.begin(); sIt != settings.end(); ++sIt) {
131 const QString &key = sIt.key();
132 const QVariant &value = sIt.value();
133 if ((key == QDBusSettings::XdgSettings::ColorSchemeKey)
134 && (namespace_ == QDBusSettings::XdgSettings::AppearanceNamespace))
135 dbusSettingChanged(QDBusListener::Provider::Gnome,
136 QDBusListener::Setting::ColorScheme,
137 convertXdgColorScheme(value));
138 else if ((key == XdgContrastKey)
139 && (namespace_ == QDBusSettings::XdgSettings::AppearanceNamespace))
140 dbusSettingChanged(QDBusListener::Provider::Gnome,
141 QDBusListener::Setting::Contrast,
142 convertXdgContrast(value));
143 else if ((key == GnomeContrastKey)
144 && (namespace_ == QDBusSettings::GnomeSettings::AllyNamespace))
145 dbusSettingChanged(QDBusListener::Provider::Gnome,
146 QDBusListener::Setting::Contrast,
147 convertGnomeContrast(value));
148 else if ((key == XdgMotionKey)
149 && namespace_ == QDBusSettings::XdgSettings::AppearanceNamespace)
150 dbusSettingChanged(QDBusListener::Provider::Gnome,
151 QDBusListener::Setting::Motion,
152 convertXdgMotion(value));
153 else if ((key == GnomeMotionKey)
154 && namespace_ == QDBusSettings::GnomeSettings::DesktopNamespace)
155 dbusSettingChanged(QDBusListener::Provider::Gnome,
156 QDBusListener::Setting::Motion,
157 convertGnomeMotion(value));
161 watcher->deleteLater();
163 connect(watcher, &QDBusPendingCallWatcher::finished,
this, onWatcherFinished);
166void QGnomePortalInterface::updateColorScheme(Qt::ColorScheme colorScheme)
168 if (m_colorScheme.has_value() && (m_colorScheme.value() == colorScheme))
170 m_colorScheme = colorScheme;
171 emit colorSchemeChanged(colorScheme);
174void QGnomePortalInterface::updateContrast(Qt::ContrastPreference contrast)
176 if (m_contrast.has_value() && (m_contrast.value() == contrast))
178 m_contrast = contrast;
179 emit contrastChanged(contrast);
182void QGnomePortalInterface::updateMotion(Qt::MotionPreference motion)
184 if (m_motion.has_value() && (m_motion.value() == motion))
187 emit motionChanged(motion);
190void QGnomePortalInterface::dbusSettingChanged(QDBusListener::Provider provider,
191 QDBusListener::Setting setting,
192 const QVariant &value)
194 if (provider != QDBusListener::Provider::Gnome && provider != QDBusListener::Provider::Gtk)
198 case QDBusListener::Setting::ColorScheme:
199 updateColorScheme(value.value<Qt::ColorScheme>());
201 case QDBusListener::Setting::Contrast:
202 updateContrast(value.value<Qt::ContrastPreference>());
204 case QDBusListener::Setting::Motion:
205 updateMotion(value.value<Qt::MotionPreference>());
207 case QDBusListener::Setting::Theme:
208 emit themeNameChanged(value.toString());
217#include "moc_qgnomeportalinterface_p.cpp"
Combined button and popup list for selecting options.