X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=src%2Fkernel%2Foptions.cpp;h=5830ca02b94c05d59802f75ac5a6ba0124453799;hb=cec0990bb3507f23b196891002cd25f80e8bbb40;hp=2c4732f62373e76b8987c245435765b9652e40eb;hpb=6d33434099abfed5a9946f1ede31be2f42e9a17b;p=vlp.git diff --git a/src/kernel/options.cpp b/src/kernel/options.cpp index 2c4732f..5830ca0 100644 --- a/src/kernel/options.cpp +++ b/src/kernel/options.cpp @@ -1,9 +1,10 @@ #include #include +#include "vlp/config.h" #include "options.h" -OptionsDialog::OptionsDialog(QWidget * parent) +OptionsDialog::OptionsDialog(QString configFilePath, QWidget * parent) : QDialog(parent) { setupUi(this); @@ -11,7 +12,7 @@ OptionsDialog::OptionsDialog(QWidget * parent) connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); - loadConfig("vlp.cfg"); + loadConfig(configFilePath.toStdString().c_str()); } OptionsDialog::~OptionsDialog() @@ -66,101 +67,36 @@ void OptionsDialog::on_delConnectionButton_clicked() * * @param fname Filename of the configuration file. */ -void OptionsDialog::loadConfig(char * fname) +void OptionsDialog::loadConfig(const char * fname) { - config_t cfg; - config_setting_t *setting; - const char *str; - - /* Hack for checking if file exists without using external libs.*/ - FILE * file = fopen(fname, "rt"); - if (!file) { - fprintf(stderr, "Error: Cannot load configuration file %s!\n", fname); - exit(3); - } - /* File exists, so file has been locked. Release it. */ - - config_init(&cfg); - - /* Read the file. If there is an error, report it and exit. */ - if (!config_read(&cfg, file)) { - fprintf(stderr, "%s! In file %s, line %d\n", - config_error_text(&cfg), - config_error_file(&cfg), - config_error_line(&cfg)); - config_destroy(&cfg); - fclose(file); - exit(3); + loglan::vlp::Config config; + config.load(fname); + + nodeNumber->setValue(config.getNodeNumber()); + switch(config.getConnectionType()) { + case loglan::vlp::EXPLICIT: + explicitConnectionMode->setEnabled(true); + registrationConnectionMode->setDisabled(true); + break; + case loglan::vlp::REGISTER: + registrationConnectionMode->setEnabled(true); + explicitConnectionMode->setDisabled(true); + break; } - setting = config_lookup(&cfg, "node_number"); - if (setting) { - nodeNumber->setValue(config_setting_get_int(setting)); - } else { - fprintf(stderr, "%s! In file %s, '%s' was not found.\n", - "Warning", fname, "node_number"); - config_destroy(&cfg); - fclose(file); - exit(3); + for (auto host : config.getHosts()) { + connections->addItem(host.c_str()); } - setting = config_lookup(&cfg, "type"); - if (setting) { - /* same as strcmp(..) == 0 */ - if (!strcmp(config_setting_get_string(setting), "explicit")) { - explicitConnectionMode->setEnabled(true); - registrationConnectionMode->setDisabled(true); - } else { - registrationConnectionMode->setEnabled(true); - explicitConnectionMode->setDisabled(true); - } - } else { - fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, "type"); - } - - setting = config_lookup(&cfg, "host"); - if (setting) { - switch(config_setting_type(setting)) { - /* TODO: Deprecated. Made for back compatibility. */ - case CONFIG_TYPE_STRING: - connections->addItem(config_setting_get_string(setting)); - break; - case CONFIG_TYPE_ARRAY: { - int size = config_setting_length(setting); - for (int i = 0; i < size; i++) { - connections->addItem(config_setting_get_string_elem(setting, i)); - } - break; - } - default: - fprintf(stderr, "%s! In file %s, bad entry type for %s." - " Will not be read.\n", - "Error", fname, "host"); - } - } else { - fprintf(stderr, "%s! In file %s, '%s' was not found.\n", - "Warning", fname, "host"); - } - - setting = config_lookup(&cfg, "progdir"); - if (setting){ - programsDirectory->setText(config_setting_get_string(setting)); - } else { - fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, "progdir"); - } - - setting = config_lookup(&cfg, "homedir"); - if (setting) { - homeDir = config_setting_get_string(setting); - } else { - fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, "homedir"); - } + programsDirectory->setText(config.getProgramDir()); +} - config_destroy(&cfg); - fclose(file); +void OptionsDialog::saveConfig(QString fname) +{ + saveConfig(fname.toStdString().c_str()); } -void OptionsDialog::saveConfig(char * fname) +void OptionsDialog::saveConfig(const char * fname) { config_t cfg; config_setting_t *root; @@ -175,9 +111,6 @@ void OptionsDialog::saveConfig(char * fname) setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT); config_setting_set_int(setting, nodeNumber->value()); - setting = config_setting_add(root, "homedir", CONFIG_TYPE_STRING); - config_setting_set_string(setting, homeDir); - setting = config_setting_add(root, "type", CONFIG_TYPE_STRING); if (explicitConnectionMode->isChecked()) { config_setting_set_string(setting, "explicit");