From cbce6c89b72029f1c36c205988fe3b6e8309846f Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 16 Sep 2015 10:23:49 +0200 Subject: [PATCH] Now updates ~/.juci/config/config.json if a new version or additional fields are added by the juci team. --- src/config.cc | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/config.h | 2 ++ src/files.h | 3 ++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/config.cc b/src/config.cc index d7bcaa1..dc6f934 100644 --- a/src/config.cc +++ b/src/config.cc @@ -5,9 +5,14 @@ #include "files.h" #include "sourcefile.h" +#include //TODO: remove +using namespace std; //TODO: remove + MainConfig::MainConfig() { find_or_create_config_files(); boost::property_tree::json_parser::read_json(Singleton::config_dir() + "config.json", cfg); + update_config_file(); + Singleton::Config::window()->keybindings = cfg.get_child("keybindings"); GenerateSource(); GenerateDirectoryFilter(); @@ -41,6 +46,49 @@ void MainConfig::find_or_create_config_files() { juci::filesystem::write(juci_style_path, juci_dark_style); } +bool MainConfig::check_config_file(const boost::property_tree::ptree &default_cfg, std::string parent_path) { + if(parent_path.size()>0) + parent_path+="."; + bool exists=true; + for(auto &node: default_cfg) { + auto path=parent_path+node.first; + try { + cfg.get(path); + } + catch(const std::exception &e) { + cfg.add(path, node.second.data()); + exists=false; + } + try { + exists&=check_config_file(default_cfg.get_child(node.first), path); + } + catch(const std::exception &e) { + } + } + return exists; +} + +void MainConfig::update_config_file() { + std::stringstream ss; + ss << configjson; + boost::property_tree::ptree default_cfg; + boost::property_tree::read_json(ss, default_cfg); + bool cfg_ok=true; + try { + if(default_cfg.get("version")!=cfg.get("version")) { + cfg_ok=false; + if(cfg.count("version")>0) + cfg.find("version")->second.data()=default_cfg.get("version"); + } + } + catch(const std::exception &e) { + cfg_ok=false; + } + cfg_ok&=check_config_file(default_cfg); + if(!cfg_ok) + boost::property_tree::write_json(Singleton::config_dir()+"config.json", cfg); +} + void MainConfig::GenerateSource() { auto source_cfg = Singleton::Config::source(); auto source_json = cfg.get_child("source"); diff --git a/src/config.h b/src/config.h index b0b5afd..bb7337b 100644 --- a/src/config.h +++ b/src/config.h @@ -8,6 +8,8 @@ class MainConfig { public: MainConfig(); void find_or_create_config_files(); + bool check_config_file(const boost::property_tree::ptree &default_cfg, std::string parent_path=""); + void update_config_file(); void PrintMenu(); void GenerateSource(); void GenerateDirectoryFilter(); diff --git a/src/files.h b/src/files.h index 6ded01b..931d8fa 100644 --- a/src/files.h +++ b/src/files.h @@ -1,7 +1,7 @@ #include const std::string configjson = "{\n" -" \"version\": \"0.9.1\",\n" +" \"version\": \"0.9.2\",\n" " \"gtk_theme\": {\n" " \"name\": \"Adwaita\", //Use \"\" for default theme, At least these two exist on all systems: Adwaita, Raleigh\n" " \"variant\": \"\" //Use \"\" for default variant, and \"dark\" for dark theme variant\n" @@ -58,6 +58,7 @@ const std::string configjson = " \"edit_find\": \"f\",\n" " \"source_goto_line\": \"g\",\n" " \"source_center_cursor\": \"l\",\n" +" \"source_cycle_diagnostics\": \"e\",\n" " \"source_goto_declaration\": \"d\",\n" " \"source_goto_method\": \"m\",\n" " \"source_rename\": \"r\",\n"