| 1 | #pragma once
|
|---|
| 2 | #include <settings/settings_core.hpp>
|
|---|
| 3 | #include <nsclient/logger.hpp>
|
|---|
| 4 | #include <json_spirit.h>
|
|---|
| 5 |
|
|---|
| 6 | class NSClientT;
|
|---|
| 7 | namespace nsclient {
|
|---|
| 8 | class settings_client {
|
|---|
| 9 | bool started_;
|
|---|
| 10 | NSClient* core_;
|
|---|
| 11 | std::wstring log_;
|
|---|
| 12 | bool default_;
|
|---|
| 13 | bool load_all_;
|
|---|
| 14 | std::wstring filter_;
|
|---|
| 15 |
|
|---|
| 16 | public:
|
|---|
| 17 | settings_client(NSClient* core, std::wstring log, bool update_defaults, bool load_all, std::wstring filter) : started_(false), core_(core), log_(log), default_(update_defaults), load_all_(load_all), filter_(filter) {
|
|---|
| 18 | startup();
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 | ~settings_client() {
|
|---|
| 23 | terminate();
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | void startup() {
|
|---|
| 27 | if (started_)
|
|---|
| 28 | return;
|
|---|
| 29 | if (!core_->boot_init(log_)) {
|
|---|
| 30 | std::wcout << _T("boot::init failed") << std::endl;
|
|---|
| 31 | return;
|
|---|
| 32 | }
|
|---|
| 33 | if (load_all_)
|
|---|
| 34 | core_->preboot_load_all_plugin_files();
|
|---|
| 35 |
|
|---|
| 36 | if (!core_->boot_load_all_plugins()) {
|
|---|
| 37 | std::wcout << _T("boot::load_all_plugins failed!") << std::endl;
|
|---|
| 38 | return;
|
|---|
| 39 | }
|
|---|
| 40 | if (!core_->boot_start_plugins(false)) {
|
|---|
| 41 | std::wcout << _T("boot::start_plugins failed!") << std::endl;
|
|---|
| 42 | return;
|
|---|
| 43 | }
|
|---|
| 44 | if (default_) {
|
|---|
| 45 | settings_manager::get_core()->update_defaults();
|
|---|
| 46 | }
|
|---|
| 47 | started_ = true;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | void terminate() {
|
|---|
| 51 | if (!started_)
|
|---|
| 52 | return;
|
|---|
| 53 | core_->stop_unload_plugins_pre();
|
|---|
| 54 | core_->stop_exit_pre();
|
|---|
| 55 | core_->stop_exit_post();
|
|---|
| 56 | started_ = false;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | int migrate_from(std::wstring src) {
|
|---|
| 60 | try {
|
|---|
| 61 | debug_msg(_T("Migrating from: ") + src);
|
|---|
| 62 | settings_manager::get_core()->migrate_from(src);
|
|---|
| 63 | return 1;
|
|---|
| 64 | } catch (settings::settings_exception e) {
|
|---|
| 65 | error_msg(_T("Failed to initialize settings: ") + e.getError());
|
|---|
| 66 | } catch (...) {
|
|---|
| 67 | error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM"));
|
|---|
| 68 | }
|
|---|
| 69 | return -1;
|
|---|
| 70 | }
|
|---|
| 71 | int migrate_to(std::wstring target) {
|
|---|
| 72 | try {
|
|---|
| 73 | debug_msg(_T("Migrating to: ") + target);
|
|---|
| 74 | settings_manager::get_core()->migrate_to(target);
|
|---|
| 75 | return 1;
|
|---|
| 76 | } catch (settings::settings_exception e) {
|
|---|
| 77 | error_msg(_T("Failed to initialize settings: ") + e.getError());
|
|---|
| 78 | } catch (...) {
|
|---|
| 79 | error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM"));
|
|---|
| 80 | }
|
|---|
| 81 | return -1;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | void dump_path(std::wstring root) {
|
|---|
| 85 | BOOST_FOREACH(std::wstring path, settings_manager::get_core()->get()->get_sections(root)) {
|
|---|
| 86 | if (!root.empty())
|
|---|
| 87 | dump_path(root + _T("/") + path);
|
|---|
| 88 | else
|
|---|
| 89 | dump_path(path);
|
|---|
| 90 | }
|
|---|
| 91 | BOOST_FOREACH(std::wstring key, settings_manager::get_core()->get()->get_keys(root)) {
|
|---|
| 92 | std::wcout << root << _T(".") << key << _T("=") << settings_manager::get_core()->get()->get_string(root, key) << std::endl;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | bool match_filter(std::wstring name) {
|
|---|
| 97 | return filter_.empty() || name.find(filter_) != std::wstring::npos;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 | int generate(std::wstring target) {
|
|---|
| 102 | try {
|
|---|
| 103 | if (target == _T("settings") || target.empty()) {
|
|---|
| 104 | settings_manager::get_core()->get()->save();
|
|---|
| 105 | } else if (target == _T("trac")) {
|
|---|
| 106 | settings::string_list s = settings_manager::get_core()->get_reg_sections();
|
|---|
| 107 | BOOST_FOREACH(std::wstring path, s) {
|
|---|
| 108 |
|
|---|
| 109 | settings::settings_core::path_description desc = settings_manager::get_core()->get_registred_path(path);
|
|---|
| 110 | std::wstring plugins;
|
|---|
| 111 | bool include = filter_.empty();
|
|---|
| 112 | BOOST_FOREACH(unsigned int i, desc.plugins) {
|
|---|
| 113 | std::wstring name = core_->get_plugin_module_name(i);
|
|---|
| 114 | if (match_filter(name))
|
|---|
| 115 | include = true;
|
|---|
| 116 | if (!plugins.empty())
|
|---|
| 117 | plugins += _T(", ");
|
|---|
| 118 | plugins += name;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | if (!include)
|
|---|
| 122 | continue;
|
|---|
| 123 |
|
|---|
| 124 | std::wcout << _T("== ") << path << _T(" ==") << std::endl;
|
|---|
| 125 | if (!desc.description.empty())
|
|---|
| 126 | std::wcout << desc.description << std::endl;
|
|---|
| 127 | std::wcout << _T("'''Used by:''' ") << plugins << std::endl;
|
|---|
| 128 | std::wcout << std::endl;
|
|---|
| 129 | settings::string_list k = settings_manager::get_core()->get_reg_keys(path);
|
|---|
| 130 | bool first = true;
|
|---|
| 131 | BOOST_FOREACH(std::wstring key, k) {
|
|---|
| 132 | settings::settings_core::key_description desc = settings_manager::get_core()->get_registred_key(path, key);
|
|---|
| 133 | if (!desc.advanced) {
|
|---|
| 134 | if (first)
|
|---|
| 135 | std::wcout << _T("'''Normal settings'''") << std::endl;
|
|---|
| 136 | first = false;
|
|---|
| 137 | strEx::replace(desc.description, _T("\n"), _T("\n|| || ||"));
|
|---|
| 138 | if (desc.defValue.empty())
|
|---|
| 139 | desc.defValue = _T(" ");
|
|---|
| 140 | std::wcout << _T("||") << key << _T("||") << desc.defValue << _T("||") << desc.title << _T(": ") << desc.description << std::endl;
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 | first = true;
|
|---|
| 144 | BOOST_FOREACH(std::wstring key, k) {
|
|---|
| 145 | settings::settings_core::key_description desc = settings_manager::get_core()->get_registred_key(path, key);
|
|---|
| 146 | if (desc.advanced) {
|
|---|
| 147 | if (first)
|
|---|
| 148 | std::wcout << _T("'''Advanced settings'''") << std::endl;
|
|---|
| 149 | first = false;
|
|---|
| 150 | std::wcout << _T("||") << key << _T("||") << desc.defValue << _T("||") << desc.title << _T(": ") << desc.description << std::endl;
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 | } else if (target == _T("doc")) {
|
|---|
| 155 | settings::string_list s = settings_manager::get_core()->get_reg_sections();
|
|---|
| 156 | BOOST_FOREACH(std::wstring path, s) {
|
|---|
| 157 |
|
|---|
| 158 | settings::settings_core::path_description desc = settings_manager::get_core()->get_registred_path(path);
|
|---|
| 159 | std::wstring plugins;
|
|---|
| 160 | bool include = filter_.empty();
|
|---|
| 161 | BOOST_FOREACH(unsigned int i, desc.plugins) {
|
|---|
| 162 | std::wstring name = core_->get_plugin_module_name(i);
|
|---|
| 163 | if (match_filter(name))
|
|---|
| 164 | include = true;
|
|---|
| 165 | if (!plugins.empty())
|
|---|
| 166 | plugins += _T(", ");
|
|---|
| 167 | plugins += name;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | if (!include)
|
|---|
| 171 | continue;
|
|---|
| 172 |
|
|---|
| 173 | std::wcout << path << std::endl;
|
|---|
| 174 | strEx::replace(desc.description, _T("\n"), _T("\n\t"));
|
|---|
| 175 | std::wcout << _T("\t") << desc.description << std::endl;
|
|---|
| 176 | std::wcout << _T("\tUsed by: ") << plugins << std::endl;
|
|---|
| 177 | std::wcout << std::endl;
|
|---|
| 178 | settings::string_list k = settings_manager::get_core()->get_reg_keys(path);
|
|---|
| 179 | bool first = true;
|
|---|
| 180 | BOOST_FOREACH(std::wstring key, k) {
|
|---|
| 181 | settings::settings_core::key_description desc = settings_manager::get_core()->get_registred_key(path, key);
|
|---|
| 182 | if (!desc.advanced) {
|
|---|
| 183 | if (first)
|
|---|
| 184 | std::wcout << _T("\tKeys:") << std::endl;
|
|---|
| 185 | first = false;
|
|---|
| 186 | std::wcout << _T("\t") << key << _T(" (=") << desc.defValue << _T(")") << std::endl;
|
|---|
| 187 | strEx::replace(desc.description, _T("\n"), _T("\n\t\t"));
|
|---|
| 188 | std::wcout << _T("\t\t") << desc.title << std::endl;
|
|---|
| 189 | std::wcout << _T("\t\t") << desc.description << std::endl;
|
|---|
| 190 | }
|
|---|
| 191 | }
|
|---|
| 192 | first = true;
|
|---|
| 193 | BOOST_FOREACH(std::wstring key, k) {
|
|---|
| 194 | settings::settings_core::key_description desc = settings_manager::get_core()->get_registred_key(path, key);
|
|---|
| 195 | if (desc.advanced) {
|
|---|
| 196 | if (first)
|
|---|
| 197 | std::wcout << _T("\tAdvanced keys:") << std::endl;
|
|---|
| 198 | first = false;
|
|---|
| 199 | std::wcout << _T("\t") << key << _T(" (=") << desc.defValue << _T(")") << std::endl;
|
|---|
| 200 | strEx::replace(desc.description, _T("\n"), _T("\n\t\t"));
|
|---|
| 201 | std::wcout << _T("\t\t") << desc.title << std::endl;
|
|---|
| 202 | std::wcout << _T("\t\t") << desc.description << std::endl;
|
|---|
| 203 | }
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 | } else if (target == _T("json") || target == _T("json-compact")) {
|
|---|
| 207 | json_spirit::wObject json_root;
|
|---|
| 208 | settings::string_list s = settings_manager::get_core()->get_reg_sections();
|
|---|
| 209 | BOOST_FOREACH(std::wstring path, s) {
|
|---|
| 210 |
|
|---|
| 211 | settings::settings_core::path_description desc = settings_manager::get_core()->get_registred_path(path);
|
|---|
| 212 | bool include = filter_.empty();
|
|---|
| 213 | json_spirit::wObject json_plugins;
|
|---|
| 214 | BOOST_FOREACH(unsigned int i, desc.plugins) {
|
|---|
| 215 | std::wstring name = core_->get_plugin_module_name(i);
|
|---|
| 216 | if (match_filter(name))
|
|---|
| 217 | include = true;
|
|---|
| 218 | json_plugins.push_back(json_spirit::wPair(strEx::itos(i), name));
|
|---|
| 219 | }
|
|---|
| 220 | if (!include)
|
|---|
| 221 | continue;
|
|---|
| 222 |
|
|---|
| 223 | json_spirit::wObject json_path;
|
|---|
| 224 | json_path.push_back(json_spirit::wPair(_T("path"), path));
|
|---|
| 225 | json_path.push_back(json_spirit::wPair(_T("title"), desc.title));
|
|---|
| 226 | json_path.push_back(json_spirit::wPair(_T("description"), desc.description));
|
|---|
| 227 | json_path.push_back(json_spirit::wPair(_T("plugins"), json_plugins));
|
|---|
| 228 |
|
|---|
| 229 | json_spirit::wObject json_keys;
|
|---|
| 230 | BOOST_FOREACH(std::wstring key, settings_manager::get_core()->get_reg_keys(path)) {
|
|---|
| 231 | settings::settings_core::key_description desc = settings_manager::get_core()->get_registred_key(path, key);
|
|---|
| 232 | json_spirit::wObject json_key;
|
|---|
| 233 | json_key.push_back(json_spirit::wPair(_T("key"), key));
|
|---|
| 234 | json_key.push_back(json_spirit::wPair(_T("title"), desc.title));
|
|---|
| 235 | json_key.push_back(json_spirit::wPair(_T("description"), desc.description));
|
|---|
| 236 | json_key.push_back(json_spirit::wPair(_T("default value"), desc.defValue));
|
|---|
| 237 | json_keys.push_back(json_spirit::wPair(key, json_key));
|
|---|
| 238 | }
|
|---|
| 239 | json_path.push_back(json_spirit::wPair(_T("keys"), json_keys));
|
|---|
| 240 | json_root.push_back(json_spirit::wPair(path, json_path));
|
|---|
| 241 | }
|
|---|
| 242 | if (target == _T("json-compact"))
|
|---|
| 243 | write(json_root, std::wcout);
|
|---|
| 244 | else
|
|---|
| 245 | write(json_root, std::wcout, json_spirit::pretty_print);
|
|---|
| 246 | } else {
|
|---|
| 247 | //settings_manager::get_core()->update_defaults();
|
|---|
| 248 | settings_manager::get_core()->get()->save_to(target);
|
|---|
| 249 | }
|
|---|
| 250 | return 1;
|
|---|
| 251 | } catch (settings::settings_exception e) {
|
|---|
| 252 | error_msg(_T("Failed to initialize settings: ") + e.getError());
|
|---|
| 253 | } catch (NSPluginException &e) {
|
|---|
| 254 | error_msg(_T("Failed to load plugins: ") + to_wstring(e.what()));
|
|---|
| 255 | } catch (std::exception &e) {
|
|---|
| 256 | error_msg(_T("Failed to initialize settings: ") + to_wstring(e.what()));
|
|---|
| 257 | } catch (...) {
|
|---|
| 258 | error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM"));
|
|---|
| 259 | }
|
|---|
| 260 | return -1;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 | void switch_context(std::wstring contect) {
|
|---|
| 265 | settings_manager::get_core()->set_primary(contect);
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | int set(std::wstring path, std::wstring key, std::wstring val) {
|
|---|
| 269 | settings::settings_core::key_type type = settings_manager::get_core()->get()->get_key_type(path, key);
|
|---|
| 270 | if (type == settings::settings_core::key_string) {
|
|---|
| 271 | settings_manager::get_core()->get()->set_string(path, key, val);
|
|---|
| 272 | } else if (type == settings::settings_core::key_integer) {
|
|---|
| 273 | settings_manager::get_core()->get()->set_int(path, key, strEx::stoi(val));
|
|---|
| 274 | } else if (type == settings::settings_core::key_bool) {
|
|---|
| 275 | settings_manager::get_core()->get()->set_bool(path, key, settings::settings_interface::string_to_bool(val));
|
|---|
| 276 | } else {
|
|---|
| 277 | error_msg(_T("Failed to set key (not found)"));
|
|---|
| 278 | return -1;
|
|---|
| 279 | }
|
|---|
| 280 | settings_manager::get_core()->get()->save();
|
|---|
| 281 | return 0;
|
|---|
| 282 | }
|
|---|
| 283 | int show(std::wstring path, std::wstring key) {
|
|---|
| 284 | std::wcout << settings_manager::get_core()->get()->get_string(path, key);
|
|---|
| 285 | return 0;
|
|---|
| 286 | }
|
|---|
| 287 | int list(std::wstring path) {
|
|---|
| 288 | try {
|
|---|
| 289 | dump_path(path);
|
|---|
| 290 | } catch (settings::settings_exception e) {
|
|---|
| 291 | error_msg(_T("Settings error: ") + e.getError());
|
|---|
| 292 | } catch (...) {
|
|---|
| 293 | error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM"));
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | return 0;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | void error_msg(std::wstring msg) {
|
|---|
| 300 | nsclient::logging::logger::get_logger()->error(_T("client"), __FILE__, __LINE__, msg.c_str());
|
|---|
| 301 | }
|
|---|
| 302 | void debug_msg(std::wstring msg) {
|
|---|
| 303 | nsclient::logging::logger::get_logger()->debug(_T("client"), __FILE__, __LINE__, msg.c_str());
|
|---|
| 304 | }
|
|---|
| 305 | };
|
|---|
| 306 | } |
|---|