Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qgtk3portalinterface.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4//
5// W A R N I N G
6// -------------
7//
8// This file is not part of the Qt API. It exists purely as an
9// implementation detail. This header file may change from version to
10// version without notice, or even be removed.
11//
12// We mean it.
13//
14
16#include "qgtk3storage_p.h"
17
18#include <QtDBus/QDBusArgument>
19#include <QtDBus/QDBusConnection>
20#include <QtDBus/QDBusMessage>
21#include <QtDBus/QDBusPendingCall>
22#include <QtDBus/QDBusPendingCallWatcher>
23#include <QtDBus/QDBusPendingReply>
24#include <QtDBus/QDBusVariant>
25#include <QtDBus/QtDBus>
26
28
29Q_LOGGING_CATEGORY(lcQGtk3PortalInterface, "qt.qpa.gtk");
30
31using namespace Qt::StringLiterals;
32
33static constexpr QLatin1StringView appearanceInterface("org.freedesktop.appearance");
34static constexpr QLatin1StringView colorSchemeKey("color-scheme");
35
36const QDBusArgument &operator>>(const QDBusArgument &argument, QMap<QString, QVariantMap> &map)
37{
39 map.clear();
40
41 while (!argument.atEnd()) {
45 argument >> key >> value;
48 }
49
51 return argument;
52}
53
55 : m_storage(s) {
56 qRegisterMetaType<QDBusVariant>();
57 qDBusRegisterMetaType<QMap<QString, QVariantMap>>();
58
59 queryColorScheme();
60
61 if (!s) {
62 qCDebug(lcQGtk3PortalInterface) << "QGtk3PortalInterface instantiated without QGtk3Storage."
63 << "No reaction to runtime theme changes.";
64 }
65}
66
68{
69 return m_colorScheme;
70}
71
72void QGtk3PortalInterface::queryColorScheme() {
75 "org.freedesktop.portal.Desktop"_L1,
76 "/org/freedesktop/portal/desktop"_L1,
77 "org.freedesktop.portal.Settings"_L1, "ReadAll"_L1);
79
80 QDBusPendingCall pendingCall = connection.asyncCall(message);
85 QDBusPendingReply<QMap<QString, QVariantMap>> reply = *watcher;
86 if (reply.isValid()) {
87 QMap<QString, QVariantMap> settings = reply.value();
88 if (!settings.isEmpty()) {
89 settingChanged(appearanceInterface, colorSchemeKey,
90 QDBusVariant(settings.value(appearanceInterface).value(colorSchemeKey)));
91 }
92 } else {
93 qCDebug(lcQGtk3PortalInterface) << "Failed to query org.freedesktop.portal.Settings: "
94 << reply.error().message();
95 }
96 watcher->deleteLater();
97 });
98
100 "org.freedesktop.portal.Desktop"_L1, "/org/freedesktop/portal/desktop"_L1,
101 "org.freedesktop.portal.Settings"_L1, "SettingChanged"_L1, this,
102 SLOT(settingChanged(QString, QString, QDBusVariant)));
103}
104
105void QGtk3PortalInterface::settingChanged(const QString &group, const QString &key,
106 const QDBusVariant &value)
107{
109 const uint colorScheme = value.variant().toUInt();
110 // From org.freedesktop.portal.Settings.xml
111 // "1" - Prefer dark appearance
113 if (m_colorScheme != newColorScheme) {
114 m_colorScheme = newColorScheme;
115 if (m_storage)
116 m_storage->handleThemeChange();
117 }
118 }
119}
120
122
123#include "moc_qgtk3portalinterface_p.cpp"
\inmodule QtDBus
void beginMapEntry()
Opens a D-Bus map entry suitable for appending the key and value entries.
void endMapEntry()
Closes a D-Bus map entry opened with beginMapEntry().
void endMap()
Closes a D-Bus map opened with beginMap().
bool atEnd() const
Returns true if there are no more elements to be extracted from this QDBusArgument.
void beginMap(int keyMetaTypeId, int valueMetaTypeId)
\inmodule QtDBus
static QDBusConnection sessionBus()
Returns a QDBusConnection object opened with the session bus.
\inmodule QtDBus
static QDBusMessage createMethodCall(const QString &destination, const QString &path, const QString &interface, const QString &method)
Constructs a new DBus message representing a method call.
void finished(QDBusPendingCallWatcher *self=nullptr)
This signal is emitted when the pending call has finished and its reply is available.
\inmodule QtDBus
\inmodule QtDBus
Qt::ColorScheme colorScheme() const
QGtk3PortalInterface(QGtk3Storage *s)
void handleThemeChange()
Handles a theme change at runtime.
iterator insert(const Key &key, const T &value)
Definition qmap.h:688
void clear()
Definition qmap.h:289
NetworkError error() const
Returns the error that was found during the processing of this request.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QMap< QString, QString > map
[6]
Combined button and popup list for selecting options.
ColorScheme
Definition qnamespace.h:50
DBusConnection * connection
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
static QGtk3Storage * m_storage
const QDBusArgument & operator>>(const QDBusArgument &argument, QMap< QString, QVariantMap > &map)
static constexpr QLatin1StringView colorSchemeKey("color-scheme")
static constexpr QLatin1StringView appearanceInterface("org.freedesktop.appearance")
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
#define SLOT(a)
Definition qobjectdefs.h:52
GLuint64 key
GLboolean GLuint group
GLuint GLsizei const GLchar * message
GLdouble s
[6]
Definition qopenglext.h:235
unsigned int uint
Definition qtypes.h:34
QFutureWatcher< int > watcher
QNetworkReply * reply
QDBusArgument argument