From: Rafał Długołęcki Date: Sat, 30 Jan 2016 18:18:28 +0000 (+0100) Subject: Implement saving of configuration files in its own class X-Git-Tag: 3.4-b1~11 X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=b176969d4b09abbb194e8bf49aeeb326b4250007;p=vlp.git Implement saving of configuration files in its own class --- diff --git a/Makefile.am b/Makefile.am index 5a43ea4..d19edf8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -234,14 +234,16 @@ bin_lgconfig_SOURCES = \ src/lgconfig/AddNodeDialog.cpp \ src/lgconfig/AddNodeDialog.moc.cpp \ src/lgconfig/NodeListDialog.cpp \ - src/lgconfig/NodeListDialog.moc.cpp + src/lgconfig/NodeListDialog.moc.cpp \ + src/global/vlp/config.cpp bin_lgconfig_CPPFLAGS = $(bin_lgconfig_CFLAGS) bin_lgconfig_LDADD = $(bin_lgconfig_LIBS) -lconfig++ bin_logedit_HEADERS = \ src/lgconfig/lgconfig.h \ src/lgconfig/ui/VLPConfigWindow.h \ src/lgconfig/ui/dialogs/AddNodeDialog.h \ - src/lgconfig/ui/dialogs/NodeListDialog.h + src/lgconfig/ui/dialogs/NodeListDialog.h \ + src/global/vlp/config.h src/lgconfig/lgconfig.moc.cpp: \ src/lgconfig/ui/VLPConfigWindow.h \ diff --git a/src/global/vlp/config.cpp b/src/global/vlp/config.cpp index 2191cd8..701b3e4 100644 --- a/src/global/vlp/config.cpp +++ b/src/global/vlp/config.cpp @@ -45,7 +45,15 @@ bool Config::load(const char * configFilePath) bool Config::save(const char * configFilePath) { - return false; + if(!config_write_file(&cfg, configFilePath)) { + fprintf(stderr, + "Error: Cannot save configuration file: %s!\n", + configFilePath + ); + return false; + } + + return true; } int Config::getNodeNumber() @@ -67,6 +75,19 @@ int Config::getNodeNumber() } void Config::setNodeNumber(int nodeNumber) { + config_setting_t *setting; + config_setting_t *root = config_root_setting(&cfg); + + setting = config_setting_get_member(root, "node_number"); + + if(!setting) { + setting = config_setting_add( + root, + "node_number", + CONFIG_TYPE_INT + ); + } + config_setting_set_int(setting, nodeNumber); } std::vector Config::getHosts() @@ -113,6 +134,21 @@ std::vector Config::getHosts() return hosts; } +void Config::setHosts(std::vector hosts) +{ + config_setting_t *root = config_root_setting(&cfg); + config_setting_t *setting = NULL; + + setting = config_setting_add(root, "host", CONFIG_TYPE_ARRAY); + + for (auto host : hosts) { + config_setting_set_string( + config_setting_add(setting, NULL, CONFIG_TYPE_STRING), + host.c_str() + ); + } +} + ConnectionType Config::getConnectionType() { config_setting_t *setting = config_lookup(&cfg, "type"); @@ -137,6 +173,25 @@ ConnectionType Config::getConnectionType() } void Config::setConnectionType(ConnectionType type) { + config_setting_t *setting; + config_setting_t *root = config_root_setting(&cfg); + + setting = config_setting_get_member(root, "type"); + + if(!setting) { + setting = config_setting_add( + root, + "type", + CONFIG_TYPE_STRING + ); + } + + if (type == EXPLICIT) { + config_setting_set_string(setting, "explicit"); + } + else if (type == REGISTER) { + config_setting_set_string(setting, "register"); + } } const char * Config::getProgramDir() @@ -156,6 +211,19 @@ const char * Config::getProgramDir() } void Config::setProgramDir(const char * programDir) { + config_setting_t *setting; + config_setting_t *root = config_root_setting(&cfg); + + setting = config_setting_get_member(root, "progdir"); + + if(!setting) { + setting = config_setting_add( + root, + "progdir", + CONFIG_TYPE_STRING + ); + } + config_setting_set_string(setting, programDir); } } diff --git a/src/global/vlp/config.h b/src/global/vlp/config.h index e1bf59b..db20815 100644 --- a/src/global/vlp/config.h +++ b/src/global/vlp/config.h @@ -44,6 +44,7 @@ public: void setNodeNumber(int nodeNumber); std::vector getHosts(); + void setHosts(std::vector hosts); ConnectionType getConnectionType(); void setConnectionType(ConnectionType type); diff --git a/src/kernel/OptionsDialog.cpp b/src/kernel/OptionsDialog.cpp index b7261cd..4e413cd 100644 --- a/src/kernel/OptionsDialog.cpp +++ b/src/kernel/OptionsDialog.cpp @@ -102,37 +102,21 @@ void OptionsDialog::saveConfig(QString fname) void OptionsDialog::saveConfig(const char * fname) { - config_t cfg; - config_setting_t *root; - config_setting_t *setting; - config_init(&cfg); - - root = config_root_setting(&cfg); - - setting = config_setting_add(root, "progdir", CONFIG_TYPE_STRING); - config_setting_set_string(setting, programsDirectory->text().toStdString().c_str()); - - setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT); - config_setting_set_int(setting, nodeNumber->value()); - - setting = config_setting_add(root, "type", CONFIG_TYPE_STRING); - if (explicitConnectionMode->isChecked()) { - config_setting_set_string(setting, "explicit"); - - config_setting_t *hosts = NULL; - hosts = config_setting_add(root, "host", CONFIG_TYPE_ARRAY); - for(int i = 0; i < connections->count(); i++) { - setting = config_setting_add(hosts, NULL, CONFIG_TYPE_STRING); - config_setting_set_string(setting, connections->item(i)->text().toStdString().c_str()); - } - } else { - config_setting_set_string(setting, "register"); - } + loglan::vlp::Config config; - if (!config_write_file(&cfg, fname)) { - fprintf(stderr, "Error while writing to file: %s.\n", fname); + config.setProgramDir(programsDirectory->text().toStdString().c_str()); + config.setNodeNumber(nodeNumber->value()); + config.setConnectionType( + explicitConnectionMode->isChecked() ? EXPLICIT : REGISTER + ); + + std::vector hosts; + for(int i = 0; i < connections->count(); i++) { + hosts.push_back(connections->item(i)->text().toStdString()); } - config_destroy(&cfg); + config.setHosts(hosts); + + config.save(fname); } } diff --git a/src/lgconfig/lgconfig.cpp b/src/lgconfig/lgconfig.cpp index 2511b5b..f3605f1 100644 --- a/src/lgconfig/lgconfig.cpp +++ b/src/lgconfig/lgconfig.cpp @@ -22,6 +22,7 @@ #include "lgconfig.h" #include "NodeListDialog.h" +#include "vlp/config.h" namespace loglan { namespace vlp { @@ -51,51 +52,32 @@ void QInstall::on_actionConfigure_triggered() i = Nodes.indexOf(pom); sprintf(pomstr, "%s.cfg", pom->addr); - config_t cfg; - config_setting_t *root, *setting; - config_init(&cfg); + + loglan::vlp::Config config; - root = config_root_setting(&cfg); + config.setProgramDir(pom->progdir); + config.setNodeNumber(pom->ID); + config.setConnectionType( + (pom->type == 0) ? EXPLICIT : REGISTER + ); - setting = config_setting_get_member(root, "progdir"); - if(!setting) { - setting = config_setting_add(root, "progdir", CONFIG_TYPE_STRING); - } - config_setting_set_string(setting, pom->progdir); - - setting = config_setting_get_member(root, "homedir"); - if(!setting) { - setting = config_setting_add(root, "homedir", CONFIG_TYPE_STRING); - } - config_setting_set_string(setting, pom->homedir); - - setting = config_setting_get_member(root, "node_number"); - if(!setting) { - setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT); - } - config_setting_set_int(setting, pom->ID); - - if (pom->type == 0) { - setting = config_setting_get_member(root, "type"); - if(!setting) { - setting = config_setting_add(root, "type", CONFIG_TYPE_STRING); - } - config_setting_set_string(setting, "explicit"); - } + std::vector hosts; for (auto node : Nodes) { if (pom != node) { - setting = config_setting_get_member(root, "host"); - if (!setting) { - setting = config_setting_add(root, "host", CONFIG_TYPE_STRING); - } - config_setting_set_string(setting, node->addr); + hosts.push_back(node->addr); } } + config.setHosts(hosts); + + config.save(pomstr); + +// TODO: add handling of homedir or remove it at all +// setting = config_setting_get_member(root, "homedir"); +// if(!setting) { +// setting = config_setting_add(root, "homedir", CONFIG_TYPE_STRING); +// } +// config_setting_set_string(setting, pom->homedir); - if(!config_write_file(&cfg, pomstr)) { - fprintf(stderr, "Error while writing to file: %s.\n", pomstr); - } - config_destroy(&cfg); pom = Nodes.at(i); }