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#include <QtCore/qpointer.h>
11
12@implementation QIOSDocumentPickerController {
13 QPointer<QIOSFileDialog> m_fileDialog;
14}
15
16- (instancetype)initWithQIOSFileDialog:(QIOSFileDialog *)fileDialog
17{
18 NSMutableArray <UTType *> *docTypes = [[[NSMutableArray alloc] init] autorelease];
19
20 QStringList nameFilters = fileDialog->options()->nameFilters();
21 if (!nameFilters.isEmpty() && (fileDialog->options()->fileMode() != QFileDialogOptions::Directory
22 || fileDialog->options()->fileMode() != QFileDialogOptions::DirectoryOnly))
23 {
24 QStringList results;
25 for (const QString &filter : nameFilters)
26 results.append(QPlatformFileDialogHelper::cleanFilterList(filter));
27
28 docTypes = [self computeAllowedFileTypes:results];
29 }
30
31 if (!docTypes.count) {
32 switch (fileDialog->options()->fileMode()) {
33 case QFileDialogOptions::AnyFile:
34 case QFileDialogOptions::ExistingFile:
35 case QFileDialogOptions::ExistingFiles:
36 [docTypes addObject:UTTypeContent];
37 [docTypes addObject:UTTypeItem];
38 [docTypes addObject:UTTypeData];
39 break;
40 // Showing files is not supported in Directory mode in iOS
41 case QFileDialogOptions::Directory:
42 case QFileDialogOptions::DirectoryOnly:
43 [docTypes addObject:UTTypeFolder];
44 break;
45 }
46 }
47
48 if (self = [super initForOpeningContentTypes:docTypes]) {
49 m_fileDialog = fileDialog;
50 self.modalPresentationStyle = UIModalPresentationFormSheet;
51 self.delegate = self;
52 self.presentationController.delegate = self;
53
54 if (m_fileDialog->options()->fileMode() == QFileDialogOptions::ExistingFiles)
55 self.allowsMultipleSelection = YES;
56
57 self.directoryURL = m_fileDialog->options()->initialDirectory().toNSURL();
58 }
59 return self;
60}
61
62- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray <NSURL *>*)urls
63{
64 Q_UNUSED(controller);
65
66 if (!m_fileDialog)
67 return;
68
69 QList<QUrl> files;
70 for (NSURL* url in urls)
71 files.append(QUrl::fromNSURL(url));
72
73 m_fileDialog->selectedFilesChanged(files);
74 emit m_fileDialog->accept();
75}
76
77- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller
78{
79 if (!m_fileDialog)
80 return;
81
82 Q_UNUSED(controller);
83 emit m_fileDialog->reject();
84}
85
86- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController
87{
88 if (!m_fileDialog)
89 return;
90
91 Q_UNUSED(presentationController);
92
93 // "Called on the delegate when the user has taken action to dismiss the
94 // presentation successfully, after all animations are finished.
95 // This is not called if the presentation is dismissed programmatically."
96
97 // So if document picker's view was dismissed, for example by swiping it away,
98 // we got this method called. But not if the dialog was cancelled or a file
99 // was selected.
100 emit m_fileDialog->reject();
101}
102
103- (NSMutableArray<UTType*>*)computeAllowedFileTypes:(QStringList)filters
104{
105 QStringList fileTypes;
106 for (const QString &filter : filters) {
107 if (filter == (QLatin1String("*")))
108 continue;
109
110 if (filter.contains(u'?'))
111 continue;
112
113 if (filter.count(u'*') != 1)
114 continue;
115
116 auto extensions = filter.split('.', Qt::SkipEmptyParts);
117 fileTypes += extensions.last();
118 }
119
120 NSMutableArray<UTType *> *result = [NSMutableArray<UTType *> arrayWithCapacity:fileTypes.size()];
121 for (const QString &string : fileTypes)
122 [result addObject:[UTType typeWithFilenameExtension:string.toNSString()]];
123
124 return result;
125}
126
127@end