2004-08-26 03:02:50 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2002-12-13 11:43:18 +03:00
|
|
|
|
2007-01-18 09:34:07 +03:00
|
|
|
#import <Cocoa/Cocoa.h>
|
2002-12-13 11:43:18 +03:00
|
|
|
|
2008-02-29 08:36:35 +03:00
|
|
|
#include "nsFilePicker.h"
|
2002-12-13 11:43:18 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIComponentManager.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2002-12-13 11:43:18 +03:00
|
|
|
#include "nsILocalFileMac.h"
|
|
|
|
#include "nsIURL.h"
|
2003-04-09 00:14:18 +04:00
|
|
|
#include "nsArrayEnumerator.h"
|
2006-10-26 00:47:14 +04:00
|
|
|
#include "nsIStringBundle.h"
|
2012-05-17 11:53:20 +04:00
|
|
|
#include "nsCocoaFeatures.h"
|
2008-03-27 06:42:57 +03:00
|
|
|
#include "nsCocoaUtils.h"
|
2011-05-27 12:15:20 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
|
2013-01-15 16:22:03 +04:00
|
|
|
// This must be included last:
|
|
|
|
#include "nsObjCExceptions.h"
|
|
|
|
|
2011-05-27 12:15:20 +04:00
|
|
|
using namespace mozilla;
|
2002-12-13 11:43:18 +03:00
|
|
|
|
2006-10-26 00:56:32 +04:00
|
|
|
const float kAccessoryViewPadding = 5;
|
|
|
|
const int kSaveTypeControlTag = 1;
|
2002-12-13 11:43:18 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool gCallSecretHiddenFileAPI = false;
|
2009-05-02 22:12:27 +04:00
|
|
|
const char kShowHiddenFilesPref[] = "filepicker.showHiddenFiles";
|
|
|
|
|
2011-04-21 21:39:12 +04:00
|
|
|
/**
|
|
|
|
* This class is an observer of NSPopUpButton selection change.
|
|
|
|
*/
|
|
|
|
@interface NSPopUpButtonObserver : NSObject
|
|
|
|
{
|
|
|
|
NSPopUpButton* mPopUpButton;
|
|
|
|
NSOpenPanel* mOpenPanel;
|
|
|
|
nsFilePicker* mFilePicker;
|
|
|
|
}
|
|
|
|
- (void) setPopUpButton:(NSPopUpButton*)aPopUpButton;
|
|
|
|
- (void) setOpenPanel:(NSOpenPanel*)aOpenPanel;
|
|
|
|
- (void) setFilePicker:(nsFilePicker*)aFilePicker;
|
|
|
|
- (void) menuChangedItem:(NSNotification*)aSender;
|
|
|
|
@end
|
|
|
|
|
2002-12-13 11:43:18 +03:00
|
|
|
NS_IMPL_ISUPPORTS1(nsFilePicker, nsIFilePicker)
|
|
|
|
|
2009-05-02 22:12:27 +04:00
|
|
|
// We never want to call the secret show hidden files API unless the pref
|
|
|
|
// has been set. Once the pref has been set we always need to call it even
|
|
|
|
// if it disappears so that we stop showing hidden files if a user deletes
|
|
|
|
// the pref. If the secret API was used once and things worked out it should
|
|
|
|
// continue working for subsequent calls so the user is at no more risk.
|
|
|
|
static void SetShowHiddenFileState(NSSavePanel* panel)
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool show = false;
|
2011-05-27 12:15:20 +04:00
|
|
|
if (NS_SUCCEEDED(Preferences::GetBool(kShowHiddenFilesPref, &show))) {
|
2011-10-01 04:20:33 +04:00
|
|
|
gCallSecretHiddenFileAPI = true;
|
2009-05-02 22:12:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (gCallSecretHiddenFileAPI) {
|
|
|
|
// invoke a method to get a Cocoa-internal nav view
|
|
|
|
SEL navViewSelector = @selector(_navView);
|
|
|
|
NSMethodSignature* navViewSignature = [panel methodSignatureForSelector:navViewSelector];
|
|
|
|
if (!navViewSignature)
|
|
|
|
return;
|
|
|
|
NSInvocation* navViewInvocation = [NSInvocation invocationWithMethodSignature:navViewSignature];
|
|
|
|
[navViewInvocation setSelector:navViewSelector];
|
|
|
|
[navViewInvocation setTarget:panel];
|
|
|
|
[navViewInvocation invoke];
|
|
|
|
|
|
|
|
// get the returned nav view
|
|
|
|
id navView = nil;
|
|
|
|
[navViewInvocation getReturnValue:&navView];
|
|
|
|
|
|
|
|
// invoke the secret show hidden file state method on the nav view
|
|
|
|
SEL showHiddenFilesSelector = @selector(setShowsHiddenFiles:);
|
|
|
|
NSMethodSignature* showHiddenFilesSignature = [navView methodSignatureForSelector:showHiddenFilesSelector];
|
|
|
|
if (!showHiddenFilesSignature)
|
|
|
|
return;
|
|
|
|
NSInvocation* showHiddenFilesInvocation = [NSInvocation invocationWithMethodSignature:showHiddenFilesSignature];
|
|
|
|
[showHiddenFilesInvocation setSelector:showHiddenFilesSelector];
|
|
|
|
[showHiddenFilesInvocation setTarget:navView];
|
|
|
|
[showHiddenFilesInvocation setArgument:&show atIndex:2];
|
|
|
|
[showHiddenFilesInvocation invoke];
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
2008-02-29 08:36:35 +03:00
|
|
|
|
2002-12-13 11:43:18 +03:00
|
|
|
nsFilePicker::nsFilePicker()
|
2013-07-22 13:12:32 +04:00
|
|
|
: mSelectedTypeIndex(0)
|
2002-12-13 11:43:18 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsFilePicker::~nsFilePicker()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-04-22 03:38:22 +04:00
|
|
|
void
|
2013-07-22 13:12:32 +04:00
|
|
|
nsFilePicker::InitNative(nsIWidget *aParent, const nsAString& aTitle)
|
2002-12-13 11:43:18 +03:00
|
|
|
{
|
|
|
|
mTitle = aTitle;
|
|
|
|
}
|
|
|
|
|
2006-10-26 00:47:14 +04:00
|
|
|
NSView* nsFilePicker::GetAccessoryView()
|
|
|
|
{
|
2008-02-19 23:58:01 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2006-10-26 00:47:14 +04:00
|
|
|
NSView* accessoryView = [[[NSView alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)] autorelease];
|
|
|
|
|
2011-04-21 21:39:12 +04:00
|
|
|
// Set a label's default value.
|
|
|
|
NSString* label = @"Format:";
|
|
|
|
|
|
|
|
// Try to get the localized string.
|
2006-10-26 00:47:14 +04:00
|
|
|
nsCOMPtr<nsIStringBundleService> sbs = do_GetService(NS_STRINGBUNDLE_CONTRACTID);
|
|
|
|
nsCOMPtr<nsIStringBundle> bundle;
|
|
|
|
nsresult rv = sbs->CreateBundle("chrome://global/locale/filepicker.properties", getter_AddRefs(bundle));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-04-21 21:39:12 +04:00
|
|
|
nsXPIDLString locaLabel;
|
|
|
|
bundle->GetStringFromName(NS_LITERAL_STRING("formatLabel").get(),
|
|
|
|
getter_Copies(locaLabel));
|
|
|
|
if (locaLabel) {
|
2013-10-10 20:59:40 +04:00
|
|
|
label = [NSString stringWithCharacters:reinterpret_cast<const unichar*>(locaLabel.get())
|
|
|
|
length:locaLabel.Length()];
|
2011-04-21 21:39:12 +04:00
|
|
|
}
|
2006-10-26 00:47:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set up label text field
|
|
|
|
NSTextField* textField = [[[NSTextField alloc] init] autorelease];
|
|
|
|
[textField setEditable:NO];
|
|
|
|
[textField setSelectable:NO];
|
|
|
|
[textField setDrawsBackground:NO];
|
|
|
|
[textField setBezeled:NO];
|
|
|
|
[textField setBordered:NO];
|
|
|
|
[textField setFont:[NSFont labelFontOfSize:13.0]];
|
2011-04-21 21:39:12 +04:00
|
|
|
[textField setStringValue:label];
|
2006-10-26 00:47:14 +04:00
|
|
|
[textField setTag:0];
|
|
|
|
[textField sizeToFit];
|
|
|
|
|
|
|
|
// set up popup button
|
|
|
|
NSPopUpButton* popupButton = [[[NSPopUpButton alloc] initWithFrame:NSMakeRect(0, 0, 0, 0) pullsDown:NO] autorelease];
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numMenuItems = mTitles.Length();
|
|
|
|
for (uint32_t i = 0; i < numMenuItems; i++) {
|
2009-01-18 23:14:14 +03:00
|
|
|
const nsString& currentTitle = mTitles[i];
|
2008-02-13 06:38:06 +03:00
|
|
|
NSString *titleString;
|
|
|
|
if (currentTitle.IsEmpty()) {
|
2009-01-18 23:14:14 +03:00
|
|
|
const nsString& currentFilter = mFilters[i];
|
2013-10-10 20:59:40 +04:00
|
|
|
titleString = [[NSString alloc] initWithCharacters:reinterpret_cast<const unichar*>(currentFilter.get())
|
2008-02-13 06:38:06 +03:00
|
|
|
length:currentFilter.Length()];
|
|
|
|
}
|
|
|
|
else {
|
2013-10-10 20:59:40 +04:00
|
|
|
titleString = [[NSString alloc] initWithCharacters:reinterpret_cast<const unichar*>(currentTitle.get())
|
2008-02-13 06:38:06 +03:00
|
|
|
length:currentTitle.Length()];
|
|
|
|
}
|
2006-10-26 00:47:14 +04:00
|
|
|
[popupButton addItemWithTitle:titleString];
|
2006-10-26 00:56:32 +04:00
|
|
|
[titleString release];
|
2006-10-26 00:47:14 +04:00
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
if (mSelectedTypeIndex >= 0 && (uint32_t)mSelectedTypeIndex < numMenuItems)
|
2008-02-29 08:36:35 +03:00
|
|
|
[popupButton selectItemAtIndex:mSelectedTypeIndex];
|
2006-10-26 00:56:32 +04:00
|
|
|
[popupButton setTag:kSaveTypeControlTag];
|
2008-02-13 06:38:06 +03:00
|
|
|
[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)];
|
2006-10-26 00:47:14 +04:00
|
|
|
|
2006-10-26 00:56:32 +04:00
|
|
|
// position everything based on control sizes with kAccessoryViewPadding pix padding
|
|
|
|
// on each side kAccessoryViewPadding pix horizontal padding between controls
|
2006-10-26 00:47:14 +04:00
|
|
|
float greatestHeight = [textField frame].size.height;
|
|
|
|
if ([popupButton frame].size.height > greatestHeight)
|
|
|
|
greatestHeight = [popupButton frame].size.height;
|
2006-10-26 00:56:32 +04:00
|
|
|
float totalViewHeight = greatestHeight + kAccessoryViewPadding * 2;
|
|
|
|
float totalViewWidth = [textField frame].size.width + [popupButton frame].size.width + kAccessoryViewPadding * 3;
|
2006-10-26 00:47:14 +04:00
|
|
|
[accessoryView setFrameSize:NSMakeSize(totalViewWidth, totalViewHeight)];
|
|
|
|
|
2006-10-26 00:56:32 +04:00
|
|
|
float textFieldOriginY = ((greatestHeight - [textField frame].size.height) / 2 + 1) + kAccessoryViewPadding;
|
|
|
|
[textField setFrameOrigin:NSMakePoint(kAccessoryViewPadding, textFieldOriginY)];
|
2006-10-26 00:47:14 +04:00
|
|
|
|
2006-10-26 00:56:32 +04:00
|
|
|
float popupOriginX = [textField frame].size.width + kAccessoryViewPadding * 2;
|
|
|
|
float popupOriginY = ((greatestHeight - [popupButton frame].size.height) / 2) + kAccessoryViewPadding;
|
2006-10-26 00:47:14 +04:00
|
|
|
[popupButton setFrameOrigin:NSMakePoint(popupOriginX, popupOriginY)];
|
|
|
|
|
|
|
|
[accessoryView addSubview:textField];
|
|
|
|
[accessoryView addSubview:popupButton];
|
|
|
|
return accessoryView;
|
2008-02-19 23:58:01 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2006-10-26 00:47:14 +04:00
|
|
|
}
|
|
|
|
|
2008-02-29 08:36:35 +03:00
|
|
|
// Display the file dialog
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsFilePicker::Show(int16_t *retval)
|
2002-12-13 11:43:18 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(retval);
|
|
|
|
|
|
|
|
*retval = returnCancel;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t userClicksOK = returnCancel;
|
2006-10-26 00:47:14 +04:00
|
|
|
|
2002-12-13 11:43:18 +03:00
|
|
|
// Random questions from DHH:
|
|
|
|
//
|
|
|
|
// Why do we pass mTitle, mDefault to the functions? Can GetLocalFile. PutLocalFile,
|
|
|
|
// and GetLocalFolder get called someplace else? It generates a bunch of warnings
|
|
|
|
// as it is right now.
|
|
|
|
//
|
|
|
|
// I think we could easily combine GetLocalFile and GetLocalFolder together, just
|
|
|
|
// setting panel pick options based on mMode. I didn't do it here b/c I wanted to
|
|
|
|
// make this look as much like Carbon nsFilePicker as possible.
|
|
|
|
|
2003-04-09 00:14:18 +04:00
|
|
|
mFiles.Clear();
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> theFile;
|
2003-04-09 00:14:18 +04:00
|
|
|
|
2002-12-13 11:43:18 +03:00
|
|
|
switch (mMode)
|
|
|
|
{
|
|
|
|
case modeOpen:
|
2011-10-01 04:20:33 +04:00
|
|
|
userClicksOK = GetLocalFiles(mTitle, false, mFiles);
|
2003-04-09 00:14:18 +04:00
|
|
|
break;
|
|
|
|
|
2002-12-13 11:43:18 +03:00
|
|
|
case modeOpenMultiple:
|
2011-10-01 04:20:33 +04:00
|
|
|
userClicksOK = GetLocalFiles(mTitle, true, mFiles);
|
2002-12-13 11:43:18 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case modeSave:
|
2003-04-09 00:14:18 +04:00
|
|
|
userClicksOK = PutLocalFile(mTitle, mDefault, getter_AddRefs(theFile));
|
2002-12-13 11:43:18 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case modeGetFolder:
|
2003-04-09 00:14:18 +04:00
|
|
|
userClicksOK = GetLocalFolder(mTitle, getter_AddRefs(theFile));
|
2002-12-13 11:43:18 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-08-14 18:09:00 +04:00
|
|
|
NS_ERROR("Unknown file picker mode");
|
2002-12-13 11:43:18 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-04-09 00:14:18 +04:00
|
|
|
if (theFile)
|
|
|
|
mFiles.AppendObject(theFile);
|
2002-12-13 11:43:18 +03:00
|
|
|
|
|
|
|
*retval = userClicksOK;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-11 16:46:59 +04:00
|
|
|
static
|
|
|
|
void UpdatePanelFileTypes(NSOpenPanel* aPanel, NSArray* aFilters)
|
|
|
|
{
|
|
|
|
// If we show all file types, also "expose" bundles' contents.
|
2011-05-11 20:00:04 +04:00
|
|
|
[aPanel setTreatsFilePackagesAsDirectories:!aFilters];
|
2011-05-11 16:46:59 +04:00
|
|
|
|
|
|
|
[aPanel setAllowedFileTypes:aFilters];
|
|
|
|
}
|
|
|
|
|
2011-04-21 21:39:12 +04:00
|
|
|
@implementation NSPopUpButtonObserver
|
|
|
|
- (void) setPopUpButton:(NSPopUpButton*)aPopUpButton
|
|
|
|
{
|
|
|
|
mPopUpButton = aPopUpButton;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setOpenPanel:(NSOpenPanel*)aOpenPanel
|
|
|
|
{
|
|
|
|
mOpenPanel = aOpenPanel;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setFilePicker:(nsFilePicker*)aFilePicker
|
|
|
|
{
|
|
|
|
mFilePicker = aFilePicker;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) menuChangedItem:(NSNotification *)aSender
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t selectedItem = [mPopUpButton indexOfSelectedItem];
|
2011-04-21 21:39:12 +04:00
|
|
|
if (selectedItem < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFilePicker->SetFilterIndex(selectedItem);
|
2011-05-11 16:46:59 +04:00
|
|
|
UpdatePanelFileTypes(mOpenPanel, mFilePicker->GetFilterList());
|
2011-04-21 21:39:12 +04:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN();
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2006-11-18 01:25:06 +03:00
|
|
|
// Use OpenPanel to do a GetFile. Returns |returnOK| if the user presses OK in the dialog.
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t
|
2012-06-06 06:08:30 +04:00
|
|
|
nsFilePicker::GetLocalFiles(const nsString& inTitle, bool inAllowMultiple, nsCOMArray<nsIFile>& outFiles)
|
2002-12-13 11:43:18 +03:00
|
|
|
{
|
2008-02-19 23:58:01 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t retVal = (int16_t)returnCancel;
|
2002-12-13 11:43:18 +03:00
|
|
|
NSOpenPanel *thePanel = [NSOpenPanel openPanel];
|
|
|
|
|
2009-05-02 22:12:27 +04:00
|
|
|
SetShowHiddenFileState(thePanel);
|
|
|
|
|
2002-12-13 11:43:18 +03:00
|
|
|
// Set the options for how the get file dialog will appear
|
|
|
|
SetDialogTitle(inTitle, thePanel);
|
2003-04-09 00:14:18 +04:00
|
|
|
[thePanel setAllowsMultipleSelection:inAllowMultiple];
|
2002-12-13 11:43:18 +03:00
|
|
|
[thePanel setCanSelectHiddenExtension:YES];
|
|
|
|
[thePanel setCanChooseDirectories:NO];
|
|
|
|
[thePanel setCanChooseFiles:YES];
|
2013-04-18 20:39:09 +04:00
|
|
|
[thePanel setResolvesAliases:YES]; //this is default - probably doesn't need to be set
|
2006-10-11 00:14:27 +04:00
|
|
|
|
2011-05-11 16:46:59 +04:00
|
|
|
// Get filters
|
|
|
|
// filters may be null, if we should allow all file types.
|
|
|
|
NSArray *filters = GetFilterList();
|
2002-12-13 11:43:18 +03:00
|
|
|
|
2003-04-09 00:14:18 +04:00
|
|
|
// set up default directory
|
2002-12-13 11:43:18 +03:00
|
|
|
NSString *theDir = PanelDefaultDirectory();
|
2006-11-18 01:25:06 +03:00
|
|
|
|
|
|
|
// if this is the "Choose application..." dialog, and no other start
|
|
|
|
// dir has been set, then use the Applications folder.
|
2013-04-18 20:39:09 +04:00
|
|
|
if (!theDir) {
|
|
|
|
if (filters && [filters count] == 1 &&
|
|
|
|
[(NSString *)[filters objectAtIndex:0] isEqualToString:@"app"])
|
|
|
|
theDir = @"/Applications/";
|
|
|
|
else
|
|
|
|
theDir = @"";
|
2006-11-18 01:25:06 +03:00
|
|
|
}
|
2008-03-27 06:42:57 +03:00
|
|
|
|
2013-04-26 21:46:32 +04:00
|
|
|
if (theDir) {
|
2013-08-12 21:29:00 +04:00
|
|
|
[thePanel setDirectoryURL:[NSURL fileURLWithPath:theDir isDirectory:YES]];
|
2013-04-26 21:46:32 +04:00
|
|
|
}
|
2013-04-18 20:39:09 +04:00
|
|
|
|
2011-04-21 21:39:12 +04:00
|
|
|
int result;
|
2008-03-27 06:42:57 +03:00
|
|
|
nsCocoaUtils::PrepareForNativeAppModalDialog();
|
2012-11-09 14:22:36 +04:00
|
|
|
if (mFilters.Length() > 1) {
|
2011-04-29 16:21:43 +04:00
|
|
|
// [NSURL initWithString:] (below) throws an exception if URLString is nil.
|
|
|
|
|
2011-04-21 21:39:12 +04:00
|
|
|
NSPopUpButtonObserver* observer = [[NSPopUpButtonObserver alloc] init];
|
|
|
|
|
|
|
|
NSView* accessoryView = GetAccessoryView();
|
|
|
|
[thePanel setAccessoryView:accessoryView];
|
|
|
|
|
|
|
|
[observer setPopUpButton:[accessoryView viewWithTag:kSaveTypeControlTag]];
|
|
|
|
[observer setOpenPanel:thePanel];
|
|
|
|
[observer setFilePicker:this];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver:observer
|
|
|
|
selector:@selector(menuChangedItem:)
|
|
|
|
name:NSMenuWillSendActionNotification object:nil];
|
|
|
|
|
2011-05-11 16:46:59 +04:00
|
|
|
UpdatePanelFileTypes(thePanel, filters);
|
2011-04-21 21:39:12 +04:00
|
|
|
result = [thePanel runModal];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:observer];
|
|
|
|
[observer release];
|
|
|
|
} else {
|
2011-05-11 16:46:59 +04:00
|
|
|
// If we show all file types, also "expose" bundles' contents.
|
|
|
|
if (!filters) {
|
2011-05-11 20:00:04 +04:00
|
|
|
[thePanel setTreatsFilePackagesAsDirectories:YES];
|
2011-05-11 16:46:59 +04:00
|
|
|
}
|
2013-04-18 20:39:09 +04:00
|
|
|
[thePanel setAllowedFileTypes:filters];
|
|
|
|
result = [thePanel runModal];
|
2011-04-21 21:39:12 +04:00
|
|
|
}
|
2008-03-27 06:42:57 +03:00
|
|
|
nsCocoaUtils::CleanUpAfterNativeAppModalDialog();
|
2002-12-13 11:43:18 +03:00
|
|
|
|
|
|
|
if (result == NSFileHandlingPanelCancelButton)
|
|
|
|
return retVal;
|
2011-06-03 02:36:11 +04:00
|
|
|
|
|
|
|
// Converts data from a NSArray of NSURL to the returned format.
|
|
|
|
// We should be careful to not call [thePanel URLs] more than once given that
|
|
|
|
// it creates a new array each time.
|
2011-06-09 13:28:16 +04:00
|
|
|
// We are using Fast Enumeration, thus the NSURL array is created once then
|
|
|
|
// iterated.
|
|
|
|
for (NSURL* url in [thePanel URLs]) {
|
2011-06-03 02:36:11 +04:00
|
|
|
if (!url) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> localFile;
|
2011-10-01 04:20:33 +04:00
|
|
|
NS_NewLocalFile(EmptyString(), true, getter_AddRefs(localFile));
|
2011-06-03 02:36:11 +04:00
|
|
|
nsCOMPtr<nsILocalFileMac> macLocalFile = do_QueryInterface(localFile);
|
|
|
|
if (macLocalFile && NS_SUCCEEDED(macLocalFile->InitWithCFURL((CFURLRef)url))) {
|
|
|
|
outFiles.AppendObject(localFile);
|
2003-04-09 00:14:18 +04:00
|
|
|
}
|
2002-12-13 11:43:18 +03:00
|
|
|
}
|
2003-04-09 00:14:18 +04:00
|
|
|
|
|
|
|
if (outFiles.Count() > 0)
|
|
|
|
retVal = returnOK;
|
|
|
|
|
2002-12-13 11:43:18 +03:00
|
|
|
return retVal;
|
2008-02-19 23:58:01 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(0);
|
2008-02-29 08:36:35 +03:00
|
|
|
}
|
2002-12-13 11:43:18 +03:00
|
|
|
|
2006-11-18 01:25:06 +03:00
|
|
|
// Use OpenPanel to do a GetFolder. Returns |returnOK| if the user presses OK in the dialog.
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t
|
2012-06-06 06:08:30 +04:00
|
|
|
nsFilePicker::GetLocalFolder(const nsString& inTitle, nsIFile** outFile)
|
2002-12-13 11:43:18 +03:00
|
|
|
{
|
2008-02-19 23:58:01 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
2010-07-07 13:48:17 +04:00
|
|
|
NS_ASSERTION(outFile, "this protected member function expects a null initialized out pointer");
|
2003-04-09 00:14:18 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t retVal = (int16_t)returnCancel;
|
2002-12-13 11:43:18 +03:00
|
|
|
NSOpenPanel *thePanel = [NSOpenPanel openPanel];
|
|
|
|
|
2009-05-02 22:12:27 +04:00
|
|
|
SetShowHiddenFileState(thePanel);
|
|
|
|
|
2002-12-13 11:43:18 +03:00
|
|
|
// Set the options for how the get file dialog will appear
|
|
|
|
SetDialogTitle(inTitle, thePanel);
|
|
|
|
[thePanel setAllowsMultipleSelection:NO]; //this is default -probably doesn't need to be set
|
|
|
|
[thePanel setCanSelectHiddenExtension:YES];
|
|
|
|
[thePanel setCanChooseDirectories:YES];
|
|
|
|
[thePanel setCanChooseFiles:NO];
|
|
|
|
[thePanel setResolvesAliases:YES]; //this is default - probably doesn't need to be set
|
2006-11-22 20:35:52 +03:00
|
|
|
[thePanel setCanCreateDirectories:YES];
|
2006-11-18 01:25:06 +03:00
|
|
|
|
|
|
|
// packages != folders
|
|
|
|
[thePanel setTreatsFilePackagesAsDirectories:NO];
|
2002-12-13 11:43:18 +03:00
|
|
|
|
|
|
|
// set up default directory
|
|
|
|
NSString *theDir = PanelDefaultDirectory();
|
2013-04-26 21:46:32 +04:00
|
|
|
if (theDir) {
|
2013-08-12 21:29:00 +04:00
|
|
|
[thePanel setDirectoryURL:[NSURL fileURLWithPath:theDir isDirectory:YES]];
|
2013-04-26 21:46:32 +04:00
|
|
|
}
|
2008-03-27 06:42:57 +03:00
|
|
|
nsCocoaUtils::PrepareForNativeAppModalDialog();
|
2013-04-18 20:39:09 +04:00
|
|
|
int result = [thePanel runModal];
|
2008-03-27 06:42:57 +03:00
|
|
|
nsCocoaUtils::CleanUpAfterNativeAppModalDialog();
|
2002-12-13 11:43:18 +03:00
|
|
|
|
|
|
|
if (result == NSFileHandlingPanelCancelButton)
|
|
|
|
return retVal;
|
2006-10-26 00:47:14 +04:00
|
|
|
|
2006-11-18 01:25:06 +03:00
|
|
|
// get the path for the folder (we allow just 1, so that's all we get)
|
2002-12-13 11:43:18 +03:00
|
|
|
NSURL *theURL = [[thePanel URLs] objectAtIndex:0];
|
2006-10-26 00:47:14 +04:00
|
|
|
if (theURL) {
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> localFile;
|
2011-10-01 04:20:33 +04:00
|
|
|
NS_NewLocalFile(EmptyString(), true, getter_AddRefs(localFile));
|
2003-04-09 00:14:18 +04:00
|
|
|
nsCOMPtr<nsILocalFileMac> macLocalFile = do_QueryInterface(localFile);
|
2006-10-26 00:47:14 +04:00
|
|
|
if (macLocalFile && NS_SUCCEEDED(macLocalFile->InitWithCFURL((CFURLRef)theURL))) {
|
2003-04-09 00:14:18 +04:00
|
|
|
*outFile = localFile;
|
|
|
|
NS_ADDREF(*outFile);
|
2002-12-13 11:43:18 +03:00
|
|
|
retVal = returnOK;
|
2003-04-09 00:14:18 +04:00
|
|
|
}
|
2002-12-13 11:43:18 +03:00
|
|
|
}
|
|
|
|
|
2003-04-09 00:14:18 +04:00
|
|
|
return retVal;
|
2008-02-19 23:58:01 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(0);
|
2008-02-29 08:36:35 +03:00
|
|
|
}
|
2002-12-13 11:43:18 +03:00
|
|
|
|
2008-02-29 08:36:35 +03:00
|
|
|
// Returns |returnOK| if the user presses OK in the dialog.
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t
|
2012-06-06 06:08:30 +04:00
|
|
|
nsFilePicker::PutLocalFile(const nsString& inTitle, const nsString& inDefaultName, nsIFile** outFile)
|
2002-12-13 11:43:18 +03:00
|
|
|
{
|
2008-02-19 23:58:01 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
2010-07-07 13:48:17 +04:00
|
|
|
NS_ASSERTION(outFile, "this protected member function expects a null initialized out pointer");
|
2003-04-09 00:14:18 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t retVal = returnCancel;
|
2002-12-13 11:43:18 +03:00
|
|
|
NSSavePanel *thePanel = [NSSavePanel savePanel];
|
2006-10-26 00:47:14 +04:00
|
|
|
|
2009-05-02 22:12:27 +04:00
|
|
|
SetShowHiddenFileState(thePanel);
|
|
|
|
|
2002-12-13 11:43:18 +03:00
|
|
|
SetDialogTitle(inTitle, thePanel);
|
2006-10-26 00:47:14 +04:00
|
|
|
|
|
|
|
// set up accessory view for file format options
|
|
|
|
NSView* accessoryView = GetAccessoryView();
|
|
|
|
[thePanel setAccessoryView:accessoryView];
|
|
|
|
|
2002-12-13 11:43:18 +03:00
|
|
|
// set up default file name
|
2003-04-09 00:14:18 +04:00
|
|
|
NSString* defaultFilename = [NSString stringWithCharacters:(const unichar*)inDefaultName.get() length:inDefaultName.Length()];
|
|
|
|
|
2002-12-13 11:43:18 +03:00
|
|
|
// set up default directory
|
|
|
|
NSString *theDir = PanelDefaultDirectory();
|
2013-04-26 21:46:32 +04:00
|
|
|
if (theDir) {
|
2013-08-12 21:29:00 +04:00
|
|
|
[thePanel setDirectoryURL:[NSURL fileURLWithPath:theDir isDirectory:YES]];
|
2013-04-26 21:46:32 +04:00
|
|
|
}
|
2006-10-26 00:47:14 +04:00
|
|
|
|
|
|
|
// load the panel
|
2008-03-27 06:42:57 +03:00
|
|
|
nsCocoaUtils::PrepareForNativeAppModalDialog();
|
2013-04-18 20:39:09 +04:00
|
|
|
[thePanel setNameFieldStringValue:defaultFilename];
|
|
|
|
int result = [thePanel runModal];
|
2008-03-27 06:42:57 +03:00
|
|
|
nsCocoaUtils::CleanUpAfterNativeAppModalDialog();
|
|
|
|
if (result == NSFileHandlingPanelCancelButton)
|
2002-12-13 11:43:18 +03:00
|
|
|
return retVal;
|
|
|
|
|
2006-10-26 00:47:14 +04:00
|
|
|
// get the save type
|
2006-10-26 00:56:32 +04:00
|
|
|
NSPopUpButton* popupButton = [accessoryView viewWithTag:kSaveTypeControlTag];
|
2008-02-29 08:36:35 +03:00
|
|
|
if (popupButton) {
|
|
|
|
mSelectedTypeIndex = [popupButton indexOfSelectedItem];
|
|
|
|
}
|
2006-10-26 00:47:14 +04:00
|
|
|
|
|
|
|
NSURL* fileURL = [thePanel URL];
|
|
|
|
if (fileURL) {
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> localFile;
|
2011-10-01 04:20:33 +04:00
|
|
|
NS_NewLocalFile(EmptyString(), true, getter_AddRefs(localFile));
|
2003-04-09 00:14:18 +04:00
|
|
|
nsCOMPtr<nsILocalFileMac> macLocalFile = do_QueryInterface(localFile);
|
2006-10-26 00:47:14 +04:00
|
|
|
if (macLocalFile && NS_SUCCEEDED(macLocalFile->InitWithCFURL((CFURLRef)fileURL))) {
|
2003-04-09 00:14:18 +04:00
|
|
|
*outFile = localFile;
|
|
|
|
NS_ADDREF(*outFile);
|
2006-10-26 00:47:14 +04:00
|
|
|
// We tell if we are replacing or not by just looking to see if the file exists.
|
|
|
|
// The user could not have hit OK and not meant to replace the file.
|
|
|
|
if ([[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]])
|
|
|
|
retVal = returnReplace;
|
|
|
|
else
|
|
|
|
retVal = returnOK;
|
2002-12-13 11:43:18 +03:00
|
|
|
}
|
2006-10-26 00:47:14 +04:00
|
|
|
}
|
2003-04-09 00:14:18 +04:00
|
|
|
|
2008-02-19 23:58:01 +03:00
|
|
|
return retVal;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(0);
|
2002-12-13 11:43:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NSArray *
|
2011-04-21 21:39:12 +04:00
|
|
|
nsFilePicker::GetFilterList()
|
2002-12-13 11:43:18 +03:00
|
|
|
{
|
2008-02-19 23:58:01 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2011-06-14 18:14:56 +04:00
|
|
|
if (!mFilters.Length()) {
|
2011-04-01 18:30:00 +04:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
if (mFilters.Length() <= (uint32_t)mSelectedTypeIndex) {
|
2011-06-14 18:14:56 +04:00
|
|
|
NS_WARNING("An out of range index has been selected. Using the first index instead.");
|
|
|
|
mSelectedTypeIndex = 0;
|
|
|
|
}
|
|
|
|
|
2011-04-01 18:30:00 +04:00
|
|
|
const nsString& filterWide = mFilters[mSelectedTypeIndex];
|
|
|
|
if (!filterWide.Length()) {
|
|
|
|
return nil;
|
2002-12-13 11:43:18 +03:00
|
|
|
}
|
2011-04-01 18:30:00 +04:00
|
|
|
|
|
|
|
if (filterWide.Equals(NS_LITERAL_STRING("*"))) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The extensions in filterWide are in the format "*.ext" but are expected
|
|
|
|
// in the format "ext" by NSOpenPanel. So we need to filter some characters.
|
|
|
|
NSMutableString* filterString = [[[NSMutableString alloc] initWithString:
|
2013-10-10 20:59:40 +04:00
|
|
|
[NSString stringWithCharacters:reinterpret_cast<const unichar*>(filterWide.get())
|
|
|
|
length:filterWide.Length()]] autorelease];
|
2012-08-07 08:09:23 +04:00
|
|
|
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@". *"];
|
2011-04-01 18:30:00 +04:00
|
|
|
NSRange range = [filterString rangeOfCharacterFromSet:set];
|
|
|
|
while (range.length) {
|
|
|
|
[filterString replaceCharactersInRange:range withString:@""];
|
|
|
|
range = [filterString rangeOfCharacterFromSet:set];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [[[NSArray alloc] initWithArray:
|
|
|
|
[filterString componentsSeparatedByString:@";"]] autorelease];
|
2008-02-19 23:58:01 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2008-02-29 08:36:35 +03:00
|
|
|
}
|
|
|
|
|
2002-12-13 11:43:18 +03:00
|
|
|
// 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)
|
|
|
|
{
|
2008-02-19 23:58:01 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2003-04-09 00:14:18 +04:00
|
|
|
[aPanel setTitle:[NSString stringWithCharacters:(const unichar*)inTitle.get() length:inTitle.Length()]];
|
2008-02-19 23:58:01 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2002-12-13 11:43:18 +03:00
|
|
|
}
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
// Converts path from an nsIFile into a NSString path
|
2002-12-13 11:43:18 +03:00
|
|
|
// If it fails, returns an empty string.
|
|
|
|
NSString *
|
|
|
|
nsFilePicker::PanelDefaultDirectory()
|
|
|
|
{
|
2008-02-19 23:58:01 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2002-12-13 11:43:18 +03:00
|
|
|
NSString *directory = nil;
|
|
|
|
if (mDisplayDirectory) {
|
|
|
|
nsAutoString pathStr;
|
|
|
|
mDisplayDirectory->GetPath(pathStr);
|
2013-10-10 20:59:40 +04:00
|
|
|
directory = [[[NSString alloc] initWithCharacters:reinterpret_cast<const unichar*>(pathStr.get())
|
|
|
|
length:pathStr.Length()] autorelease];
|
2002-12-13 11:43:18 +03:00
|
|
|
}
|
|
|
|
return directory;
|
2008-02-19 23:58:01 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2002-12-13 11:43:18 +03:00
|
|
|
}
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
NS_IMETHODIMP nsFilePicker::GetFile(nsIFile **aFile)
|
2002-12-13 11:43:18 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aFile);
|
2012-07-30 18:20:58 +04:00
|
|
|
*aFile = nullptr;
|
2003-04-09 00:14:18 +04:00
|
|
|
|
|
|
|
// just return the first file
|
2008-02-29 08:36:35 +03:00
|
|
|
if (mFiles.Count() > 0) {
|
2003-04-09 00:14:18 +04:00
|
|
|
*aFile = mFiles.ObjectAt(0);
|
|
|
|
NS_IF_ADDREF(*aFile);
|
|
|
|
}
|
|
|
|
|
2002-12-13 11:43:18 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-02-26 19:12:17 +03:00
|
|
|
NS_IMETHODIMP nsFilePicker::GetFileURL(nsIURI **aFileURL)
|
2002-12-13 11:43:18 +03:00
|
|
|
{
|
2003-04-09 00:14:18 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aFileURL);
|
2012-07-30 18:20:58 +04:00
|
|
|
*aFileURL = nullptr;
|
2003-04-09 00:14:18 +04:00
|
|
|
|
|
|
|
if (mFiles.Count() == 0)
|
|
|
|
return NS_OK;
|
2002-12-13 11:43:18 +03:00
|
|
|
|
2008-02-26 19:12:17 +03:00
|
|
|
return NS_NewFileURI(aFileURL, mFiles.ObjectAt(0));
|
2002-12-13 11:43:18 +03:00
|
|
|
}
|
|
|
|
|
2003-04-09 00:14:18 +04:00
|
|
|
NS_IMETHODIMP nsFilePicker::GetFiles(nsISimpleEnumerator **aFiles)
|
|
|
|
{
|
|
|
|
return NS_NewArrayEnumerator(aFiles, mFiles);
|
|
|
|
}
|
2002-12-13 11:43:18 +03:00
|
|
|
|
2004-04-22 03:38:22 +04:00
|
|
|
NS_IMETHODIMP nsFilePicker::SetDefaultString(const nsAString& aString)
|
2002-12-13 11:43:18 +03:00
|
|
|
{
|
|
|
|
mDefault = aString;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-04-22 03:38:22 +04:00
|
|
|
NS_IMETHODIMP nsFilePicker::GetDefaultString(nsAString& aString)
|
2002-12-13 11:43:18 +03:00
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The default extension to use for files
|
2004-04-22 03:38:22 +04:00
|
|
|
NS_IMETHODIMP nsFilePicker::GetDefaultExtension(nsAString& aExtension)
|
2002-12-13 11:43:18 +03:00
|
|
|
{
|
2004-04-22 03:38:22 +04:00
|
|
|
aExtension.Truncate();
|
2002-12-13 11:43:18 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-04-22 03:38:22 +04:00
|
|
|
NS_IMETHODIMP nsFilePicker::SetDefaultExtension(const nsAString& aExtension)
|
2002-12-13 11:43:18 +03:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append an entry to the filters array
|
|
|
|
NS_IMETHODIMP
|
2004-04-22 03:38:22 +04:00
|
|
|
nsFilePicker::AppendFilter(const nsAString& aTitle, const nsAString& aFilter)
|
2002-12-13 11:43:18 +03:00
|
|
|
{
|
2011-04-01 18:30:00 +04:00
|
|
|
// "..apps" has to be translated with native executable extensions.
|
|
|
|
if (aFilter.EqualsLiteral("..apps")) {
|
|
|
|
mFilters.AppendElement(NS_LITERAL_STRING("*.app"));
|
|
|
|
} else {
|
|
|
|
mFilters.AppendElement(aFilter);
|
|
|
|
}
|
2009-01-18 23:14:14 +03:00
|
|
|
mTitles.AppendElement(aTitle);
|
2002-12-13 11:43:18 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the filter index - do we still need this?
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsFilePicker::GetFilterIndex(int32_t *aFilterIndex)
|
2002-12-13 11:43:18 +03:00
|
|
|
{
|
2008-02-29 08:36:35 +03:00
|
|
|
*aFilterIndex = mSelectedTypeIndex;
|
2002-12-13 11:43:18 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the filter index - do we still need this?
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsFilePicker::SetFilterIndex(int32_t aFilterIndex)
|
2002-12-13 11:43:18 +03:00
|
|
|
{
|
2008-02-29 08:36:35 +03:00
|
|
|
mSelectedTypeIndex = aFilterIndex;
|
2002-12-13 11:43:18 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|