From ce20b81070c8283a7895e46dd79d560fb0b81462 Mon Sep 17 00:00:00 2001 From: Doris Verria Date: Wed, 14 Dec 2022 22:40:15 +0100 Subject: [PATCH] iOS File Dialog: Support setting name filters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: QTBUG-93624 Pick-to: 6.5 Change-Id: I2e1b522d572fd36f58a3de90f0a685f5d22dfeeb Reviewed-by: Tor Arne Vestbø --- .../ios/qiosdocumentpickercontroller.mm | 58 ++++++++++++++----- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/src/plugins/platforms/ios/qiosdocumentpickercontroller.mm b/src/plugins/platforms/ios/qiosdocumentpickercontroller.mm index 765d8848d2..350c965e87 100644 --- a/src/plugins/platforms/ios/qiosdocumentpickercontroller.mm +++ b/src/plugins/platforms/ios/qiosdocumentpickercontroller.mm @@ -14,19 +14,30 @@ { NSMutableArray *docTypes = [[[NSMutableArray alloc] init] autorelease]; - switch (fileDialog->options()->fileMode()) { - case QFileDialogOptions::AnyFile: - case QFileDialogOptions::ExistingFile: - case QFileDialogOptions::ExistingFiles: - [docTypes addObject:[UTType typeWithIdentifier:(__bridge NSString *)kUTTypeContent]]; - [docTypes addObject:[UTType typeWithIdentifier:(__bridge NSString *)kUTTypeItem]]; - [docTypes addObject:[UTType typeWithIdentifier:(__bridge NSString *)kUTTypeData]]; - break; - // Showing files is not supported in Directory mode in iOS - case QFileDialogOptions::Directory: - case QFileDialogOptions::DirectoryOnly: - [docTypes addObject:[UTType typeWithIdentifier:(__bridge NSString *)kUTTypeFolder]]; - break; + QStringList nameFilters = fileDialog->options()->nameFilters(); + if (!nameFilters.isEmpty() && (fileDialog->options()->fileMode() != QFileDialogOptions::Directory + || fileDialog->options()->fileMode() != QFileDialogOptions::DirectoryOnly)) + { + QStringList results; + for (const QString &filter : nameFilters) + results.append(QPlatformFileDialogHelper::cleanFilterList(filter)); + + docTypes = [self computeAllowedFileTypes:results]; + } else { + switch (fileDialog->options()->fileMode()) { + case QFileDialogOptions::AnyFile: + case QFileDialogOptions::ExistingFile: + case QFileDialogOptions::ExistingFiles: + [docTypes addObject:[UTType typeWithIdentifier:(__bridge NSString *)kUTTypeContent]]; + [docTypes addObject:[UTType typeWithIdentifier:(__bridge NSString *)kUTTypeItem]]; + [docTypes addObject:[UTType typeWithIdentifier:(__bridge NSString *)kUTTypeData]]; + break; + // Showing files is not supported in Directory mode in iOS + case QFileDialogOptions::Directory: + case QFileDialogOptions::DirectoryOnly: + [docTypes addObject:[UTType typeWithIdentifier:(__bridge NSString *)kUTTypeFolder]]; + break; + } } if (self = [super initForOpeningContentTypes:docTypes]) { @@ -75,4 +86,25 @@ emit m_fileDialog->reject(); } +- (NSMutableArray*)computeAllowedFileTypes:(QStringList)filters +{ + QStringList fileTypes; + for (const QString &filter : filters) { + if (filter.contains(u'?')) + continue; + + if (filter.count(u'*') != 1) + continue; + + auto extensions = filter.split('.', Qt::SkipEmptyParts); + fileTypes += extensions.last(); + } + + NSMutableArray *result = [NSMutableArray arrayWithCapacity:fileTypes.size()]; + for (const QString &string : fileTypes) + [result addObject:[UTType typeWithFilenameExtension:string.toNSString()]]; + + return result; +} + @end