source: nscp/service/settings_client.hpp @ 3b11e65

0.4.00.4.10.4.2
Last change on this file since 3b11e65 was 7e54a5f, checked in by Michael Medin <michael@…>, 2 years ago

Fixed a bunch of issues with the "old settings reader/importer"

  • Property mode set to 100644
File size: 5.8 KB
Line 
1#pragma once
2#include <settings/settings_core.hpp>
3#include "logger.hpp"
4
5class NSClientT;
6namespace nsclient {
7        class settings_client {
8                NSClient* core_;
9                std::wstring current_;
10                bool default_;
11
12
13        public:
14                settings_client(NSClient* core) : core_(core) {}
15
16                std::wstring get_source() {
17                        settings_manager::get_core()->get()->get_context();
18                }
19
20                void boot() {
21                        if (!current_.empty())
22                                core_->set_settings_context(current_);
23                        if (!core_->initCore(false)) {
24                                std::wcout << _T("Service *NOT* started!") << std::endl;
25                                return;
26                        }
27                        if (default_)
28                                settings_manager::get_core()->update_defaults();
29                }
30
31                void exit() {
32                        core_->exitCore(true);
33                }
34
35                void set_current(std::wstring current) { current_ = current; }
36                void set_update_defaults(bool def) { default_ = def; }
37
38                int migrate_from(std::wstring src) {
39                        try {
40                                debug_msg(_T("Migrating from: ") + src);
41                                core_->load_all_plugins(NSCAPI::dontStart);
42                                settings_manager::get_core()->migrate_from(src);
43                                return 1;
44                        } catch (settings::settings_exception e) {
45                                error_msg(_T("Failed to initialize settings: ") + e.getError());
46                        } catch (...) {
47                                error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM"));
48                        }
49                        return -1;
50                }
51                int migrate_to(std::wstring target) {
52                        try {
53                                debug_msg(_T("Migrating to: ") + target);
54                                core_->load_all_plugins(NSCAPI::dontStart);
55                                settings_manager::get_core()->migrate_to(target);
56                                return 1;
57                        } catch (settings::settings_exception e) {
58                                error_msg(_T("Failed to initialize settings: ") + e.getError());
59                        } catch (...) {
60                                error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM"));
61                        }
62                        return -1;
63                }
64
65                void dump_path(std::wstring root) {
66                        BOOST_FOREACH(std::wstring path, settings_manager::get_core()->get()->get_sections(root)) {
67                                if (!root.empty())
68                                        dump_path(root + _T("/") + path);
69                                else
70                                        dump_path(path);
71                        }
72                        BOOST_FOREACH(std::wstring key, settings_manager::get_core()->get()->get_keys(root)) {
73                                std::wcout << root << _T(".") << key << _T("=") << settings_manager::get_core()->get()->get_string(root, key) << std::endl;
74                        }
75                }
76
77
78                int generate(std::wstring target) {
79                        try {
80                                core_->load_all_plugins(NSCAPI::dontStart);
81                                settings_manager::get_core()->update_defaults();
82                                if (target == _T("settings") || target.empty()) {
83                                        settings_manager::get_core()->get()->save();
84                                } else if (target == _T("trac")) {
85                                        settings::string_list s = settings_manager::get_core()->get_reg_sections();
86                                        BOOST_FOREACH(std::wstring path, s) {
87                                                std::wcout << _T("== ") << path << _T(" ==") << std::endl;
88                                                settings::string_list k = settings_manager::get_core()->get_reg_keys(path);
89                                                bool first = true;
90                                                BOOST_FOREACH(std::wstring key, k) {
91                                                        settings::settings_core::key_description desc = settings_manager::get_core()->get_registred_key(path, key);
92                                                        if (!desc.advanced) {
93                                                                if (first)
94                                                                        std::wcout << _T("'''Normal settings'''") << std::endl;
95                                                                first = false;
96                                                                std::wcout << _T("||") << key << _T("||") << desc.defValue << _T("||") << desc.title << _T(": ") << desc.description << std::endl;
97                                                        }
98                                                }
99                                                first = true;
100                                                BOOST_FOREACH(std::wstring key, k) {
101                                                        settings::settings_core::key_description desc = settings_manager::get_core()->get_registred_key(path, key);
102                                                        if (desc.advanced) {
103                                                                if (first)
104                                                                        std::wcout << _T("'''Advanced settings'''") << std::endl;
105                                                                first = false;
106                                                                std::wcout << _T("||") << key << _T("||") << desc.defValue << _T("||") << desc.title << _T(": ") << desc.description << std::endl;
107                                                        }
108                                                }
109                                        }
110                                } else {
111                                        settings_manager::get_core()->update_defaults();
112                                        settings_manager::get_core()->get()->save_to(target);
113                                }
114                                return 1;
115                        } catch (settings::settings_exception e) {
116                                error_msg(_T("Failed to initialize settings: ") + e.getError());
117                        } catch (NSPluginException &e) {
118                                error_msg(_T("Failed to load plugins: ") + to_wstring(e.what()));
119                        } catch (std::exception &e) {
120                                error_msg(_T("Failed to initialize settings: ") + to_wstring(e.what()));
121                        } catch (...) {
122                                error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM"));
123                        }
124                        return -1;
125                }
126
127
128                int set(std::wstring path, std::wstring key, std::wstring val) {
129                        core_->load_all_plugins(NSCAPI::dontStart);
130                        settings::settings_core::key_type type = settings_manager::get_core()->get()->get_key_type(path, key);
131                        if (type == settings::settings_core::key_string) {
132                                settings_manager::get_core()->get()->set_string(path, key, val);
133                        } else if (type == settings::settings_core::key_integer) {
134                                settings_manager::get_core()->get()->set_int(path, key, strEx::stoi(val));
135                        } else if (type == settings::settings_core::key_bool) {
136                                settings_manager::get_core()->get()->set_bool(path, key, settings::settings_interface::string_to_bool(val));
137                        } else {
138                                error_msg(_T("Failed to set key (not found)"));
139                                return -1;
140                        }
141                        settings_manager::get_core()->get()->save();
142                        return 0;
143                }
144                int show(std::wstring path, std::wstring key) {
145                        //core_->load_all_plugins(NSCAPI::dontStart);
146                        std::wcout << settings_manager::get_core()->get()->get_string(path, key);
147                        return 0;
148                }
149                int list(std::wstring path) {
150
151                        try {
152                                dump_path(path);
153                        } catch (settings::settings_exception e) {
154                                error_msg(_T("Settings error: ") + e.getError());
155                        } catch (...) {
156                                error_msg(_T("FATAL ERROR IN SETTINGS SUBSYTEM"));
157                        }
158
159                        return 0;
160                }
161
162                void error_msg(std::wstring msg) {
163                        std::string s = nsclient::logger_helper::create_error(__FILE__, __LINE__, msg.c_str());
164                        core_->reportMessage(s.c_str());
165                }
166                void debug_msg(std::wstring msg) {
167                        std::string s = nsclient::logger_helper::create_info(__FILE__, __LINE__, msg.c_str());
168                        core_->reportMessage(s.c_str());
169                }
170        };
171}
Note: See TracBrowser for help on using the repository browser.