149e3573a6e79563f5cdb4e5a94033476bc5f86a
[vlp.git] / src / kernel / options.cpp
1 #include <QtGui/QDialog>
2 #include <libconfig.h>
3
4 #include "vlp/config.h"
5 #include "options.h"
6
7 OptionsDialog::OptionsDialog(QString configFilePath, QWidget * parent)
8         : QDialog(parent)
9 {
10         setupUi(this);
11         
12         connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
13         connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
14         
15         loadConfig(configFilePath.toStdString().c_str());
16 }
17
18 OptionsDialog::~OptionsDialog()
19 {
20 }
21
22 /**
23  * Adds IP address to the configuration.
24  * Additional window is displayed to add address to the list
25  */
26 void OptionsDialog::on_addConnectionButton_clicked()
27 {
28         QDialog d(this, Qt::Dialog);
29         QLabel lab("IP Address:", &d);
30         QLineEdit ed("", &d);
31         QPushButton ob("", &d);
32         QPushButton cb("", &d);
33
34         if (connections) {
35                 ob.setGeometry(30, 60, 80, 30);
36                 ob.setText("Ok");
37                 ob.setDefault(TRUE);
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);
42                 cb.setText("Cancel");
43                 d.resize(240, 100);
44                 connect(&ob, SIGNAL(clicked()), &d, SLOT(accept()));
45                 connect(&cb, SIGNAL(clicked()), &d, SLOT(reject())); 
46                 if (d.exec())
47                         if (strcmp(ed.text().toAscii().data(), "") != 0) {
48                                 connections->addItem(ed.text());
49                         }
50         }
51 }
52
53 /**
54  * Deletes current address from available connections.
55  */
56 void OptionsDialog::on_delConnectionButton_clicked()
57 {
58         if (connections->currentRow() != -1) {
59                 connections->takeItem(connections->currentRow());
60         }
61 }
62
63 /**
64  * Loads configuration from the given file.
65  *
66  * @see QKernel::LoadConfig
67  *
68  * @param fname Filename of the configuration file.
69  */
70 void OptionsDialog::loadConfig(const char * fname)
71 {
72         loglan::vlp::Config config;
73         config.load(fname);
74
75         nodeNumber->setValue(config.getNodeNumber());
76         switch(config.getConnectionType()) {
77         case loglan::vlp::EXPLICIT:
78                 explicitConnectionMode->setEnabled(true);
79                 registrationConnectionMode->setDisabled(true);
80                 break;
81         case loglan::vlp::REGISTER:
82                 registrationConnectionMode->setEnabled(true);
83                 explicitConnectionMode->setDisabled(true);
84                 break;
85         }
86
87         std::vector<std::string> hosts = config.getHosts();
88         for (int i = 0; i < hosts.size(); i++) {
89                 connections->addItem(hosts[i].c_str());
90         }
91
92         programsDirectory->setText(config.getProgramDir());
93 }
94
95 void OptionsDialog::saveConfig(QString fname)
96 {
97         saveConfig(fname.toStdString().c_str());
98 }
99
100 void OptionsDialog::saveConfig(const char * fname)
101 {
102         config_t cfg;
103         config_setting_t *root;
104         config_setting_t *setting;
105         config_init(&cfg);
106
107         root = config_root_setting(&cfg);
108
109         setting = config_setting_add(root, "progdir", CONFIG_TYPE_STRING);
110         config_setting_set_string(setting, programsDirectory->text().toStdString().c_str());
111
112         setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT);
113         config_setting_set_int(setting, nodeNumber->value());
114
115         setting = config_setting_add(root, "type", CONFIG_TYPE_STRING);
116         if (explicitConnectionMode->isChecked()) {
117                 config_setting_set_string(setting, "explicit");
118
119                 config_setting_t *hosts = NULL;
120                 hosts = config_setting_add(root, "host", CONFIG_TYPE_ARRAY);
121                 for(int i = 0; i < connections->count(); i++) {
122                         setting = config_setting_add(hosts, NULL, CONFIG_TYPE_STRING);
123                         config_setting_set_string(setting, connections->item(i)->text().toStdString().c_str());
124                 }
125         } else {
126                 config_setting_set_string(setting, "register");
127         }
128
129         if (!config_write_file(&cfg, fname)) {
130                 fprintf(stderr, "Error while writing to file: %s.\n", fname);
131         }
132         config_destroy(&cfg);
133 }