1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the plugins of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qxdgdesktopportaltheme.h"
41#include "qxdgdesktopportalfiledialog_p.h"
42
43#include <private/qguiapplication_p.h>
44#include <qpa/qplatformtheme_p.h>
45#include <qpa/qplatformthemefactory_p.h>
46#include <qpa/qplatformintegration.h>
47
48#include <QDBusConnection>
49#include <QDBusMessage>
50#include <QDBusPendingCall>
51#include <QDBusPendingCallWatcher>
52#include <QDBusPendingReply>
53
54QT_BEGIN_NAMESPACE
55
56class QXdgDesktopPortalThemePrivate : public QPlatformThemePrivate
57{
58public:
59 QXdgDesktopPortalThemePrivate()
60 : QPlatformThemePrivate()
61 { }
62
63 ~QXdgDesktopPortalThemePrivate()
64 {
65 delete baseTheme;
66 }
67
68 QPlatformTheme *baseTheme = nullptr;
69 uint fileChooserPortalVersion = 0;
70};
71
72QXdgDesktopPortalTheme::QXdgDesktopPortalTheme()
73 : d_ptr(new QXdgDesktopPortalThemePrivate)
74{
75 Q_D(QXdgDesktopPortalTheme);
76
77 QStringList themeNames;
78 themeNames += QGuiApplicationPrivate::platform_integration->themeNames();
79 // 1) Look for a theme plugin.
80 for (const QString &themeName : qAsConst(t&: themeNames)) {
81 d->baseTheme = QPlatformThemeFactory::create(key: themeName, platformPluginPath: nullptr);
82 if (d->baseTheme)
83 break;
84 }
85
86 // 2) If no theme plugin was found ask the platform integration to
87 // create a theme
88 if (!d->baseTheme) {
89 for (const QString &themeName : qAsConst(t&: themeNames)) {
90 d->baseTheme = QGuiApplicationPrivate::platform_integration->createPlatformTheme(name: themeName);
91 if (d->baseTheme)
92 break;
93 }
94 // No error message; not having a theme plugin is allowed.
95 }
96
97 // 3) Fall back on the built-in "null" platform theme.
98 if (!d->baseTheme)
99 d->baseTheme = new QPlatformTheme;
100
101 // Get information about portal version
102 QDBusMessage message = QDBusMessage::createMethodCall(destination: QLatin1String("org.freedesktop.portal.Desktop"),
103 path: QLatin1String("/org/freedesktop/portal/desktop"),
104 interface: QLatin1String("org.freedesktop.DBus.Properties"),
105 method: QLatin1String("Get"));
106 message << QLatin1String("org.freedesktop.portal.FileChooser") << QLatin1String("version");
107 QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(message);
108 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall);
109 QObject::connect(sender: watcher, signal: &QDBusPendingCallWatcher::finished, slot: [d] (QDBusPendingCallWatcher *watcher) {
110 QDBusPendingReply<QVariant> reply = *watcher;
111 if (reply.isValid()) {
112 d->fileChooserPortalVersion = reply.value().toUInt();
113 }
114 });
115}
116
117QPlatformMenuItem* QXdgDesktopPortalTheme::createPlatformMenuItem() const
118{
119 Q_D(const QXdgDesktopPortalTheme);
120 return d->baseTheme->createPlatformMenuItem();
121}
122
123QPlatformMenu* QXdgDesktopPortalTheme::createPlatformMenu() const
124{
125 Q_D(const QXdgDesktopPortalTheme);
126 return d->baseTheme->createPlatformMenu();
127}
128
129QPlatformMenuBar* QXdgDesktopPortalTheme::createPlatformMenuBar() const
130{
131 Q_D(const QXdgDesktopPortalTheme);
132 return d->baseTheme->createPlatformMenuBar();
133}
134
135void QXdgDesktopPortalTheme::showPlatformMenuBar()
136{
137 Q_D(const QXdgDesktopPortalTheme);
138 return d->baseTheme->showPlatformMenuBar();
139}
140
141bool QXdgDesktopPortalTheme::usePlatformNativeDialog(DialogType type) const
142{
143 Q_D(const QXdgDesktopPortalTheme);
144
145 if (type == FileDialog)
146 return true;
147
148 return d->baseTheme->usePlatformNativeDialog(type);
149}
150
151QPlatformDialogHelper* QXdgDesktopPortalTheme::createPlatformDialogHelper(DialogType type) const
152{
153 Q_D(const QXdgDesktopPortalTheme);
154
155 if (type == FileDialog) {
156 // Older versions of FileChooser portal don't support opening directories, therefore we fallback
157 // to native file dialog opened inside the sandbox to open a directory.
158 if (d->fileChooserPortalVersion < 3 && d->baseTheme->usePlatformNativeDialog(type))
159 return new QXdgDesktopPortalFileDialog(static_cast<QPlatformFileDialogHelper*>(d->baseTheme->createPlatformDialogHelper(type)));
160
161 return new QXdgDesktopPortalFileDialog;
162 }
163
164 return d->baseTheme->createPlatformDialogHelper(type);
165}
166
167#ifndef QT_NO_SYSTEMTRAYICON
168QPlatformSystemTrayIcon* QXdgDesktopPortalTheme::createPlatformSystemTrayIcon() const
169{
170 Q_D(const QXdgDesktopPortalTheme);
171 return d->baseTheme->createPlatformSystemTrayIcon();
172}
173#endif
174
175const QPalette *QXdgDesktopPortalTheme::palette(Palette type) const
176{
177 Q_D(const QXdgDesktopPortalTheme);
178 return d->baseTheme->palette(type);
179}
180
181const QFont* QXdgDesktopPortalTheme::font(Font type) const
182{
183 Q_D(const QXdgDesktopPortalTheme);
184 return d->baseTheme->font(type);
185}
186
187QVariant QXdgDesktopPortalTheme::themeHint(ThemeHint hint) const
188{
189 Q_D(const QXdgDesktopPortalTheme);
190 return d->baseTheme->themeHint(hint);
191}
192
193QPixmap QXdgDesktopPortalTheme::standardPixmap(StandardPixmap sp, const QSizeF &size) const
194{
195 Q_D(const QXdgDesktopPortalTheme);
196 return d->baseTheme->standardPixmap(sp, size);
197}
198
199QIcon QXdgDesktopPortalTheme::fileIcon(const QFileInfo &fileInfo,
200 QPlatformTheme::IconOptions iconOptions) const
201{
202 Q_D(const QXdgDesktopPortalTheme);
203 return d->baseTheme->fileIcon(fileInfo, iconOptions);
204}
205
206QIconEngine * QXdgDesktopPortalTheme::createIconEngine(const QString &iconName) const
207{
208 Q_D(const QXdgDesktopPortalTheme);
209 return d->baseTheme->createIconEngine(iconName);
210}
211
212#if QT_CONFIG(shortcut)
213QList<QKeySequence> QXdgDesktopPortalTheme::keyBindings(QKeySequence::StandardKey key) const
214{
215 Q_D(const QXdgDesktopPortalTheme);
216 return d->baseTheme->keyBindings(key);
217}
218#endif
219
220QString QXdgDesktopPortalTheme::standardButtonText(int button) const
221{
222 Q_D(const QXdgDesktopPortalTheme);
223 return d->baseTheme->standardButtonText(button);
224}
225
226QT_END_NAMESPACE
227

source code of qtbase/src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportaltheme.cpp