Browse Source

Now updates ~/.juci/config/config.json if a new version or additional fields are added by the juci team.

merge-requests/365/head
eidheim 10 years ago
parent
commit
cbce6c89b7
  1. 48
      src/config.cc
  2. 2
      src/config.h
  3. 3
      src/files.h

48
src/config.cc

@ -5,9 +5,14 @@
#include "files.h"
#include "sourcefile.h"
#include <iostream> //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<std::string>(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<std::string>("version")!=cfg.get<std::string>("version")) {
cfg_ok=false;
if(cfg.count("version")>0)
cfg.find("version")->second.data()=default_cfg.get<std::string>("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");

2
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();

3
src/files.h

@ -1,7 +1,7 @@
#include <string>
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\": \"<primary>f\",\n"
" \"source_goto_line\": \"<primary>g\",\n"
" \"source_center_cursor\": \"<primary>l\",\n"
" \"source_cycle_diagnostics\": \"<primary>e\",\n"
" \"source_goto_declaration\": \"<primary>d\",\n"
" \"source_goto_method\": \"<primary>m\",\n"
" \"source_rename\": \"<primary>r\",\n"

Loading…
Cancel
Save