f167848b5af6e8dd61ff1e31d90168b26fbe60d4
[vlp.git] / src / lgconfig / lgconfig.cpp
1 #include <QtGui/QApplication>
2 #include <QtGui/QMainWindow>
3 #include <Qt3Support/q3multilineedit.h>
4 #include <QtGui/QDialog>
5 #include <QtGui/QButtonGroup>
6 #include <QtGui/QLabel>
7 #include <QtGui/QLineEdit>
8 #include <Qt3Support/Q3ListBox>
9 #include <QtGui/QPushButton>
10 #include <QtGui/QRadioButton>
11 #include <QtCore/QList>
12 #include <QtCore/qfile.h>
13 #include <QtGui/qcombobox.h>
14 #include <QtGui/QToolTip>
15 #include <QtGui/QFont>
16 #include <QtGui/QPixmap>
17 #include <QtGui/QMessageBox>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21
22 #include <libconfig.h>
23
24 #include "lgconfig.h"
25 #include "AddNodeDialog.h"
26
27 namespace loglan {
28 namespace vlp {
29
30 QInstall::QInstall()
31 {
32         setupUi(this);
33
34         Nodes.clear();
35 }
36
37 bool QInstall::check_id(int id)
38 {
39         VLPEntry *node;
40         QListIterator<VLPEntry *> nodesIterator(Nodes);
41
42         while (nodesIterator.hasNext()) {
43                 node = nodesIterator.next();
44                 if (node->ID == id)
45                         return FALSE;
46         }
47         return TRUE;
48 }
49
50 bool QInstall::check_addr(char *addr)
51 {
52         VLPEntry *node;
53         QListIterator<VLPEntry *> nodesIterator(Nodes);
54
55         while (nodesIterator.hasNext()) {
56                 node = nodesIterator.next();
57                 if (strcmp(node->addr, addr) == 0)
58                         return FALSE;
59         }
60         return TRUE;
61 }
62
63 void QInstall::AddNode()
64 {
65         dialog::AddNodeDialog dialog(this);
66
67         VLPEntry *pom;
68
69         if (dialog.exec()) {
70                 pom = new VLPEntry;
71                 pom->ID = dialog.getNodeNumber();
72                 if (check_id(pom->ID)) {
73                         strcpy(pom->addr, dialog.getIPAddress().toStdString().c_str());
74                         if (check_addr(pom->addr)) {
75                                 if (dialog.getConnectionType() == "Explicit") {
76                                         pom->type=0;
77                                 }
78                                 strcpy(pom->progdir, dialog.getProgramsDirectory().toStdString().c_str());
79                                 strcpy(pom->homedir, dialog.getVLPDirectory().toStdString().c_str());
80                                 Nodes.append(pom);
81                                 
82                                 QString info;
83                                 info.sprintf("Node: %d\tAddr: %s\tHome dir: %s",
84                                         pom->ID, pom->addr, pom->homedir
85                                 );
86
87                                 nodelist->insertItem(info);
88                                 strcpy(pom->item, info.toStdString().c_str());
89                         } else {
90                                 QMessageBox::warning(this, "Error!", "Only one VLP on a single computer!", "Ok");
91                         }
92                 } else {
93                         QMessageBox::warning(this, "Error!", "ID must be unique!", "Ok");
94                 }
95         }
96 }
97
98 void QInstall::DelNode()
99 {
100         char pom[255];
101         VLPEntry *vpom;
102
103         if (nodelist->currentItem() >= 0) {
104                 strcpy(pom, nodelist->text(nodelist->currentItem()).toStdString().c_str());
105
106                 QListIterator<VLPEntry *> nodesIterator(Nodes);
107                 
108                 while (nodesIterator.hasNext()) {
109                         vpom = nodesIterator.next();
110                         if (strcmp(pom, vpom->item) == 0)
111                                 break;
112                 }
113                 if (vpom != NULL)
114                         if (QMessageBox::question(this, "Delete VLP", "Are you sure?", "Yes", "No")) {
115                                 nodelist->removeItem(nodelist->currentItem());
116                                 Nodes.removeOne(vpom);
117                         }
118         }
119 }
120
121 void QInstall::on_actionConfigure_triggered()
122 {
123         QDialog dlg(this);
124         QLabel *tmpQLabel;
125         QPushButton *addbtn, *delbtn, *okbtn, *cancelbtn;
126         VLPEntry *pom;
127         int i;
128         char pomstr[255];
129
130         //dlg.setStyle(WindowsStyle);
131         nodelist = new Q3ListBox(&dlg);
132         nodelist->setGeometry(20, 40, 480, 160);
133
134         tmpQLabel = new QLabel(&dlg);
135         tmpQLabel->setGeometry(20, 10, 100, 30);
136         tmpQLabel->setText("Nodes:");
137
138         addbtn = new QPushButton(&dlg);
139         addbtn->setGeometry(30, 210, 100, 30);
140         addbtn->setText("Add VLP");
141         connect(addbtn, SIGNAL(clicked()), this, SLOT(AddNode()));
142
143         delbtn = new QPushButton(&dlg);
144         delbtn->setGeometry(150, 210, 100, 30);
145         delbtn->setText("Del VLP");
146         connect(delbtn, SIGNAL(clicked()), this, SLOT(DelNode()));
147
148         okbtn = new QPushButton(&dlg);
149         okbtn->setGeometry(270, 210, 100, 30);
150         okbtn->setText("Save files");
151         connect(okbtn, SIGNAL(clicked()), &dlg, SLOT(accept()));
152
153         cancelbtn = new QPushButton(&dlg);
154         cancelbtn->setGeometry(390, 210, 100, 30);
155         cancelbtn->setText("Cancel");
156         connect(cancelbtn, SIGNAL(clicked()), &dlg, SLOT(reject()));
157
158         dlg.resize(520, 260);
159
160         if (dlg.exec()) {
161                 if (!Nodes.isEmpty()) {
162                         QListIterator<VLPEntry *> nodesIterator(Nodes);
163                         
164                         while (nodesIterator.hasNext()) {
165                                 pom = nodesIterator.next();
166                                 i = Nodes.indexOf(pom);
167                                 sprintf(pomstr, "%s.cfg", pom->addr);
168
169                                 config_t cfg;
170                                 config_setting_t *root, *setting;
171                                 config_init(&cfg);
172
173                                 root = config_root_setting(&cfg);
174
175                                 setting = config_setting_get_member(root, "progdir");
176                                 if(!setting) {
177                                         setting = config_setting_add(root, "progdir", CONFIG_TYPE_STRING);
178                                 }
179                                 config_setting_set_string(setting, pom->progdir);
180
181                                 setting = config_setting_get_member(root, "homedir");
182                                 if(!setting) {
183                                         setting = config_setting_add(root, "homedir", CONFIG_TYPE_STRING);
184                                 }
185                                 config_setting_set_string(setting, pom->homedir);
186
187                                 setting = config_setting_get_member(root, "node_number");
188                                 if(!setting) {
189                                         setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT);
190                                 }
191                                 config_setting_set_int(setting, pom->ID);
192
193                                 if (pom->type == 0) {
194                                         setting = config_setting_get_member(root, "type");
195                                         if(!setting) {
196                                                 setting = config_setting_add(root, "type", CONFIG_TYPE_STRING);
197                                         }
198                                         config_setting_set_string(setting, "explicit");
199                                 }
200                                 for (auto node : Nodes) {
201                                         if (pom != node) {
202                                                 setting = config_setting_get_member(root, "host");
203                                                 if (!setting) {
204                                                         setting = config_setting_add(root, "host", CONFIG_TYPE_STRING);
205                                                 }
206                                                 config_setting_set_string(setting, node->addr);
207                                         }
208                                 }
209
210                                 if(!config_write_file(&cfg, pomstr)) {
211                                         fprintf(stderr, "Error while writing to file: %s.\n", pomstr);
212                                 }
213                                 config_destroy(&cfg);
214
215                                 pom = Nodes.at(i);
216                         }
217                 }
218         }
219 }
220
221
222 void QInstall::on_actionQuit_triggered()
223 {
224         QApplication::instance()->quit();
225 }
226
227 }
228 }
229
230
231 int main(int argc, char **argv)
232 {
233         QApplication *app = new QApplication(argc,argv);
234         loglan::vlp::QInstall cfg;
235         cfg.show();
236         return app->exec();
237 }