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 \
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()
}
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()
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");
}
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()
}
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);
}
}
void setNodeNumber(int nodeNumber);
std::vector<std::string> getHosts();
+ void setHosts(std::vector<std::string> hosts);
ConnectionType getConnectionType();
void setConnectionType(ConnectionType type);
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);
}
}
#include "lgconfig.h"
#include "NodeListDialog.h"
+#include "vlp/config.h"
namespace loglan {
namespace vlp {
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);
}