#include <QtGui/QDialog>
#include <libconfig.h>
+#include "vlp/config.h"
#include "options.h"
OptionsDialog::OptionsDialog(QString configFilePath, QWidget * parent)
*/
void OptionsDialog::loadConfig(const char * fname)
{
- config_t cfg;
- config_setting_t *setting;
-
- /* 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);
- }
-
- 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);
- }
-
- 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");
+ 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, "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");
+ std::vector<std::string> hosts = config.getHosts();
+ for (int i = 0; i < hosts.size(); i++) {
+ connections->addItem(hosts[i].c_str());
}
- 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");
- }
-
- config_destroy(&cfg);
- fclose(file);
+ programsDirectory->setText(config.getProgramDir());
}
void OptionsDialog::saveConfig(QString fname)