Bug 426680 - Set allowedFileTypes on NSSavePanel so the file extension isn't selected. r=mstange.
authorNate Weaver <wevah@derailer.org>
Fri, 22 Jul 2016 08:24:02 +0200
changeset 346260 666ed766fc728df065573de3efb46721eec1edc4
parent 346259 6cd25af34d5cacb3fec462f09f5a24abcb2ddd18
child 346261 d14e19de0a1ecb1d89c0f6e2fb2c9a9651695343
push id6389
push userraliiev@mozilla.com
push dateMon, 19 Sep 2016 13:38:22 +0000
treeherdermozilla-beta@01d67bfe6c81 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmstange
bugs426680
milestone50.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 426680 - Set allowedFileTypes on NSSavePanel so the file extension isn't selected. r=mstange.
widget/cocoa/nsFilePicker.mm
--- a/widget/cocoa/nsFilePicker.mm
+++ b/widget/cocoa/nsFilePicker.mm
@@ -453,16 +453,31 @@ nsFilePicker::PutLocalFile(const nsStrin
 
   // set up accessory view for file format options
   NSView* accessoryView = GetAccessoryView();
   [thePanel setAccessoryView:accessoryView];
 
   // 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)
+  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];
+    }
+  }
+
   // set up default directory
   NSString *theDir = PanelDefaultDirectory();
   if (theDir) {
     [thePanel setDirectoryURL:[NSURL fileURLWithPath:theDir isDirectory:YES]];
   }
 
   // load the panel
   nsCocoaUtils::PrepareForNativeAppModalDialog();