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