make filepicker remember last type selected. b=417910 r=smorgan sr=roc a=beltzner
authorjoshmoz@gmail.com
Thu, 28 Feb 2008 21:36:35 -0800
changeset 12428 48375a18b29f629e83d51a96df0044fa28e7e9ae
parent 12427 2774b727a51a4dfec00d9de7d65a1d9b1ee5ccf5
child 12429 5a23828ebfc65bb188cf67cc456733287f204c06
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmorgan, roc, beltzner
bugs417910
milestone1.9b4pre
make filepicker remember last type selected. b=417910 r=smorgan sr=roc a=beltzner
widget/src/cocoa/nsFilePicker.h
widget/src/cocoa/nsFilePicker.mm
--- a/widget/src/cocoa/nsFilePicker.h
+++ b/widget/src/cocoa/nsFilePicker.h
@@ -91,23 +91,20 @@ protected:
   PRInt16 GetLocalFolder(const nsString& inTitle, nsILocalFile** outFile);
   PRInt16 PutLocalFile(const nsString& inTitle, const nsString& inDefaultName, nsILocalFile** outFile);
 
   NSArray  *GenerateFilterList();
   void     SetDialogTitle(const nsString& inTitle, id aDialog);
   NSString *PanelDefaultDirectory();
   NSView* GetAccessoryView();
                                                 
-  PRBool                 mAllFilesDisplayed;
   nsString               mTitle;
   PRInt16                mMode;
   nsCOMArray<nsILocalFile> mFiles;
   nsString               mDefault;
 
   nsStringArray          mFilters; 
   nsStringArray          mTitles;
-  
-  PRInt32                mSelectedType;  //this is in some NS_IMETHODIMP, but otherwise unsed.
-  static OSType          sCurrentProcessSignature;
 
+  PRInt32                mSelectedTypeIndex;
 };
 
 #endif // nsFilePicker_h_
--- a/widget/src/cocoa/nsFilePicker.mm
+++ b/widget/src/cocoa/nsFilePicker.mm
@@ -35,74 +35,68 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
-// Arrgh.  Bitten by bug 154232 here.  
-// Need to undef DARWIN before including Cocoa & (by default) CoreFoundation
-// so we can use CFURLGetFSRef.
+#import <Cocoa/Cocoa.h>
 
-#undef DARWIN
-#import <Cocoa/Cocoa.h>
-#define DARWIN  
-
+#include "nsFilePicker.h"
 #include "nsObjCExceptions.h"
 #include "nsCOMPtr.h"
 #include "nsReadableUtils.h"
 #include "nsNetUtil.h"
 #include "nsIComponentManager.h"
 #include "nsILocalFile.h"
 #include "nsILocalFileMac.h"
 #include "nsIURL.h"
 #include "nsArrayEnumerator.h"
 #include "nsIStringBundle.h"
-
-#include "nsFilePicker.h"
+#include "nsIPrefService.h"
+#include "nsIPrefBranch.h"
 
 const float kAccessoryViewPadding = 5;
 const int   kSaveTypeControlTag = 1;
+const char  kLastTypeIndexPref[] = "filepicker.lastTypeIndex";
 
 NS_IMPL_ISUPPORTS1(nsFilePicker, nsIFilePicker)
 
-//-------------------------------------------------------------------------
-//
-// nsFilePicker constructor
-//
-//-------------------------------------------------------------------------
+
 nsFilePicker::nsFilePicker()
-: mAllFilesDisplayed(PR_TRUE)
-, mMode(0)
-, mSelectedType(0)
+: mMode(0)
+, mSelectedTypeIndex(0)
 {
 }
 
 
-//-------------------------------------------------------------------------
-//
-// nsFilePicker destructor
-//
-//-------------------------------------------------------------------------
 nsFilePicker::~nsFilePicker()
 {
-  // string arrays clean themselves up
 }
 
 
 void
 nsFilePicker::InitNative(nsIWidget *aParent, const nsAString& aTitle,
                          PRInt16 aMode)
 {
   mTitle = aTitle;
   mMode = aMode;
+
+  // read in initial type index from prefs
+  nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
+  if (prefs) {
+    int prefIndex;
+    if (NS_SUCCEEDED(prefs->GetIntPref(kLastTypeIndexPref, &prefIndex)))
+      mSelectedTypeIndex = prefIndex;
+  }
 }
 
+
 NSView* nsFilePicker::GetAccessoryView()
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
 
   NSView* accessoryView = [[[NSView alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)] autorelease];
 
   // get the localized string for "Save As:"
   NSString* saveAsLabel = @"Save As:"; // backup in case we can't get a localized string
@@ -141,16 +135,18 @@ NSView* nsFilePicker::GetAccessoryView()
     }
     else {
       titleString = [[NSString alloc] initWithCharacters:currentTitle.get()
                                                   length:currentTitle.Length()];
     }
     [popupButton addItemWithTitle:titleString];
     [titleString release];
   }
+  if (mSelectedTypeIndex >= 0 && mSelectedTypeIndex < numMenuItems)
+    [popupButton selectItemAtIndex:mSelectedTypeIndex];
   [popupButton setTag:kSaveTypeControlTag];
   [popupButton sizeToFit]; // we have to do sizeToFit to get the height calculated for us
   // This is just a default width that works well, doesn't truncate the vast majority of
   // things that might end up in the menu.
   [popupButton setFrameSize:NSMakeSize(180, [popupButton frame].size.height)];
 
   // position everything based on control sizes with kAccessoryViewPadding pix padding
   // on each side kAccessoryViewPadding pix horizontal padding between controls
@@ -170,21 +166,18 @@ NSView* nsFilePicker::GetAccessoryView()
 
   [accessoryView addSubview:textField];
   [accessoryView addSubview:popupButton];
   return accessoryView;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
 }
 
-//-------------------------------------------------------------------------
-//
-// Show - Display the file dialog
-//
-//-------------------------------------------------------------------------
+
+// Display the file dialog
 NS_IMETHODIMP nsFilePicker::Show(PRInt16 *retval)
 {
   NS_ENSURE_ARG_POINTER(retval);
 
   *retval = returnCancel;
 
   PRInt16 userClicksOK = returnCancel;
 
@@ -227,23 +220,17 @@ NS_IMETHODIMP nsFilePicker::Show(PRInt16
   if (theFile)
     mFiles.AppendObject(theFile);
   
   *retval = userClicksOK;
   return NS_OK;
 }
 
 
-//-------------------------------------------------------------------------
-//
-// GetLocalFiles
-//
 // Use OpenPanel to do a GetFile. Returns |returnOK| if the user presses OK in the dialog. 
-//
-//-------------------------------------------------------------------------
 PRInt16
 nsFilePicker::GetLocalFiles(const nsString& inTitle, PRBool inAllowMultiple, nsCOMArray<nsILocalFile>& outFiles)
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
 
   PRInt16 retVal = (PRInt16)returnCancel;
   NSOpenPanel *thePanel = [NSOpenPanel openPanel];
 
@@ -291,26 +278,20 @@ nsFilePicker::GetLocalFiles(const nsStri
   }
 
   if (outFiles.Count() > 0)
     retVal = returnOK;
 
   return retVal;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(0);
-} // GetFiles
+}
 
 
-//-------------------------------------------------------------------------
-//
-// GetLocalFolder
-//
 // Use OpenPanel to do a GetFolder. Returns |returnOK| if the user presses OK in the dialog.
-//
-//-------------------------------------------------------------------------
 PRInt16
 nsFilePicker::GetLocalFolder(const nsString& inTitle, nsILocalFile** outFile)
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
 
   NS_ENSURE_ARG(outFile);
   *outFile = nsnull;
   
@@ -347,23 +328,20 @@ nsFilePicker::GetLocalFolder(const nsStr
       NS_ADDREF(*outFile);
       retVal = returnOK;
     }
   }
 
   return retVal;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(0);
-} // GetFolder
+}
 
-//-------------------------------------------------------------------------
-//
-// PutLocalFile. Returns |returnOK| if the user presses OK in the dialog.
-//
-//-------------------------------------------------------------------------
+
+// Returns |returnOK| if the user presses OK in the dialog.
 PRInt16
 nsFilePicker::PutLocalFile(const nsString& inTitle, const nsString& inDefaultName, nsILocalFile** outFile)
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
 
   NS_ENSURE_ARG(outFile);
   *outFile = nsnull;
 
@@ -383,18 +361,23 @@ nsFilePicker::PutLocalFile(const nsStrin
   NSString *theDir = PanelDefaultDirectory();
 
   // load the panel
   if ([thePanel runModalForDirectory:theDir file:defaultFilename] == NSFileHandlingPanelCancelButton)
     return retVal;
 
   // get the save type
   NSPopUpButton* popupButton = [accessoryView viewWithTag:kSaveTypeControlTag];
-  if (popupButton)
-    mSelectedType = [popupButton indexOfSelectedItem];
+  if (popupButton) {
+    mSelectedTypeIndex = [popupButton indexOfSelectedItem];
+    // save out to prefs for initializing other file picker instances
+    nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
+    if (prefs)
+      prefs->SetIntPref(kLastTypeIndexPref, mSelectedTypeIndex);
+  }
 
   NSURL* fileURL = [thePanel URL];
   if (fileURL) { 
     nsCOMPtr<nsILocalFile> localFile;
     NS_NewLocalFile(EmptyString(), PR_TRUE, getter_AddRefs(localFile));
     nsCOMPtr<nsILocalFileMac> macLocalFile = do_QueryInterface(localFile);
     if (macLocalFile && NS_SUCCEEDED(macLocalFile->InitWithCFURL((CFURLRef)fileURL))) {
       *outFile = localFile;
@@ -408,26 +391,20 @@ nsFilePicker::PutLocalFile(const nsStrin
     }
   }
 
   return retVal;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(0);
 }
 
-//-------------------------------------------------------------------------
-//
-// GenerateFilterList
-//
+
 // Take the list of file types (in a nice win32-specific format) and fills up
 // an NSArray of them for the Open Panel.  Note: Will return nil if we should allow
 // all file types.
-//
-//-------------------------------------------------------------------------
-
 NSArray *
 nsFilePicker::GenerateFilterList()
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
 
   NSArray *filterArray = nil;
   if (mFilters.Count() > 0) {
     // Set up our filter string
@@ -471,45 +448,34 @@ nsFilePicker::GenerateFilterList()
       // every time we find a semicolon, we've found a new filter.
       // components SeparatedByString should do that for us.
       filterArray = [[[NSArray alloc] initWithArray:[giantFilterString componentsSeparatedByString:@";"]] autorelease];
     }
   }
   return filterArray;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
-} // GenerateFilterList
+}
 
-//-------------------------------------------------------------------------
-//
-// SetDialogTitle
-// 
+
 // Sets the dialog title to whatever it should be.  If it fails, eh,
 // the OS will provide a sensible default.
-//
-//-------------------------------------------------------------------------
-
 void
 nsFilePicker::SetDialogTitle(const nsString& inTitle, id aPanel)
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
 
   [aPanel setTitle:[NSString stringWithCharacters:(const unichar*)inTitle.get() length:inTitle.Length()]];
 
   NS_OBJC_END_TRY_ABORT_BLOCK;
 } 
 
-//-------------------------------------------------------------------------
-//
-// PanelDefaultDirectory
-//
+
 // Converts path from an nsILocalFile into a NSString path
 // If it fails, returns an empty string.
-//
-//-------------------------------------------------------------------------
 NSString *
 nsFilePicker::PanelDefaultDirectory()
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
 
   NSString *directory = nil;
   if (mDisplayDirectory) {
     nsAutoString pathStr;
@@ -517,111 +483,92 @@ nsFilePicker::PanelDefaultDirectory()
     directory = [[[NSString alloc] initWithCharacters:pathStr.get() length:pathStr.Length()] autorelease];
   }
   return directory;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
 }
 
 
-//-------------------------------------------------------------------------
 NS_IMETHODIMP nsFilePicker::GetFile(nsILocalFile **aFile)
 {
   NS_ENSURE_ARG_POINTER(aFile);
   *aFile = nsnull;
   
   // just return the first file
-  if (mFiles.Count() > 0)
-  {
+  if (mFiles.Count() > 0) {
     *aFile = mFiles.ObjectAt(0);
     NS_IF_ADDREF(*aFile);
   }
 
   return NS_OK;
 }
 
-//-------------------------------------------------------------------------
+
 NS_IMETHODIMP nsFilePicker::GetFileURL(nsIURI **aFileURL)
 {
   NS_ENSURE_ARG_POINTER(aFileURL);
   *aFileURL = nsnull;
 
   if (mFiles.Count() == 0)
     return NS_OK;
 
   return NS_NewFileURI(aFileURL, mFiles.ObjectAt(0));
 }
 
-//-------------------------------------------------------------------------
+
 NS_IMETHODIMP nsFilePicker::GetFiles(nsISimpleEnumerator **aFiles)
 {
   return NS_NewArrayEnumerator(aFiles, mFiles);
 }
 
-//-------------------------------------------------------------------------
-//
-// Get the file + path
-//
-//-------------------------------------------------------------------------
+
 NS_IMETHODIMP nsFilePicker::SetDefaultString(const nsAString& aString)
 {
   mDefault = aString;
   return NS_OK;
 }
 
 NS_IMETHODIMP nsFilePicker::GetDefaultString(nsAString& aString)
 {
   return NS_ERROR_FAILURE;
 }
 
-//-------------------------------------------------------------------------
-//
+
 // The default extension to use for files
-//
-//-------------------------------------------------------------------------
 NS_IMETHODIMP nsFilePicker::GetDefaultExtension(nsAString& aExtension)
 {
   aExtension.Truncate();
   return NS_OK;
 }
 
+
 NS_IMETHODIMP nsFilePicker::SetDefaultExtension(const nsAString& aExtension)
 {
   return NS_OK;
 }
 
-//-------------------------------------------------------------------------
-//
+
 // Append an entry to the filters array
-//
-//-------------------------------------------------------------------------
 NS_IMETHODIMP
 nsFilePicker::AppendFilter(const nsAString& aTitle, const nsAString& aFilter)
 {
   mFilters.AppendString(aFilter);
   mTitles.AppendString(aTitle);
   
   return NS_OK;
 }
 
 
-//-------------------------------------------------------------------------
-//
 // Get the filter index - do we still need this?
-//
-//-------------------------------------------------------------------------
 NS_IMETHODIMP nsFilePicker::GetFilterIndex(PRInt32 *aFilterIndex)
 {
-  *aFilterIndex = mSelectedType;
+  *aFilterIndex = mSelectedTypeIndex;
   return NS_OK;
 }
 
-//-------------------------------------------------------------------------
-//
+
 // Set the filter index - do we still need this?
-//
-//-------------------------------------------------------------------------
 NS_IMETHODIMP nsFilePicker::SetFilterIndex(PRInt32 aFilterIndex)
 {
-  mSelectedType = aFilterIndex;
+  mSelectedTypeIndex = aFilterIndex;
   return NS_OK;
 }
-