From 2ee625232de39584548ec4bad6193cf6434dfe7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Mon, 26 Oct 2015 16:00:11 +0100 Subject: [PATCH] Better lookup of home directory, fixing rest of paths that were concatinated incorrectly, removal of singletons that aren't singletons --- src/config.cc | 66 +++++++++++++++++++++++++++++++++-------------- src/config.h | 4 +++ src/files.h | 2 ++ src/filesystem.cc | 34 ------------------------ src/filesystem.h | 2 -- src/singletons.cc | 21 --------------- 6 files changed, 53 insertions(+), 76 deletions(-) diff --git a/src/config.cc b/src/config.cc index 8c121da..20d4e08 100644 --- a/src/config.cc +++ b/src/config.cc @@ -8,14 +8,24 @@ using namespace std; //TODO: remove MainConfig::MainConfig() { - find_or_create_config_files(); + auto search_envs = init_home_path(); + auto config_json = (home/"config"/"config.json").string(); // This causes some redundant copies, but assures windows support try { - boost::property_tree::json_parser::read_json(Singleton::config_json(), cfg); + find_or_create_config_files(); + if(home.empty()) { + std::string searched_envs = "["; + for(auto &env : search_envs) + searched_envs+=env+", "; + searched_envs.erase(searched_envs.end()-2, searched_envs.end()); + searched_envs+="]"; + throw std::runtime_error("One of these environment variables needs to point to a writable directory to save configuration." + searched_envs); + } + boost::property_tree::json_parser::read_json(config_json, cfg); update_config_file(); retrieve_config(); } catch(const std::exception &e) { - Singleton::terminal()->print("Error reading "+ Singleton::config_json()+": "+e.what()+"\n"); + Singleton::terminal()->print("Error reading "+ config_json + ": "+e.what()+"\n"); std::stringstream ss; ss << configjson; boost::property_tree::read_json(ss, cfg); @@ -24,27 +34,28 @@ MainConfig::MainConfig() { } void MainConfig::find_or_create_config_files() { - std::vector files = {"config.json", "plugins.py"}; - boost::filesystem::create_directories(boost::filesystem::path(Singleton::config_dir())); - for (auto &file : files) { - auto path = Singleton::config_dir(); - path /= file; - if (!boost::filesystem::is_regular_file(path)) { - if (file == "config.json") filesystem::write(path, configjson); - if (file == "plugins.py") filesystem::write(path, pluginspy); - } - } - - boost::filesystem::create_directories(boost::filesystem::path(Singleton::style_dir())); - auto juci_style_path=Singleton::style_dir(); + auto config_dir = home/"config"; + auto config_json = config_dir/"config.json"; + auto plugins_py = config_dir/"plugins.py"; + + boost::filesystem::create_directories(config_dir); // io exp captured by calling method + + if (!boost::filesystem::exists(config_json)) + filesystem::write(config_json, configjson); // vars configjson and pluginspy + if (!boost::filesystem::exists(plugins_py)) // live in files.h + filesystem::write(plugins_py, pluginspy); + + auto juci_style_path = home/"styles"; + boost::filesystem::create_directories(juci_style_path); // io exp captured by calling method + juci_style_path/="juci-light.xml"; if(!boost::filesystem::exists(juci_style_path)) filesystem::write(juci_style_path, juci_light_style); - juci_style_path=Singleton::style_dir(); + juci_style_path=juci_style_path.parent_path(); juci_style_path/="juci-dark.xml"; if(!boost::filesystem::exists(juci_style_path)) filesystem::write(juci_style_path, juci_dark_style); - juci_style_path=Singleton::style_dir(); + juci_style_path=juci_style_path.parent_path(); juci_style_path/="juci-dark-blue.xml"; if(!boost::filesystem::exists(juci_style_path)) filesystem::write(juci_style_path, juci_dark_blue_style); @@ -107,7 +118,7 @@ void MainConfig::update_config_file() { } cfg_ok&=check_config_file(default_cfg); if(!cfg_ok) { - boost::property_tree::write_json(Singleton::config_json(), cfg); + boost::property_tree::write_json((home/"config"/"config.json").string(), cfg); } } @@ -157,3 +168,20 @@ void MainConfig::GenerateDirectoryFilter() { dir_cfg->ignored.emplace_back(i.second.get_value()); JDEBUG("Directory filter fetched"); } +std::vector MainConfig::init_home_path(){ + std::vector locations = JUCI_ENV_SEARCH_LOCATIONS; + char *ptr = nullptr; + for (auto &env : locations) { + ptr=std::getenv(env.c_str()); + if (ptr==nullptr) + break; + else + if (boost::filesystem::exists(ptr)) { + home /= ".juci"; + home /= ptr; + return locations; + } + } + home=""; + return locations; +} diff --git a/src/config.h b/src/config.h index 190de24..47a032a 100644 --- a/src/config.h +++ b/src/config.h @@ -13,7 +13,11 @@ public: void PrintMenu(); void GenerateSource(); void GenerateDirectoryFilter(); + const boost::filesystem::path& juci_home_path() { return home; } const; + private: + std::vector init_home_path(); boost::property_tree::ptree cfg; + boost::filesystem::path home; }; #endif diff --git a/src/files.h b/src/files.h index 8f49573..39c7cc8 100644 --- a/src/files.h +++ b/src/files.h @@ -2,6 +2,8 @@ #define JUCI_VERSION "0.9.3" +#define JUCI_ENV_SEARCH_LOCATIONS {"AppData", "HOME", "JUCI_HOME"} + const std::string configjson = "{\n" " \"version\": \""+std::string(JUCI_VERSION)+"\",\n" diff --git a/src/filesystem.cc b/src/filesystem.cc index 7ea66a3..02d05fd 100644 --- a/src/filesystem.cc +++ b/src/filesystem.cc @@ -18,40 +18,6 @@ std::string 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); -} - -/** - * Returns home folder, empty on error - */ -boost::filesystem::path 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 { - JERROR("No write permissions in home, var:"); - DEBUG_VAR(home); - return ""; - } -} - -/** - * Returns tmp folder, empty on error. - */ -boost::filesystem::path filesystem::get_tmp_folder() { - boost::system::error_code code; - auto path = boost::filesystem::temp_directory_path(code); - if (code.value()!=0) { - return ""; - } - return path; -} - int filesystem::read(const std::string &path, Glib::RefPtr text_buffer) { std::ifstream input(path, std::ofstream::binary); diff --git a/src/filesystem.h b/src/filesystem.h index ab2921d..46bcc67 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -25,7 +25,5 @@ public: 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 boost::filesystem::path get_home_folder(); - static boost::filesystem::path get_tmp_folder(); }; #endif // JUCI_SOURCEFILE_H_ diff --git a/src/singletons.cc b/src/singletons.cc index 7f07185..32a44cb 100644 --- a/src/singletons.cc +++ b/src/singletons.cc @@ -39,24 +39,3 @@ Gtk::Label *Singleton::info() { return info_.get(); } -boost::filesystem::path Singleton::create_config_path(const std::string &subfolder) { - auto home = filesystem::get_home_folder(); - if(home.empty()) { - Singleton::terminal()->print("Could not find/write to home directory. Using defaults, no settings will be saved."); - home = filesystem::get_tmp_folder(); - if(home.empty()) { - std::string message("Please fix permissions of your home folder"); - std::cerr << message << std::endl; - JFATAL(message); - throw new std::exception; - } - } - home /= subfolder; - return home; -} - -boost::filesystem::path Singleton::config_dir() { return create_config_path({".juci", "config"}); } -boost::filesystem::path Singleton::log_dir() { return create_config_path({".juci", "log"}); } -boost::filesystem::path Singleton::style_dir() { return create_config_path({".juci", "styles"}); } -std::string Singleton::config_json() { return (Singleton::config_dir()/"config.json").string(); } -