7c7fcb2ef49ebf1a7fc6253e091a3086bd1c7fc6
[vlp.git] / src / lgconfig / lgconfig.cpp
1 #include <QtGui/QApplication>
2 #include <QtGui/QMainWindow>
3 #include <Qt3Support/q3multilineedit.h>
4 #include <QtGui/QMenuBar>
5 // #include <qpopmenu.h>
6 #include <QtGui/QDialog>
7 #include <QtGui/QButtonGroup>
8 #include <QtGui/QLabel>
9 #include <QtGui/QLineEdit>
10 #include <Qt3Support/Q3ListBox>
11 #include <QtGui/QPushButton>
12 #include <QtGui/QRadioButton>
13 #include <QtCore/QList>
14 #include <QtCore/qfile.h>
15 #include <QtGui/qcombobox.h>
16 #include <QtGui/QToolTip>
17 #include <QtGui/QFont>
18 #include <QtGui/QPixmap>
19 #include <QtGui/QMessageBox>
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 QMainWindow {
38         Q_OBJECT
39 public:
40         QMenuBar *bar;
41         Q3ListBox *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         setWindowTitle("VLP Configuration Tool");
67
68         QMenu * programMenu = NULL;
69         programMenu = menuBar()->addMenu("&Program");
70         programMenu->addAction("Configure", this, SLOT(SetOptions()));
71         programMenu->addAction("Quit", app, SLOT(quit()));
72
73 //      bar = new QMenuBar(this);
74 //      bar->insertItem("Configure", this, SLOT(SetOptions()));
75 //      bar->insertItem("Quit", app, SLOT(quit()));
76 //      bar->setFont(f);
77 //      infob->setGeometry(0, bar->height(), 200, 30);
78         if (mp.load("logo.bmp")) {
79 //              infob->setPixmap(mp);
80 //              infob->resize(mp.width(), mp.height());
81         }
82 //      resize(infob->width(), infob->height() + bar->height());
83         Nodes.clear();
84 }
85
86 bool QInstall::check_id(int id)
87 {
88         VLPEntry *pom;
89         QListIterator<VLPEntry *> nodesIterator(Nodes);
90
91         while (nodesIterator.hasNext()) {
92                 pom = nodesIterator.next();
93                 if (pom->ID == id)
94                         return FALSE;
95         }
96         return TRUE;
97 }
98
99 bool QInstall::check_addr(char *addr)
100 {
101         VLPEntry *pom;
102         QListIterator<VLPEntry *> nodesIterator(Nodes);
103
104         while (nodesIterator.hasNext()) {
105                 pom = nodesIterator.next();
106                 if (strcmp(pom->addr, addr) == 0)
107                         return FALSE;
108         }
109         return TRUE;
110 }
111
112 /**
113  * @attention Currently not in use
114  */
115 void QInstall::Info()
116 {
117 }
118
119 void QInstall::AddNode()
120 {
121         QDialog dlg(this);
122         QLabel *tmpQLabel;
123         QLineEdit *id, *addr, *progs, *home;
124         QPushButton *okbtn, *cancelbtn;
125         VLPEntry *pom;
126         char pomstr[255];
127
128         tmpQLabel = new QLabel(&dlg);
129         tmpQLabel->setGeometry(110, 10, 180, 30);
130         tmpQLabel->setFrameStyle(49);
131         tmpQLabel->setText("Virtual Processor Properties");
132
133         id = new QLineEdit(&dlg);
134         id->setGeometry(130, 50, 50, 30);
135         id->setText("");
136
137         tmpQLabel = new QLabel(&dlg);
138         tmpQLabel->setGeometry(20, 50, 90, 30);
139         tmpQLabel->setText("Node number");
140
141         tmpQLabel = new QLabel(&dlg);
142         tmpQLabel->setGeometry(20, 90, 80, 30);
143         tmpQLabel->setText("IP Address");
144
145         addr = new QLineEdit(&dlg);
146         addr->setGeometry(130, 90, 120, 30);
147         addr->setText("");
148
149         tmpQLabel = new QLabel(&dlg);
150         tmpQLabel->setGeometry(20, 130, 100, 30);
151         tmpQLabel->setText("Connection type");
152
153         QComboBox* tmpQComboBox;
154         tmpQComboBox = new QComboBox(&dlg);
155         tmpQComboBox->setGeometry(130, 130, 100, 30);
156         tmpQComboBox->insertItem(0, "Explicit");
157
158         tmpQLabel = new QLabel(&dlg);
159         tmpQLabel->setGeometry(20, 170, 110, 30);
160         tmpQLabel->setText("Programs directory");
161
162         progs = new QLineEdit(&dlg);
163         progs->setGeometry(130, 170, 230, 30);
164         progs->setText("");
165
166         tmpQLabel = new QLabel(&dlg);
167         tmpQLabel->setGeometry(20, 210, 100, 30);
168         tmpQLabel->setText("VLP directory");
169
170         home = new QLineEdit(&dlg);
171         home->setGeometry(130, 210, 230, 30);
172         home->setText("");
173
174         okbtn = new QPushButton(&dlg);
175         okbtn->setGeometry(80, 250, 100, 30);
176         okbtn->setText("Ok");
177         connect(okbtn,SIGNAL(clicked()), &dlg, SLOT(accept()));
178         cancelbtn = new QPushButton(&dlg);
179         cancelbtn->setGeometry(210, 250, 100, 30);
180         cancelbtn->setText("Cancel");
181         connect(cancelbtn,SIGNAL(clicked()), &dlg, SLOT(reject()));
182         dlg.resize(380, 300);
183         if (dlg.exec()) {
184                 pom = new VLPEntry;
185                 pom->ID = id->text().toInt();
186                 if (check_id(pom->ID)) {
187                         strcpy(pom->addr, addr->text().toStdString().c_str());
188                         if (check_addr(pom->addr)) {
189                                 if (tmpQComboBox->currentText() == "Explicit") {
190                                         pom->type=0;
191                                 }
192                                 strcpy(pom->progdir, progs->text().toStdString().c_str());
193                                 strcpy(pom->homedir, home->text().toStdString().c_str());
194                                 Nodes.append(pom);
195                                 sprintf(pomstr, "Node: %d\t"
196                                                 "Addr: %s\t"
197                                                 "Home dir: %s", pom->ID, pom->addr, pom->homedir);
198                                 nodelist->insertItem(pomstr);
199                                 strcpy(pom->item, pomstr);
200                         } else {
201                                 QMessageBox::warning(this, "Error!", "Only one VLP on a single computer!", "Ok");
202                         }
203                 } else {
204                         QMessageBox::warning(this, "Error!", "ID must be unique!", "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()).toStdString().c_str());
216
217                 QListIterator<VLPEntry *> nodesIterator(Nodes);
218                 
219                 while (nodesIterator.hasNext()) {
220                         vpom = nodesIterator.next();
221                         if (strcmp(pom, vpom->item) == 0)
222                                 break;
223                 }
224                 if (vpom != NULL)
225                         if (QMessageBox::question(this, "Delete VLP", "Are you sure?", "Yes", "No")) {
226                                 nodelist->removeItem(nodelist->currentItem());
227                                 Nodes.removeOne(vpom);
228                         }
229         }
230 }
231
232 void QInstall::SetOptions()
233 {
234         QDialog dlg(this);
235         QLabel *tmpQLabel;
236         QPushButton *addbtn, *delbtn, *okbtn, *cancelbtn;
237         VLPEntry *pom;
238         int i;
239         char pomstr[255];
240
241         //dlg.setStyle(WindowsStyle);
242         nodelist = new Q3ListBox(&dlg);
243         nodelist->setGeometry(20, 40, 480, 160);
244
245         tmpQLabel = new QLabel(&dlg);
246         tmpQLabel->setGeometry(20, 10, 100, 30);
247         tmpQLabel->setText("Nodes:");
248
249         addbtn = new QPushButton(&dlg);
250         addbtn->setGeometry(30, 210, 100, 30);
251         addbtn->setText("Add VLP");
252         connect(addbtn, SIGNAL(clicked()), this, SLOT(AddNode()));
253
254         delbtn = new QPushButton(&dlg);
255         delbtn->setGeometry(150, 210, 100, 30);
256         delbtn->setText("Del VLP");
257         connect(delbtn, SIGNAL(clicked()), this, SLOT(DelNode()));
258
259         okbtn = new QPushButton(&dlg);
260         okbtn->setGeometry(270, 210, 100, 30);
261         okbtn->setText("Save files");
262         connect(okbtn, SIGNAL(clicked()), &dlg, SLOT(accept()));
263
264         cancelbtn = new QPushButton(&dlg);
265         cancelbtn->setGeometry(390, 210, 100, 30);
266         cancelbtn->setText("Cancel");
267         connect(cancelbtn, SIGNAL(clicked()), &dlg, SLOT(reject()));
268
269         dlg.resize(520, 260);
270
271         if (dlg.exec()) {
272                 if (!Nodes.isEmpty()) {
273                         QListIterator<VLPEntry *> nodesIterator(Nodes);
274                         
275                         while (nodesIterator.hasNext()) {
276                                 pom = nodesIterator.next();
277                                 i = Nodes.indexOf(pom);
278                                 sprintf(pomstr, "%s.cfg", pom->addr);
279
280                                 config_t cfg;
281                                 config_setting_t *root, *setting;
282                                 config_init(&cfg);
283
284                                 root = config_root_setting(&cfg);
285
286                                 setting = config_setting_get_member(root, "progdir");
287                                 if(!setting) {
288                                         setting = config_setting_add(root, "progdir", CONFIG_TYPE_STRING);
289                                 }
290                                 config_setting_set_string(setting, pom->progdir);
291
292                                 setting = config_setting_get_member(root, "homedir");
293                                 if(!setting) {
294                                         setting = config_setting_add(root, "homedir", CONFIG_TYPE_STRING);
295                                 }
296                                 config_setting_set_string(setting, pom->homedir);
297
298                                 setting = config_setting_get_member(root, "node_number");
299                                 if(!setting) {
300                                         setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT);
301                                 }
302                                 config_setting_set_int(setting, pom->ID);
303
304                                 if (pom->type == 0) {
305                                         setting = config_setting_get_member(root, "type");
306                                         if(!setting) {
307                                                 setting = config_setting_add(root, "type", CONFIG_TYPE_STRING);
308                                         }
309                                         config_setting_set_string(setting, "explicit");
310                                 }
311                                 for (auto node : Nodes) {
312                                         if (pom != node) {
313                                                 setting = config_setting_get_member(root, "host");
314                                                 if (!setting) {
315                                                         setting = config_setting_add(root, "host", CONFIG_TYPE_STRING);
316                                                 }
317                                                 config_setting_set_string(setting, node->addr);
318                                         }
319                                 }
320
321                                 if(!config_write_file(&cfg, pomstr)) {
322                                         fprintf(stderr, "Error while writing to file: %s.\n", pomstr);
323                                 }
324                                 config_destroy(&cfg);
325
326                                 pom = Nodes.at(i);
327                         }
328                 }
329         }
330 }
331
332 #include "lgconfig.moc"
333
334 int main(int argc, char **argv)
335 {
336         app = new QApplication(argc,argv);
337         QInstall cfg;
338         cfg.show();
339         return app->exec();
340 }