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