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#include <QtCore/private/qdarwinsecurityscopedfileengine_p.h>
12
13@implementation QIOSDocumentPickerController {
14 QPointer<QIOSFileDialog> m_fileDialog;
15}
16
17- (instancetype)initWithQIOSFileDialog:(QIOSFileDialog *)fileDialog
18{
19 NSMutableArray <UTType *> *docTypes = [[[NSMutableArray alloc] init] autorelease];
20
21 const auto options = fileDialog->options();
22
23 const QStringList nameFilters = options->nameFilters();
24 if (!nameFilters.isEmpty() && (options->fileMode() != QFileDialogOptions::Directory
25 || options->fileMode() != QFileDialogOptions::DirectoryOnly))
26 {
27 QStringList results;
28 for (const QString &filter : nameFilters)
29 results.append(QPlatformFileDialogHelper::cleanFilterList(filter));
30
31 docTypes = [self computeAllowedFileTypes:results];
32 }
33
34 if (!docTypes.count) {
35 switch (options->fileMode()) {
36 case QFileDialogOptions::AnyFile:
37 case QFileDialogOptions::ExistingFile:
38 case QFileDialogOptions::ExistingFiles:
39 [docTypes addObject:UTTypeContent];
40 [docTypes addObject:UTTypeItem];
41 [docTypes addObject:UTTypeData];
42 break;
43 // Showing files is not supported in Directory mode in iOS
44 case QFileDialogOptions::Directory:
45 case QFileDialogOptions::DirectoryOnly:
46 [docTypes addObject:UTTypeFolder];
47 break;
48 }
49 }
50
51 if (options->acceptMode() == QFileDialogOptions::AcceptSave) {
52 auto selectedUrls = options->initiallySelectedFiles();
53 auto suggestedFileName = !selectedUrls.isEmpty() ? selectedUrls.first().fileName() : "Untitled";
54
55 // Create an empty dummy file, so that the export dialog will allow us
56 // to choose the export destination, which we are then given access to
57 // write to.
58 NSURL *dummyExportFile = [NSFileManager.defaultManager.temporaryDirectory
59 URLByAppendingPathComponent:suggestedFileName.toNSString()];
60 [NSFileManager.defaultManager createFileAtPath:dummyExportFile.path contents:nil attributes:nil];
61
62 if (!(self = [super initForExportingURLs:@[dummyExportFile]]))
63 return nil;
64
65 // Note, we don't set the directoryURL, as if the directory can't be
66 // accessed, or written to, the file dialog is shown but is empty.
67 // FIXME: See comment below for open dialogs as well
68 } else {
69 if (!(self = [super initForOpeningContentTypes:docTypes asCopy:NO]))
70 return nil;
71
72 if (options->fileMode() == QFileDialogOptions::ExistingFiles)
73 self.allowsMultipleSelection = YES;
74
75 // FIXME: This doesn't seem to have any effect
76 self.directoryURL = options->initialDirectory().toNSURL();
77 }
78
79 m_fileDialog = fileDialog;
80 self.modalPresentationStyle = UIModalPresentationFormSheet;
81 self.delegate = self;
82 self.presentationController.delegate = self;
83
84 return self;
85}
86
87- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray <NSURL *>*)urls
88{
89 Q_UNUSED(controller);
90
91 if (!m_fileDialog)
92 return;
93
94 QList<QUrl> files;
95 for (NSURL* url in urls)
96 files.append(qt_apple_urlFromPossiblySecurityScopedURL(url));
97
98 m_fileDialog->selectedFilesChanged(files);
99 emit m_fileDialog->accept();
100}
101
102- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller
103{
104 if (!m_fileDialog)
105 return;
106
107 Q_UNUSED(controller);
108 emit m_fileDialog->reject();
109}
110
111- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController
112{
113 if (!m_fileDialog)
114 return;
115
116 Q_UNUSED(presentationController);
117
118 // "Called on the delegate when the user has taken action to dismiss the
119 // presentation successfully, after all animations are finished.
120 // This is not called if the presentation is dismissed programmatically."
121
122 // So if document picker's view was dismissed, for example by swiping it away,
123 // we got this method called. But not if the dialog was cancelled or a file
124 // was selected.
125 emit m_fileDialog->reject();
126}
127
128- (NSMutableArray<UTType*>*)computeAllowedFileTypes:(QStringList)filters
129{
130 QStringList fileTypes;
131 for (const QString &filter : filters) {
132 if (filter == (QLatin1String("*")))
133 continue;
134
135 if (filter.contains(u'?'))
136 continue;
137
138 if (filter.count(u'*') != 1)
139 continue;
140
141 auto extensions = filter.split('.', Qt::SkipEmptyParts);
142 fileTypes += extensions.last();
143 }
144
145 NSMutableArray<UTType *> *result = [NSMutableArray<UTType *> arrayWithCapacity:fileTypes.size()];
146 for (const QString &string : fileTypes)
147 [result addObject:[UTType typeWithFilenameExtension:string.toNSString()]];
148
149 return result;
150}
151
152@end