Removed system function invocations and placed forked execl instead
[vlp.git] / src / kernel / options.cpp
1 #include <QtGui/QDialog>
2 #include <libconfig.h>
3
4 #include "options.h"
5
6 OptionsDialog::OptionsDialog(QString configFilePath, 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(configFilePath.toStdString().c_str());
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(const char * fname)
70 {
71         config_t cfg;
72         config_setting_t *setting;
73
74         /* Hack for checking if file exists without using external libs.*/
75         FILE * file = fopen(fname, "rt");
76         if (!file) {
77                 fprintf(stderr, "Error: Cannot load configuration file %s!\n", fname);
78                 exit(3);
79         }
80         /* File exists, so file has been locked. Release it. */
81
82         config_init(&cfg);
83
84         /* Read the file. If there is an error, report it and exit. */
85         if (!config_read(&cfg, file)) {
86                 fprintf(stderr, "%s! In file %s, line %d\n", 
87                         config_error_text(&cfg), 
88                         config_error_file(&cfg), 
89                         config_error_line(&cfg));
90                 config_destroy(&cfg);
91                 fclose(file);
92                 exit(3);
93         }
94
95         setting = config_lookup(&cfg, "node_number");
96         if (setting) {
97                 nodeNumber->setValue(config_setting_get_int(setting));
98         } else {
99                 fprintf(stderr, "%s! In file %s, '%s' was not found.\n", 
100                                         "Warning", fname, "node_number");
101                 config_destroy(&cfg);
102                 fclose(file);
103                 exit(3);
104         }
105
106         setting = config_lookup(&cfg, "type");
107         if (setting) {
108                 /* same as strcmp(..) == 0 */
109                 if (!strcmp(config_setting_get_string(setting), "explicit")) {
110                         explicitConnectionMode->setEnabled(true);
111                         registrationConnectionMode->setDisabled(true);
112                 } else {
113                         registrationConnectionMode->setEnabled(true);
114                         explicitConnectionMode->setDisabled(true);
115                 }
116         } else {
117                 fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, "type");
118         }
119
120         setting = config_lookup(&cfg, "host");
121         if (setting) {
122                 switch(config_setting_type(setting)) {
123                 /* TODO: Deprecated. Made for back compatibility. */
124                 case CONFIG_TYPE_STRING:
125                         connections->addItem(config_setting_get_string(setting));
126                         break;
127                 case CONFIG_TYPE_ARRAY: {
128                         int size = config_setting_length(setting);
129                         for (int i = 0; i < size; i++) {
130                                 connections->addItem(config_setting_get_string_elem(setting, i));
131                         }
132                         break;
133                 }
134                 default:
135                         fprintf(stderr, "%s! In file %s, bad entry type for %s."
136                                                         " Will not be read.\n", 
137                                                         "Error", fname, "host");
138                 }
139         } else {
140                 fprintf(stderr, "%s! In file %s, '%s' was not found.\n", 
141                                                 "Warning", fname, "host");
142         }
143
144         setting = config_lookup(&cfg, "progdir");
145         if (setting){
146                 programsDirectory->setText(config_setting_get_string(setting));
147         } else {
148                 fprintf(stderr, "%s! In file %s, '%s' was not found.\n", "Warning", fname, "progdir");
149         }
150
151         config_destroy(&cfg);
152         fclose(file);
153 }
154
155 void OptionsDialog::saveConfig(QString fname)
156 {
157         saveConfig(fname.toStdString().c_str());
158 }
159
160 void OptionsDialog::saveConfig(const char * fname)
161 {
162         config_t cfg;
163         config_setting_t *root;
164         config_setting_t *setting;
165         config_init(&cfg);
166
167         root = config_root_setting(&cfg);
168
169         setting = config_setting_add(root, "progdir", CONFIG_TYPE_STRING);
170         config_setting_set_string(setting, programsDirectory->text().toStdString().c_str());
171
172         setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT);
173         config_setting_set_int(setting, nodeNumber->value());
174
175         setting = config_setting_add(root, "type", CONFIG_TYPE_STRING);
176         if (explicitConnectionMode->isChecked()) {
177                 config_setting_set_string(setting, "explicit");
178
179                 config_setting_t *hosts = NULL;
180                 hosts = config_setting_add(root, "host", CONFIG_TYPE_ARRAY);
181                 for(int i = 0; i < connections->count(); i++) {
182                         setting = config_setting_add(hosts, NULL, CONFIG_TYPE_STRING);
183                         config_setting_set_string(setting, connections->item(i)->text().toStdString().c_str());
184                 }
185         } else {
186                 config_setting_set_string(setting, "register");
187         }
188
189         if (!config_write_file(&cfg, fname)) {
190                 fprintf(stderr, "Error while writing to file: %s.\n", fname);
191         }
192         config_destroy(&cfg);
193 }