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