Implement saving of configuration files in its own class
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Sat, 30 Jan 2016 18:18:28 +0000 (19:18 +0100)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Sat, 30 Jan 2016 18:18:28 +0000 (19:18 +0100)
Makefile.am
src/global/vlp/config.cpp
src/global/vlp/config.h
src/kernel/OptionsDialog.cpp
src/lgconfig/lgconfig.cpp

index 5a43ea4405f54a63c7a255d4c5b512dd08a35414..d19edf84c2ce6bc62e5fcae159946b10bc64cfc2 100644 (file)
@@ -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 \
index 2191cd879bab3a16c51ecc3643b442fdded6868e..701b3e43ca65fac784a97b25f67dad4598171186 100644 (file)
@@ -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<std::string> Config::getHosts()
@@ -113,6 +134,21 @@ std::vector<std::string> Config::getHosts()
        return hosts;
 }
 
+void Config::setHosts(std::vector<std::string> 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);
 }
 
 }
index e1bf59bd68a6d93dee59e7f512e503342c5b567e..db2081579bfdad15ca234dfcfc438ca75596505a 100644 (file)
@@ -44,6 +44,7 @@ public:
        void setNodeNumber(int nodeNumber);
 
        std::vector<std::string> getHosts();
+       void setHosts(std::vector<std::string> hosts);
 
        ConnectionType getConnectionType();
        void setConnectionType(ConnectionType type);
index b7261cd639835eefce7dd81c7d50b738fcbfd0d9..4e413cd596c2a20dd88cd8ebb3fd4cf7a3366197 100644 (file)
@@ -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<std::string> 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);
 }
 
 }
index 2511b5bf02838a63c8ada14d3483e829ed1f2f08..f3605f16d91c90c0c0c5531dcaf77ff1358a84f9 100644 (file)
@@ -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<std::string> 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);
                        }