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
|
|
|
|
2013-09-23 06:29:55 +00:00
|
|
|
#include "base/bind.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 13:30:54 +00:00
|
|
|
#include "common/v8_conversions.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 {
|
|
|
|
|
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-09-23 14:52:48 +00:00
|
|
|
int type;
|
|
|
|
std::vector<std::string> buttons;
|
|
|
|
std::string title, message, detail;
|
|
|
|
if (!FromV8Arguments(args, &type, &buttons, &title, &message, &detail))
|
2013-05-03 13:03:26 +00:00
|
|
|
return node::ThrowTypeError("Bad argument");
|
|
|
|
|
2013-09-23 13:30:54 +00:00
|
|
|
NativeWindow* native_window = FromV8Value(args[5]);
|
|
|
|
v8::Persistent<v8::Function> callback = FromV8Value(args[6]);
|
2013-05-03 13:03:26 +00:00
|
|
|
|
2013-09-23 06:29:55 +00:00
|
|
|
if (callback.IsEmpty()) {
|
|
|
|
int chosen = atom::ShowMessageBox(
|
2013-09-23 13:30:54 +00:00
|
|
|
native_window,
|
|
|
|
(MessageBoxType)type,
|
|
|
|
buttons,
|
|
|
|
title,
|
|
|
|
message,
|
|
|
|
detail);
|
2013-09-23 06:29:55 +00:00
|
|
|
return scope.Close(v8::Integer::New(chosen));
|
|
|
|
} else {
|
|
|
|
atom::ShowMessageBox(
|
|
|
|
native_window,
|
2013-09-23 13:30:54 +00:00
|
|
|
(MessageBoxType)type,
|
2013-09-23 06:29:55 +00:00
|
|
|
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-09-23 14:52:48 +00:00
|
|
|
std::string title;
|
|
|
|
base::FilePath default_path;
|
|
|
|
int properties;
|
|
|
|
if (!FromV8Arguments(args, &title, &default_path, &properties))
|
2013-05-20 13:46:43 +00:00
|
|
|
return node::ThrowTypeError("Bad argument");
|
2013-05-03 11:31:24 +00:00
|
|
|
|
2013-09-23 13:30:54 +00:00
|
|
|
NativeWindow* native_window = FromV8Value(args[3]);
|
|
|
|
v8::Persistent<v8::Function> callback = FromV8Value(args[4]);
|
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,
|
2013-09-23 12:08:49 +00:00
|
|
|
base::Bind(&CallV8Function2<const std::vector<base::FilePath>&>,
|
2013-09-23 11:23:49 +00:00
|
|
|
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-09-23 14:52:48 +00:00
|
|
|
std::string title;
|
|
|
|
base::FilePath default_path;
|
|
|
|
if (!FromV8Arguments(args, &title, &default_path))
|
2013-05-03 11:31:24 +00:00
|
|
|
return node::ThrowTypeError("Bad argument");
|
|
|
|
|
2013-09-23 13:30:54 +00:00
|
|
|
NativeWindow* native_window = FromV8Value(args[2]);
|
|
|
|
v8::Persistent<v8::Function> callback = FromV8Value(args[3]);
|
2013-05-03 11:31:24 +00:00
|
|
|
|
2013-09-23 12:08:49 +00:00
|
|
|
if (callback.IsEmpty()) {
|
|
|
|
base::FilePath path;
|
|
|
|
if (!file_dialog::ShowSaveDialog(native_window,
|
|
|
|
title,
|
|
|
|
default_path,
|
|
|
|
&path))
|
|
|
|
return v8::Undefined();
|
2013-05-03 11:31:24 +00:00
|
|
|
|
2013-09-23 12:08:49 +00:00
|
|
|
return scope.Close(ToV8Value(path));
|
|
|
|
} else {
|
|
|
|
file_dialog::ShowSaveDialog(
|
|
|
|
native_window,
|
|
|
|
title,
|
|
|
|
default_path,
|
|
|
|
base::Bind(&CallV8Function2<const base::FilePath&>, callback));
|
|
|
|
return v8::Undefined();
|
|
|
|
}
|
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)
|