Browse Source

More robust path handling. Fixes some bugs on windows

merge-requests/365/head
Jørgen Lien Sellæg 10 years ago
parent
commit
3dfa3be423
  1. 20
      src/config.cc
  2. 6
      src/filesystem.cc
  3. 4
      src/filesystem.h
  4. 4
      src/juci.cc
  5. 19
      src/singletons.cc
  6. 9
      src/singletons.h
  7. 2
      src/source.cc
  8. 4
      src/window.cc

20
src/config.cc

@ -10,12 +10,12 @@ using namespace std; //TODO: remove
MainConfig::MainConfig() {
find_or_create_config_files();
try {
boost::property_tree::json_parser::read_json(Singleton::config_dir() + "config.json", cfg);
boost::property_tree::json_parser::read_json(Singleton::config_json(), cfg);
update_config_file();
retrieve_config();
}
catch(const std::exception &e) {
Singleton::terminal()->print("Error reading "+Singleton::config_dir() + "config.json: "+e.what()+"\n");
Singleton::terminal()->print("Error reading "+ Singleton::config_json()+": "+e.what()+"\n");
std::stringstream ss;
ss << configjson;
boost::property_tree::read_json(ss, cfg);
@ -27,7 +27,8 @@ void MainConfig::find_or_create_config_files() {
std::vector<std::string> files = {"config.json", "plugins.py"};
boost::filesystem::create_directories(boost::filesystem::path(Singleton::config_dir()));
for (auto &file : files) {
auto path = boost::filesystem::path(Singleton::config_dir() + file);
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);
@ -35,16 +36,16 @@ void MainConfig::find_or_create_config_files() {
}
boost::filesystem::create_directories(boost::filesystem::path(Singleton::style_dir()));
boost::filesystem::path juci_style_path=Singleton::style_dir();
juci_style_path+="juci-light.xml";
auto juci_style_path=Singleton::style_dir();
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-dark.xml";
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-dark-blue.xml";
juci_style_path/="juci-dark-blue.xml";
if(!boost::filesystem::exists(juci_style_path))
filesystem::write(juci_style_path, juci_dark_blue_style);
}
@ -105,8 +106,9 @@ void MainConfig::update_config_file() {
cfg_ok=false;
}
cfg_ok&=check_config_file(default_cfg);
if(!cfg_ok)
boost::property_tree::write_json(Singleton::config_dir()+"config.json", cfg);
if(!cfg_ok) {
boost::property_tree::write_json(Singleton::config_json(), cfg);
}
}
void MainConfig::GenerateSource() {

6
src/filesystem.cc

@ -26,7 +26,7 @@ std::string safe_get_env(const std::string &env) {
/**
* Returns home folder, empty on error
*/
std::string filesystem::get_home_folder() {
boost::filesystem::path filesystem::get_home_folder() {
auto home=safe_get_env("HOME");
if(home.empty())
home=safe_get_env("AppData");
@ -43,13 +43,13 @@ std::string filesystem::get_home_folder() {
/**
* Returns tmp folder, empty on error.
*/
std::string filesystem::get_tmp_folder() {
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.string();
return path;
}
int filesystem::read(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) {

4
src/filesystem.h

@ -25,7 +25,7 @@ public:
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 std::string get_home_folder();
static std::string get_tmp_folder();
static boost::filesystem::path get_home_folder();
static boost::filesystem::path get_tmp_folder();
};
#endif // JUCI_SOURCEFILE_H_

4
src/juci.cc

@ -5,7 +5,9 @@ using namespace std; //TODO: remove
void app::init_logging() {
boost::log::add_common_attributes();
boost::log::add_file_log(boost::log::keywords::file_name = Singleton::log_dir() + "juci.log",
auto log_dir = Singleton::log_dir();
log_dir /= "juci.log";
boost::log::add_file_log(boost::log::keywords::file_name = log_dir,
boost::log::keywords::auto_flush = true);
JINFO("Logging initalized");
}

19
src/singletons.cc

@ -39,9 +39,8 @@ Gtk::Label *Singleton::info() {
return info_.get();
}
std::string Singleton::create_config_path(const std::string &subfolder) {
boost::filesystem::path home;
home = filesystem::get_home_folder();
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();
@ -53,11 +52,15 @@ std::string Singleton::create_config_path(const std::string &subfolder) {
}
}
home /= subfolder;
return home.string();
return home;
}
std::string Singleton::config_dir() { return create_config_path(".juci/config"); }
std::string Singleton::log_dir() { return create_config_path(".juci/log"); }
std::string Singleton::style_dir() { return create_config_path(".juci/styles"); }
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() {
auto conf_dir = Singleton::config_dir();
conf_dir /= "config.json"; // to ensure correct paths on windows
return conf_dir.string();
}

9
src/singletons.h

@ -27,10 +27,11 @@ public:
static std::unique_ptr<Directories::Config> directories_;
static std::unique_ptr<Terminal::Config> terminal_;
};
static std::string create_config_path(const std::string &subfolder);
static std::string config_dir();
static std::string log_dir();
static std::string style_dir();
static boost::filesystem::path create_config_path(const std::string &subfolder);
static boost::filesystem::path config_dir();
static boost::filesystem::path log_dir();
static boost::filesystem::path style_dir();
static std::string config_json();
static Terminal *terminal();
static Directories *directories();
static Gtk::Label *status();

2
src/source.cc

@ -295,7 +295,7 @@ void Source::View::set_tab_char_and_size(char tab_char, unsigned tab_size) {
void Source::View::configure() {
//TODO: Move this to notebook? Might take up too much memory doing this for every tab.
auto style_scheme_manager=Gsv::StyleSchemeManager::get_default();
style_scheme_manager->prepend_search_path(Singleton::style_dir());
style_scheme_manager->prepend_search_path(Singleton::style_dir().string());
if(Singleton::Config::source()->style.size()>0) {
auto scheme = style_scheme_manager->get_scheme(Singleton::Config::source()->style);

4
src/window.cc

@ -259,13 +259,13 @@ void Window::create_menu() {
}
});
menu.action_group->add(Gtk::Action::create("Preferences", "Preferences..."), Gtk::AccelKey(menu.key_map["preferences"]), [this]() {
notebook.open(Singleton::config_dir()+"config.json");
notebook.open(Singleton::config_json());
});
menu.action_group->add(Gtk::Action::create("FileSave", "Save"), Gtk::AccelKey(menu.key_map["save"]), [this]() {
if(notebook.save_current()) {
if(notebook.get_current_page()!=-1) {
if(notebook.get_current_view()->file_path==Singleton::config_dir()+"config.json") {
if(notebook.get_current_view()->file_path==Singleton::config_json()) {
configure();
for(int c=0;c<notebook.size();c++) {
notebook.get_view(c)->configure();

Loading…
Cancel
Save