From be6599807d83e8fd59fd4584ce96a33a9a8c86ee Mon Sep 17 00:00:00 2001 From: Corne Dorrestijn Date: Tue, 12 Aug 2014 17:01:56 +0200 Subject: [PATCH 1/5] Added getAppData function to the app api --- atom/browser/api/atom_api_app.cc | 26 +++++++++++++++++++++++++- atom/browser/api/atom_api_app.h | 1 + 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 6ea5464d6f3..19e6e677b4b 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -8,12 +8,19 @@ #include "base/values.h" #include "base/command_line.h" +#include "base/environment.h" +#include "base/files/file_path.h" +#include "base/path_service.h" #include "atom/browser/browser.h" #include "native_mate/dictionary.h" #include "native_mate/object_template_builder.h" #include "atom/common/node_includes.h" +#if defined(OS_LINUX) +#include "base/nix/xdg_util.h" +#endif + using atom::Browser; namespace atom { @@ -60,6 +67,22 @@ void App::OnFinishLaunching() { Emit("ready"); } +std::string App::GetDataPath() { + base::FilePath path; +#if defined(OS_LINUX) + scoped_ptr env(base::Environment::Create()); + path = base::nix::GetXDGDirectory(env.get(), + base::nix::kXdgConfigHomeEnvVar, + base::nix::kDotConfigDir); +#else + CHECK(PathService::Get(base::DIR_APP_DATA, &path)); +#endif + + base::FilePath dataPath = path.Append(base::FilePath::FromUTF8Unsafe(Browser::Get()->GetName())); + + return dataPath.value(); +} + mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder( v8::Isolate* isolate) { Browser* browser = Browser::Get(); @@ -75,7 +98,8 @@ mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder( .SetMethod("getName", base::Bind(&Browser::GetName, base::Unretained(browser))) .SetMethod("setName", base::Bind(&Browser::SetName, - base::Unretained(browser))); + base::Unretained(browser))) + .SetMethod("getDataPath", &App::GetDataPath); } // static diff --git a/atom/browser/api/atom_api_app.h b/atom/browser/api/atom_api_app.h index 6a5a58edb79..ae11123eb6d 100644 --- a/atom/browser/api/atom_api_app.h +++ b/atom/browser/api/atom_api_app.h @@ -34,6 +34,7 @@ class App : public mate::EventEmitter, virtual void OnActivateWithNoOpenWindows() OVERRIDE; virtual void OnWillFinishLaunching() OVERRIDE; virtual void OnFinishLaunching() OVERRIDE; + virtual std::string GetDataPath(); // mate::Wrappable implementations: virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder( From dec05988f40153fb5a9ce0e9661eca65fbebb1dd Mon Sep 17 00:00:00 2001 From: Corne Dorrestijn Date: Wed, 13 Aug 2014 14:16:55 +0200 Subject: [PATCH 2/5] Fixes for windows --- atom/browser/api/atom_api_app.cc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 19e6e677b4b..17ce3435882 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -78,9 +78,16 @@ std::string App::GetDataPath() { CHECK(PathService::Get(base::DIR_APP_DATA, &path)); #endif - base::FilePath dataPath = path.Append(base::FilePath::FromUTF8Unsafe(Browser::Get()->GetName())); + base::FilePath data_path = path.Append( + base::FilePath::FromUTF8Unsafe(Browser::Get()->GetName())); - return dataPath.value(); + // FilePath.value() returns a std::wstring in windows and + // std::string on other platforms. + std::vector writable(data_path.value().begin(), + data_path.value().end()); + writable.push_back('\0'); + + return &writable[0]; } mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder( From e9879a3e4b1f6215003c21781f133491e8fd5936 Mon Sep 17 00:00:00 2001 From: Corne Dorrestijn Date: Wed, 13 Aug 2014 14:24:35 +0200 Subject: [PATCH 3/5] Removed whitespace and added vector import --- atom/browser/api/atom_api_app.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 17ce3435882..71d3fadf574 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -5,6 +5,7 @@ #include "atom/browser/api/atom_api_app.h" #include +#include #include "base/values.h" #include "base/command_line.h" @@ -81,7 +82,7 @@ std::string App::GetDataPath() { base::FilePath data_path = path.Append( base::FilePath::FromUTF8Unsafe(Browser::Get()->GetName())); - // FilePath.value() returns a std::wstring in windows and + // FilePath.value() returns a std::wstring in windows and // std::string on other platforms. std::vector writable(data_path.value().begin(), data_path.value().end()); From 54ee12308dc940829c00b70eb44d6874737f98dc Mon Sep 17 00:00:00 2001 From: Corne Dorrestijn Date: Fri, 15 Aug 2014 16:52:16 +0200 Subject: [PATCH 4/5] reused converted and fixed indentation --- atom/browser/api/atom_api_app.cc | 12 +++--------- atom/browser/api/atom_api_app.h | 3 ++- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 71d3fadf574..fa0ef84b813 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -68,7 +68,7 @@ void App::OnFinishLaunching() { Emit("ready"); } -std::string App::GetDataPath() { +base::FilePath App::GetDataPath() { base::FilePath path; #if defined(OS_LINUX) scoped_ptr env(base::Environment::Create()); @@ -80,15 +80,9 @@ std::string App::GetDataPath() { #endif base::FilePath data_path = path.Append( - base::FilePath::FromUTF8Unsafe(Browser::Get()->GetName())); + base::FilePath::FromUTF8Unsafe(Browser::Get()->GetName())); - // FilePath.value() returns a std::wstring in windows and - // std::string on other platforms. - std::vector writable(data_path.value().begin(), - data_path.value().end()); - writable.push_back('\0'); - - return &writable[0]; + return data_path; } mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder( diff --git a/atom/browser/api/atom_api_app.h b/atom/browser/api/atom_api_app.h index ae11123eb6d..d0cbddaadff 100644 --- a/atom/browser/api/atom_api_app.h +++ b/atom/browser/api/atom_api_app.h @@ -10,6 +10,7 @@ #include "base/compiler_specific.h" #include "atom/browser/api/event_emitter.h" #include "atom/browser/browser_observer.h" +#include "atom/common/native_mate_converters/file_path_converter.h" #include "native_mate/handle.h" namespace atom { @@ -34,7 +35,7 @@ class App : public mate::EventEmitter, virtual void OnActivateWithNoOpenWindows() OVERRIDE; virtual void OnWillFinishLaunching() OVERRIDE; virtual void OnFinishLaunching() OVERRIDE; - virtual std::string GetDataPath(); + virtual base::FilePath GetDataPath(); // mate::Wrappable implementations: virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder( From 3afb4e017c9793c5902bdd52d557d30dc8ea2853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corn=C3=A9=20Dorrestijn?= Date: Fri, 15 Aug 2014 17:29:36 +0200 Subject: [PATCH 5/5] Added the getDataPath function to the app api docs --- docs/api/app.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/api/app.md b/docs/api/app.md index 7a6488556ed..2c1288a4266 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -87,6 +87,15 @@ executed. It is possible that a window cancels the quitting by returning Quit the application directly, it will not try to close all windows so cleanup code will not run. +## app.getDataPath() + +Returns the right data path for the OS appended with the app name. + + * `%APPDATA%\My App Name` (`C:\Users\[username]\AppData\Roaming\My App Name`) + in Windows + * `~/.config/My App Name` in Linux + * `/Users/[username]/Library/Application Support/My App Name` in OS X + ## app.getVersion() Returns the version of loaded application, if no version is found in