20 #include "mainconfigsource.h" 22 #include "configerror.h" 28 m_loader(std::move(loader)),
29 m_typeMapping(std::move(typeMapping)),
30 m_configsByGroup(std::map<std::string, std::vector<std::string>>()),
35 log_debug() <<
"Initializing main config source";
50 std::vector<StringConfig> MainConfigSource::stringConfigs()
52 return m_stringConfigs;
55 std::vector<IntConfig> MainConfigSource::intConfigs()
60 std::vector<BoolConfig> MainConfigSource::boolConfigs()
65 void MainConfigSource::loadConfig()
68 m_config = m_loader->loadConfig();
69 }
catch (Glib::Error &error) {
74 void MainConfigSource::readLoadedConfig()
77 std::vector<Glib::ustring> groups = m_config->get_groups();
78 for (
auto &group : groups) {
79 std::vector<Glib::ustring> keys = m_config->get_keys(group);
81 for (
auto &key : keys) {
82 m_configsByGroup[group].push_back(key);
85 }
catch (Glib::Error &error) {
90 void MainConfigSource::createConfigItems()
92 for (
auto &configGroup : m_configsByGroup) {
93 auto group = configGroup.first;
94 auto keys = configGroup.second;
96 for (
auto &key : keys) {
98 create(group, key, type);
103 ConfigType MainConfigSource::typeOf(
const std::string &group,
const std::string &key)
106 return m_typeMapping.at(std::make_pair(group, key));
107 }
catch (std::exception &error) {
108 std::string message =
"Unknown config " + group +
"::" + key;
109 log_error() << message;
114 void MainConfigSource::create(
const std::string &group,
const std::string &key,
ConfigType type)
117 case ConfigType::String:
118 m_stringConfigs.push_back(createConfig<StringConfig, std::string>(group, key));
120 case ConfigType::Integer:
121 m_intConfigs.push_back(createConfig<IntConfig, int>(group, key));
123 case ConfigType::Boolean:
124 m_boolConfigs.push_back(createConfig<BoolConfig, bool>(group, key));
131 template<
typename T,
typename U>
132 T MainConfigSource::createConfig(
const std::string &group,
const std::string &name)
134 U value = getGlibValue<U>(group, name);
135 T config(group, name, value);
136 config.setSource(ConfigSourceType::Main);
142 std::string MainConfigSource::getGlibValue(
const std::string &group,
const std::string &key)
const 145 std::string value = m_config->get_string(Glib::ustring(group), Glib::ustring(key));
147 }
catch (Glib::KeyFileError &error) {
148 std::string message =
"Could not parse string value: \"" + error.what() +
"\"";
149 log_error() << message;
157 int MainConfigSource::getGlibValue(
const std::string &group,
const std::string &key)
const 160 int value = m_config->get_integer(Glib::ustring(group), Glib::ustring(key));
162 }
catch (Glib::KeyFileError &error) {
163 std::string message =
"Could not parse integer value: \"" + error.what() +
"\"";
164 log_error() << message;
172 bool MainConfigSource::getGlibValue(
const std::string &group,
const std::string &key)
const 175 bool value = m_config->get_boolean(Glib::ustring(group), Glib::ustring(key));
177 }
catch (Glib::KeyFileError &error) {
178 std::string message =
"Could not parse boolean value: \"" + error.what() +
"\"";
179 log_error() << message;
An error occured in a Glib operation.
Represents a config item of type bool.
Represents a config item of type string.
ConfigType
Represents the type of a config value.
A config is found which is not defined in ConfigDefinition.
MainConfigSource(std::unique_ptr< ConfigLoader > loader, TypeMap typeMapping)
Developers guide to adding a config item:
Represents a config item of type int.
An error occured which is an internal error in the config code.