2c4732f62373e76b8987c245435765b9652e40eb
[vlp.git] / src / kernel / options.cpp
1 #include <QtGui/QDialog>
2 #include <libconfig.h>
3
4 #include "options.h"
5
6 OptionsDialog::OptionsDialog(QWidget * parent)
7         : QDialog(parent)
8 {
9         setupUi(this);
10         
11         connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
12         connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
13         
14         loadConfig("vlp.cfg");
15 }
16
17 OptionsDialog::~OptionsDialog()
18 {
19 }
20
21 /**
22  * Adds IP address to the configuration.
23  * Additional window is displayed to add address to the list
24  */
25 void OptionsDialog::on_addConnectionButton_clicked()
26 {
27         QDialog d(this, Qt::Dialog);
28         QLabel lab("IP Address:", &d);
29         QLineEdit ed("", &d);
30         QPushButton ob("", &d);
31         QPushButton cb("", &d);
32
33         if (connections) {
34                 ob.setGeometry(30, 60, 80, 30);
35                 ob.setText("Ok");
36                 ob.setDefault(TRUE);
37                 lab.setGeometry(10, 10, 60, 30);
38                 lab.setText("Address");
39                 ed.setGeometry(70, 10, 140, 30);
40                 cb.setGeometry(130, 60, 80, 30);
41                 cb.setText("Cancel");
42                 d.resize(240, 100);
43                 connect(&ob, SIGNAL(clicked()), &d, SLOT(accept()));
44                 connect(&cb, SIGNAL(clicked()), &d, SLOT(reject())); 
45                 if (d.exec())
46                         if (strcmp(ed.text().toAscii().data(), "") != 0) {
47                                 connections->addItem(ed.text());
48                         }
49         }
50 }
51
52 /**
53  * Deletes current address from available connections.
54  */
55 void OptionsDialog::on_delConnectionButton_clicked()
56 {
57         if (connections->currentRow() != -1) {
58                 connections->takeItem(connections->currentRow());
59         }
60 }
61
62 /**
63  * Loads configuration from the given file.
64  *
65  * @see QKernel::LoadConfig
66  *
67  * @param fname Filename of the configuration file.
68  */
69 void OptionsDialog::loadConfig(char * fname)
70 {
71         config_t cfg;
72         config_setting_t *setting;
73         const char *str;
74
75         /* Hack for checking if file exists without using external libs.*/
76         FILE * file = fopen(fname, "rt");
77         if (!file) {
78                 fprintf(stderr, "Error: Cannot load configuration file %s!\n", fname);
79                 exit(3);
80         }
81         /* File exists, so file has been locked. Release it. */
82
83         config_init(&cfg);
84
85         /* Read the file. If there is an error, report it and exit. */
86         if (!config_read(&cfg, file)) {
87                 fprintf(stderr, "%s! In file %s, line %d\n", 
88                         config_error_text(&cfg), 
89                         config_error_file(&cfg), 
90                         config_error_line(&cfg));
91                 config_destroy(&cfg);
92                 fclose(file);
93                 exit(3);
94         }
95
96         setting = config_lookup(&cfg, "node_number");
97         if (setting) {
98                 nodeNumber->setValue(config_setting_get_int(setting));
99         } else {
100                 fprintf(stderr, "%s! In file %s, '%s' was not found.\n", 
101                                         "Warning", fname, "node_number");
102                 config_destroy(&cfg);
103                 fclose(file);
104                 exit(3);
105         }
106
107         setting = config_lookup(&cfg, "type");
108         if (setting) {
109                 /* same as strcmp(..) == 0 */
110                 if (!strcmp(config_setting_get_string(setting), "explicit")) {
111                         explicitConnectionMode->setEnabled(true);
112                         registrationConnectionMode->setDisabled(true);
113                 } else {
114                         registrationConnectionMode->setEnabled(true);
115                         explicitConnectionMode->setDisabled(true);
116                 }
117         } else {
118                 fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, "type");
119         }
120
121         setting = config_lookup(&cfg, "host");
122         if (setting) {
123                 switch(config_setting_type(setting)) {
124                 /* TODO: Deprecated. Made for back compatibility. */
125                 case CONFIG_TYPE_STRING:
126                         connections->addItem(config_setting_get_string(setting));
127                         break;
128                 case CONFIG_TYPE_ARRAY: {
129                         int size = config_setting_length(setting);
130                         for (int i = 0; i < size; i++) {
131                                 connections->addItem(config_setting_get_string_elem(setting, i));
132                         }
133                         break;
134                 }
135                 default:
136                         fprintf(stderr, "%s! In file %s, bad entry type for %s."
137                                                         " Will not be read.\n", 
138                                                         "Error", fname, "host");
139                 }
140         } else {
141                 fprintf(stderr, "%s! In file %s, '%s' was not found.\n", 
142                                                 "Warning", fname, "host");
143         }
144
145         setting = config_lookup(&cfg, "progdir");
146         if (setting){
147                 programsDirectory->setText(config_setting_get_string(setting));
148         } else {
149                 fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, "progdir");
150         }
151
152         setting = config_lookup(&cfg, "homedir");
153         if (setting) {
154                 homeDir = config_setting_get_string(setting);
155         } else {
156                 fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, "homedir");
157         }
158
159         config_destroy(&cfg);
160         fclose(file);
161 }
162
163 void OptionsDialog::saveConfig(char * fname)
164 {
165         config_t cfg;
166         config_setting_t *root;
167         config_setting_t *setting;
168         config_init(&cfg);
169
170         root = config_root_setting(&cfg);
171
172         setting = config_setting_add(root, "progdir", CONFIG_TYPE_STRING);
173         config_setting_set_string(setting, programsDirectory->text().toStdString().c_str());
174
175         setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT);
176         config_setting_set_int(setting, nodeNumber->value());
177
178         setting = config_setting_add(root, "homedir", CONFIG_TYPE_STRING);
179         config_setting_set_string(setting, homeDir);
180
181         setting = config_setting_add(root, "type", CONFIG_TYPE_STRING);
182         if (explicitConnectionMode->isChecked()) {
183                 config_setting_set_string(setting, "explicit");
184
185                 config_setting_t *hosts = NULL;
186                 hosts = config_setting_add(root, "host", CONFIG_TYPE_ARRAY);
187                 for(int i = 0; i < connections->count(); i++) {
188                         setting = config_setting_add(hosts, NULL, CONFIG_TYPE_STRING);
189                         config_setting_set_string(setting, connections->item(i)->text().toStdString().c_str());
190                 }
191         } else {
192                 config_setting_set_string(setting, "register");
193         }
194
195         if (!config_write_file(&cfg, fname)) {
196                 fprintf(stderr, "Error while writing to file: %s.\n", fname);
197         }
198         config_destroy(&cfg);
199 }