From 23be4f05d5c89239f47203ed1d466c814ab6fa89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Sat, 17 Oct 2015 12:04:11 +0200 Subject: [PATCH] Deduce home instead of setting it --- src/singletons.h | 16 +++++++--------- src/sourcefile.cc | 17 +++++++++++++++++ src/sourcefile.h | 1 + 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/singletons.h b/src/singletons.h index ed0d82d..9d96ec9 100644 --- a/src/singletons.h +++ b/src/singletons.h @@ -10,12 +10,7 @@ #include "menu.h" #include #include - -#ifdef _WIN32 -#define HOME "AppData" -#else -#define HOME "HOME" -#endif +#include "sourcefile.h" class Singleton { public: @@ -32,9 +27,12 @@ public: static std::unique_ptr directories_; static std::unique_ptr terminal_; }; - static std::string config_dir() { return std::string(getenv(HOME)) + "/.juci/config/"; } - static std::string log_dir() { return std::string(getenv(HOME)) + "/.juci/log/"; } - static std::string style_dir() { return std::string(getenv(HOME)) + "/.juci/styles/"; } + static std::string create_config_path(const std::string &subfolder) { + return juci::filesystem::get_home_folder() + subfolder; + } + static std::string config_dir() { return create_config_path("/.juci/config/"); } + static std::string log_dir() { return create_config_path("/.juci/log/"); } + static std::string style_dir() { return create_config_path("/.juci/styles/"); } static Terminal *terminal(); static Directories *directories(); static Gtk::Label *status(); diff --git a/src/sourcefile.cc b/src/sourcefile.cc index ef9fdd6..dd0bd1c 100644 --- a/src/sourcefile.cc +++ b/src/sourcefile.cc @@ -15,6 +15,23 @@ std::string juci::filesystem::read(const std::string &path) { return ss.str(); } +std::string safe_get_env(const std::string &env) { + auto ptr = std::getenv(env.c_str()); + return nullptr==ptr ? "" : std::string(ptr); +} + +std::string juci::filesystem::get_home_folder() { + auto home=safe_get_env("HOME"); + if(home.empty()) + home=safe_get_env("AppData"); + auto status = boost::filesystem::status(home); + if((status.permissions() & 0222)>=2) { + return home; + } else { + throw new std::exception; + } +} + int juci::filesystem::read(const std::string &path, Glib::RefPtr text_buffer) { std::ifstream input(path, std::ofstream::binary); diff --git a/src/sourcefile.h b/src/sourcefile.h index 4507ab7..ef53fbe 100644 --- a/src/sourcefile.h +++ b/src/sourcefile.h @@ -25,6 +25,7 @@ namespace juci { static bool write(const boost::filesystem::path &path) { return write(path, ""); }; static bool write(const std::string &path, Glib::RefPtr text_buffer); static bool write(const boost::filesystem::path &path, Glib::RefPtr text_buffer) { return write(path.string(), text_buffer); } + static std::string get_home_folder(); }; } // namepace juci #endif // JUCI_SOURCEFILE_H_