From 101cb8e5d97f2f26ff0b3efd7b6e8b0e336e62be Mon Sep 17 00:00:00 2001 From: Jan Grulich Date: Thu, 30 Aug 2018 10:45:49 +0200 Subject: Make flatpak portal support to be used also by Snap applications Snap now uses xdg-desktop-portal for portal support. Add check for apps running in Snap and make them use portals by default. We also should be using different name for the platform theme used by sandboxed apps. Change-Id: Ibaa35b7549b3d94775d7edb937f729a300d071b6 Reviewed-by: Thiago Macieira --- src/gui/kernel/qguiapplication.cpp | 10 +- .../services/genericunix/qgenericunixservices.cpp | 24 +- src/plugins/platformthemes/flatpak/flatpak.json | 3 - src/plugins/platformthemes/flatpak/flatpak.pro | 17 - src/plugins/platformthemes/flatpak/main.cpp | 65 ---- .../platformthemes/flatpak/qflatpakfiledialog.cpp | 407 --------------------- .../platformthemes/flatpak/qflatpakfiledialog_p.h | 106 ------ .../platformthemes/flatpak/qflatpaktheme.cpp | 200 ---------- src/plugins/platformthemes/flatpak/qflatpaktheme.h | 90 ----- src/plugins/platformthemes/platformthemes.pro | 2 +- .../platformthemes/xdgdesktopportal/main.cpp | 67 ++++ .../qxdgdesktopportalfiledialog.cpp | 407 +++++++++++++++++++++ .../qxdgdesktopportalfiledialog_p.h | 106 ++++++ .../xdgdesktopportal/qxdgdesktopportaltheme.cpp | 200 ++++++++++ .../xdgdesktopportal/qxdgdesktopportaltheme.h | 90 +++++ .../xdgdesktopportal/xdgdesktopportal.json | 3 + .../xdgdesktopportal/xdgdesktopportal.pro | 17 + 17 files changed, 908 insertions(+), 906 deletions(-) delete mode 100644 src/plugins/platformthemes/flatpak/flatpak.json delete mode 100644 src/plugins/platformthemes/flatpak/flatpak.pro delete mode 100644 src/plugins/platformthemes/flatpak/main.cpp delete mode 100644 src/plugins/platformthemes/flatpak/qflatpakfiledialog.cpp delete mode 100644 src/plugins/platformthemes/flatpak/qflatpakfiledialog_p.h delete mode 100644 src/plugins/platformthemes/flatpak/qflatpaktheme.cpp delete mode 100644 src/plugins/platformthemes/flatpak/qflatpaktheme.h create mode 100644 src/plugins/platformthemes/xdgdesktopportal/main.cpp create mode 100644 src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportalfiledialog.cpp create mode 100644 src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportalfiledialog_p.h create mode 100644 src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportaltheme.cpp create mode 100644 src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportaltheme.h create mode 100644 src/plugins/platformthemes/xdgdesktopportal/xdgdesktopportal.json create mode 100644 src/plugins/platformthemes/xdgdesktopportal/xdgdesktopportal.pro diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index bc1f83a9c8..9bf2a33e2a 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -256,10 +256,10 @@ static inline void clearFontUnlocked() QGuiApplicationPrivate::app_font = 0; } -static bool checkRunningUnderFlatpak() +static bool checkNeedPortalSupport() { #if QT_CONFIG(dbus) - return !QStandardPaths::locate(QStandardPaths::RuntimeLocation, QLatin1String("flatpak-info")).isEmpty(); + return !QStandardPaths::locate(QStandardPaths::RuntimeLocation, QLatin1String("flatpak-info")).isEmpty() || qEnvironmentVariableIsSet("SNAP"); #else return false; #endif // QT_CONFIG(dbus) @@ -1225,9 +1225,9 @@ static void init_platform(const QString &pluginNamesWithArguments, const QString if (!platformThemeName.isEmpty()) themeNames.append(platformThemeName); - // 2) Special case - check whether we are in sandbox to use flatpak platform theme for portals support - if (checkRunningUnderFlatpak()) { - themeNames.append(QStringLiteral("flatpak")); + // 2) Special case - check whether it's a flatpak or snap app to use xdg-desktop-portal platform theme for portals support + if (checkNeedPortalSupport()) { + themeNames.append(QStringLiteral("xdgdesktopportal")); } // 3) Ask the platform integration for a list of theme names diff --git a/src/platformsupport/services/genericunix/qgenericunixservices.cpp b/src/platformsupport/services/genericunix/qgenericunixservices.cpp index 5b15cccaf1..67884cef92 100644 --- a/src/platformsupport/services/genericunix/qgenericunixservices.cpp +++ b/src/platformsupport/services/genericunix/qgenericunixservices.cpp @@ -50,7 +50,7 @@ #include #if QT_CONFIG(dbus) -// These QtCore includes are needed for flatpak support +// These QtCore includes are needed for xdg-desktop-portal support #include #include @@ -172,12 +172,12 @@ static inline bool launch(const QString &launcher, const QUrl &url) } #if QT_CONFIG(dbus) -static inline bool checkRunningUnderFlatpak() +static inline bool checkNeedPortalSupport() { - return !QStandardPaths::locate(QStandardPaths::RuntimeLocation, QLatin1String("flatpak-info")).isEmpty(); + return !QStandardPaths::locate(QStandardPaths::RuntimeLocation, QLatin1String("flatpak-info")).isEmpty() || qEnvironmentVariableIsSet("SNAP"); } -static inline bool flatpakOpenFile(const QUrl &url) +static inline bool xdgDesktopPortalOpenFile(const QUrl &url) { // DBus signature: // OpenFile (IN s parent_window, @@ -212,7 +212,7 @@ static inline bool flatpakOpenFile(const QUrl &url) return false; } -static inline bool flatpakOpenUrl(const QUrl &url) +static inline bool xdgDesktopPortalOpenUrl(const QUrl &url) { // DBus signature: // OpenURI (IN s parent_window, @@ -236,7 +236,7 @@ static inline bool flatpakOpenUrl(const QUrl &url) return !reply.isError(); } -static inline bool flatpakSendEmail(const QUrl &url) +static inline bool xdgDesktopPortalSendEmail(const QUrl &url) { // DBus signature: // ComposeEmail (IN s parent_window, @@ -294,15 +294,15 @@ bool QGenericUnixServices::openUrl(const QUrl &url) { if (url.scheme() == QLatin1String("mailto")) { #if QT_CONFIG(dbus) - if (checkRunningUnderFlatpak()) - return flatpakSendEmail(url); + if (checkNeedPortalSupport()) + return xdgDesktopPortalSendEmail(url); #endif return openDocument(url); } #if QT_CONFIG(dbus) - if (checkRunningUnderFlatpak()) - return flatpakOpenUrl(url); + if (checkNeedPortalSupport()) + return xdgDesktopPortalOpenUrl(url); #endif if (m_webBrowser.isEmpty() && !detectWebBrowser(desktopEnvironment(), true, &m_webBrowser)) { @@ -315,8 +315,8 @@ bool QGenericUnixServices::openUrl(const QUrl &url) bool QGenericUnixServices::openDocument(const QUrl &url) { #if QT_CONFIG(dbus) - if (checkRunningUnderFlatpak()) - return flatpakOpenFile(url); + if (checkNeedPortalSupport()) + return xdgDesktopPortalOpenFile(url); #endif if (m_documentLauncher.isEmpty() && !detectWebBrowser(desktopEnvironment(), false, &m_documentLauncher)) { diff --git a/src/plugins/platformthemes/flatpak/flatpak.json b/src/plugins/platformthemes/flatpak/flatpak.json deleted file mode 100644 index 71f834fd08..0000000000 --- a/src/plugins/platformthemes/flatpak/flatpak.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "Keys": [ "flatpak" ] -} diff --git a/src/plugins/platformthemes/flatpak/flatpak.pro b/src/plugins/platformthemes/flatpak/flatpak.pro deleted file mode 100644 index 1e5dbb7a6c..0000000000 --- a/src/plugins/platformthemes/flatpak/flatpak.pro +++ /dev/null @@ -1,17 +0,0 @@ -TARGET = qflatpak - -PLUGIN_TYPE = platformthemes -PLUGIN_EXTENDS = - -PLUGIN_CLASS_NAME = QFlatpakThemePlugin -load(qt_plugin) - -QT += core-private dbus gui-private theme_support-private - -HEADERS += \ - qflatpaktheme.h \ - qflatpakfiledialog_p.h - -SOURCES += \ - main.cpp \ - qflatpaktheme.cpp \ - qflatpakfiledialog.cpp diff --git a/src/plugins/platformthemes/flatpak/main.cpp b/src/plugins/platformthemes/flatpak/main.cpp deleted file mode 100644 index 7888eed8b2..0000000000 --- a/src/plugins/platformthemes/flatpak/main.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 Red Hat, Inc -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include -#include "qflatpaktheme.h" - -QT_BEGIN_NAMESPACE - -class QFlatpakThemePlugin : public QPlatformThemePlugin -{ - Q_OBJECT - Q_PLUGIN_METADATA(IID QPlatformThemeFactoryInterface_iid FILE "flatpak.json") - -public: - QPlatformTheme *create(const QString &key, const QStringList ¶ms) override; -}; - -QPlatformTheme *QFlatpakThemePlugin::create(const QString &key, const QStringList ¶ms) -{ - Q_UNUSED(params); - if (!key.compare(QLatin1String("flatpak"), Qt::CaseInsensitive)) - return new QFlatpakTheme; - - return nullptr; -} - -QT_END_NAMESPACE - -#include "main.moc" diff --git a/src/plugins/platformthemes/flatpak/qflatpakfiledialog.cpp b/src/plugins/platformthemes/flatpak/qflatpakfiledialog.cpp deleted file mode 100644 index 39b93bc4b8..0000000000 --- a/src/plugins/platformthemes/flatpak/qflatpakfiledialog.cpp +++ /dev/null @@ -1,407 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 Red Hat, Inc -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qflatpakfiledialog_p.h" - -#include - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE - -QDBusArgument &operator <<(QDBusArgument &arg, const QFlatpakFileDialog::FilterCondition &filterCondition) -{ - arg.beginStructure(); - arg << filterCondition.type << filterCondition.pattern; - arg.endStructure(); - return arg; -} - -const QDBusArgument &operator >>(const QDBusArgument &arg, QFlatpakFileDialog::FilterCondition &filterCondition) -{ - uint type; - QString filterPattern; - arg.beginStructure(); - arg >> type >> filterPattern; - filterCondition.type = (QFlatpakFileDialog::ConditionType)type; - filterCondition.pattern = filterPattern; - arg.endStructure(); - - return arg; -} - -QDBusArgument &operator <<(QDBusArgument &arg, const QFlatpakFileDialog::Filter filter) -{ - arg.beginStructure(); - arg << filter.name << filter.filterConditions; - arg.endStructure(); - return arg; -} - -const QDBusArgument &operator >>(const QDBusArgument &arg, QFlatpakFileDialog::Filter &filter) -{ - QString name; - QFlatpakFileDialog::FilterConditionList filterConditions; - arg.beginStructure(); - arg >> name >> filterConditions; - filter.name = name; - filter.filterConditions = filterConditions; - arg.endStructure(); - - return arg; -} - -class QFlatpakFileDialogPrivate -{ -public: - QFlatpakFileDialogPrivate(QPlatformFileDialogHelper *nativeFileDialog) - : nativeFileDialog(nativeFileDialog) - { } - - WId winId = 0; - bool modal = false; - bool multipleFiles = false; - bool saveFile = false; - QString acceptLabel; - QString directory; - QString title; - QStringList nameFilters; - QStringList mimeTypesFilters; - QStringList selectedFiles; - QPlatformFileDialogHelper *nativeFileDialog = nullptr; -}; - -QFlatpakFileDialog::QFlatpakFileDialog(QPlatformFileDialogHelper *nativeFileDialog) - : QPlatformFileDialogHelper() - , d_ptr(new QFlatpakFileDialogPrivate(nativeFileDialog)) -{ - Q_D(QFlatpakFileDialog); - - if (d->nativeFileDialog) { - connect(d->nativeFileDialog, SIGNAL(accept()), this, SIGNAL(accept())); - connect(d->nativeFileDialog, SIGNAL(reject()), this, SIGNAL(reject())); - } -} - -QFlatpakFileDialog::~QFlatpakFileDialog() -{ -} - -void QFlatpakFileDialog::initializeDialog() -{ - Q_D(QFlatpakFileDialog); - - if (d->nativeFileDialog) - d->nativeFileDialog->setOptions(options()); - - if (options()->fileMode() == QFileDialogOptions::ExistingFiles) - d->multipleFiles = true; - - if (options()->isLabelExplicitlySet(QFileDialogOptions::Accept)) - d->acceptLabel = options()->labelText(QFileDialogOptions::Accept); - - if (!options()->windowTitle().isEmpty()) - d->title = options()->windowTitle(); - - if (options()->acceptMode() == QFileDialogOptions::AcceptSave) - d->saveFile = true; - - if (!options()->nameFilters().isEmpty()) - d->nameFilters = options()->nameFilters(); - - if (!options()->mimeTypeFilters().isEmpty()) - d->mimeTypesFilters = options()->mimeTypeFilters(); - - setDirectory(options()->initialDirectory()); -} - -void QFlatpakFileDialog::openPortal() -{ - Q_D(const QFlatpakFileDialog); - - QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.portal.Desktop"), - QLatin1String("/org/freedesktop/portal/desktop"), - QLatin1String("org.freedesktop.portal.FileChooser"), - d->saveFile ? QLatin1String("SaveFile") : QLatin1String("OpenFile")); - QString parentWindowId = QLatin1String("x11:") + QString::number(d->winId); - - QVariantMap options; - if (!d->acceptLabel.isEmpty()) - options.insert(QLatin1String("accept_label"), d->acceptLabel); - - options.insert(QLatin1String("modal"), d->modal); - options.insert(QLatin1String("multiple"), d->multipleFiles); - - if (d->saveFile) { - if (!d->directory.isEmpty()) - options.insert(QLatin1String("current_folder"), d->directory.toLatin1()); - - if (!d->selectedFiles.isEmpty()) - options.insert(QLatin1String("current_file"), d->selectedFiles.first().toLatin1()); - } - - // Insert filters - qDBusRegisterMetaType(); - qDBusRegisterMetaType(); - qDBusRegisterMetaType(); - qDBusRegisterMetaType(); - - FilterList filterList; - - if (!d->mimeTypesFilters.isEmpty()) { - for (const QString &mimeTypefilter : d->mimeTypesFilters) { - QMimeDatabase mimeDatabase; - QMimeType mimeType = mimeDatabase.mimeTypeForName(mimeTypefilter); - - // Creates e.g. (1, "image/png") - FilterCondition filterCondition; - filterCondition.type = MimeType; - filterCondition.pattern = mimeTypefilter; - - // Creates e.g. [((1, "image/png"))] - FilterConditionList filterConditions; - filterConditions << filterCondition; - - // Creates e.g. [("Images", [((1, "image/png"))])] - Filter filter; - filter.name = mimeType.comment(); - filter.filterConditions = filterConditions; - - filterList << filter; - } - } else if (!d->nameFilters.isEmpty()) { - for (const QString &filter : d->nameFilters) { - // Do parsing: - // Supported format is ("Images (*.png *.jpg)") - QRegularExpression regexp(QPlatformFileDialogHelper::filterRegExp); - QRegularExpressionMatch match = regexp.match(filter); - if (match.hasMatch()) { - QString userVisibleName = match.captured(1); - QStringList filterStrings = match.captured(2).split(QLatin1Char(' '), QString::SkipEmptyParts); - - FilterConditionList filterConditions; - for (const QString &filterString : filterStrings) { - FilterCondition filterCondition; - filterCondition.type = GlobalPattern; - filterCondition.pattern = filterString; - filterConditions << filterCondition; - } - - Filter filter; - filter.name = userVisibleName; - filter.filterConditions = filterConditions; - - filterList << filter; - } - } - } - - if (!filterList.isEmpty()) - options.insert(QLatin1String("filters"), QVariant::fromValue(filterList)); - - options.insert(QLatin1String("handle_token"), QStringLiteral("qt%1").arg(QRandomGenerator::global()->generate())); - - // TODO choices a(ssa(ss)s) - // List of serialized combo boxes to add to the file chooser. - - message << parentWindowId << d->title << options; - - QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(message); - QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall); - connect(watcher, &QDBusPendingCallWatcher::finished, this, [this] (QDBusPendingCallWatcher *watcher) { - QDBusPendingReply reply = *watcher; - if (reply.isError()) { - Q_EMIT reject(); - } else { - QDBusConnection::sessionBus().connect(nullptr, - reply.value().path(), - QLatin1String("org.freedesktop.portal.Request"), - QLatin1String("Response"), - this, - SLOT(gotResponse(uint,QVariantMap))); - } - }); -} - -bool QFlatpakFileDialog::defaultNameFilterDisables() const -{ - return false; -} - -void QFlatpakFileDialog::setDirectory(const QUrl &directory) -{ - Q_D(QFlatpakFileDialog); - - if (d->nativeFileDialog) { - d->nativeFileDialog->setOptions(options()); - d->nativeFileDialog->setDirectory(directory); - } - - d->directory = directory.path(); -} - -QUrl QFlatpakFileDialog::directory() const -{ - Q_D(const QFlatpakFileDialog); - - if (d->nativeFileDialog && (options()->fileMode() == QFileDialogOptions::Directory || options()->fileMode() == QFileDialogOptions::DirectoryOnly)) - return d->nativeFileDialog->directory(); - - return d->directory; -} - -void QFlatpakFileDialog::selectFile(const QUrl &filename) -{ - Q_D(QFlatpakFileDialog); - - if (d->nativeFileDialog) { - d->nativeFileDialog->setOptions(options()); - d->nativeFileDialog->selectFile(filename); - } - - d->selectedFiles << filename.path(); -} - -QList QFlatpakFileDialog::selectedFiles() const -{ - Q_D(const QFlatpakFileDialog); - - if (d->nativeFileDialog && (options()->fileMode() == QFileDialogOptions::Directory || options()->fileMode() == QFileDialogOptions::DirectoryOnly)) - return d->nativeFileDialog->selectedFiles(); - - QList files; - for (const QString &file : d->selectedFiles) { - files << QUrl(file); - } - return files; -} - -void QFlatpakFileDialog::setFilter() -{ - Q_D(QFlatpakFileDialog); - - if (d->nativeFileDialog) { - d->nativeFileDialog->setOptions(options()); - d->nativeFileDialog->setFilter(); - } -} - -void QFlatpakFileDialog::selectNameFilter(const QString &filter) -{ - Q_D(QFlatpakFileDialog); - - if (d->nativeFileDialog) { - d->nativeFileDialog->setOptions(options()); - d->nativeFileDialog->selectNameFilter(filter); - } -} - -QString QFlatpakFileDialog::selectedNameFilter() const -{ - // TODO - return QString(); -} - -void QFlatpakFileDialog::exec() -{ - Q_D(QFlatpakFileDialog); - - if (d->nativeFileDialog && (options()->fileMode() == QFileDialogOptions::Directory || options()->fileMode() == QFileDialogOptions::DirectoryOnly)) { - d->nativeFileDialog->exec(); - return; - } - - // HACK we have to avoid returning until we emit that the dialog was accepted or rejected - QEventLoop loop; - loop.connect(this, SIGNAL(accept()), SLOT(quit())); - loop.connect(this, SIGNAL(reject()), SLOT(quit())); - loop.exec(); -} - -void QFlatpakFileDialog::hide() -{ - Q_D(QFlatpakFileDialog); - - if (d->nativeFileDialog) - d->nativeFileDialog->hide(); -} - -bool QFlatpakFileDialog::show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent) -{ - Q_D(QFlatpakFileDialog); - - initializeDialog(); - - d->modal = windowModality != Qt::NonModal; - d->winId = parent ? parent->winId() : 0; - - if (d->nativeFileDialog && (options()->fileMode() == QFileDialogOptions::Directory || options()->fileMode() == QFileDialogOptions::DirectoryOnly)) - return d->nativeFileDialog->show(windowFlags, windowModality, parent); - - openPortal(); - - return true; -} - -void QFlatpakFileDialog::gotResponse(uint response, const QVariantMap &results) -{ - Q_D(QFlatpakFileDialog); - - if (!response) { - if (results.contains(QLatin1String("uris"))) - d->selectedFiles = results.value(QLatin1String("uris")).toStringList(); - - Q_EMIT accept(); - } else { - Q_EMIT reject(); - } -} - -QT_END_NAMESPACE diff --git a/src/plugins/platformthemes/flatpak/qflatpakfiledialog_p.h b/src/plugins/platformthemes/flatpak/qflatpakfiledialog_p.h deleted file mode 100644 index bd1dae209d..0000000000 --- a/src/plugins/platformthemes/flatpak/qflatpakfiledialog_p.h +++ /dev/null @@ -1,106 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 Red Hat, Inc -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#ifndef QFLATPAKFILEDIALOG_P_H -#define QFLATPAKFILEDIALOG_P_H - -#include -#include - -QT_BEGIN_NAMESPACE - -class QFlatpakFileDialogPrivate; - -class QFlatpakFileDialog : public QPlatformFileDialogHelper -{ - Q_OBJECT - Q_DECLARE_PRIVATE(QFlatpakFileDialog) -public: - enum ConditionType : uint { - GlobalPattern = 0, - MimeType = 1 - }; - // Filters a(sa(us)) - // Example: [('Images', [(0, '*.ico'), (1, 'image/png')]), ('Text', [(0, '*.txt')])] - struct FilterCondition { - ConditionType type; - QString pattern; // E.g. '*ico' or 'image/png' - }; - typedef QVector FilterConditionList; - - struct Filter { - QString name; // E.g. 'Images' or 'Text - FilterConditionList filterConditions;; // E.g. [(0, '*.ico'), (1, 'image/png')] or [(0, '*.txt')] - }; - typedef QVector FilterList; - - QFlatpakFileDialog(QPlatformFileDialogHelper *nativeFileDialog = nullptr); - ~QFlatpakFileDialog(); - - bool defaultNameFilterDisables() const override; - QUrl directory() const override; - void setDirectory(const QUrl &directory) override; - void selectFile(const QUrl &filename) override; - QList selectedFiles() const override; - void setFilter() override; - void selectNameFilter(const QString &filter) override; - QString selectedNameFilter() const override; - - void exec() override; - bool show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent) override; - void hide() override; - -private Q_SLOTS: - void gotResponse(uint response, const QVariantMap &results); - -private: - void initializeDialog(); - void openPortal(); - - QScopedPointer d_ptr; -}; - -QT_END_NAMESPACE - -Q_DECLARE_METATYPE(QFlatpakFileDialog::FilterCondition); -Q_DECLARE_METATYPE(QFlatpakFileDialog::FilterConditionList); -Q_DECLARE_METATYPE(QFlatpakFileDialog::Filter); -Q_DECLARE_METATYPE(QFlatpakFileDialog::FilterList); - -#endif // QFLATPAKFILEDIALOG_P_H - diff --git a/src/plugins/platformthemes/flatpak/qflatpaktheme.cpp b/src/plugins/platformthemes/flatpak/qflatpaktheme.cpp deleted file mode 100644 index 142d91a00b..0000000000 --- a/src/plugins/platformthemes/flatpak/qflatpaktheme.cpp +++ /dev/null @@ -1,200 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qflatpaktheme.h" -#include "qflatpakfiledialog_p.h" - -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE - -class QFlatpakThemePrivate : public QPlatformThemePrivate -{ -public: - QFlatpakThemePrivate() - : QPlatformThemePrivate() - { } - - ~QFlatpakThemePrivate() - { - delete baseTheme; - } - - QPlatformTheme *baseTheme; -}; - -QFlatpakTheme::QFlatpakTheme() - : d_ptr(new QFlatpakThemePrivate) -{ - Q_D(QFlatpakTheme); - - QStringList themeNames; - themeNames += QGuiApplicationPrivate::platform_integration->themeNames(); - // 1) Look for a theme plugin. - for (const QString &themeName : qAsConst(themeNames)) { - d->baseTheme = QPlatformThemeFactory::create(themeName, nullptr); - if (d->baseTheme) - break; - } - - // 2) If no theme plugin was found ask the platform integration to - // create a theme - if (!d->baseTheme) { - for (const QString &themeName : qAsConst(themeNames)) { - d->baseTheme = QGuiApplicationPrivate::platform_integration->createPlatformTheme(themeName); - if (d->baseTheme) - break; - } - // No error message; not having a theme plugin is allowed. - } - - // 3) Fall back on the built-in "null" platform theme. - if (!d->baseTheme) - d->baseTheme = new QPlatformTheme; -} - -QPlatformMenuItem* QFlatpakTheme::createPlatformMenuItem() const -{ - Q_D(const QFlatpakTheme); - return d->baseTheme->createPlatformMenuItem(); -} - -QPlatformMenu* QFlatpakTheme::createPlatformMenu() const -{ - Q_D(const QFlatpakTheme); - return d->baseTheme->createPlatformMenu(); -} - -QPlatformMenuBar* QFlatpakTheme::createPlatformMenuBar() const -{ - Q_D(const QFlatpakTheme); - return d->baseTheme->createPlatformMenuBar(); -} - -void QFlatpakTheme::showPlatformMenuBar() -{ - Q_D(const QFlatpakTheme); - return d->baseTheme->showPlatformMenuBar(); -} - -bool QFlatpakTheme::usePlatformNativeDialog(DialogType type) const -{ - Q_D(const QFlatpakTheme); - - if (type == FileDialog) - return true; - - return d->baseTheme->usePlatformNativeDialog(type); -} - -QPlatformDialogHelper* QFlatpakTheme::createPlatformDialogHelper(DialogType type) const -{ - Q_D(const QFlatpakTheme); - - if (type == FileDialog) { - if (d->baseTheme->usePlatformNativeDialog(type)) - return new QFlatpakFileDialog(static_cast(d->baseTheme->createPlatformDialogHelper(type))); - - return new QFlatpakFileDialog; - } - - return d->baseTheme->createPlatformDialogHelper(type); -} - -#ifndef QT_NO_SYSTEMTRAYICON -QPlatformSystemTrayIcon* QFlatpakTheme::createPlatformSystemTrayIcon() const -{ - Q_D(const QFlatpakTheme); - return d->baseTheme->createPlatformSystemTrayIcon(); -} -#endif - -const QPalette *QFlatpakTheme::palette(Palette type) const -{ - Q_D(const QFlatpakTheme); - return d->baseTheme->palette(type); -} - -const QFont* QFlatpakTheme::font(Font type) const -{ - Q_D(const QFlatpakTheme); - return d->baseTheme->font(type); -} - -QVariant QFlatpakTheme::themeHint(ThemeHint hint) const -{ - Q_D(const QFlatpakTheme); - return d->baseTheme->themeHint(hint); -} - -QPixmap QFlatpakTheme::standardPixmap(StandardPixmap sp, const QSizeF &size) const -{ - Q_D(const QFlatpakTheme); - return d->baseTheme->standardPixmap(sp, size); -} - -QIcon QFlatpakTheme::fileIcon(const QFileInfo &fileInfo, - QPlatformTheme::IconOptions iconOptions) const -{ - Q_D(const QFlatpakTheme); - return d->baseTheme->fileIcon(fileInfo, iconOptions); -} - -QIconEngine * QFlatpakTheme::createIconEngine(const QString &iconName) const -{ - Q_D(const QFlatpakTheme); - return d->baseTheme->createIconEngine(iconName); -} - -QList QFlatpakTheme::keyBindings(QKeySequence::StandardKey key) const -{ - Q_D(const QFlatpakTheme); - return d->baseTheme->keyBindings(key); -} - -QString QFlatpakTheme::standardButtonText(int button) const -{ - Q_D(const QFlatpakTheme); - return d->baseTheme->standardButtonText(button); -} - -QT_END_NAMESPACE diff --git a/src/plugins/platformthemes/flatpak/qflatpaktheme.h b/src/plugins/platformthemes/flatpak/qflatpaktheme.h deleted file mode 100644 index 87f79a2395..0000000000 --- a/src/plugins/platformthemes/flatpak/qflatpaktheme.h +++ /dev/null @@ -1,90 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QFLATPAKTHEME_H -#define QFLATPAKTHEME_H - -#include - -QT_BEGIN_NAMESPACE - -class QFlatpakThemePrivate; - -class QFlatpakTheme : public QPlatformTheme -{ - Q_DECLARE_PRIVATE(QFlatpakTheme) -public: - QFlatpakTheme(); - - QPlatformMenuItem *createPlatformMenuItem() const override; - QPlatformMenu *createPlatformMenu() const override; - QPlatformMenuBar *createPlatformMenuBar() const override; - void showPlatformMenuBar() override; - - bool usePlatformNativeDialog(DialogType type) const override; - QPlatformDialogHelper *createPlatformDialogHelper(DialogType type) const override; - -#ifndef QT_NO_SYSTEMTRAYICON - QPlatformSystemTrayIcon *createPlatformSystemTrayIcon() const override; -#endif - - const QPalette *palette(Palette type = SystemPalette) const override; - - const QFont *font(Font type = SystemFont) const override; - - QVariant themeHint(ThemeHint hint) const override; - - QPixmap standardPixmap(StandardPixmap sp, const QSizeF &size) const override; - QIcon fileIcon(const QFileInfo &fileInfo, - QPlatformTheme::IconOptions iconOptions = 0) const override; - - QIconEngine *createIconEngine(const QString &iconName) const override; - - QList keyBindings(QKeySequence::StandardKey key) const override; - - QString standardButtonText(int button) const override; - -private: - QScopedPointer d_ptr; - Q_DISABLE_COPY(QFlatpakTheme) -}; - -QT_END_NAMESPACE - -#endif // QFLATPAKTHEME_H diff --git a/src/plugins/platformthemes/platformthemes.pro b/src/plugins/platformthemes/platformthemes.pro index 17b1d91c6a..06ffc4cc9f 100644 --- a/src/plugins/platformthemes/platformthemes.pro +++ b/src/plugins/platformthemes/platformthemes.pro @@ -1,6 +1,6 @@ TEMPLATE = subdirs QT_FOR_CONFIG += widgets-private -qtConfig(dbus):qtConfig(regularexpression): SUBDIRS += flatpak +qtConfig(dbus):qtConfig(regularexpression): SUBDIRS += xdgdesktopportal qtHaveModule(widgets):qtConfig(gtk3): SUBDIRS += gtk3 diff --git a/src/plugins/platformthemes/xdgdesktopportal/main.cpp b/src/plugins/platformthemes/xdgdesktopportal/main.cpp new file mode 100644 index 0000000000..64a03d479f --- /dev/null +++ b/src/plugins/platformthemes/xdgdesktopportal/main.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2017-2018 Red Hat, Inc +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include "qxdgdesktopportaltheme.h" + +QT_BEGIN_NAMESPACE + +class QXdgDesktopPortalThemePlugin : public QPlatformThemePlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID QPlatformThemeFactoryInterface_iid FILE "xdgdesktopportal.json") + +public: + QPlatformTheme *create(const QString &key, const QStringList ¶ms) override; +}; + +QPlatformTheme *QXdgDesktopPortalThemePlugin::create(const QString &key, const QStringList ¶ms) +{ + Q_UNUSED(params); + if (!key.compare(QLatin1String("xdgdesktopportal"), Qt::CaseInsensitive) || + !key.compare(QLatin1String("flatpak"), Qt::CaseInsensitive) || + !key.compare(QLatin1String("snap"), Qt::CaseInsensitive)) + return new QXdgDesktopPortalTheme; + + return nullptr; +} + +QT_END_NAMESPACE + +#include "main.moc" diff --git a/src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportalfiledialog.cpp b/src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportalfiledialog.cpp new file mode 100644 index 0000000000..cda267d24b --- /dev/null +++ b/src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportalfiledialog.cpp @@ -0,0 +1,407 @@ +/**************************************************************************** +** +** Copyright (C) 2017-2018 Red Hat, Inc +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qxdgdesktopportalfiledialog_p.h" + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +QDBusArgument &operator <<(QDBusArgument &arg, const QXdgDesktopPortalFileDialog::FilterCondition &filterCondition) +{ + arg.beginStructure(); + arg << filterCondition.type << filterCondition.pattern; + arg.endStructure(); + return arg; +} + +const QDBusArgument &operator >>(const QDBusArgument &arg, QXdgDesktopPortalFileDialog::FilterCondition &filterCondition) +{ + uint type; + QString filterPattern; + arg.beginStructure(); + arg >> type >> filterPattern; + filterCondition.type = (QXdgDesktopPortalFileDialog::ConditionType)type; + filterCondition.pattern = filterPattern; + arg.endStructure(); + + return arg; +} + +QDBusArgument &operator <<(QDBusArgument &arg, const QXdgDesktopPortalFileDialog::Filter filter) +{ + arg.beginStructure(); + arg << filter.name << filter.filterConditions; + arg.endStructure(); + return arg; +} + +const QDBusArgument &operator >>(const QDBusArgument &arg, QXdgDesktopPortalFileDialog::Filter &filter) +{ + QString name; + QXdgDesktopPortalFileDialog::FilterConditionList filterConditions; + arg.beginStructure(); + arg >> name >> filterConditions; + filter.name = name; + filter.filterConditions = filterConditions; + arg.endStructure(); + + return arg; +} + +class QXdgDesktopPortalFileDialogPrivate +{ +public: + QXdgDesktopPortalFileDialogPrivate(QPlatformFileDialogHelper *nativeFileDialog) + : nativeFileDialog(nativeFileDialog) + { } + + WId winId = 0; + bool modal = false; + bool multipleFiles = false; + bool saveFile = false; + QString acceptLabel; + QString directory; + QString title; + QStringList nameFilters; + QStringList mimeTypesFilters; + QStringList selectedFiles; + QPlatformFileDialogHelper *nativeFileDialog = nullptr; +}; + +QXdgDesktopPortalFileDialog::QXdgDesktopPortalFileDialog(QPlatformFileDialogHelper *nativeFileDialog) + : QPlatformFileDialogHelper() + , d_ptr(new QXdgDesktopPortalFileDialogPrivate(nativeFileDialog)) +{ + Q_D(QXdgDesktopPortalFileDialog); + + if (d->nativeFileDialog) { + connect(d->nativeFileDialog, SIGNAL(accept()), this, SIGNAL(accept())); + connect(d->nativeFileDialog, SIGNAL(reject()), this, SIGNAL(reject())); + } +} + +QXdgDesktopPortalFileDialog::~QXdgDesktopPortalFileDialog() +{ +} + +void QXdgDesktopPortalFileDialog::initializeDialog() +{ + Q_D(QXdgDesktopPortalFileDialog); + + if (d->nativeFileDialog) + d->nativeFileDialog->setOptions(options()); + + if (options()->fileMode() == QFileDialogOptions::ExistingFiles) + d->multipleFiles = true; + + if (options()->isLabelExplicitlySet(QFileDialogOptions::Accept)) + d->acceptLabel = options()->labelText(QFileDialogOptions::Accept); + + if (!options()->windowTitle().isEmpty()) + d->title = options()->windowTitle(); + + if (options()->acceptMode() == QFileDialogOptions::AcceptSave) + d->saveFile = true; + + if (!options()->nameFilters().isEmpty()) + d->nameFilters = options()->nameFilters(); + + if (!options()->mimeTypeFilters().isEmpty()) + d->mimeTypesFilters = options()->mimeTypeFilters(); + + setDirectory(options()->initialDirectory()); +} + +void QXdgDesktopPortalFileDialog::openPortal() +{ + Q_D(const QXdgDesktopPortalFileDialog); + + QDBusMessage message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.portal.Desktop"), + QLatin1String("/org/freedesktop/portal/desktop"), + QLatin1String("org.freedesktop.portal.FileChooser"), + d->saveFile ? QLatin1String("SaveFile") : QLatin1String("OpenFile")); + QString parentWindowId = QLatin1String("x11:") + QString::number(d->winId); + + QVariantMap options; + if (!d->acceptLabel.isEmpty()) + options.insert(QLatin1String("accept_label"), d->acceptLabel); + + options.insert(QLatin1String("modal"), d->modal); + options.insert(QLatin1String("multiple"), d->multipleFiles); + + if (d->saveFile) { + if (!d->directory.isEmpty()) + options.insert(QLatin1String("current_folder"), d->directory.toLatin1()); + + if (!d->selectedFiles.isEmpty()) + options.insert(QLatin1String("current_file"), d->selectedFiles.first().toLatin1()); + } + + // Insert filters + qDBusRegisterMetaType(); + qDBusRegisterMetaType(); + qDBusRegisterMetaType(); + qDBusRegisterMetaType(); + + FilterList filterList; + + if (!d->mimeTypesFilters.isEmpty()) { + for (const QString &mimeTypefilter : d->mimeTypesFilters) { + QMimeDatabase mimeDatabase; + QMimeType mimeType = mimeDatabase.mimeTypeForName(mimeTypefilter); + + // Creates e.g. (1, "image/png") + FilterCondition filterCondition; + filterCondition.type = MimeType; + filterCondition.pattern = mimeTypefilter; + + // Creates e.g. [((1, "image/png"))] + FilterConditionList filterConditions; + filterConditions << filterCondition; + + // Creates e.g. [("Images", [((1, "image/png"))])] + Filter filter; + filter.name = mimeType.comment(); + filter.filterConditions = filterConditions; + + filterList << filter; + } + } else if (!d->nameFilters.isEmpty()) { + for (const QString &filter : d->nameFilters) { + // Do parsing: + // Supported format is ("Images (*.png *.jpg)") + QRegularExpression regexp(QPlatformFileDialogHelper::filterRegExp); + QRegularExpressionMatch match = regexp.match(filter); + if (match.hasMatch()) { + QString userVisibleName = match.captured(1); + QStringList filterStrings = match.captured(2).split(QLatin1Char(' '), QString::SkipEmptyParts); + + FilterConditionList filterConditions; + for (const QString &filterString : filterStrings) { + FilterCondition filterCondition; + filterCondition.type = GlobalPattern; + filterCondition.pattern = filterString; + filterConditions << filterCondition; + } + + Filter filter; + filter.name = userVisibleName; + filter.filterConditions = filterConditions; + + filterList << filter; + } + } + } + + if (!filterList.isEmpty()) + options.insert(QLatin1String("filters"), QVariant::fromValue(filterList)); + + options.insert(QLatin1String("handle_token"), QStringLiteral("qt%1").arg(QRandomGenerator::global()->generate())); + + // TODO choices a(ssa(ss)s) + // List of serialized combo boxes to add to the file chooser. + + message << parentWindowId << d->title << options; + + QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(message); + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall); + connect(watcher, &QDBusPendingCallWatcher::finished, this, [this] (QDBusPendingCallWatcher *watcher) { + QDBusPendingReply reply = *watcher; + if (reply.isError()) { + Q_EMIT reject(); + } else { + QDBusConnection::sessionBus().connect(nullptr, + reply.value().path(), + QLatin1String("org.freedesktop.portal.Request"), + QLatin1String("Response"), + this, + SLOT(gotResponse(uint,QVariantMap))); + } + }); +} + +bool QXdgDesktopPortalFileDialog::defaultNameFilterDisables() const +{ + return false; +} + +void QXdgDesktopPortalFileDialog::setDirectory(const QUrl &directory) +{ + Q_D(QXdgDesktopPortalFileDialog); + + if (d->nativeFileDialog) { + d->nativeFileDialog->setOptions(options()); + d->nativeFileDialog->setDirectory(directory); + } + + d->directory = directory.path(); +} + +QUrl QXdgDesktopPortalFileDialog::directory() const +{ + Q_D(const QXdgDesktopPortalFileDialog); + + if (d->nativeFileDialog && (options()->fileMode() == QFileDialogOptions::Directory || options()->fileMode() == QFileDialogOptions::DirectoryOnly)) + return d->nativeFileDialog->directory(); + + return d->directory; +} + +void QXdgDesktopPortalFileDialog::selectFile(const QUrl &filename) +{ + Q_D(QXdgDesktopPortalFileDialog); + + if (d->nativeFileDialog) { + d->nativeFileDialog->setOptions(options()); + d->nativeFileDialog->selectFile(filename); + } + + d->selectedFiles << filename.path(); +} + +QList QXdgDesktopPortalFileDialog::selectedFiles() const +{ + Q_D(const QXdgDesktopPortalFileDialog); + + if (d->nativeFileDialog && (options()->fileMode() == QFileDialogOptions::Directory || options()->fileMode() == QFileDialogOptions::DirectoryOnly)) + return d->nativeFileDialog->selectedFiles(); + + QList files; + for (const QString &file : d->selectedFiles) { + files << QUrl(file); + } + return files; +} + +void QXdgDesktopPortalFileDialog::setFilter() +{ + Q_D(QXdgDesktopPortalFileDialog); + + if (d->nativeFileDialog) { + d->nativeFileDialog->setOptions(options()); + d->nativeFileDialog->setFilter(); + } +} + +void QXdgDesktopPortalFileDialog::selectNameFilter(const QString &filter) +{ + Q_D(QXdgDesktopPortalFileDialog); + + if (d->nativeFileDialog) { + d->nativeFileDialog->setOptions(options()); + d->nativeFileDialog->selectNameFilter(filter); + } +} + +QString QXdgDesktopPortalFileDialog::selectedNameFilter() const +{ + // TODO + return QString(); +} + +void QXdgDesktopPortalFileDialog::exec() +{ + Q_D(QXdgDesktopPortalFileDialog); + + if (d->nativeFileDialog && (options()->fileMode() == QFileDialogOptions::Directory || options()->fileMode() == QFileDialogOptions::DirectoryOnly)) { + d->nativeFileDialog->exec(); + return; + } + + // HACK we have to avoid returning until we emit that the dialog was accepted or rejected + QEventLoop loop; + loop.connect(this, SIGNAL(accept()), SLOT(quit())); + loop.connect(this, SIGNAL(reject()), SLOT(quit())); + loop.exec(); +} + +void QXdgDesktopPortalFileDialog::hide() +{ + Q_D(QXdgDesktopPortalFileDialog); + + if (d->nativeFileDialog) + d->nativeFileDialog->hide(); +} + +bool QXdgDesktopPortalFileDialog::show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent) +{ + Q_D(QXdgDesktopPortalFileDialog); + + initializeDialog(); + + d->modal = windowModality != Qt::NonModal; + d->winId = parent ? parent->winId() : 0; + + if (d->nativeFileDialog && (options()->fileMode() == QFileDialogOptions::Directory || options()->fileMode() == QFileDialogOptions::DirectoryOnly)) + return d->nativeFileDialog->show(windowFlags, windowModality, parent); + + openPortal(); + + return true; +} + +void QXdgDesktopPortalFileDialog::gotResponse(uint response, const QVariantMap &results) +{ + Q_D(QXdgDesktopPortalFileDialog); + + if (!response) { + if (results.contains(QLatin1String("uris"))) + d->selectedFiles = results.value(QLatin1String("uris")).toStringList(); + + Q_EMIT accept(); + } else { + Q_EMIT reject(); + } +} + +QT_END_NAMESPACE diff --git a/src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportalfiledialog_p.h b/src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportalfiledialog_p.h new file mode 100644 index 0000000000..c1f1a2c005 --- /dev/null +++ b/src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportalfiledialog_p.h @@ -0,0 +1,106 @@ +/**************************************************************************** +** +** Copyright (C) 2017-2018 Red Hat, Inc +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QXDGDESKTOPPORTALFILEDIALOG_P_H +#define QXDGDESKTOPPORTALFILEDIALOG_P_H + +#include +#include + +QT_BEGIN_NAMESPACE + +class QXdgDesktopPortalFileDialogPrivate; + +class QXdgDesktopPortalFileDialog : public QPlatformFileDialogHelper +{ + Q_OBJECT + Q_DECLARE_PRIVATE(QXdgDesktopPortalFileDialog) +public: + enum ConditionType : uint { + GlobalPattern = 0, + MimeType = 1 + }; + // Filters a(sa(us)) + // Example: [('Images', [(0, '*.ico'), (1, 'image/png')]), ('Text', [(0, '*.txt')])] + struct FilterCondition { + ConditionType type; + QString pattern; // E.g. '*ico' or 'image/png' + }; + typedef QVector FilterConditionList; + + struct Filter { + QString name; // E.g. 'Images' or 'Text + FilterConditionList filterConditions;; // E.g. [(0, '*.ico'), (1, 'image/png')] or [(0, '*.txt')] + }; + typedef QVector FilterList; + + QXdgDesktopPortalFileDialog(QPlatformFileDialogHelper *nativeFileDialog = nullptr); + ~QXdgDesktopPortalFileDialog(); + + bool defaultNameFilterDisables() const override; + QUrl directory() const override; + void setDirectory(const QUrl &directory) override; + void selectFile(const QUrl &filename) override; + QList selectedFiles() const override; + void setFilter() override; + void selectNameFilter(const QString &filter) override; + QString selectedNameFilter() const override; + + void exec() override; + bool show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent) override; + void hide() override; + +private Q_SLOTS: + void gotResponse(uint response, const QVariantMap &results); + +private: + void initializeDialog(); + void openPortal(); + + QScopedPointer d_ptr; +}; + +QT_END_NAMESPACE + +Q_DECLARE_METATYPE(QXdgDesktopPortalFileDialog::FilterCondition); +Q_DECLARE_METATYPE(QXdgDesktopPortalFileDialog::FilterConditionList); +Q_DECLARE_METATYPE(QXdgDesktopPortalFileDialog::Filter); +Q_DECLARE_METATYPE(QXdgDesktopPortalFileDialog::FilterList); + +#endif // QXDGDESKTOPPORTALFILEDIALOG_P_H + diff --git a/src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportaltheme.cpp b/src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportaltheme.cpp new file mode 100644 index 0000000000..f07ca3f098 --- /dev/null +++ b/src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportaltheme.cpp @@ -0,0 +1,200 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qxdgdesktopportaltheme.h" +#include "qxdgdesktopportalfiledialog_p.h" + +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QXdgDesktopPortalThemePrivate : public QPlatformThemePrivate +{ +public: + QXdgDesktopPortalThemePrivate() + : QPlatformThemePrivate() + { } + + ~QXdgDesktopPortalThemePrivate() + { + delete baseTheme; + } + + QPlatformTheme *baseTheme; +}; + +QXdgDesktopPortalTheme::QXdgDesktopPortalTheme() + : d_ptr(new QXdgDesktopPortalThemePrivate) +{ + Q_D(QXdgDesktopPortalTheme); + + QStringList themeNames; + themeNames += QGuiApplicationPrivate::platform_integration->themeNames(); + // 1) Look for a theme plugin. + for (const QString &themeName : qAsConst(themeNames)) { + d->baseTheme = QPlatformThemeFactory::create(themeName, nullptr); + if (d->baseTheme) + break; + } + + // 2) If no theme plugin was found ask the platform integration to + // create a theme + if (!d->baseTheme) { + for (const QString &themeName : qAsConst(themeNames)) { + d->baseTheme = QGuiApplicationPrivate::platform_integration->createPlatformTheme(themeName); + if (d->baseTheme) + break; + } + // No error message; not having a theme plugin is allowed. + } + + // 3) Fall back on the built-in "null" platform theme. + if (!d->baseTheme) + d->baseTheme = new QPlatformTheme; +} + +QPlatformMenuItem* QXdgDesktopPortalTheme::createPlatformMenuItem() const +{ + Q_D(const QXdgDesktopPortalTheme); + return d->baseTheme->createPlatformMenuItem(); +} + +QPlatformMenu* QXdgDesktopPortalTheme::createPlatformMenu() const +{ + Q_D(const QXdgDesktopPortalTheme); + return d->baseTheme->createPlatformMenu(); +} + +QPlatformMenuBar* QXdgDesktopPortalTheme::createPlatformMenuBar() const +{ + Q_D(const QXdgDesktopPortalTheme); + return d->baseTheme->createPlatformMenuBar(); +} + +void QXdgDesktopPortalTheme::showPlatformMenuBar() +{ + Q_D(const QXdgDesktopPortalTheme); + return d->baseTheme->showPlatformMenuBar(); +} + +bool QXdgDesktopPortalTheme::usePlatformNativeDialog(DialogType type) const +{ + Q_D(const QXdgDesktopPortalTheme); + + if (type == FileDialog) + return true; + + return d->baseTheme->usePlatformNativeDialog(type); +} + +QPlatformDialogHelper* QXdgDesktopPortalTheme::createPlatformDialogHelper(DialogType type) const +{ + Q_D(const QXdgDesktopPortalTheme); + + if (type == FileDialog) { + if (d->baseTheme->usePlatformNativeDialog(type)) + return new QXdgDesktopPortalFileDialog(static_cast(d->baseTheme->createPlatformDialogHelper(type))); + + return new QXdgDesktopPortalFileDialog; + } + + return d->baseTheme->createPlatformDialogHelper(type); +} + +#ifndef QT_NO_SYSTEMTRAYICON +QPlatformSystemTrayIcon* QXdgDesktopPortalTheme::createPlatformSystemTrayIcon() const +{ + Q_D(const QXdgDesktopPortalTheme); + return d->baseTheme->createPlatformSystemTrayIcon(); +} +#endif + +const QPalette *QXdgDesktopPortalTheme::palette(Palette type) const +{ + Q_D(const QXdgDesktopPortalTheme); + return d->baseTheme->palette(type); +} + +const QFont* QXdgDesktopPortalTheme::font(Font type) const +{ + Q_D(const QXdgDesktopPortalTheme); + return d->baseTheme->font(type); +} + +QVariant QXdgDesktopPortalTheme::themeHint(ThemeHint hint) const +{ + Q_D(const QXdgDesktopPortalTheme); + return d->baseTheme->themeHint(hint); +} + +QPixmap QXdgDesktopPortalTheme::standardPixmap(StandardPixmap sp, const QSizeF &size) const +{ + Q_D(const QXdgDesktopPortalTheme); + return d->baseTheme->standardPixmap(sp, size); +} + +QIcon QXdgDesktopPortalTheme::fileIcon(const QFileInfo &fileInfo, + QPlatformTheme::IconOptions iconOptions) const +{ + Q_D(const QXdgDesktopPortalTheme); + return d->baseTheme->fileIcon(fileInfo, iconOptions); +} + +QIconEngine * QXdgDesktopPortalTheme::createIconEngine(const QString &iconName) const +{ + Q_D(const QXdgDesktopPortalTheme); + return d->baseTheme->createIconEngine(iconName); +} + +QList QXdgDesktopPortalTheme::keyBindings(QKeySequence::StandardKey key) const +{ + Q_D(const QXdgDesktopPortalTheme); + return d->baseTheme->keyBindings(key); +} + +QString QXdgDesktopPortalTheme::standardButtonText(int button) const +{ + Q_D(const QXdgDesktopPortalTheme); + return d->baseTheme->standardButtonText(button); +} + +QT_END_NAMESPACE diff --git a/src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportaltheme.h b/src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportaltheme.h new file mode 100644 index 0000000000..b72e676419 --- /dev/null +++ b/src/plugins/platformthemes/xdgdesktopportal/qxdgdesktopportaltheme.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the plugins of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QXDGDESKTOPPORTALTHEME_H +#define QXDGDESKTOPPORTALTHEME_H + +#include + +QT_BEGIN_NAMESPACE + +class QXdgDesktopPortalThemePrivate; + +class QXdgDesktopPortalTheme : public QPlatformTheme +{ + Q_DECLARE_PRIVATE(QXdgDesktopPortalTheme) +public: + QXdgDesktopPortalTheme(); + + QPlatformMenuItem *createPlatformMenuItem() const override; + QPlatformMenu *createPlatformMenu() const override; + QPlatformMenuBar *createPlatformMenuBar() const override; + void showPlatformMenuBar() override; + + bool usePlatformNativeDialog(DialogType type) const override; + QPlatformDialogHelper *createPlatformDialogHelper(DialogType type) const override; + +#ifndef QT_NO_SYSTEMTRAYICON + QPlatformSystemTrayIcon *createPlatformSystemTrayIcon() const override; +#endif + + const QPalette *palette(Palette type = SystemPalette) const override; + + const QFont *font(Font type = SystemFont) const override; + + QVariant themeHint(ThemeHint hint) const override; + + QPixmap standardPixmap(StandardPixmap sp, const QSizeF &size) const override; + QIcon fileIcon(const QFileInfo &fileInfo, + QPlatformTheme::IconOptions iconOptions = 0) const override; + + QIconEngine *createIconEngine(const QString &iconName) const override; + + QList keyBindings(QKeySequence::StandardKey key) const override; + + QString standardButtonText(int button) const override; + +private: + QScopedPointer d_ptr; + Q_DISABLE_COPY(QXdgDesktopPortalTheme) +}; + +QT_END_NAMESPACE + +#endif // QXDGDESKTOPPORTALTHEME_H diff --git a/src/plugins/platformthemes/xdgdesktopportal/xdgdesktopportal.json b/src/plugins/platformthemes/xdgdesktopportal/xdgdesktopportal.json new file mode 100644 index 0000000000..c69062d9a1 --- /dev/null +++ b/src/plugins/platformthemes/xdgdesktopportal/xdgdesktopportal.json @@ -0,0 +1,3 @@ +{ + "Keys": [ "xdgdesktopportal", "flatpak", "snap" ] +} diff --git a/src/plugins/platformthemes/xdgdesktopportal/xdgdesktopportal.pro b/src/plugins/platformthemes/xdgdesktopportal/xdgdesktopportal.pro new file mode 100644 index 0000000000..0a71484cf9 --- /dev/null +++ b/src/plugins/platformthemes/xdgdesktopportal/xdgdesktopportal.pro @@ -0,0 +1,17 @@ +TARGET = qxdgdesktopportal + +PLUGIN_TYPE = platformthemes +PLUGIN_EXTENDS = - +PLUGIN_CLASS_NAME = QXdgDesktopPortalThemePlugin +load(qt_plugin) + +QT += core-private dbus gui-private theme_support-private + +HEADERS += \ + qxdgdesktopportaltheme.h \ + qxdgdesktopportalfiledialog_p.h + +SOURCES += \ + main.cpp \ + qxdgdesktopportaltheme.cpp \ + qxdgdesktopportalfiledialog.cpp -- cgit v1.2.3