]> khamidou — GitHub mirrors - electron.git/commitdiff
Add Shell API.
authorCheng Zhao <zcbenz@gmail.com>
Mon, 29 Apr 2013 13:57:05 +0000 (21:57 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 29 Apr 2013 13:57:05 +0000 (21:57 +0800)
Include APIs:
shell.showItemInFolder
shell.openItem
shell.openExternal
shell.moveItemToTrash

atom.gyp
common/api/atom_api_shell.cc [new file with mode: 0644]
common/api/atom_api_shell.h [new file with mode: 0644]
common/api/atom_extensions.h
common/api/lib/shell.coffee [new file with mode: 0644]
common/platform_util.h
common/platform_util_mac.mm

index 73f8e8dbf385ee791093734ccd833c0eccca5690..9813108306ba1e64b0ed71642db7b83be8dec706 100644 (file)
--- a/atom.gyp
+++ b/atom.gyp
@@ -13,6 +13,7 @@
       'browser/atom/objects_registry.coffee',
       'browser/atom/rpc_server.coffee',
       'common/api/lib/id_weak_map.coffee',
+      'common/api/lib/shell.coffee',
       'renderer/api/lib/ipc.coffee',
       'renderer/api/lib/remote.coffee',
     ],
@@ -47,6 +48,8 @@
       'common/api/atom_api_idle_gc.cc',
       'common/api/atom_api_id_weak_map.cc',
       'common/api/atom_api_id_weak_map.h',
+      'common/api/atom_api_shell.cc',
+      'common/api/atom_api_shell.h',
       'common/api/atom_api_v8_util.cc',
       'common/api/atom_bindings.cc',
       'common/api/atom_bindings.h',
         '.',
         'vendor',
       ],
+      'conditions': [
+        ['OS=="mac"', {
+          'link_settings': {
+            'libraries': [
+              '$(SDKROOT)/System/Library/Frameworks/Carbon.framework',
+            ],
+          },
+        }],
+      ],
     },
     {
       'target_name': 'generated_sources',
diff --git a/common/api/atom_api_shell.cc b/common/api/atom_api_shell.cc
new file mode 100644 (file)
index 0000000..5e99b3c
--- /dev/null
@@ -0,0 +1,95 @@
+// 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.
+
+#include "common/api/atom_api_shell.h"
+
+#include <string>
+
+#include "base/files/file_path.h"
+#include "common/platform_util.h"
+#include "googleurl/src/gurl.h"
+#include "vendor/node/src/node.h"
+
+namespace atom {
+
+namespace api {
+
+namespace {
+
+bool V8ValueToFilePath(v8::Handle<v8::Value> value, base::FilePath* path) {
+  if (!value->IsString())
+    return false;
+
+  std::string file_path_string(*v8::String::Utf8Value(value));
+  base::FilePath file_path = base::FilePath::FromUTF8Unsafe(file_path_string);
+
+  if (file_path.empty())
+    return false;
+
+  *path = file_path;
+  return true;
+}
+
+}  // namespace
+
+// static
+v8::Handle<v8::Value> Shell::ShowItemInFolder(const v8::Arguments &args) {
+  v8::HandleScope scope;
+
+  base::FilePath file_path;
+  if (!V8ValueToFilePath(args[0], &file_path))
+    return node::ThrowTypeError("Bad argument");
+
+  platform_util::ShowItemInFolder(file_path);
+  return v8::Undefined();
+}
+
+// static
+v8::Handle<v8::Value> Shell::OpenItem(const v8::Arguments &args) {
+  v8::HandleScope scope;
+
+  base::FilePath file_path;
+  if (!V8ValueToFilePath(args[0], &file_path))
+    return node::ThrowTypeError("Bad argument");
+
+  platform_util::OpenItem(file_path);
+  return v8::Undefined();
+}
+
+// static
+v8::Handle<v8::Value> Shell::OpenExternal(const v8::Arguments &args) {
+  v8::HandleScope scope;
+
+  if (!args[0]->IsString())
+    return node::ThrowTypeError("Bad argument");
+
+  platform_util::OpenExternal(GURL(*v8::String::Utf8Value(args[0])));
+  return v8::Undefined();
+}
+
+// static
+v8::Handle<v8::Value> Shell::MoveItemToTrash(const v8::Arguments &args) {
+  v8::HandleScope scope;
+
+  base::FilePath file_path;
+  if (!V8ValueToFilePath(args[0], &file_path))
+    return node::ThrowTypeError("Bad argument");
+
+  platform_util::MoveItemToTrash(file_path);
+  return v8::Undefined();
+}
+
+// static
+void Shell::Initialize(v8::Handle<v8::Object> target) {
+  node::SetMethod(target, "showItemInFolder", ShowItemInFolder);
+  node::SetMethod(target, "openItem", OpenItem);
+  node::SetMethod(target, "openExternal", OpenExternal);
+  node::SetMethod(target, "moveItemToTrash", MoveItemToTrash);
+}
+
+}  // namespace api
+
+}  // namespace atom
+
+NODE_MODULE(atom_common_shell, atom::api::Shell::Initialize)
diff --git a/common/api/atom_api_shell.h b/common/api/atom_api_shell.h
new file mode 100644 (file)
index 0000000..4188413
--- /dev/null
@@ -0,0 +1,32 @@
+// 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.
+
+#ifndef ATOM_COMMON_API_ATOM_API_SHELL_H_
+#define ATOM_COMMON_API_ATOM_API_SHELL_H_
+
+#include "base/basictypes.h"
+#include "v8/include/v8.h"
+
+namespace atom {
+
+namespace api {
+
+class Shell {
+ public:
+  static void Initialize(v8::Handle<v8::Object> target);
+
+ private:
+  static v8::Handle<v8::Value> ShowItemInFolder(const v8::Arguments &args);
+  static v8::Handle<v8::Value> OpenItem(const v8::Arguments &args);
+  static v8::Handle<v8::Value> OpenExternal(const v8::Arguments &args);
+  static v8::Handle<v8::Value> MoveItemToTrash(const v8::Arguments &args);
+
+  DISALLOW_IMPLICIT_CONSTRUCTORS(Shell);
+};
+
+}  // namespace api
+
+}  // namespace atom
+
+#endif  // ATOM_COMMON_API_ATOM_API_SHELL_H_
index 4967dd48094d8132db9b0c406f5b1ddb0856f50a..1aa20408a663d7b65693d386937567e802decab7 100644 (file)
@@ -20,6 +20,7 @@ NODE_EXT_LIST_ITEM(atom_renderer_ipc)
 // renderer processes.
 NODE_EXT_LIST_ITEM(atom_common_idle_gc)
 NODE_EXT_LIST_ITEM(atom_common_id_weak_map)
+NODE_EXT_LIST_ITEM(atom_common_shell)
 NODE_EXT_LIST_ITEM(atom_common_v8_util)
 
 NODE_EXT_LIST_END
diff --git a/common/api/lib/shell.coffee b/common/api/lib/shell.coffee
new file mode 100644 (file)
index 0000000..5fb935b
--- /dev/null
@@ -0,0 +1 @@
+module.exports = process.atomBinding 'shell'
index 9878b770a90c5fd6ee7201ee970081cd4f097a2c..5e8c561d5a2c29c9654bac5249677366ad1d74f8 100644 (file)
@@ -25,6 +25,9 @@ void OpenItem(const base::FilePath& full_path);
 // (For example, mailto: URLs in the default mail user agent.)
 void OpenExternal(const GURL& url);
 
+// Move a file to trash.
+void MoveItemToTrash(const base::FilePath& full_path);
+
 }  // platform_util
 
 #endif  // ATOM_COMMON_PLATFORM_UTIL_H_
index 2d4cfa0ff9ae85302f17090ac86c49634b9c0cbf..2652586c26ab27917c2b3bab4442f028c5d4febc 100644 (file)
@@ -126,4 +126,19 @@ void OpenExternal(const GURL& url) {
     LOG(WARNING) << "NSWorkspace failed to open URL " << url;
 }
 
+void MoveItemToTrash(const base::FilePath& full_path) {
+  DCHECK([NSThread isMainThread]);
+  NSString* path_string = base::SysUTF8ToNSString(full_path.value());
+  NSArray* file_array =
+      [NSArray arrayWithObject:[path_string lastPathComponent]];
+  if (!path_string || !file_array || ![[NSWorkspace sharedWorkspace]
+      performFileOperation:NSWorkspaceRecycleOperation
+                    source:[path_string stringByDeletingLastPathComponent]
+               destination:@""
+                     files:file_array
+                       tag:nil])
+    LOG(WARNING) << "NSWorkspace failed to move file " << full_path.value()
+                 << " to trash";
+}
+
 }  // namespace platform_util