Bug 1321321 - Ensure that we always save files with the given extension / Work-around NSSavePanel limitations when saving files with multiple extensions. r=mstange.

This commit is contained in:
stefanh@inbox.com 2017-02-25 00:34:46 +01:00
Родитель 071e150be8
Коммит 4db4fecd9e
1 изменённых файлов: 26 добавлений и 11 удалений

Просмотреть файл

@ -458,23 +458,38 @@ nsFilePicker::PutLocalFile(const nsString& inTitle, const nsString& inDefaultNam
// set up default file name
NSString* defaultFilename = [NSString stringWithCharacters:(const unichar*)inDefaultName.get() length:inDefaultName.Length()];
// set up allowed types; this prevents the extension from being selected
// use the UTI for the file type to allow alternate extensions (e.g., jpg vs. jpeg)
// Set up the allowed type. This prevents the extension from being selected.
NSString* extension = defaultFilename.pathExtension;
if (extension.length != 0) {
CFStringRef type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)extension, NULL);
if (type) {
thePanel.allowedFileTypes = @[(NSString*)type];
CFRelease(type);
} else {
// if there's no UTI for the file extension, use the extension itself.
thePanel.allowedFileTypes = @[extension];
}
thePanel.allowedFileTypes = @[extension];
}
// Allow users to change the extension.
thePanel.allowsOtherFileTypes = YES;
// If extensions are hidden and were saving a file with multiple extensions,
// only the last extension will be hidden in the panel (".tar.gz" will become
// ".tar"). If the remaining extension is known, the OS will think that we're
// trying to add a non-default extension. To avoid the confusion, we ensure
// that all extensions are shown in the panel if the remaining extension is
// known by the OS.
NSString* fileName =
[[defaultFilename lastPathComponent] stringByDeletingPathExtension];
NSString* otherExtension = fileName.pathExtension;
if (otherExtension.length != 0) {
// There's another extension here. Get the UTI.
CFStringRef type =
UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
(CFStringRef)otherExtension, NULL);
if (type) {
if (!CFStringHasPrefix(type, CFSTR("dyn."))) {
// We have a UTI, otherwise the type would have a "dyn." prefix. Ensure
// extensions are shown in the panel.
[thePanel setExtensionHidden:NO];
}
CFRelease(type);
}
}
// set up default directory
NSString *theDir = PanelDefaultDirectory();
if (theDir) {