atom_api_dialog.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include <string>
  5. #include <utility>
  6. #include <vector>
  7. #include "atom/browser/api/atom_api_browser_window.h"
  8. #include "atom/browser/native_window.h"
  9. #include "atom/browser/ui/certificate_trust.h"
  10. #include "atom/browser/ui/file_dialog.h"
  11. #include "atom/browser/ui/message_box.h"
  12. #include "atom/common/native_mate_converters/callback.h"
  13. #include "atom/common/native_mate_converters/file_path_converter.h"
  14. #include "atom/common/native_mate_converters/image_converter.h"
  15. #include "atom/common/native_mate_converters/net_converter.h"
  16. #include "native_mate/dictionary.h"
  17. #include "atom/common/node_includes.h"
  18. namespace mate {
  19. template <>
  20. struct Converter<file_dialog::Filter> {
  21. static bool FromV8(v8::Isolate* isolate,
  22. v8::Local<v8::Value> val,
  23. file_dialog::Filter* out) {
  24. mate::Dictionary dict;
  25. if (!ConvertFromV8(isolate, val, &dict))
  26. return false;
  27. if (!dict.Get("name", &(out->first)))
  28. return false;
  29. if (!dict.Get("extensions", &(out->second)))
  30. return false;
  31. return true;
  32. }
  33. };
  34. template <>
  35. struct Converter<file_dialog::DialogSettings> {
  36. static bool FromV8(v8::Isolate* isolate,
  37. v8::Local<v8::Value> val,
  38. file_dialog::DialogSettings* out) {
  39. mate::Dictionary dict;
  40. if (!ConvertFromV8(isolate, val, &dict))
  41. return false;
  42. dict.Get("window", &(out->parent_window));
  43. dict.Get("title", &(out->title));
  44. dict.Get("message", &(out->message));
  45. dict.Get("buttonLabel", &(out->button_label));
  46. dict.Get("nameFieldLabel", &(out->name_field_label));
  47. dict.Get("defaultPath", &(out->default_path));
  48. dict.Get("filters", &(out->filters));
  49. dict.Get("properties", &(out->properties));
  50. dict.Get("showsTagField", &(out->shows_tag_field));
  51. #if defined(MAS_BUILD)
  52. dict.Get("securityScopedBookmarks", &(out->security_scoped_bookmarks));
  53. #endif
  54. return true;
  55. }
  56. };
  57. } // namespace mate
  58. namespace {
  59. void ShowMessageBox(int type,
  60. const std::vector<std::string>& buttons,
  61. int default_id,
  62. int cancel_id,
  63. int options,
  64. const std::string& title,
  65. const std::string& message,
  66. const std::string& detail,
  67. const std::string& checkbox_label,
  68. bool checkbox_checked,
  69. const gfx::ImageSkia& icon,
  70. atom::NativeWindow* window,
  71. mate::Arguments* args) {
  72. v8::Local<v8::Value> peek = args->PeekNext();
  73. atom::MessageBoxCallback callback;
  74. if (mate::Converter<atom::MessageBoxCallback>::FromV8(args->isolate(), peek,
  75. &callback)) {
  76. atom::ShowMessageBox(window, static_cast<atom::MessageBoxType>(type),
  77. buttons, default_id, cancel_id, options, title,
  78. message, detail, checkbox_label, checkbox_checked,
  79. icon, callback);
  80. } else {
  81. int chosen = atom::ShowMessageBox(
  82. window, static_cast<atom::MessageBoxType>(type), buttons, default_id,
  83. cancel_id, options, title, message, detail, icon);
  84. args->Return(chosen);
  85. }
  86. }
  87. void ShowOpenDialog(const file_dialog::DialogSettings& settings,
  88. mate::Arguments* args) {
  89. v8::Local<v8::Value> peek = args->PeekNext();
  90. file_dialog::OpenDialogCallback callback;
  91. if (mate::Converter<file_dialog::OpenDialogCallback>::FromV8(
  92. args->isolate(), peek, &callback)) {
  93. file_dialog::ShowOpenDialog(settings, callback);
  94. } else {
  95. std::vector<base::FilePath> paths;
  96. if (file_dialog::ShowOpenDialog(settings, &paths))
  97. args->Return(paths);
  98. }
  99. }
  100. void ShowSaveDialog(const file_dialog::DialogSettings& settings,
  101. mate::Arguments* args) {
  102. v8::Local<v8::Value> peek = args->PeekNext();
  103. file_dialog::SaveDialogCallback callback;
  104. if (mate::Converter<file_dialog::SaveDialogCallback>::FromV8(
  105. args->isolate(), peek, &callback)) {
  106. file_dialog::ShowSaveDialog(settings, callback);
  107. } else {
  108. base::FilePath path;
  109. if (file_dialog::ShowSaveDialog(settings, &path))
  110. args->Return(path);
  111. }
  112. }
  113. void Initialize(v8::Local<v8::Object> exports,
  114. v8::Local<v8::Value> unused,
  115. v8::Local<v8::Context> context,
  116. void* priv) {
  117. mate::Dictionary dict(context->GetIsolate(), exports);
  118. dict.SetMethod("showMessageBox", &ShowMessageBox);
  119. dict.SetMethod("showErrorBox", &atom::ShowErrorBox);
  120. dict.SetMethod("showOpenDialog", &ShowOpenDialog);
  121. dict.SetMethod("showSaveDialog", &ShowSaveDialog);
  122. #if defined(OS_MACOSX) || defined(OS_WIN)
  123. dict.SetMethod("showCertificateTrustDialog",
  124. &certificate_trust::ShowCertificateTrust);
  125. #endif
  126. }
  127. } // namespace
  128. NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_dialog, Initialize)