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
qcocoafiledialoghelper.mm
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
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#include <QtCore/qglobal.h>
6
7#include <AppKit/AppKit.h>
8
10#include "qcocoahelpers.h"
12
13#include <QtCore/qbuffer.h>
14#include <QtCore/qdebug.h>
15#include <QtCore/qstringlist.h>
16#include <QtCore/qvarlengtharray.h>
17#include <QtCore/qabstracteventdispatcher.h>
18#include <QtCore/qdir.h>
19#include <QtCore/qregularexpression.h>
20#include <QtCore/qpointer.h>
21#include <QtCore/private/qcore_mac_p.h>
22#include <QtCore/private/qdarwinsecurityscopedfileengine_p.h>
23
24#include <QtGui/qguiapplication.h>
25#include <QtGui/private/qguiapplication_p.h>
26
27#include <qpa/qplatformtheme.h>
28#include <qpa/qplatformnativeinterface.h>
29
30#include <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
31
32QT_USE_NAMESPACE
33
34using namespace Qt::StringLiterals;
35
36static NSString *strippedText(QString s)
37{
38 s.remove("..."_L1);
39 return QPlatformTheme::removeMnemonics(s).trimmed().toNSString();
40}
41
42// NSOpenPanel extends NSSavePanel with some extra APIs
43static NSOpenPanel *openpanel_cast(NSSavePanel *panel)
44{
45 if ([panel isKindOfClass:NSOpenPanel.class])
46 return static_cast<NSOpenPanel*>(panel);
47 else
48 return nil;
49}
50
52
53@implementation QNSOpenSavePanelDelegate {
54 @public
55 NSSavePanel *m_panel;
56 NSView *m_accessoryView;
57 NSPopUpButton *m_popupButton;
58 NSTextField *m_textField;
59 QPointer<QCocoaFileDialogHelper> m_helper;
60
61 SharedPointerFileDialogOptions m_options;
62 QString m_currentSelection;
63 QStringList m_nameFilterDropDownList;
64 QStringList m_selectedNameFilter;
65}
66
67- (instancetype)initWithAcceptMode:(const QString &)selectFile
68 options:(SharedPointerFileDialogOptions)options
69 helper:(QCocoaFileDialogHelper *)helper
70{
71 if ((self = [super init])) {
72 m_options = options;
73
74 if (m_options->acceptMode() == QFileDialogOptions::AcceptOpen)
75 m_panel = [[NSOpenPanel openPanel] retain];
76 else
77 m_panel = [[NSSavePanel savePanel] retain];
78
79 m_panel.canSelectHiddenExtension = YES;
80 m_panel.level = NSModalPanelWindowLevel;
81
82 m_helper = helper;
83
84 m_nameFilterDropDownList = m_options->nameFilters();
85 QString selectedVisualNameFilter = m_options->initiallySelectedNameFilter();
86 m_selectedNameFilter = [self findStrippedFilterWithVisualFilterName:selectedVisualNameFilter];
87
88 m_panel.extensionHidden = [&]{
89 for (const auto &nameFilter : m_nameFilterDropDownList) {
90 const auto extensions = QPlatformFileDialogHelper::cleanFilterList(nameFilter);
91 for (const auto &extension : extensions) {
92 // Explicitly show extensions if we detect a filter
93 // of "all files", as clicking a single file with
94 // extensions hidden will then populate the name
95 // field with only the file name, without any
96 // extension.
97 if (extension == "*"_L1 || extension == "*.*"_L1)
98 return false;
99
100 // Explicitly show extensions if we detect a filter
101 // that has a multi-part extension. This prevents
102 // confusing situations where the user clicks e.g.
103 // 'foo.tar.gz' and 'foo.tar' is populated in the
104 // file name box, but when then clicking save macOS
105 // will warn that the file needs to end in .gz,
106 // due to thinking the user tried to save the file
107 // as a 'tar' file instead. Unfortunately this
108 // property can only be set before the panel is
109 // shown, so we can't toggle it on and off based
110 // on the active filter.
111 if (extension.count('.') > 1)
112 return false;
113 }
114 }
115 return true;
116 }();
117
118 const QFileInfo sel(selectFile);
119 if (sel.isDir() && !sel.isBundle()){
120 m_panel.directoryURL = [NSURL fileURLWithPath:sel.absoluteFilePath().toNSString()];
121 m_currentSelection.clear();
122 } else {
123 m_panel.directoryURL = [NSURL fileURLWithPath:sel.absolutePath().toNSString()];
124 m_currentSelection = sel.absoluteFilePath();
125 }
126
127 [self createPopUpButton:selectedVisualNameFilter hideDetails:options->testOption(QFileDialogOptions::HideNameFilterDetails)];
128 [self createTextField];
129 [self createAccessory];
130
131 m_panel.accessoryView = m_nameFilterDropDownList.size() > 1 ? m_accessoryView : nil;
132 // -setAccessoryView: can result in -panel:directoryDidChange:
133 // resetting our current directory. Set the delegate
134 // here to make sure it gets the correct value.
135 m_panel.delegate = self;
136
137 if (auto *openPanel = openpanel_cast(m_panel))
138 openPanel.accessoryViewDisclosed = YES;
139
140 [self updateProperties];
141 }
142 return self;
143}
144
145- (void)dealloc
146{
147 [m_panel orderOut:m_panel];
148 m_panel.accessoryView = nil;
149 [m_popupButton release];
150 [m_textField release];
151 [m_accessoryView release];
152 m_panel.delegate = nil;
153 [m_panel release];
154 [super dealloc];
155}
156
157- (bool)showPanel:(Qt::WindowModality) windowModality withParent:(QWindow *)parent
158{
159 const QFileInfo info(m_currentSelection);
160 NSString *filepath = info.filePath().toNSString();
161 NSURL *url = [NSURL fileURLWithPath:filepath isDirectory:info.isDir()];
162 bool selectable = (m_options->acceptMode() == QFileDialogOptions::AcceptSave)
163 || [self panel:m_panel shouldEnableURL:url];
164
165 if (!openpanel_cast(m_panel))
166 m_panel.nameFieldStringValue = selectable ? info.fileName().toNSString() : @"";
167
168 [self updateProperties];
169
170 auto completionHandler = ^(NSInteger result) {
171 if (m_helper)
172 m_helper->panelClosed(result);
173 };
174
175 if (windowModality == Qt::WindowModal && parent) {
176 NSView *view = reinterpret_cast<NSView*>(parent->winId());
177 [m_panel beginSheetModalForWindow:view.window completionHandler:completionHandler];
178 } else if (windowModality == Qt::ApplicationModal) {
179 return true; // Defer until exec()
180 } else {
181 [m_panel beginWithCompletionHandler:completionHandler];
182 }
183
184 return true;
185}
186
187-(void)runApplicationModalPanel
188{
189 // Note: If NSApp is not running (which is the case if e.g a top-most
190 // QEventLoop has been interrupted, and the second-most event loop has not
191 // yet been reactivated (regardless if [NSApp run] is still on the stack)),
192 // showing a native modal dialog will fail.
193 if (!m_helper)
194 return;
195
196 QMacAutoReleasePool pool;
197
198 // Call processEvents in case the event dispatcher has been interrupted, and needs to do
199 // cleanup of modal sessions. Do this before showing the native dialog, otherwise it will
200 // close down during the cleanup.
201 qApp->processEvents(QEventLoop::ExcludeUserInputEvents | QEventLoop::ExcludeSocketNotifiers);
202
203 // Make sure we don't interrupt the runModal call below.
204 QCocoaEventDispatcher::clearCurrentThreadCocoaEventDispatcherInterruptFlag();
205
206 auto result = [m_panel runModal];
207 m_helper->panelClosed(result);
208
209 // Wake up the event dispatcher so it can check whether the
210 // current event loop should continue spinning or not.
211 QCoreApplication::eventDispatcher()->wakeUp();
212}
213
214- (void)closePanel
215{
216 m_currentSelection = QString::fromNSString(m_panel.URL.path).normalized(QString::NormalizationForm_C);
217
218 if (m_panel.sheet)
219 [NSApp endSheet:m_panel];
220 else if (NSApp.modalWindow == m_panel)
221 [NSApp stopModal];
222 else
223 [m_panel close];
224}
225
226- (BOOL)panel:(id)sender shouldEnableURL:(NSURL *)url
227{
228 Q_UNUSED(sender);
229
230 NSString *filename = url.path;
231 if (!filename.length)
232 return NO;
233
234 const QFileInfo fileInfo(QString::fromNSString(filename));
235
236 // Always accept directories regardless of their names.
237 // This also includes symlinks and aliases to directories.
238 if (fileInfo.isDir()) {
239 // Unless it's a bundle, and we should treat bundles as files.
240 // FIXME: We'd like to use QFileInfo::isBundle() here, but the
241 // detection in QFileInfo goes deeper than NSWorkspace does
242 // (likely a bug), and as a result causes TCC permission
243 // dialogs to pop up when used.
244 bool treatBundlesAsFiles = !m_panel.treatsFilePackagesAsDirectories;
245 if (!(treatBundlesAsFiles && [NSWorkspace.sharedWorkspace isFilePackageAtPath:filename]))
246 return YES;
247 }
248
249 if (![self fileInfoMatchesCurrentNameFilter:fileInfo])
250 return NO;
251
252 QDir::Filters filter = m_options->filter();
253 if ((!(filter & (QDir::Dirs | QDir::AllDirs)) && fileInfo.isDir())
254 || (!(filter & QDir::Files) && (fileInfo.isFile() && !fileInfo.isSymLink()))
255 || ((filter & QDir::NoSymLinks) && fileInfo.isSymLink()))
256 return NO;
257
258 bool filterPermissions = ((filter & QDir::PermissionMask)
259 && (filter & QDir::PermissionMask) != QDir::PermissionMask);
260 if (filterPermissions) {
261 if ((!(filter & QDir::Readable) && fileInfo.isReadable())
262 || (!(filter & QDir::Writable) && fileInfo.isWritable())
263 || (!(filter & QDir::Executable) && fileInfo.isExecutable()))
264 return NO;
265 }
266
267 // We control the visibility of hidden files via the showsHiddenFiles
268 // property on the panel, based on QDir::Hidden being set. But the user
269 // can also toggle this via the Command+Shift+. keyboard shortcut,
270 // in which case they have explicitly requested to show hidden files,
271 // and we should enable them even if QDir::Hidden was not set. In
272 // effect, we don't need to filter on QDir::Hidden here.
273
274 return YES;
275}
276
277- (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError * _Nullable *)outError
278{
279 Q_ASSERT(sender == m_panel);
280
281 if (![m_panel.allowedContentTypes count] && !m_selectedNameFilter.isEmpty()) {
282 // The save panel hasn't done filtering on our behalf,
283 // either because we couldn't represent the filter via
284 // allowedContentTypes, or we opted out due to a multi part
285 // extension, so do the filtering/validation ourselves.
286 QFileInfo fileInfo(QString::fromNSString(url.path).normalized(QString::NormalizationForm_C));
287
288 if ([self fileInfoMatchesCurrentNameFilter:fileInfo])
289 return YES;
290
291 if (fileInfo.suffix().isEmpty()) {
292 // The filter requires a file name with an extension.
293 // We're going to add a default file name in selectedFiles,
294 // to match the native behavior. Check now that we can
295 // overwrite the file, if is already exists.
296 fileInfo = [self applyDefaultSuffixFromCurrentNameFilter:fileInfo];
297
298 if (!fileInfo.exists() || m_options->testOption(QFileDialogOptions::DontConfirmOverwrite))
299 return YES;
300
301 QMacAutoReleasePool pool;
302 auto *alert = [[NSAlert new] autorelease];
303 alert.alertStyle = NSAlertStyleCritical;
304
305 alert.messageText = [NSString stringWithFormat:qt_mac_AppKitString(@"SavePanel",
306 @"\\U201c%@\\U201d already exists. Do you want to replace it?"),
307 fileInfo.fileName().toNSString()];
308 alert.informativeText = [NSString stringWithFormat:qt_mac_AppKitString(@"SavePanel",
309 @"A file or folder with the same name already exists in the folder %@. "
310 "Replacing it will overwrite its current contents."),
311 fileInfo.absoluteDir().dirName().toNSString()];
312
313 auto *replaceButton = [alert addButtonWithTitle:qt_mac_AppKitString(@"SavePanel", @"Replace")];
314 replaceButton.hasDestructiveAction = YES;
315 replaceButton.tag = 1337;
316 [alert addButtonWithTitle:qt_mac_AppKitString(@"Common", @"Cancel")];
317
318 [alert beginSheetModalForWindow:m_panel
319 completionHandler:^(NSModalResponse returnCode) {
320 [NSApp stopModalWithCode:returnCode];
321 }];
322 return [NSApp runModalForWindow:alert.window] == replaceButton.tag;
323 } else {
324 QFileInfo firstFilter(m_selectedNameFilter.first());
325 auto *domain = qGuiApp->organizationDomain().toNSString();
326 *outError = [NSError errorWithDomain:domain code:0 userInfo:@{
327 NSLocalizedDescriptionKey:[NSString stringWithFormat:qt_mac_AppKitString(@"SavePanel",
328 @"You cannot save this document with extension \\U201c.%1$@\\U201d at the end "
329 "of the name. The required extension is \\U201c.%2$@\\U201d."),
330 fileInfo.completeSuffix().toNSString(), firstFilter.completeSuffix().toNSString()]
331 }];
332 return NO;
333 }
334 }
335
336 return YES;
337}
338
339- (QFileInfo)applyDefaultSuffixFromCurrentNameFilter:(const QFileInfo &)fileInfo
340{
341 QFileInfo filterInfo(m_selectedNameFilter.first());
342 return QFileInfo(fileInfo.absolutePath(),
343 fileInfo.baseName() + '.' + filterInfo.completeSuffix());
344}
345
346- (bool)fileInfoMatchesCurrentNameFilter:(const QFileInfo &)fileInfo
347{
348 // No filter means accept everything
349 if (m_selectedNameFilter.isEmpty())
350 return true;
351
352 // Check if the current file name filter accepts the file
353 for (const auto &filter : m_selectedNameFilter) {
354 if (QDir::match(filter, fileInfo.fileName()))
355 return true;
356 }
357
358 return false;
359}
360
361- (void)setNameFilters:(const QStringList &)filters hideDetails:(BOOL)hideDetails
362{
363 [m_popupButton removeAllItems];
364 m_nameFilterDropDownList = filters;
365 if (filters.size() > 0){
366 for (int i = 0; i < filters.size(); ++i) {
367 const QString filter = hideDetails ? [self removeExtensions:filters.at(i)] : filters.at(i);
368 [m_popupButton.menu addItemWithTitle:filter.toNSString() action:nil keyEquivalent:@""];
369 }
370 [m_popupButton selectItemAtIndex:0];
371 m_panel.accessoryView = m_accessoryView;
372 } else {
373 m_panel.accessoryView = nil;
374 }
375
376 [self filterChanged:self];
377}
378
379- (void)filterChanged:(id)sender
380{
381 // This m_delegate function is called when the _name_ filter changes.
382 Q_UNUSED(sender);
383 if (!m_helper)
384 return;
385 const QString selection = m_nameFilterDropDownList.value([m_popupButton indexOfSelectedItem]);
386 m_selectedNameFilter = [self findStrippedFilterWithVisualFilterName:selection];
387 [m_panel validateVisibleColumns];
388 [self updateProperties];
389
390 const QStringList filters = m_options->nameFilters();
391 const int menuIndex = m_popupButton.indexOfSelectedItem;
392 emit m_helper->filterSelected(menuIndex >= 0 && menuIndex < filters.size() ? filters.at(menuIndex) : QString());
393}
394
395- (QList<QUrl>)selectedFiles
396{
397 if (auto *openPanel = openpanel_cast(m_panel)) {
398 QList<QUrl> result;
399 for (NSURL *url in openPanel.URLs)
400 result << qt_apple_urlFromPossiblySecurityScopedURL(url);
401 return result;
402 } else {
403 QUrl result = qt_apple_urlFromPossiblySecurityScopedURL(m_panel.URL);
404 if (qt_apple_isSandboxed())
405 return { result }; // Can't tweak suffix
406
407 QFileInfo fileInfo(result.toLocalFile());
408
409 if (fileInfo.suffix().isEmpty() && ![self fileInfoMatchesCurrentNameFilter:fileInfo]) {
410 // We end up in this situation if we accept a file name without extension
411 // in panel:validateURL:error. If so, we match the behavior of the native
412 // save dialog and add the first of the accepted extension from the filter.
413 fileInfo = [self applyDefaultSuffixFromCurrentNameFilter:fileInfo];
414 }
415
416 // If neither the user or the NSSavePanel have provided a suffix, use
417 // the default suffix (if it exists).
418 const QString defaultSuffix = m_options->defaultSuffix();
419 if (fileInfo.suffix().isEmpty() && !defaultSuffix.isEmpty()) {
420 fileInfo.setFile(fileInfo.absolutePath(),
421 fileInfo.baseName() + '.' + defaultSuffix);
422 }
423
424 return { QUrl::fromLocalFile(fileInfo.filePath()) };
425 }
426}
427
428- (void)updateProperties
429{
430 const QFileDialogOptions::FileMode fileMode = m_options->fileMode();
431 bool chooseFilesOnly = fileMode == QFileDialogOptions::ExistingFile
432 || fileMode == QFileDialogOptions::ExistingFiles;
433 bool chooseDirsOnly = fileMode == QFileDialogOptions::Directory
434 || fileMode == QFileDialogOptions::DirectoryOnly
435 || m_options->testOption(QFileDialogOptions::ShowDirsOnly);
436
437 m_panel.title = m_options->windowTitle().toNSString();
438 m_panel.canCreateDirectories = !(m_options->testOption(QFileDialogOptions::ReadOnly));
439
440 if (m_options->isLabelExplicitlySet(QFileDialogOptions::Accept))
441 m_panel.prompt = strippedText(m_options->labelText(QFileDialogOptions::Accept));
442 if (m_options->isLabelExplicitlySet(QFileDialogOptions::FileName))
443 m_panel.nameFieldLabel = strippedText(m_options->labelText(QFileDialogOptions::FileName));
444
445 if (auto *openPanel = openpanel_cast(m_panel)) {
446 openPanel.canChooseFiles = !chooseDirsOnly;
447 openPanel.canChooseDirectories = !chooseFilesOnly;
448 openPanel.allowsMultipleSelection = (fileMode == QFileDialogOptions::ExistingFiles);
449 openPanel.resolvesAliases = !(m_options->testOption(QFileDialogOptions::DontResolveSymlinks));
450 }
451
452 m_popupButton.hidden = chooseDirsOnly; // TODO hide the whole sunken pane instead?
453
454 m_panel.allowedContentTypes = [self computeAllowedContentTypes];
455
456 // Setting allowedContentTypes to @[] is not enough to reset any
457 // automatically added extension based on a previous filter.
458 // This is problematic because extensions can in some cases
459 // be hidden from the user, resulting in confusion when the
460 // resulting file name doesn't match the current empty filter.
461 // We work around this by temporarily resetting the allowed
462 // content type to one without an extension, which forces
463 // the save panel to update and remove the extension.
464 const bool nameFieldHasExtension = m_panel.nameFieldStringValue.pathExtension.length > 0;
465 if (![m_panel.allowedContentTypes count] && !nameFieldHasExtension && !openpanel_cast(m_panel)) {
466 if (!UTTypeDirectory.preferredFilenameExtension) {
467 m_panel.allowedContentTypes = @[ UTTypeDirectory ];
468 m_panel.allowedContentTypes = @[];
469 } else {
470 qWarning() << "UTTypeDirectory unexpectedly reported an extension";
471 }
472 }
473
474 m_panel.showsHiddenFiles = m_options->filter().testFlag(QDir::Hidden);
475
476 if (m_panel.visible)
477 [m_panel validateVisibleColumns];
478}
479
480- (void)panelSelectionDidChange:(id)sender
481{
482 Q_UNUSED(sender);
483
484 if (!m_helper)
485 return;
486
487 // Save panels only allow you to select directories, which
488 // means currentChanged will only be emitted when selecting
489 // a directory, and if so, with the latest chosen file name,
490 // which is confusing and inconsistent. We choose to bail
491 // out entirely for save panels, to give consistent behavior.
492 if (!openpanel_cast(m_panel))
493 return;
494
495 if (m_panel.visible) {
496 const QString selection = QString::fromNSString(m_panel.URL.path).normalized(QString::NormalizationForm_C);
497 if (selection != m_currentSelection) {
498 m_currentSelection = selection;
499 emit m_helper->currentChanged(QUrl::fromLocalFile(selection));
500 }
501 }
502}
503
504- (void)panel:(id)sender directoryDidChange:(NSString *)path
505{
506 Q_UNUSED(sender);
507
508 if (!m_helper)
509 return;
510
511 m_helper->panelDirectoryDidChange(path);
512}
513
514/*
515 Computes a list of UTTypes ("public.plain-text" e.g.)
516 for the current name filter.
517
518 If a filter do not conform to the format *.xyz or * or *.*,
519 or contains an extensions with more than one part (e.g. "tar.gz")
520 we treat that as allowing all file types, and do our own
521 validation in panel:validateURL:error.
522*/
523- (NSArray<UTType*>*)computeAllowedContentTypes
524{
525 if (m_options->acceptMode() != QFileDialogOptions::AcceptSave)
526 return @[]; // panel:shouldEnableURL: does the file filtering for NSOpenPanel
527
528 auto *types = [[NSMutableArray<UTType*> new] autorelease];
529 for (const QString &filter : std::as_const(m_selectedNameFilter)) {
530 if (!filter.startsWith("*."_L1))
531 continue;
532
533 if (filter.contains(u'?'))
534 continue;
535
536 if (filter.count(u'*') != 1)
537 continue;
538
539 auto extensions = filter.split('.', Qt::SkipEmptyParts);
540 if (extensions.count() > 2)
541 return @[];
542
543 auto *utType = [UTType typeWithFilenameExtension:extensions.last().toNSString()];
544 [types addObject:utType];
545 }
546
547 return types;
548}
549
550- (QString)removeExtensions:(const QString &)filter
551{
552 QRegularExpression regExp(QString::fromLatin1(QPlatformFileDialogHelper::filterRegExp));
553 QRegularExpressionMatch match = regExp.match(filter);
554 if (match.hasMatch())
555 return match.captured(1).trimmed();
556 return filter;
557}
558
559- (void)createTextField
560{
561 NSRect textRect = { { 0.0, 3.0 }, { 100.0, 25.0 } };
562 m_textField = [[NSTextField alloc] initWithFrame:textRect];
563 m_textField.cell.font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSControlSizeRegular]];
564 m_textField.alignment = NSTextAlignmentRight;
565 m_textField.editable = false;
566 m_textField.selectable = false;
567 m_textField.bordered = false;
568 m_textField.drawsBackground = false;
569 if (m_options->isLabelExplicitlySet(QFileDialogOptions::FileType))
570 m_textField.stringValue = strippedText(m_options->labelText(QFileDialogOptions::FileType));
571}
572
573- (void)createPopUpButton:(const QString &)selectedFilter hideDetails:(BOOL)hideDetails
574{
575 NSRect popUpRect = { { 100.0, 5.0 }, { 250.0, 25.0 } };
576 m_popupButton = [[NSPopUpButton alloc] initWithFrame:popUpRect pullsDown:NO];
577 m_popupButton.target = self;
578 m_popupButton.action = @selector(filterChanged:);
579
580 if (!m_nameFilterDropDownList.isEmpty()) {
581 int filterToUse = -1;
582 for (int i = 0; i < m_nameFilterDropDownList.size(); ++i) {
583 const QString currentFilter = m_nameFilterDropDownList.at(i);
584 if (selectedFilter == currentFilter ||
585 (filterToUse == -1 && currentFilter.startsWith(selectedFilter)))
586 filterToUse = i;
587 QString filter = hideDetails ? [self removeExtensions:currentFilter] : currentFilter;
588 [m_popupButton.menu addItemWithTitle:filter.toNSString() action:nil keyEquivalent:@""];
589 }
590 if (filterToUse != -1)
591 [m_popupButton selectItemAtIndex:filterToUse];
592 }
593}
594
595- (QStringList) findStrippedFilterWithVisualFilterName:(QString)name
596{
597 for (const QString &currentFilter : std::as_const(m_nameFilterDropDownList)) {
598 if (currentFilter.startsWith(name))
599 return QPlatformFileDialogHelper::cleanFilterList(currentFilter);
600 }
601 return QStringList();
602}
603
604- (void)createAccessory
605{
606 NSRect accessoryRect = { { 0.0, 0.0 }, { 450.0, 33.0 } };
607 m_accessoryView = [[NSView alloc] initWithFrame:accessoryRect];
608 [m_accessoryView addSubview:m_textField];
609 [m_accessoryView addSubview:m_popupButton];
610}
611
612@end
613
614QT_BEGIN_NAMESPACE
615
616QCocoaFileDialogHelper::QCocoaFileDialogHelper()
617{
618}
619
621{
622 if (!m_delegate)
623 return;
624
625 QMacAutoReleasePool pool;
626 [m_delegate release];
627 m_delegate = nil;
628}
629
630void QCocoaFileDialogHelper::panelClosed(NSInteger result)
631{
632 if (result == NSModalResponseOK)
633 emit accept();
634 else
635 emit reject();
636}
637
638void QCocoaFileDialogHelper::setDirectory(const QUrl &directory)
639{
640 m_directory = directory;
641
642 if (m_delegate)
643 m_delegate->m_panel.directoryURL = [NSURL fileURLWithPath:directory.toLocalFile().toNSString()];
644}
645
647{
648 return m_directory;
649}
650
652{
653 if (!path || [path isEqual:NSNull.null] || !path.length)
654 return;
655
656 const auto oldDirectory = m_directory;
657 m_directory = QUrl::fromLocalFile(
658 QString::fromNSString(path).normalized(QString::NormalizationForm_C));
659
660 if (m_directory != oldDirectory) {
661 // FIXME: Plumb old directory back to QFileDialog's lastVisitedDir?
662 emit directoryEntered(m_directory);
663 }
664}
665
666void QCocoaFileDialogHelper::selectFile(const QUrl &filename)
667{
668 QString filePath = filename.toLocalFile();
669 if (QDir::isRelativePath(filePath))
670 filePath = QFileInfo(directory().toLocalFile(), filePath).filePath();
671
672 // There seems to no way to select a file once the dialog is running.
673 // So do the next best thing, set the file's directory:
674 setDirectory(QFileInfo(filePath).absolutePath());
675}
676
678{
679 if (m_delegate)
680 return [m_delegate selectedFiles];
681 return QList<QUrl>();
682}
683
685{
686 if (!m_delegate)
687 return;
688
689 [m_delegate updateProperties];
690}
691
692void QCocoaFileDialogHelper::selectNameFilter(const QString &filter)
693{
694 if (!options())
695 return;
696 const int index = options()->nameFilters().indexOf(filter);
697 if (index != -1) {
698 if (!m_delegate) {
699 options()->setInitiallySelectedNameFilter(filter);
700 return;
701 }
702 [m_delegate->m_popupButton selectItemAtIndex:index];
703 [m_delegate filterChanged:nil];
704 }
705}
706
708{
709 if (!m_delegate)
710 return options()->initiallySelectedNameFilter();
711 int index = [m_delegate->m_popupButton indexOfSelectedItem];
712 if (index >= options()->nameFilters().count())
713 return QString();
714 return index != -1 ? options()->nameFilters().at(index) : QString();
715}
716
718{
719 if (!m_delegate)
720 return;
721
722 [m_delegate closePanel];
723
724 if (m_eventLoop)
725 m_eventLoop->exit();
726}
727
728bool QCocoaFileDialogHelper::show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent)
729{
730 if (windowFlags & Qt::WindowStaysOnTopHint) {
731 // The native file dialog tries all it can to stay
732 // on the NSModalPanel level. And it might also show
733 // its own "create directory" dialog that we cannot control.
734 // So we need to use the non-native version in this case...
735 return false;
736 }
737
738 if (qt_apple_isSandboxed()) {
739 static bool canRead = qt_mac_processHasEntitlement(
740 u"com.apple.security.files.user-selected.read-only"_s);
741 static bool canReadWrite = qt_mac_processHasEntitlement(
742 u"com.apple.security.files.user-selected.read-write"_s);
743
744 if (options()->acceptMode() == QFileDialogOptions::AcceptSave
745 && !canReadWrite) {
746 qWarning() << "Sandboxed application is missing user-selected files"
747 << "read-write entitlement. Falling back to non-native dialog";
748 return false;
749 }
750
751 if (!canReadWrite && !canRead) {
752 qWarning() << "Sandboxed application is missing user-selected files"
753 << "entitlement. Falling back to non-native dialog";
754 return false;
755 }
756 }
757
758 createNSOpenSavePanelDelegate();
759
760 return [m_delegate showPanel:windowModality withParent:parent];
761}
762
763void QCocoaFileDialogHelper::createNSOpenSavePanelDelegate()
764{
765 QMacAutoReleasePool pool;
766
767 const SharedPointerFileDialogOptions &opts = options();
768 const QList<QUrl> selectedFiles = opts->initiallySelectedFiles();
769 const QUrl directory = m_directory.isEmpty() ? opts->initialDirectory() : m_directory;
770 const bool selectDir = selectedFiles.isEmpty();
771 QString selection(selectDir ? directory.toLocalFile() : selectedFiles.front().toLocalFile());
772 QNSOpenSavePanelDelegate *delegate = [[QNSOpenSavePanelDelegate alloc]
773 initWithAcceptMode:
774 selection
775 options:opts
776 helper:this];
777
778 [static_cast<QNSOpenSavePanelDelegate *>(m_delegate) release];
779 m_delegate = delegate;
780}
781
783{
784 Q_ASSERT(m_delegate);
785
786 if (m_delegate->m_panel.visible) {
787 // WindowModal or NonModal, so already shown above
788 QEventLoop eventLoop;
789 m_eventLoop = &eventLoop;
790 eventLoop.exec(QEventLoop::DialogExec);
791 m_eventLoop = nullptr;
792 } else {
793 // ApplicationModal, so show and block using native APIs
794 [m_delegate runApplicationModalPanel];
795 }
796}
797
799{
800 return true;
801}
802
803QT_END_NAMESPACE
QList< QUrl > selectedFiles() const override
bool defaultNameFilterDisables() const override
QString selectedNameFilter() const override
void panelDirectoryDidChange(NSString *path)
QUrl directory() const override
void setDirectory(const QUrl &directory) override
bool show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow *parent) override
void selectNameFilter(const QString &filter) override
void selectFile(const QUrl &filename) override
void panelClosed(NSInteger result)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:177
QSharedPointer< QFileDialogOptions > SharedPointerFileDialogOptions
static NSString * strippedText(QString s)
static NSOpenPanel * openpanel_cast(NSSavePanel *panel)
QList< QString > QStringList
Constructs a string list that contains the given string, str.