Code refactoring
[vlp.git] / src / lgconfig / lgconfig.cpp
1 #include <QtGui/QApplication>
2 #include <QtCore/QList>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 #include <libconfig.h>
8
9 #include "lgconfig.h"
10 #include "NodeListDialog.h"
11 #include "vlp/config.h"
12
13 namespace loglan {
14 namespace vlp {
15
16 QInstall::QInstall()
17 {
18         setupUi(this);
19 }
20
21 void QInstall::on_actionConfigure_triggered()
22 {
23         dialog::NodeListDialog dialog(nodes, this);
24
25         if (dialog.exec()) {
26                 nodes = dialog.getNodes();
27
28                 if (!nodes.isEmpty()) {
29
30                         VLPEntry *pom;
31                         int i;
32                         char pomstr[255];
33
34                         QListIterator<VLPEntry *> nodesIterator(nodes);
35
36                         while (nodesIterator.hasNext()) {
37                                 pom = nodesIterator.next();
38                                 i = nodes.indexOf(pom);
39                                 sprintf(pomstr, "%s.cfg", pom->addr);
40
41                                 
42                                 loglan::vlp::Config config;
43
44                                 config.setProgramDir(pom->progdir);
45                                 config.setNodeNumber(pom->ID);
46                                 config.setConnectionType(
47                                         (pom->type == 0) ? EXPLICIT : REGISTER
48                                 );
49
50                                 std::vector<std::string> hosts;
51                                 for (auto node : nodes) {
52                                         if (pom != node) {
53                                                 hosts.push_back(node->addr);
54                                         }
55                                 }
56                                 config.setHosts(hosts);
57
58                                 config.save(pomstr);
59
60 // TODO: add handling of homedir or remove it at all
61 //                              setting = config_setting_get_member(root, "homedir");
62 //                              if(!setting) {
63 //                                      setting = config_setting_add(root, "homedir", CONFIG_TYPE_STRING);
64 //                              }
65 //                              config_setting_set_string(setting, pom->homedir);
66
67
68                                 pom = nodes.at(i);
69                         }
70                 }
71         }
72 }
73
74
75 void QInstall::on_actionQuit_triggered()
76 {
77         QApplication::instance()->quit();
78 }
79
80 }
81 }
82
83
84 int main(int argc, char **argv)
85 {
86         QApplication *app = new QApplication(argc,argv);
87         loglan::vlp::QInstall cfg;
88         cfg.show();
89         return app->exec();
90 }