Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qiosdocumentpickercontroller.mm
Go to the documentation of this file.
1// Copyright (C) 2020 Harald Meyer.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#import <UIKit/UIKit.h>
6#import <MobileCoreServices/MobileCoreServices.h>
7
9
10@implementation QIOSDocumentPickerController {
11 QIOSFileDialog *m_fileDialog;
12}
13
14- (instancetype)initWithQIOSFileDialog:(QIOSFileDialog *)fileDialog
15{
16 NSMutableArray <UTType *> *docTypes = [[[NSMutableArray alloc] init] autorelease];
17
18 QStringList nameFilters = fileDialog->options()->nameFilters();
19 if (!nameFilters.isEmpty() && (fileDialog->options()->fileMode() != QFileDialogOptions::Directory
20 || fileDialog->options()->fileMode() != QFileDialogOptions::DirectoryOnly))
21 {
22 QStringList results;
23 for (const QString &filter : nameFilters)
24 results.append(QPlatformFileDialogHelper::cleanFilterList(filter));
25
26 docTypes = [self computeAllowedFileTypes:results];
27 }
28
29 if (!docTypes.count) {
30 switch (fileDialog->options()->fileMode()) {
31 case QFileDialogOptions::AnyFile:
32 case QFileDialogOptions::ExistingFile:
33 case QFileDialogOptions::ExistingFiles:
34 [docTypes addObject:UTTypeContent];
35 [docTypes addObject:UTTypeItem];
36 [docTypes addObject:UTTypeData];
37 break;
38 // Showing files is not supported in Directory mode in iOS
39 case QFileDialogOptions::Directory:
40 case QFileDialogOptions::DirectoryOnly:
41 [docTypes addObject:UTTypeFolder];
42 break;
43 }
44 }
45
46 if (self = [super initForOpeningContentTypes:docTypes]) {
47 m_fileDialog = fileDialog;
48 self.modalPresentationStyle = UIModalPresentationFormSheet;
49 self.delegate = self;
50 self.presentationController.delegate = self;
51
52 if (m_fileDialog->options()->fileMode() == QFileDialogOptions::ExistingFiles)
53 self.allowsMultipleSelection = YES;
54
55 self.directoryURL = m_fileDialog->options()->initialDirectory().toNSURL();
56 }
57 return self;
58}
59
60- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray <NSURL *>*)urls
61{
62 Q_UNUSED(controller);
63
64 QList<QUrl> files;
65 for (NSURL* url in urls)
66 files.append(QUrl::fromNSURL(url));
67
68 m_fileDialog->selectedFilesChanged(files);
69 emit m_fileDialog->accept();
70}
71
72- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller
73{
74 Q_UNUSED(controller);
75 emit m_fileDialog->reject();
76}
77
78- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController
79{
80 Q_UNUSED(presentationController);
81
82 // "Called on the delegate when the user has taken action to dismiss the
83 // presentation successfully, after all animations are finished.
84 // This is not called if the presentation is dismissed programmatically."
85
86 // So if document picker's view was dismissed, for example by swiping it away,
87 // we got this method called. But not if the dialog was cancelled or a file
88 // was selected.
89 emit m_fileDialog->reject();
90}
91
92- (NSMutableArray<UTType*>*)computeAllowedFileTypes:(QStringList)filters
93{
94 QStringList fileTypes;
95 for (const QString &filter : filters) {
96 if (filter == (QLatin1String("*")))
97 continue;
98
99 if (filter.contains(u'?'))
100 continue;
101
102 if (filter.count(u'*') != 1)
103 continue;
104
105 auto extensions = filter.split('.', Qt::SkipEmptyParts);
106 fileTypes += extensions.last();
107 }
108
109 NSMutableArray<UTType *> *result = [NSMutableArray<UTType *> arrayWithCapacity:fileTypes.size()];
110 for (const QString &string : fileTypes)
111 [result addObject:[UTType typeWithFilenameExtension:string.toNSString()]];
112
113 return result;
114}
115
116@end