2013-05-03 11:31:24 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2013-05-03 13:03:26 +00:00
|
|
|
#include "browser/api/atom_api_dialog.h"
|
2013-05-03 11:31:24 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2013-09-23 06:29:55 +00:00
|
|
|
#include "base/bind.h"
|
2013-05-03 11:31:24 +00:00
|
|
|
#include "base/utf_string_conversions.h"
|
|
|
|
#include "base/values.h"
|
2013-05-05 12:36:46 +00:00
|
|
|
#include "browser/api/atom_api_window.h"
|
2013-05-03 11:31:24 +00:00
|
|
|
#include "browser/native_window.h"
|
2013-08-13 08:51:47 +00:00
|
|
|
#include "browser/ui/file_dialog.h"
|
|
|
|
#include "browser/ui/message_box.h"
|
2013-09-23 06:29:55 +00:00
|
|
|
#include "vendor/node/src/node_internals.h"
|
|
|
|
|
|
|
|
using node::node_isolate;
|
2013-05-03 11:31:24 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
base::FilePath V8ValueToFilePath(v8::Handle<v8::Value> path) {
|
|
|
|
std::string path_string(*v8::String::Utf8Value(path));
|
|
|
|
return base::FilePath::FromUTF8Unsafe(path_string);
|
|
|
|
}
|
|
|
|
|
2013-09-23 06:29:55 +00:00
|
|
|
v8::Handle<v8::Value> ToV8Value(const base::FilePath& path) {
|
2013-05-20 13:46:43 +00:00
|
|
|
std::string path_string(path.AsUTF8Unsafe());
|
|
|
|
return v8::String::New(path_string.data(), path_string.size());
|
|
|
|
}
|
|
|
|
|
2013-09-23 11:23:49 +00:00
|
|
|
v8::Handle<v8::Value> ToV8Value(void*) {
|
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
2013-09-23 06:29:55 +00:00
|
|
|
v8::Handle<v8::Value> ToV8Value(int code) {
|
|
|
|
return v8::Integer::New(code);
|
|
|
|
}
|
|
|
|
|
2013-09-23 11:23:49 +00:00
|
|
|
v8::Handle<v8::Value> ToV8Value(const std::vector<base::FilePath>& paths) {
|
|
|
|
v8::Handle<v8::Array> result = v8::Array::New(paths.size());
|
|
|
|
for (size_t i = 0; i < paths.size(); ++i)
|
|
|
|
result->Set(i, ToV8Value(paths[i]));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-09-23 06:29:55 +00:00
|
|
|
template<typename T>
|
|
|
|
void CallV8Function(v8::Persistent<v8::Function> callback, T arg) {
|
|
|
|
DCHECK(!callback.IsEmpty());
|
|
|
|
|
|
|
|
v8::HandleScope scope;
|
|
|
|
v8::Handle<v8::Value> value = ToV8Value(arg);
|
|
|
|
callback->Call(callback, 1, &value);
|
|
|
|
callback.Dispose(node_isolate);
|
|
|
|
}
|
|
|
|
|
2013-09-23 11:23:49 +00:00
|
|
|
template<typename T>
|
|
|
|
void CallV8Function2(v8::Persistent<v8::Function> callback,
|
|
|
|
bool result,
|
|
|
|
T arg) {
|
|
|
|
if (result)
|
|
|
|
return CallV8Function<T>(callback, arg);
|
|
|
|
else
|
|
|
|
return CallV8Function<void*>(callback, NULL);
|
|
|
|
}
|
|
|
|
|
2013-05-20 13:46:43 +00:00
|
|
|
void Initialize(v8::Handle<v8::Object> target) {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
|
|
|
NODE_SET_METHOD(target, "showMessageBox", ShowMessageBox);
|
|
|
|
NODE_SET_METHOD(target, "showOpenDialog", ShowOpenDialog);
|
|
|
|
NODE_SET_METHOD(target, "showSaveDialog", ShowSaveDialog);
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:31:24 +00:00
|
|
|
} // namespace
|
|
|
|
|
2013-05-03 13:03:26 +00:00
|
|
|
v8::Handle<v8::Value> ShowMessageBox(const v8::Arguments &args) {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
2013-05-18 02:47:06 +00:00
|
|
|
if (!args[0]->IsNumber() || // type
|
|
|
|
!args[1]->IsArray() || // buttons
|
|
|
|
!args[2]->IsString() || // title
|
|
|
|
!args[3]->IsString() || // message
|
|
|
|
!args[4]->IsString()) // detail
|
2013-05-03 13:03:26 +00:00
|
|
|
return node::ThrowTypeError("Bad argument");
|
|
|
|
|
2013-06-07 07:59:12 +00:00
|
|
|
NativeWindow* native_window = NULL;
|
|
|
|
if (args[5]->IsObject()) {
|
|
|
|
Window* window = Window::Unwrap<Window>(args[5]->ToObject());
|
|
|
|
if (!window || !window->window())
|
|
|
|
return node::ThrowError("Invalid window");
|
|
|
|
|
|
|
|
native_window = window->window();
|
|
|
|
}
|
|
|
|
|
2013-09-23 06:29:55 +00:00
|
|
|
v8::Persistent<v8::Function> callback;
|
|
|
|
if (args[6]->IsFunction()) {
|
|
|
|
callback = v8::Persistent<v8::Function>::New(
|
|
|
|
node_isolate,
|
|
|
|
v8::Local<v8::Function>::Cast(args[6]));
|
|
|
|
}
|
|
|
|
|
2013-05-18 02:47:06 +00:00
|
|
|
MessageBoxType type = (MessageBoxType)(args[0]->IntegerValue());
|
2013-05-03 13:03:26 +00:00
|
|
|
|
|
|
|
std::vector<std::string> buttons;
|
2013-05-18 02:47:06 +00:00
|
|
|
v8::Handle<v8::Array> v8_buttons = v8::Handle<v8::Array>::Cast(args[1]);
|
2013-05-03 13:03:26 +00:00
|
|
|
for (uint32_t i = 0; i < v8_buttons->Length(); ++i)
|
|
|
|
buttons.push_back(*v8::String::Utf8Value(v8_buttons->Get(i)));
|
|
|
|
|
2013-05-18 02:47:06 +00:00
|
|
|
std::string title(*v8::String::Utf8Value(args[2]));
|
|
|
|
std::string message(*v8::String::Utf8Value(args[3]));
|
|
|
|
std::string detail(*v8::String::Utf8Value(args[4]));
|
2013-05-03 13:03:26 +00:00
|
|
|
|
2013-09-23 06:29:55 +00:00
|
|
|
if (callback.IsEmpty()) {
|
|
|
|
int chosen = atom::ShowMessageBox(
|
|
|
|
native_window, type, buttons, title, message, detail);
|
|
|
|
return scope.Close(v8::Integer::New(chosen));
|
|
|
|
} else {
|
|
|
|
atom::ShowMessageBox(
|
|
|
|
native_window,
|
|
|
|
type,
|
|
|
|
buttons,
|
|
|
|
title,
|
|
|
|
message,
|
|
|
|
detail,
|
|
|
|
base::Bind(&CallV8Function<int>, callback));
|
|
|
|
return v8::Undefined();
|
|
|
|
}
|
2013-05-03 13:03:26 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 13:46:43 +00:00
|
|
|
v8::Handle<v8::Value> ShowOpenDialog(const v8::Arguments &args) {
|
|
|
|
v8::HandleScope scope;
|
2013-05-03 11:31:24 +00:00
|
|
|
|
2013-05-20 13:46:43 +00:00
|
|
|
if (!args[0]->IsString() || // title
|
|
|
|
!args[1]->IsString() || // default_path
|
|
|
|
!args[2]->IsNumber()) // properties
|
|
|
|
return node::ThrowTypeError("Bad argument");
|
2013-05-03 11:31:24 +00:00
|
|
|
|
2013-09-23 08:36:33 +00:00
|
|
|
NativeWindow* native_window = NULL;
|
|
|
|
if (args[3]->IsObject()) {
|
|
|
|
Window* window = Window::Unwrap<Window>(args[3]->ToObject());
|
|
|
|
if (!window || !window->window())
|
|
|
|
return node::ThrowError("Invalid window");
|
|
|
|
|
|
|
|
native_window = window->window();
|
|
|
|
}
|
|
|
|
|
2013-09-23 11:23:49 +00:00
|
|
|
v8::Persistent<v8::Function> callback;
|
|
|
|
if (args[4]->IsFunction()) {
|
|
|
|
callback = v8::Persistent<v8::Function>::New(
|
|
|
|
node_isolate,
|
|
|
|
v8::Local<v8::Function>::Cast(args[4]));
|
|
|
|
}
|
|
|
|
|
2013-05-20 13:46:43 +00:00
|
|
|
std::string title(*v8::String::Utf8Value(args[0]));
|
|
|
|
base::FilePath default_path(V8ValueToFilePath(args[1]));
|
|
|
|
int properties = args[2]->IntegerValue();
|
2013-05-03 11:31:24 +00:00
|
|
|
|
2013-09-23 11:23:49 +00:00
|
|
|
if (callback.IsEmpty()) {
|
|
|
|
std::vector<base::FilePath> paths;
|
|
|
|
if (!file_dialog::ShowOpenDialog(native_window,
|
|
|
|
title,
|
|
|
|
default_path,
|
|
|
|
properties,
|
|
|
|
&paths))
|
|
|
|
return v8::Undefined();
|
|
|
|
|
|
|
|
v8::Handle<v8::Array> result = v8::Array::New(paths.size());
|
|
|
|
for (size_t i = 0; i < paths.size(); ++i)
|
|
|
|
result->Set(i, ToV8Value(paths[i]));
|
|
|
|
|
|
|
|
return scope.Close(result);
|
|
|
|
} else {
|
|
|
|
file_dialog::ShowOpenDialog(
|
|
|
|
native_window,
|
|
|
|
title,
|
|
|
|
default_path,
|
|
|
|
properties,
|
|
|
|
base::Bind(&CallV8Function2<const std::vector<base::FilePath>>,
|
|
|
|
callback));
|
2013-05-20 13:46:43 +00:00
|
|
|
return v8::Undefined();
|
2013-09-23 11:23:49 +00:00
|
|
|
}
|
2013-05-03 11:31:24 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 13:46:43 +00:00
|
|
|
v8::Handle<v8::Value> ShowSaveDialog(const v8::Arguments &args) {
|
2013-05-03 11:31:24 +00:00
|
|
|
v8::HandleScope scope;
|
|
|
|
|
2013-05-05 12:36:46 +00:00
|
|
|
if (!args[0]->IsObject() || // window
|
2013-05-20 13:46:43 +00:00
|
|
|
!args[1]->IsString() || // title
|
|
|
|
!args[2]->IsString()) // default_path
|
2013-05-03 11:31:24 +00:00
|
|
|
return node::ThrowTypeError("Bad argument");
|
|
|
|
|
2013-05-05 12:36:46 +00:00
|
|
|
Window* window = Window::Unwrap<Window>(args[0]->ToObject());
|
|
|
|
if (!window || !window->window())
|
|
|
|
return node::ThrowError("Invalid window");
|
2013-05-03 11:31:24 +00:00
|
|
|
|
2013-05-20 13:46:43 +00:00
|
|
|
std::string title(*v8::String::Utf8Value(args[1]));
|
|
|
|
base::FilePath default_path(V8ValueToFilePath(args[2]));
|
2013-05-03 11:31:24 +00:00
|
|
|
|
2013-05-20 13:46:43 +00:00
|
|
|
base::FilePath path;
|
|
|
|
if (!file_dialog::ShowSaveDialog(window->window(),
|
|
|
|
title,
|
|
|
|
default_path,
|
|
|
|
&path))
|
|
|
|
return v8::Undefined();
|
2013-05-03 11:31:24 +00:00
|
|
|
|
2013-09-23 06:29:55 +00:00
|
|
|
return scope.Close(ToV8Value(path));
|
2013-05-03 11:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
2013-05-20 13:46:43 +00:00
|
|
|
NODE_MODULE(atom_browser_dialog, atom::api::Initialize)
|