diff options
author | Szabolcs David <davidsz@inf.u-szeged.hu> | 2019-11-19 17:49:19 +0100 |
---|---|---|
committer | Szabolcs David <davidsz@inf.u-szeged.hu> | 2019-11-29 14:34:10 +0100 |
commit | 6f0173c166d6f7a2237a031808a33b0d787acd1c (patch) | |
tree | e0a8661df378661ed97e3bdf1c0e5d05582ddc84 /src/core/file_picker_controller.cpp | |
parent | aca2a6df402d6454c7bbe8b0e9816645fcec8342 (diff) |
Support accept attribute of file input
Set name filters of QFileDialog and QML FileDialog to avoid presenting
all file types.
Task-number: QTBUG-76564
Change-Id: I321214a30bc7e875ad132b015c63282f4eb482bf
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/core/file_picker_controller.cpp')
-rw-r--r-- | src/core/file_picker_controller.cpp | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/src/core/file_picker_controller.cpp b/src/core/file_picker_controller.cpp index 63b93c502..13e9437d2 100644 --- a/src/core/file_picker_controller.cpp +++ b/src/core/file_picker_controller.cpp @@ -46,10 +46,11 @@ #include "content/browser/web_contents/web_contents_impl.h" #include "content/public/browser/file_select_listener.h" -#include <QFileInfo> #include <QDir> -#include <QVariant> +#include <QFileInfo> +#include <QMimeDatabase> #include <QStringList> +#include <QVariant> namespace QtWebEngineCore { @@ -165,4 +166,51 @@ QString FilePickerController::defaultFileName() const return m_defaultFileName; } +QStringList FilePickerController::nameFilters(const QStringList &acceptedMimeTypes) +{ + QStringList nameFilters; + QStringList acceptedGlobs; + QMimeDatabase mimeDatabase; + + for (QString type : acceptedMimeTypes) { + if (type.startsWith(".")) { + // A single suffix + // Filename.type doesn't have to exist and mimeTypeForFile() supports + // custom suffixes as valid (but unknown) MIME types. + const QMimeType &mimeType = mimeDatabase.mimeTypeForFile("filename" + type); + if (mimeType.isValid()) { + QString glob = "*" + type; + acceptedGlobs.append(glob); + nameFilters.append(mimeType.comment() + " (" + glob + ")"); + } + } else if (type.contains("/") && !type.endsWith("*")) { + // All suffixes for a given MIME type + const QMimeType &mimeType = mimeDatabase.mimeTypeForName(type); + if (mimeType.isValid() && !mimeType.globPatterns().isEmpty()) { + QString globs = mimeType.globPatterns().join(" "); + acceptedGlobs.append(globs); + nameFilters.append(mimeType.comment() + " (" + globs + ")"); + } + } else if (type.endsWith("/*")) { + // All MIME types for audio/*, image/* or video/* + // as separate filters as Chrome does + static const QList<QMimeType> &allMimeTypes = mimeDatabase.allMimeTypes(); + type = type.remove("/*"); + for (const QMimeType &m : allMimeTypes) { + if (m.name().startsWith(type) && !m.globPatterns().isEmpty()) { + QString globs = m.globPatterns().join(" "); + acceptedGlobs.append(globs); + nameFilters.append(m.comment() + " (" + globs + ")"); + } + } + } else { + NOTREACHED(); + } + } + + nameFilters.prepend(QObject::tr("Accepted types") + " (" + acceptedGlobs.join(" ") + ")"); + + return nameFilters; +} + } // namespace |