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