summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/cocoa
diff options
context:
space:
mode:
authorTor Arne Vestbø <tor.arne.vestbo@qt.io>2024-01-05 14:32:30 +0100
committerTor Arne Vestbø <tor.arne.vestbo@qt.io>2024-01-17 14:23:09 +0100
commitd651cdb9885353af1ea0647733f0c002bf2d168b (patch)
tree30b5bf7bb19b4214c9b45fdfe2375502468450af /src/plugins/platforms/cocoa
parent40506954979fa7cdd34da8251f179557eb9be4f3 (diff)
macOS: Work around lack of native support for multi-part extensions
Multi-part extensions such as "tar.gz" are not natively supported by macOS, e.g. one can not create a [UTType typeWithFilenameExtension:] for such an extension, and this goes all the way down to Foundation. As a result NSSavePanel gets confused when assigning a multi-part extension to allowedFileTypes, because it's using NSString operations such as stringByDeletingPathExtension or pathExtension, which also lack support for multi-part extensions. We've worked around this in the past by reducing these extensions to their last component, e.g. "tar.gz" reduced to "gz", but this results in the save panel turning the input file name "foo" into "foo.gz" if the user doesn't provide the full file name. Various attempts at working around the lack of multi-part extension support by breaking allowedFileTypes into ["tar.gz", "gz"], or doing selectively toggling of allowedFileTypes in panel:validateURL:error, have all proved to have corner cases and shortcomings of their own. As a last resort, we now treat multi-part extensions manually, by disabling the allowedFileTypes filter, and doing our own validation in the panel:validateURL:error callback. This requires us to also manually handle automatic extension for file names without extensions, as well as overwrite confirmation in the cases we do add an extension manually. The overwrite dialog and error messages for incompatible extensions have been modeled after their native macOS 14 counterparts, using translated strings from AppKit. Task-number: QTBUG-109877 Pick-to: 6.7 Change-Id: I6b7ce3c44b4c3b24802aa1f66f4593457ae4c929 Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Diffstat (limited to 'src/plugins/platforms/cocoa')
-rw-r--r--src/plugins/platforms/cocoa/qcocoafiledialoghelper.mm126
1 files changed, 105 insertions, 21 deletions
diff --git a/src/plugins/platforms/cocoa/qcocoafiledialoghelper.mm b/src/plugins/platforms/cocoa/qcocoafiledialoghelper.mm
index f4ff5e85f8..044a282686 100644
--- a/src/plugins/platforms/cocoa/qcocoafiledialoghelper.mm
+++ b/src/plugins/platforms/cocoa/qcocoafiledialoghelper.mm
@@ -242,15 +242,7 @@ typedef QSharedPointer<QFileDialogOptions> SharedPointerFileDialogOptions;
return YES;
}
- const QString qtFileName = fileInfo.fileName();
- // No filter means accept everything
- bool nameMatches = m_selectedNameFilter.isEmpty();
- // Check if the current file name filter accepts the file:
- for (int i = 0; !nameMatches && i < m_selectedNameFilter.size(); ++i) {
- if (QDir::match(m_selectedNameFilter.at(i), qtFileName))
- nameMatches = true;
- }
- if (!nameMatches)
+ if (![self fileInfoMatchesCurrentNameFilter:fileInfo])
return NO;
QDir::Filters filter = m_options->filter();
@@ -278,6 +270,90 @@ typedef QSharedPointer<QFileDialogOptions> SharedPointerFileDialogOptions;
return YES;
}
+- (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError * _Nullable *)outError
+{
+ Q_ASSERT(sender == m_panel);
+
+ if (!m_panel.allowedFileTypes && !m_selectedNameFilter.isEmpty()) {
+ // The save panel hasn't done filtering on our behalf,
+ // either because we couldn't represent the filter via
+ // allowedFileTypes, or we opted out due to a multi part
+ // extension, so do the filtering/validation ourselves.
+ QFileInfo fileInfo(QString::fromNSString(url.path).normalized(QString::NormalizationForm_C));
+
+ if ([self fileInfoMatchesCurrentNameFilter:fileInfo])
+ return YES;
+
+ if (fileInfo.suffix().isEmpty()) {
+ // The filter requires a file name with an extension.
+ // We're going to add a default file name in selectedFiles,
+ // to match the native behavior. Check now that we can
+ // overwrite the file, if is already exists.
+ fileInfo = [self applyDefaultSuffixFromCurrentNameFilter:fileInfo];
+
+ if (!fileInfo.exists() || m_options->testOption(QFileDialogOptions::DontConfirmOverwrite))
+ return YES;
+
+ QMacAutoReleasePool pool;
+ auto *alert = [[NSAlert new] autorelease];
+ alert.alertStyle = NSAlertStyleCritical;
+
+ alert.messageText = [NSString stringWithFormat:qt_mac_AppKitString(@"SavePanel",
+ @"\\U201c%@\\U201d already exists. Do you want to replace it?"),
+ fileInfo.fileName().toNSString()];
+ alert.informativeText = [NSString stringWithFormat:qt_mac_AppKitString(@"SavePanel",
+ @"A file or folder with the same name already exists in the folder %@. "
+ "Replacing it will overwrite its current contents."),
+ fileInfo.absoluteDir().dirName().toNSString()];
+
+ auto *replaceButton = [alert addButtonWithTitle:qt_mac_AppKitString(@"SavePanel", @"Replace")];
+ replaceButton.hasDestructiveAction = YES;
+ replaceButton.tag = 1337;
+ [alert addButtonWithTitle:qt_mac_AppKitString(@"Common", @"Cancel")];
+
+ [alert beginSheetModalForWindow:m_panel
+ completionHandler:^(NSModalResponse returnCode) {
+ [NSApp stopModalWithCode:returnCode];
+ }];
+ return [NSApp runModalForWindow:alert.window] == replaceButton.tag;
+ } else {
+ QFileInfo firstFilter(m_selectedNameFilter.first());
+ auto *domain = qGuiApp->organizationDomain().toNSString();
+ *outError = [NSError errorWithDomain:domain code:0 userInfo:@{
+ NSLocalizedDescriptionKey:[NSString stringWithFormat:qt_mac_AppKitString(@"SavePanel",
+ @"You cannot save this document with extension \\U201c.%1$@\\U201d at the end "
+ "of the name. The required extension is \\U201c.%2$@\\U201d."),
+ fileInfo.completeSuffix().toNSString(), firstFilter.completeSuffix().toNSString()]
+ }];
+ return NO;
+ }
+ }
+
+ return YES;
+}
+
+- (QFileInfo)applyDefaultSuffixFromCurrentNameFilter:(const QFileInfo &)fileInfo
+{
+ QFileInfo filterInfo(m_selectedNameFilter.first());
+ return QFileInfo(fileInfo.absolutePath(),
+ fileInfo.baseName() + '.' + filterInfo.completeSuffix());
+}
+
+- (bool)fileInfoMatchesCurrentNameFilter:(const QFileInfo &)fileInfo
+{
+ // No filter means accept everything
+ if (m_selectedNameFilter.isEmpty())
+ return true;
+
+ // Check if the current file name filter accepts the file
+ for (const auto &filter : m_selectedNameFilter) {
+ if (QDir::match(filter, fileInfo.fileName()))
+ return true;
+ }
+
+ return false;
+}
+
- (void)setNameFilters:(const QStringList &)filters hideDetails:(BOOL)hideDetails
{
[m_popupButton removeAllItems];
@@ -322,18 +398,25 @@ typedef QSharedPointer<QFileDialogOptions> SharedPointerFileDialogOptions;
}
return result;
} else {
- QList<QUrl> result;
QString filename = QString::fromNSString(m_panel.URL.path).normalized(QString::NormalizationForm_C);
- const QString defaultSuffix = m_options->defaultSuffix();
- const QFileInfo fileInfo(filename);
+ QFileInfo fileInfo(filename);
+
+ if (fileInfo.suffix().isEmpty() && ![self fileInfoMatchesCurrentNameFilter:fileInfo]) {
+ // We end up in this situation if we accept a file name without extension
+ // in panel:validateURL:error. If so, we match the behavior of the native
+ // save dialog and add the first of the accepted extension from the filter.
+ fileInfo = [self applyDefaultSuffixFromCurrentNameFilter:fileInfo];
+ }
// If neither the user or the NSSavePanel have provided a suffix, use
// the default suffix (if it exists).
- if (fileInfo.suffix().isEmpty() && !defaultSuffix.isEmpty())
- filename.append('.').append(defaultSuffix);
+ const QString defaultSuffix = m_options->defaultSuffix();
+ if (fileInfo.suffix().isEmpty() && !defaultSuffix.isEmpty()) {
+ fileInfo.setFile(fileInfo.absolutePath(),
+ fileInfo.baseName() + '.' + defaultSuffix);
+ }
- result << QUrl::fromLocalFile(filename);
- return result;
+ return { QUrl::fromLocalFile(fileInfo.filePath()) };
}
}
@@ -428,11 +511,9 @@ typedef QSharedPointer<QFileDialogOptions> SharedPointerFileDialogOptions;
for the current name filter, and updates the save panel.
If a filter do not conform to the format *.xyz or * or *.*,
- all files types are allowed.
-
- Extensions with more than one part (e.g. "tar.gz") are
- reduced to their final part, as NSSavePanel does not deal
- well with multi-part extensions.
+ or contains an extensions with more than one part (e.g. "tar.gz")
+ we treat that as allowing all file types, and do our own
+ validation in panel:validateURL:error.
*/
- (NSArray<NSString*>*)computeAllowedFileTypes
{
@@ -451,6 +532,9 @@ typedef QSharedPointer<QFileDialogOptions> SharedPointerFileDialogOptions;
continue;
auto extensions = filter.split('.', Qt::SkipEmptyParts);
+ if (extensions.count() > 2)
+ return nil;
+
fileTypes += extensions.last();
}