Move main lgconfig layout code into separate ui file
[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 #include "lgconfig.h"
27
28 QApplication *app;
29
30 QInstall::QInstall()
31 {
32         setupUi(this);
33
34         Nodes.clear();
35 }
36
37 bool QInstall::check_id(int id)
38 {
39         VLPEntry *pom;
40         QListIterator<VLPEntry *> nodesIterator(Nodes);
41
42         while (nodesIterator.hasNext()) {
43                 pom = nodesIterator.next();
44                 if (pom->ID == id)
45                         return FALSE;
46         }
47         return TRUE;
48 }
49
50 bool QInstall::check_addr(char *addr)
51 {
52         VLPEntry *pom;
53         QListIterator<VLPEntry *> nodesIterator(Nodes);
54
55         while (nodesIterator.hasNext()) {
56                 pom = nodesIterator.next();
57                 if (strcmp(pom->addr, addr) == 0)
58                         return FALSE;
59         }
60         return TRUE;
61 }
62
63 void QInstall::AddNode()
64 {
65         QDialog dlg(this);
66         QLabel *tmpQLabel;
67         QLineEdit *id, *addr, *progs, *home;
68         QPushButton *okbtn, *cancelbtn;
69         VLPEntry *pom;
70         char pomstr[255];
71
72         tmpQLabel = new QLabel(&dlg);
73         tmpQLabel->setGeometry(110, 10, 180, 30);
74         tmpQLabel->setFrameStyle(49);
75         tmpQLabel->setText("Virtual Processor Properties");
76
77         id = new QLineEdit(&dlg);
78         id->setGeometry(130, 50, 50, 30);
79         id->setText("");
80
81         tmpQLabel = new QLabel(&dlg);
82         tmpQLabel->setGeometry(20, 50, 90, 30);
83         tmpQLabel->setText("Node number");
84
85         tmpQLabel = new QLabel(&dlg);
86         tmpQLabel->setGeometry(20, 90, 80, 30);
87         tmpQLabel->setText("IP Address");
88
89         addr = new QLineEdit(&dlg);
90         addr->setGeometry(130, 90, 120, 30);
91         addr->setText("");
92
93         tmpQLabel = new QLabel(&dlg);
94         tmpQLabel->setGeometry(20, 130, 100, 30);
95         tmpQLabel->setText("Connection type");
96
97         QComboBox* tmpQComboBox;
98         tmpQComboBox = new QComboBox(&dlg);
99         tmpQComboBox->setGeometry(130, 130, 100, 30);
100         tmpQComboBox->insertItem(0, "Explicit");
101
102         tmpQLabel = new QLabel(&dlg);
103         tmpQLabel->setGeometry(20, 170, 110, 30);
104         tmpQLabel->setText("Programs directory");
105
106         progs = new QLineEdit(&dlg);
107         progs->setGeometry(130, 170, 230, 30);
108         progs->setText("");
109
110         tmpQLabel = new QLabel(&dlg);
111         tmpQLabel->setGeometry(20, 210, 100, 30);
112         tmpQLabel->setText("VLP directory");
113
114         home = new QLineEdit(&dlg);
115         home->setGeometry(130, 210, 230, 30);
116         home->setText("");
117
118         okbtn = new QPushButton(&dlg);
119         okbtn->setGeometry(80, 250, 100, 30);
120         okbtn->setText("Ok");
121         connect(okbtn,SIGNAL(clicked()), &dlg, SLOT(accept()));
122         cancelbtn = new QPushButton(&dlg);
123         cancelbtn->setGeometry(210, 250, 100, 30);
124         cancelbtn->setText("Cancel");
125         connect(cancelbtn,SIGNAL(clicked()), &dlg, SLOT(reject()));
126         dlg.resize(380, 300);
127         if (dlg.exec()) {
128                 pom = new VLPEntry;
129                 pom->ID = id->text().toInt();
130                 if (check_id(pom->ID)) {
131                         strcpy(pom->addr, addr->text().toStdString().c_str());
132                         if (check_addr(pom->addr)) {
133                                 if (tmpQComboBox->currentText() == "Explicit") {
134                                         pom->type=0;
135                                 }
136                                 strcpy(pom->progdir, progs->text().toStdString().c_str());
137                                 strcpy(pom->homedir, home->text().toStdString().c_str());
138                                 Nodes.append(pom);
139                                 sprintf(pomstr, "Node: %d\t"
140                                                 "Addr: %s\t"
141                                                 "Home dir: %s", pom->ID, pom->addr, pom->homedir);
142                                 nodelist->insertItem(pomstr);
143                                 strcpy(pom->item, pomstr);
144                         } else {
145                                 QMessageBox::warning(this, "Error!", "Only one VLP on a single computer!", "Ok");
146                         }
147                 } else {
148                         QMessageBox::warning(this, "Error!", "ID must be unique!", "Ok");
149                 }
150         }
151 }
152
153 void QInstall::DelNode()
154 {
155         char pom[255];
156         VLPEntry *vpom;
157
158         if (nodelist->currentItem() >= 0) {
159                 strcpy(pom, nodelist->text(nodelist->currentItem()).toStdString().c_str());
160
161                 QListIterator<VLPEntry *> nodesIterator(Nodes);
162                 
163                 while (nodesIterator.hasNext()) {
164                         vpom = nodesIterator.next();
165                         if (strcmp(pom, vpom->item) == 0)
166                                 break;
167                 }
168                 if (vpom != NULL)
169                         if (QMessageBox::question(this, "Delete VLP", "Are you sure?", "Yes", "No")) {
170                                 nodelist->removeItem(nodelist->currentItem());
171                                 Nodes.removeOne(vpom);
172                         }
173         }
174 }
175
176 void QInstall::on_actionConfigure_triggered()
177 {
178         QDialog dlg(this);
179         QLabel *tmpQLabel;
180         QPushButton *addbtn, *delbtn, *okbtn, *cancelbtn;
181         VLPEntry *pom;
182         int i;
183         char pomstr[255];
184
185         //dlg.setStyle(WindowsStyle);
186         nodelist = new Q3ListBox(&dlg);
187         nodelist->setGeometry(20, 40, 480, 160);
188
189         tmpQLabel = new QLabel(&dlg);
190         tmpQLabel->setGeometry(20, 10, 100, 30);
191         tmpQLabel->setText("Nodes:");
192
193         addbtn = new QPushButton(&dlg);
194         addbtn->setGeometry(30, 210, 100, 30);
195         addbtn->setText("Add VLP");
196         connect(addbtn, SIGNAL(clicked()), this, SLOT(AddNode()));
197
198         delbtn = new QPushButton(&dlg);
199         delbtn->setGeometry(150, 210, 100, 30);
200         delbtn->setText("Del VLP");
201         connect(delbtn, SIGNAL(clicked()), this, SLOT(DelNode()));
202
203         okbtn = new QPushButton(&dlg);
204         okbtn->setGeometry(270, 210, 100, 30);
205         okbtn->setText("Save files");
206         connect(okbtn, SIGNAL(clicked()), &dlg, SLOT(accept()));
207
208         cancelbtn = new QPushButton(&dlg);
209         cancelbtn->setGeometry(390, 210, 100, 30);
210         cancelbtn->setText("Cancel");
211         connect(cancelbtn, SIGNAL(clicked()), &dlg, SLOT(reject()));
212
213         dlg.resize(520, 260);
214
215         if (dlg.exec()) {
216                 if (!Nodes.isEmpty()) {
217                         QListIterator<VLPEntry *> nodesIterator(Nodes);
218                         
219                         while (nodesIterator.hasNext()) {
220                                 pom = nodesIterator.next();
221                                 i = Nodes.indexOf(pom);
222                                 sprintf(pomstr, "%s.cfg", pom->addr);
223
224                                 config_t cfg;
225                                 config_setting_t *root, *setting;
226                                 config_init(&cfg);
227
228                                 root = config_root_setting(&cfg);
229
230                                 setting = config_setting_get_member(root, "progdir");
231                                 if(!setting) {
232                                         setting = config_setting_add(root, "progdir", CONFIG_TYPE_STRING);
233                                 }
234                                 config_setting_set_string(setting, pom->progdir);
235
236                                 setting = config_setting_get_member(root, "homedir");
237                                 if(!setting) {
238                                         setting = config_setting_add(root, "homedir", CONFIG_TYPE_STRING);
239                                 }
240                                 config_setting_set_string(setting, pom->homedir);
241
242                                 setting = config_setting_get_member(root, "node_number");
243                                 if(!setting) {
244                                         setting = config_setting_add(root, "node_number", CONFIG_TYPE_INT);
245                                 }
246                                 config_setting_set_int(setting, pom->ID);
247
248                                 if (pom->type == 0) {
249                                         setting = config_setting_get_member(root, "type");
250                                         if(!setting) {
251                                                 setting = config_setting_add(root, "type", CONFIG_TYPE_STRING);
252                                         }
253                                         config_setting_set_string(setting, "explicit");
254                                 }
255                                 for (auto node : Nodes) {
256                                         if (pom != node) {
257                                                 setting = config_setting_get_member(root, "host");
258                                                 if (!setting) {
259                                                         setting = config_setting_add(root, "host", CONFIG_TYPE_STRING);
260                                                 }
261                                                 config_setting_set_string(setting, node->addr);
262                                         }
263                                 }
264
265                                 if(!config_write_file(&cfg, pomstr)) {
266                                         fprintf(stderr, "Error while writing to file: %s.\n", pomstr);
267                                 }
268                                 config_destroy(&cfg);
269
270                                 pom = Nodes.at(i);
271                         }
272                 }
273         }
274 }
275
276
277 void QInstall::on_actionQuit_triggered()
278 {
279         QApplication::instance()->quit();
280 }
281
282
283 int main(int argc, char **argv)
284 {
285         app = new QApplication(argc,argv);
286         QInstall cfg;
287         cfg.show();
288         return app->exec();
289 }