Browse Source

Better lookup of home directory, fixing rest of paths that were concatinated incorrectly, removal of singletons that aren't singletons

merge-requests/365/head
Jørgen Lien Sellæg 10 years ago
parent
commit
2ee625232d
  1. 64
      src/config.cc
  2. 4
      src/config.h
  3. 2
      src/files.h
  4. 34
      src/filesystem.cc
  5. 2
      src/filesystem.h
  6. 21
      src/singletons.cc

64
src/config.cc

@ -8,14 +8,24 @@
using namespace std; //TODO: remove using namespace std; //TODO: remove
MainConfig::MainConfig() { 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 { 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(); update_config_file();
retrieve_config(); retrieve_config();
} }
catch(const std::exception &e) { 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; std::stringstream ss;
ss << configjson; ss << configjson;
boost::property_tree::read_json(ss, cfg); boost::property_tree::read_json(ss, cfg);
@ -24,27 +34,28 @@ MainConfig::MainConfig() {
} }
void MainConfig::find_or_create_config_files() { void MainConfig::find_or_create_config_files() {
std::vector<std::string> files = {"config.json", "plugins.py"}; auto config_dir = home/"config";
boost::filesystem::create_directories(boost::filesystem::path(Singleton::config_dir())); auto config_json = config_dir/"config.json";
for (auto &file : files) { auto plugins_py = config_dir/"plugins.py";
auto path = Singleton::config_dir();
path /= file; boost::filesystem::create_directories(config_dir); // io exp captured by calling method
if (!boost::filesystem::is_regular_file(path)) {
if (file == "config.json") filesystem::write(path, configjson); if (!boost::filesystem::exists(config_json))
if (file == "plugins.py") filesystem::write(path, pluginspy); 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
boost::filesystem::create_directories(boost::filesystem::path(Singleton::style_dir()));
auto juci_style_path=Singleton::style_dir();
juci_style_path/="juci-light.xml"; juci_style_path/="juci-light.xml";
if(!boost::filesystem::exists(juci_style_path)) if(!boost::filesystem::exists(juci_style_path))
filesystem::write(juci_style_path, juci_light_style); 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"; juci_style_path/="juci-dark.xml";
if(!boost::filesystem::exists(juci_style_path)) if(!boost::filesystem::exists(juci_style_path))
filesystem::write(juci_style_path, juci_dark_style); 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"; juci_style_path/="juci-dark-blue.xml";
if(!boost::filesystem::exists(juci_style_path)) if(!boost::filesystem::exists(juci_style_path))
filesystem::write(juci_style_path, juci_dark_blue_style); 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); cfg_ok&=check_config_file(default_cfg);
if(!cfg_ok) { 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<std::string>()); dir_cfg->ignored.emplace_back(i.second.get_value<std::string>());
JDEBUG("Directory filter fetched"); JDEBUG("Directory filter fetched");
} }
std::vector<std::string> MainConfig::init_home_path(){
std::vector<std::string> 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;
}

4
src/config.h

@ -13,7 +13,11 @@ public:
void PrintMenu(); void PrintMenu();
void GenerateSource(); void GenerateSource();
void GenerateDirectoryFilter(); void GenerateDirectoryFilter();
const boost::filesystem::path& juci_home_path() { return home; } const;
private: private:
std::vector<std::string> init_home_path();
boost::property_tree::ptree cfg; boost::property_tree::ptree cfg;
boost::filesystem::path home;
}; };
#endif #endif

2
src/files.h

@ -2,6 +2,8 @@
#define JUCI_VERSION "0.9.3" #define JUCI_VERSION "0.9.3"
#define JUCI_ENV_SEARCH_LOCATIONS {"AppData", "HOME", "JUCI_HOME"}
const std::string configjson = const std::string configjson =
"{\n" "{\n"
" \"version\": \""+std::string(JUCI_VERSION)+"\",\n" " \"version\": \""+std::string(JUCI_VERSION)+"\",\n"

34
src/filesystem.cc

@ -18,40 +18,6 @@ std::string filesystem::read(const std::string &path) {
return ss.str(); 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<Gtk::TextBuffer> text_buffer) { int filesystem::read(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) {
std::ifstream input(path, std::ofstream::binary); std::ifstream input(path, std::ofstream::binary);

2
src/filesystem.h

@ -25,7 +25,5 @@ public:
static bool write(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer); static bool write(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer);
static bool write(const boost::filesystem::path &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) { return write(path.string(), text_buffer); } static bool write(const boost::filesystem::path &path, Glib::RefPtr<Gtk::TextBuffer> 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_ #endif // JUCI_SOURCEFILE_H_

21
src/singletons.cc

@ -39,24 +39,3 @@ Gtk::Label *Singleton::info() {
return info_.get(); 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(); }

Loading…
Cancel
Save