vlp-7 More documentation in code.
[vlp.git] / src / lgconfig / lgconfig.cpp
1 #include <qapp.h>
2 #include <qframe.h>
3 #include <qmlined.h>
4 #include <qmenubar.h>
5 #include <qpopmenu.h>
6 #include <qdialog.h>
7 #include <qbttngrp.h>
8 #include <qlabel.h>
9 #include <qlined.h>
10 #include <qlistbox.h>
11 #include <qpushbt.h>
12 #include <qradiobt.h>
13 #include <qlist.h>
14 #include <qfile.h>
15 #include <qcombo.h>
16 #include <qtooltip.h>
17 #include <qfont.h>
18 #include <qpixmap.h>
19 #include <qmsgbox.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23
24 #include <libconfig.h>
25
26 class VLPEntry {
27 public:
28         int ID;
29         char addr[255];
30         /** 0 - explicit */
31         int type;
32         char progdir[255];
33         char homedir[255];
34         char item[255];
35 };
36
37 class QInstall: public QFrame {
38         Q_OBJECT
39 public:
40         QMenuBar *bar;
41         QListBox *nodelist;
42         QPushButton *infob;
43         QList<VLPEntry> Nodes;
44
45         QInstall();
46         bool check_id(int);
47         bool check_addr(char*);
48
49 public slots:
50         void SetOptions();
51         void AddNode();
52         void DelNode();
53         void Info();
54 };
55
56
57 QApplication *app;
58
59 QInstall::QInstall()
60 {
61         QFont f("Helvetica", 12, QFont::Bold);
62         QPixmap mp;
63
64         infob = new QPushButton(this);
65
66         bar = new QMenuBar(this);
67         bar->insertItem("Configure", this, SLOT(SetOptions()));
68         bar->insertItem("Quit", app, SLOT(quit()));
69         bar->setFont(f);
70         setCaption("VLP Configuration Tool");
71         infob->setGeometry(0, bar->height(), 200, 30);
72         if (mp.load("logo.bmp")) {
73                 infob->setPixmap(mp);
74                 infob->resize(mp.width(), mp.height());
75         }
76         resize(infob->width(), infob->height() + bar->height());
77         Nodes.clear();
78 }
79
80 bool QInstall::check_id(int id)
81 {
82         VLPEntry *pom;
83
84         pom = Nodes.first();
85         while (pom != NULL) {
86                 if (pom->ID == id)
87                         return FALSE;
88                 pom = Nodes.next();
89         }
90         return TRUE;
91 }
92
93 bool QInstall::check_addr(char *addr)
94 {
95         VLPEntry *pom;
96
97         pom = Nodes.first();
98         while (pom != NULL) {
99                 if (strcmp(pom->addr, addr) == 0)
100                         return FALSE;
101                 pom = Nodes.next();
102         }
103         return TRUE;
104 }
105
106 /**
107  * @attention Currently not in use
108  */
109 void QInstall::Info()
110 {
111 }
112
113 void QInstall::AddNode()
114 {
115         QDialog dlg(this, "", TRUE);
116         QLabel *tmpQLabel;
117         QLineEdit *id, *addr, *progs, *home;
118         QPushButton *okbtn, *cancelbtn;
119         VLPEntry *pom;
120         char pomstr[255];
121
122         tmpQLabel = new QLabel(&dlg, "Label_2");
123         tmpQLabel->setGeometry(110, 10, 180, 30);
124         tmpQLabel->setFrameStyle(49);
125         tmpQLabel->setText("Virtual Processor Properties");
126
127         id = new QLineEdit(&dlg, "LineEdit_1");
128         id->setGeometry(130, 50, 50, 30);
129         id->setText("");
130
131         tmpQLabel = new QLabel(&dlg, "Label_3");
132         tmpQLabel->setGeometry(20, 50, 90, 30);
133         tmpQLabel->setText("Node number");
134
135         tmpQLabel = new QLabel(&dlg, "Label_4");
136         tmpQLabel->setGeometry(20, 90, 80, 30);
137         tmpQLabel->setText("IP Address");
138
139         addr = new QLineEdit(&dlg, "LineEdit_2");
140         addr->setGeometry(130, 90, 120, 30);
141         addr->setText("");
142
143         tmpQLabel = new QLabel(&dlg, "Label_5");
144         tmpQLabel->setGeometry(20, 130, 100, 30);
145         tmpQLabel->setText("Connection type");
146
147         QComboBox* tmpQComboBox;
148         tmpQComboBox = new QComboBox(FALSE, &dlg, "ComboBox_1");
149         tmpQComboBox->setGeometry(130, 130, 100, 30);
150         tmpQComboBox->setSizeLimit(2);
151         tmpQComboBox->setAutoResize(FALSE);
152         tmpQComboBox->insertItem("Explicit");
153
154         tmpQLabel = new QLabel(&dlg, "Label_6");
155         tmpQLabel->setGeometry(20, 170, 110, 30);
156         tmpQLabel->setText("Programs directory");
157
158         progs = new QLineEdit(&dlg, "LineEdit_4");
159         progs->setGeometry(130, 170, 230, 30);
160         progs->setText("");
161
162         tmpQLabel = new QLabel(&dlg, "Label_7");
163         tmpQLabel->setGeometry(20, 210, 100, 30);
164         tmpQLabel->setText("VLP directory");
165
166         home = new QLineEdit(&dlg, "LineEdit_5");
167         home->setGeometry(130, 210, 230, 30);
168         home->setText("");
169
170         okbtn = new QPushButton(&dlg, "PushButton_5");
171         okbtn->setGeometry(80, 250, 100, 30);
172         okbtn->setText("Ok");
173         connect(okbtn,SIGNAL(clicked()), &dlg, SLOT(accept()));
174         cancelbtn = new QPushButton(&dlg, "PushButton_6");
175         cancelbtn->setGeometry(210, 250, 100, 30);
176         cancelbtn->setText("Cancel");
177         connect(cancelbtn,SIGNAL(clicked()), &dlg, SLOT(reject()));
178         dlg.resize(380, 300);
179         if (dlg.exec()) {
180                 pom = new VLPEntry;
181                 pom->ID = atoi(id->text());
182                 if (check_id(pom->ID)) {
183                         strcpy(pom->addr, addr->text());
184                         if (check_addr(pom->addr)) {
185                                 if (strcmp(tmpQComboBox->currentText(),
186                                                         "Explicit") == 0) {
187                                         pom->type=0;
188                                 }
189                                 strcpy(pom->progdir, progs->text());
190                                 strcpy(pom->homedir, home->text());
191                                 Nodes.append(pom);
192                                 sprintf(pomstr, "Node: %d\t"
193                                                 "Addr: %s\t"
194                                                 "Home dir: %s", pom->ID, pom->addr, pom->homedir);
195                                 nodelist->insertItem(pomstr);
196                                 strcpy(pom->item, pomstr);
197                         } else {
198                                 QMessageBox::message("Error!",
199                                         "Only one VLP on a single computer!",
200                                         "Ok");
201                         }
202                 } else {
203                         QMessageBox::message("Error!", "ID must be unique!",
204                                                                         "Ok");
205                 }
206         }
207 }
208
209 void QInstall::DelNode()
210 {
211         char pom[255];
212         VLPEntry *vpom;
213
214         if (nodelist->currentItem() >= 0) {
215                 strcpy(pom, nodelist->text(nodelist->currentItem()));
216                 vpom = Nodes.first();
217                 while (vpom != NULL) {
218                         if (strcmp(pom, vpom->item) == 0)
219                                 break;
220                         vpom = Nodes.next();
221                 }
222                 if (vpom != NULL)
223                         if (QMessageBox::query("Delete VLP", "Are you sure?",
224                                                                 "Yes", "No")) {
225                                 nodelist->removeItem(nodelist->currentItem());
226                                 Nodes.remove(vpom);
227                         }
228         }
229 }
230
231 void QInstall::SetOptions()
232 {
233         QDialog dlg(this, "", TRUE);
234         QLabel *tmpQLabel;
235         QPushButton *addbtn, *delbtn, *okbtn, *cancelbtn;
236         VLPEntry *pom;
237         int i, j;
238         char pomstr[255];
239
240         //dlg.setStyle(WindowsStyle);
241         nodelist = new QListBox(&dlg, "ListBox_1");
242         nodelist->setGeometry(20, 40, 480, 160);
243
244         tmpQLabel = new QLabel(&dlg, "Label_1");
245         tmpQLabel->setGeometry(20, 10, 100, 30);
246         tmpQLabel->setText("Nodes:");
247
248         addbtn = new QPushButton(&dlg, "PushButton_1");
249         addbtn->setGeometry(30, 210, 100, 30);
250         addbtn->setText("Add VLP");
251         connect(addbtn, SIGNAL(clicked()), this, SLOT(AddNode()));
252
253         delbtn = new QPushButton(&dlg, "PushButton_2");
254         delbtn->setGeometry(150, 210, 100, 30);
255         delbtn->setText("Del VLP");
256         connect(delbtn, SIGNAL(clicked()), this, SLOT(DelNode()));
257
258         okbtn = new QPushButton(&dlg, "PushButton_3");
259         okbtn->setGeometry(270, 210, 100, 30);
260         okbtn->setText("Save files");
261         connect(okbtn, SIGNAL(clicked()), &dlg, SLOT(accept()));
262
263         cancelbtn = new QPushButton(&dlg, "PushButton_4");
264         cancelbtn->setGeometry(390, 210, 100, 30);
265         cancelbtn->setText("Cancel");
266         connect(cancelbtn, SIGNAL(clicked()), &dlg, SLOT(reject()));
267
268         dlg.resize(520, 260);
269
270         if (dlg.exec()) {
271                 if (!Nodes.isEmpty()) {
272                         pom = Nodes.first();
273                         while (pom != NULL) {
274                                 j = Nodes.at();
275                                 sprintf(pomstr, "%s.cfg", pom->addr);
276
277                                 config_t cfg;
278                                 config_setting_t *root, *setting;
279                                 config_init(&cfg);
280
281                                 root = config_root_setting(&cfg);
282
283                                 setting = config_setting_get_member(root, "progdir");
284                                 if(!setting) {
285                                         setting = config_setting_add(root, "progdir", CONFIG_TYPE_STRING);
286                                 }
287                                 config_setting_set_string(setting, pom->progdir);
288
289                                 setting = config_setting_get_member(root, "homedir");
290                                 if(!setting) {
291                                         setting = config_setting_add(root, "homedir", CONFIG_TYPE_STRING);
292                                 }
293                                 config_setting_set_string(setting, pom->homedir);
294
295                                 setting = config_setting_get_member(root, "node_number");
296                                 if(!setting) {
297                                         setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT);
298                                 }
299                                 config_setting_set_int(setting, pom->ID);
300
301                                 if (pom->type == 0) {
302                                         setting = config_setting_get_member(root, "type");
303                                         if(!setting) {
304                                                 setting = config_setting_add(root, "type", CONFIG_TYPE_STRING);
305                                         }
306                                         config_setting_set_string(setting, "explicit");
307                                 }
308                                 for (i = 0; i < Nodes.count(); i++) {
309                                         if (pom != Nodes.at(i)) {
310                                                 setting = config_setting_get_member(root, "host");
311                                                 if (!setting) {
312                                                         setting = config_setting_add(root, "host", CONFIG_TYPE_STRING);
313                                                 }
314                                                 config_setting_set_string(setting, Nodes.at(i)->addr);
315                                         }
316                                 }
317
318                                 if(!config_write_file(&cfg, pomstr)) {
319                                         fprintf(stderr, "Error while writing to file: %s.\n", pomstr);
320                                 }
321                                 config_destroy(&cfg);
322
323                                 pom = Nodes.at(j);
324                                 pom = Nodes.next();
325                         }
326                 }
327         }
328 }
329
330 #include "lgconfig.moc"
331
332 int main(int argc, char **argv)
333 {
334         app = new QApplication(argc,argv);
335         QInstall cfg;
336         //app->setStyle(WindowsStyle);
337         app->setMainWidget(&cfg);
338         cfg.show();
339         return app->exec();
340 }