2013-05-20 13:46:43 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-05-20 13:46:43 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/ui/file_dialog.h"
|
2013-05-20 13:46:43 +00:00
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
2014-03-16 01:37:04 +00:00
|
|
|
#import <CoreServices/CoreServices.h>
|
2013-05-20 13:46:43 +00:00
|
|
|
|
2014-03-16 01:37:04 +00:00
|
|
|
#include "atom/browser/native_window.h"
|
2013-05-20 13:46:43 +00:00
|
|
|
#include "base/file_util.h"
|
|
|
|
#include "base/strings/sys_string_conversions.h"
|
|
|
|
|
|
|
|
namespace file_dialog {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void SetupDialog(NSSavePanel* dialog,
|
|
|
|
const std::string& title,
|
|
|
|
const base::FilePath& default_path) {
|
|
|
|
if (!title.empty())
|
|
|
|
[dialog setTitle:base::SysUTF8ToNSString(title)];
|
|
|
|
|
|
|
|
NSString* default_dir = nil;
|
|
|
|
NSString* default_filename = nil;
|
|
|
|
if (!default_path.empty()) {
|
2013-12-11 07:48:19 +00:00
|
|
|
if (base::DirectoryExists(default_path)) {
|
2013-05-20 13:46:43 +00:00
|
|
|
default_dir = base::SysUTF8ToNSString(default_path.value());
|
|
|
|
} else {
|
|
|
|
default_dir = base::SysUTF8ToNSString(default_path.DirName().value());
|
|
|
|
default_filename =
|
|
|
|
base::SysUTF8ToNSString(default_path.BaseName().value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (default_dir)
|
|
|
|
[dialog setDirectoryURL:[NSURL fileURLWithPath:default_dir]];
|
|
|
|
if (default_filename)
|
|
|
|
[dialog setNameFieldStringValue:default_filename];
|
|
|
|
|
2013-09-23 11:42:07 +00:00
|
|
|
[dialog setCanSelectHiddenExtension:YES];
|
2013-05-20 13:46:43 +00:00
|
|
|
[dialog setAllowsOtherFileTypes:YES];
|
|
|
|
}
|
|
|
|
|
2013-09-23 11:22:36 +00:00
|
|
|
void SetupDialogForProperties(NSOpenPanel* dialog, int properties) {
|
|
|
|
[dialog setCanChooseFiles:(properties & FILE_DIALOG_OPEN_FILE)];
|
|
|
|
if (properties & FILE_DIALOG_OPEN_DIRECTORY)
|
|
|
|
[dialog setCanChooseDirectories:YES];
|
|
|
|
if (properties & FILE_DIALOG_CREATE_DIRECTORY)
|
|
|
|
[dialog setCanCreateDirectories:YES];
|
|
|
|
if (properties & FILE_DIALOG_MULTI_SELECTIONS)
|
|
|
|
[dialog setAllowsMultipleSelection:YES];
|
|
|
|
}
|
|
|
|
|
2013-09-23 11:42:07 +00:00
|
|
|
// Run modal dialog with parent window and return user's choice.
|
|
|
|
int RunModalDialog(NSSavePanel* dialog, atom::NativeWindow* parent_window) {
|
|
|
|
__block int chosen = NSFileHandlingPanelCancelButton;
|
2014-07-31 02:31:08 +00:00
|
|
|
if (!parent_window || !parent_window->GetNativeWindow()) {
|
2013-09-23 11:42:07 +00:00
|
|
|
chosen = [dialog runModal];
|
|
|
|
} else {
|
|
|
|
NSWindow* window = parent_window->GetNativeWindow();
|
|
|
|
|
|
|
|
[dialog beginSheetModalForWindow:window
|
|
|
|
completionHandler:^(NSInteger c) {
|
|
|
|
chosen = c;
|
|
|
|
[NSApp stopModal];
|
|
|
|
}];
|
|
|
|
[NSApp runModalForWindow:window];
|
|
|
|
}
|
|
|
|
|
|
|
|
return chosen;
|
|
|
|
}
|
|
|
|
|
2013-09-23 11:22:36 +00:00
|
|
|
void ReadDialogPaths(NSOpenPanel* dialog, std::vector<base::FilePath>* paths) {
|
|
|
|
NSArray* urls = [dialog URLs];
|
|
|
|
for (NSURL* url in urls)
|
|
|
|
if ([url isFileURL])
|
|
|
|
paths->push_back(base::FilePath(base::SysNSStringToUTF8([url path])));
|
|
|
|
}
|
|
|
|
|
2013-05-20 13:46:43 +00:00
|
|
|
} // namespace
|
|
|
|
|
2013-09-23 08:27:22 +00:00
|
|
|
bool ShowOpenDialog(atom::NativeWindow* parent_window,
|
|
|
|
const std::string& title,
|
2013-05-20 13:46:43 +00:00
|
|
|
const base::FilePath& default_path,
|
2014-08-06 04:44:02 +00:00
|
|
|
const Filters& filters,
|
2013-05-20 13:46:43 +00:00
|
|
|
int properties,
|
|
|
|
std::vector<base::FilePath>* paths) {
|
|
|
|
DCHECK(paths);
|
|
|
|
NSOpenPanel* dialog = [NSOpenPanel openPanel];
|
|
|
|
|
|
|
|
SetupDialog(dialog, title, default_path);
|
2013-09-23 11:22:36 +00:00
|
|
|
SetupDialogForProperties(dialog, properties);
|
2013-05-20 13:46:43 +00:00
|
|
|
|
2013-09-23 11:42:07 +00:00
|
|
|
int chosen = RunModalDialog(dialog, parent_window);
|
2013-09-23 08:27:22 +00:00
|
|
|
if (chosen == NSFileHandlingPanelCancelButton)
|
2013-05-20 13:46:43 +00:00
|
|
|
return false;
|
|
|
|
|
2013-09-23 11:22:36 +00:00
|
|
|
ReadDialogPaths(dialog, paths);
|
2013-05-20 13:46:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-23 11:22:36 +00:00
|
|
|
void ShowOpenDialog(atom::NativeWindow* parent_window,
|
|
|
|
const std::string& title,
|
|
|
|
const base::FilePath& default_path,
|
2014-08-06 04:44:02 +00:00
|
|
|
const Filters& filters,
|
2013-09-23 11:22:36 +00:00
|
|
|
int properties,
|
|
|
|
const OpenDialogCallback& c) {
|
|
|
|
NSOpenPanel* dialog = [NSOpenPanel openPanel];
|
|
|
|
|
|
|
|
SetupDialog(dialog, title, default_path);
|
|
|
|
SetupDialogForProperties(dialog, properties);
|
|
|
|
|
|
|
|
// Duplicate the callback object here since c is a reference and gcd would
|
|
|
|
// only store the pointer, by duplication we can force gcd to store a copy.
|
|
|
|
__block OpenDialogCallback callback = c;
|
|
|
|
|
|
|
|
NSWindow* window = parent_window ? parent_window->GetNativeWindow() : NULL;
|
|
|
|
[dialog beginSheetModalForWindow:window
|
|
|
|
completionHandler:^(NSInteger chosen) {
|
|
|
|
if (chosen == NSFileHandlingPanelCancelButton) {
|
|
|
|
callback.Run(false, std::vector<base::FilePath>());
|
|
|
|
} else {
|
|
|
|
std::vector<base::FilePath> paths;
|
|
|
|
ReadDialogPaths(dialog, &paths);
|
|
|
|
callback.Run(true, paths);
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2013-09-23 11:36:52 +00:00
|
|
|
bool ShowSaveDialog(atom::NativeWindow* parent_window,
|
2013-05-20 13:46:43 +00:00
|
|
|
const std::string& title,
|
|
|
|
const base::FilePath& default_path,
|
2014-08-06 04:44:02 +00:00
|
|
|
const Filters& filters,
|
2013-05-20 13:46:43 +00:00
|
|
|
base::FilePath* path) {
|
|
|
|
DCHECK(path);
|
|
|
|
NSSavePanel* dialog = [NSSavePanel savePanel];
|
|
|
|
|
|
|
|
SetupDialog(dialog, title, default_path);
|
|
|
|
|
2013-09-23 11:42:07 +00:00
|
|
|
int chosen = RunModalDialog(dialog, parent_window);
|
2013-09-23 11:36:52 +00:00
|
|
|
if (chosen == NSFileHandlingPanelCancelButton || ![[dialog URL] isFileURL])
|
|
|
|
return false;
|
2013-05-20 13:46:43 +00:00
|
|
|
|
2013-09-23 11:36:52 +00:00
|
|
|
*path = base::FilePath(base::SysNSStringToUTF8([[dialog URL] path]));
|
|
|
|
return true;
|
2013-05-20 13:46:43 +00:00
|
|
|
}
|
|
|
|
|
2013-09-23 12:08:32 +00:00
|
|
|
void ShowSaveDialog(atom::NativeWindow* parent_window,
|
|
|
|
const std::string& title,
|
|
|
|
const base::FilePath& default_path,
|
2014-08-06 04:44:02 +00:00
|
|
|
const Filters& filters,
|
2013-09-23 12:08:32 +00:00
|
|
|
const SaveDialogCallback& c) {
|
|
|
|
NSSavePanel* dialog = [NSSavePanel savePanel];
|
|
|
|
|
|
|
|
SetupDialog(dialog, title, default_path);
|
|
|
|
|
|
|
|
__block SaveDialogCallback callback = c;
|
|
|
|
|
|
|
|
NSWindow* window = parent_window ? parent_window->GetNativeWindow() : NULL;
|
|
|
|
[dialog beginSheetModalForWindow:window
|
|
|
|
completionHandler:^(NSInteger chosen) {
|
|
|
|
if (chosen == NSFileHandlingPanelCancelButton) {
|
|
|
|
callback.Run(false, base::FilePath());
|
|
|
|
} else {
|
|
|
|
std::string path = base::SysNSStringToUTF8([[dialog URL] path]);
|
|
|
|
callback.Run(true, base::FilePath(path));
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2013-05-20 13:46:43 +00:00
|
|
|
} // namespace file_dialog
|