1 #include <QtGui/QDialog>
4 #include "vlp/config.h"
7 OptionsDialog::OptionsDialog(QString configFilePath, QWidget * parent)
12 connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
13 connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
15 loadConfig(configFilePath.toStdString().c_str());
18 OptionsDialog::~OptionsDialog()
23 * Adds IP address to the configuration.
24 * Additional window is displayed to add address to the list
26 void OptionsDialog::on_addConnectionButton_clicked()
28 QDialog d(this, Qt::Dialog);
29 QLabel lab("IP Address:", &d);
31 QPushButton ob("", &d);
32 QPushButton cb("", &d);
35 ob.setGeometry(30, 60, 80, 30);
38 lab.setGeometry(10, 10, 60, 30);
39 lab.setText("Address");
40 ed.setGeometry(70, 10, 140, 30);
41 cb.setGeometry(130, 60, 80, 30);
44 connect(&ob, SIGNAL(clicked()), &d, SLOT(accept()));
45 connect(&cb, SIGNAL(clicked()), &d, SLOT(reject()));
47 if (strcmp(ed.text().toAscii().data(), "") != 0) {
48 connections->addItem(ed.text());
54 * Deletes current address from available connections.
56 void OptionsDialog::on_delConnectionButton_clicked()
58 if (connections->currentRow() != -1) {
59 connections->takeItem(connections->currentRow());
64 * Loads configuration from the given file.
66 * @see QKernel::LoadConfig
68 * @param fname Filename of the configuration file.
70 void OptionsDialog::loadConfig(const char * fname)
72 loglan::vlp::Config config;
75 nodeNumber->setValue(config.getNodeNumber());
76 switch(config.getConnectionType()) {
77 case loglan::vlp::EXPLICIT:
78 explicitConnectionMode->setEnabled(true);
79 registrationConnectionMode->setDisabled(true);
81 case loglan::vlp::REGISTER:
82 registrationConnectionMode->setEnabled(true);
83 explicitConnectionMode->setDisabled(true);
87 for (auto host : config.getHosts()) {
88 connections->addItem(host.c_str());
91 programsDirectory->setText(config.getProgramDir());
94 void OptionsDialog::saveConfig(QString fname)
96 saveConfig(fname.toStdString().c_str());
99 void OptionsDialog::saveConfig(const char * fname)
102 config_setting_t *root;
103 config_setting_t *setting;
106 root = config_root_setting(&cfg);
108 setting = config_setting_add(root, "progdir", CONFIG_TYPE_STRING);
109 config_setting_set_string(setting, programsDirectory->text().toStdString().c_str());
111 setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT);
112 config_setting_set_int(setting, nodeNumber->value());
114 setting = config_setting_add(root, "type", CONFIG_TYPE_STRING);
115 if (explicitConnectionMode->isChecked()) {
116 config_setting_set_string(setting, "explicit");
118 config_setting_t *hosts = NULL;
119 hosts = config_setting_add(root, "host", CONFIG_TYPE_ARRAY);
120 for(int i = 0; i < connections->count(); i++) {
121 setting = config_setting_add(hosts, NULL, CONFIG_TYPE_STRING);
122 config_setting_set_string(setting, connections->item(i)->text().toStdString().c_str());
125 config_setting_set_string(setting, "register");
128 if (!config_write_file(&cfg, fname)) {
129 fprintf(stderr, "Error while writing to file: %s.\n", fname);
131 config_destroy(&cfg);