From 5443da6c2795255db55697471190b3eba16b28dc Mon Sep 17 00:00:00 2001 From: David Faure Date: Sat, 9 May 2015 14:12:28 +0200 Subject: [PATCH] QFileDialog: add setSupportedSchemes and pass it along to the QPA. This also finishes the implementation of the static methods which were being passed a supportedSchemes argument but weren't using it. Now they can pass it along to the QFileDialog instance, which then passes it to the helper used by the QPA implementation of the file dialog. The default implementation only supports local files and can therefore ignore this, but other implementations can use this argument to restrict the protocols allowed to the user. [ChangeLog][Widgets][QFileDialog] Add supportedSchemes property. Change-Id: I5235f70e785da1c06866a8355ef98f571890c4a2 Reviewed-by: Kevin Ottens Reviewed-by: Friedemann Kleint --- src/gui/kernel/qplatformdialoghelper.cpp | 13 +++++++++ src/gui/kernel/qplatformdialoghelper.h | 3 ++ src/widgets/dialogs/qfiledialog.cpp | 36 ++++++++++++++++++------ src/widgets/dialogs/qfiledialog.h | 4 +++ 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/gui/kernel/qplatformdialoghelper.cpp b/src/gui/kernel/qplatformdialoghelper.cpp index 2d0458f705..25894fd504 100644 --- a/src/gui/kernel/qplatformdialoghelper.cpp +++ b/src/gui/kernel/qplatformdialoghelper.cpp @@ -422,6 +422,7 @@ public: QUrl initialDirectory; QString initiallySelectedNameFilter; QList initiallySelectedFiles; + QStringList supportedSchemes; }; QFileDialogOptions::QFileDialogOptions() : d(new QFileDialogOptionsPrivate) @@ -613,6 +614,18 @@ void QFileDialogOptions::setInitiallySelectedFiles(const QList &files) d->initiallySelectedFiles = files; } +// Schemes supported by the application +void QFileDialogOptions::setSupportedSchemes(const QStringList &schemes) +{ + d->supportedSchemes = schemes; +} + +QStringList QFileDialogOptions::supportedSchemes() const +{ + return d->supportedSchemes; +} + +// Return true if the URL is supported by the filedialog implementation *and* by the application. bool QPlatformFileDialogHelper::isSupportedUrl(const QUrl &url) const { return url.isLocalFile(); diff --git a/src/gui/kernel/qplatformdialoghelper.h b/src/gui/kernel/qplatformdialoghelper.h index 8b2b9881b7..ec88770862 100644 --- a/src/gui/kernel/qplatformdialoghelper.h +++ b/src/gui/kernel/qplatformdialoghelper.h @@ -348,6 +348,9 @@ public: QList initiallySelectedFiles() const; void setInitiallySelectedFiles(const QList &); + void setSupportedSchemes(const QStringList &schemes); + QStringList supportedSchemes() const; + private: QSharedDataPointer d; }; diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp index 45f41bb0d2..86ca64d82a 100644 --- a/src/widgets/dialogs/qfiledialog.cpp +++ b/src/widgets/dialogs/qfiledialog.cpp @@ -1700,6 +1700,30 @@ void QFileDialog::setAcceptMode(QFileDialog::AcceptMode mode) d->retranslateWindowTitle(); } +/*! + \property QFileDialog::supportedSchemes + \brief the URL schemes that the file dialog should allow navigating to. + \since 5.6 + + Setting this property allows to restrict the type of URLs the + user will be able to select. It is a way for the application to declare + the protocols it will support to fetch the file content. An empty list + means that no restriction is applied (the default). + Supported for local files ("file" scheme) is implicit and always enabled; + it is not necessary to include it in the restriction. +*/ + +void QFileDialog::setSupportedSchemes(const QStringList &schemes) +{ + Q_D(QFileDialog); + d->options->setSupportedSchemes(schemes); +} + +QStringList QFileDialog::supportedSchemes() const +{ + return d_func()->options->supportedSchemes(); +} + /* Returns the file system model index that is the root index in the views @@ -2114,8 +2138,6 @@ QUrl QFileDialog::getOpenFileUrl(QWidget *parent, Options options, const QStringList &supportedSchemes) { - Q_UNUSED(supportedSchemes); // TODO - QFileDialogArgs args; args.parent = parent; args.caption = caption; @@ -2126,6 +2148,7 @@ QUrl QFileDialog::getOpenFileUrl(QWidget *parent, args.options = options; QFileDialog dialog(args); + dialog.setSupportedSchemes(supportedSchemes); if (selectedFilter && !selectedFilter->isEmpty()) dialog.selectNameFilter(*selectedFilter); if (dialog.exec() == QDialog::Accepted) { @@ -2237,8 +2260,6 @@ QList QFileDialog::getOpenFileUrls(QWidget *parent, Options options, const QStringList &supportedSchemes) { - Q_UNUSED(supportedSchemes); - QFileDialogArgs args; args.parent = parent; args.caption = caption; @@ -2249,6 +2270,7 @@ QList QFileDialog::getOpenFileUrls(QWidget *parent, args.options = options; QFileDialog dialog(args); + dialog.setSupportedSchemes(supportedSchemes); if (selectedFilter && !selectedFilter->isEmpty()) dialog.selectNameFilter(*selectedFilter); if (dialog.exec() == QDialog::Accepted) { @@ -2356,8 +2378,6 @@ QUrl QFileDialog::getSaveFileUrl(QWidget *parent, Options options, const QStringList &supportedSchemes) { - Q_UNUSED(supportedSchemes); - QFileDialogArgs args; args.parent = parent; args.caption = caption; @@ -2368,6 +2388,7 @@ QUrl QFileDialog::getSaveFileUrl(QWidget *parent, args.options = options; QFileDialog dialog(args); + dialog.setSupportedSchemes(supportedSchemes); dialog.setAcceptMode(AcceptSave); if (selectedFilter && !selectedFilter->isEmpty()) dialog.selectNameFilter(*selectedFilter); @@ -2461,8 +2482,6 @@ QUrl QFileDialog::getExistingDirectoryUrl(QWidget *parent, Options options, const QStringList &supportedSchemes) { - Q_UNUSED(supportedSchemes); - QFileDialogArgs args; args.parent = parent; args.caption = caption; @@ -2471,6 +2490,7 @@ QUrl QFileDialog::getExistingDirectoryUrl(QWidget *parent, args.options = options; QFileDialog dialog(args); + dialog.setSupportedSchemes(supportedSchemes); if (dialog.exec() == QDialog::Accepted) return dialog.selectedUrls().value(0); return QUrl(); diff --git a/src/widgets/dialogs/qfiledialog.h b/src/widgets/dialogs/qfiledialog.h index 6bbeb3817a..bba8c5f3c7 100644 --- a/src/widgets/dialogs/qfiledialog.h +++ b/src/widgets/dialogs/qfiledialog.h @@ -66,6 +66,7 @@ class Q_WIDGETS_EXPORT QFileDialog : public QDialog Q_PROPERTY(bool nameFilterDetailsVisible READ isNameFilterDetailsVisible WRITE setNameFilterDetailsVisible DESIGNABLE false) Q_PROPERTY(Options options READ options WRITE setOptions) + Q_PROPERTY(QStringList supportedSchemes READ supportedSchemes WRITE setSupportedSchemes) public: enum ViewMode { Detail, List }; @@ -167,6 +168,9 @@ public: void setLabelText(DialogLabel label, const QString &text); QString labelText(DialogLabel label) const; + void setSupportedSchemes(const QStringList &schemes); + QStringList supportedSchemes() const; + #ifndef QT_NO_PROXYMODEL void setProxyModel(QAbstractProxyModel *model); QAbstractProxyModel *proxyModel() const;