fb08ffec53e6c0d97b35066acd53cc527b3f431d
[vlp.git] / src / lgconfig / NodeListDialog.cpp
1 #include <QtGui/QListWidgetItem>
2 #include <QtGui/QMessageBox>
3
4 #include "lgconfig.h" /* VLPEntry */
5 #include "NodeListDialog.h"
6 #include "AddNodeDialog.h"
7
8 namespace loglan {
9 namespace vlp {
10 namespace dialog {
11
12 NodeListDialog::NodeListDialog(QList<VLPEntry*> nodes, QWidget * parent)
13         : QDialog(parent)
14 {
15         setupUi(this);
16         
17         _nodes = nodes;
18
19         connect(saveButton, SIGNAL(clicked()), this, SLOT(accept()));
20         connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
21 }
22
23 NodeListDialog::~NodeListDialog()
24 {
25 }
26
27 bool NodeListDialog::check_id(int id)
28 {
29         QListIterator<VLPEntry *> nodesIterator(_nodes);
30         VLPEntry *node = NULL;
31
32         while (nodesIterator.hasNext()) {
33                 node = nodesIterator.next();
34                 if (node->ID == id)
35                         return false;
36         }
37         return true;
38 }
39
40 bool NodeListDialog::check_addr(QString addr)
41 {
42         QListIterator<VLPEntry *> nodesIterator(_nodes);
43         VLPEntry *node = NULL;
44
45         while (nodesIterator.hasNext()) {
46                 node = nodesIterator.next();
47                 if (node->addr == addr)
48                         return false;
49         }
50         return true;
51 }
52
53 QList<VLPEntry*> NodeListDialog::getNodes()
54 {
55         return _nodes;
56 }
57
58 void NodeListDialog::on_addButton_clicked()
59 {
60         dialog::AddNodeDialog dialog(this);
61
62         if (dialog.exec()) {
63                 int nodeId = dialog.getNodeNumber();
64                 QString ipAddress = dialog.getIPAddress();
65                 if (check_id(nodeId)) {
66                         if (check_addr(ipAddress)) {
67
68                                 VLPEntry *vlpEntry = new VLPEntry;
69                                 vlpEntry->ID = nodeId;
70                                 if (dialog.getConnectionType() == "Explicit") {
71                                         vlpEntry->type = 0;
72                                 }
73                                 strcpy(vlpEntry->addr, ipAddress.toStdString().c_str());
74                                 strcpy(vlpEntry->progdir, dialog.getProgramsDirectory().toStdString().c_str());
75                                 strcpy(vlpEntry->homedir, dialog.getVLPDirectory().toStdString().c_str());
76                                 
77                                 QString info;
78                                 info.sprintf("Node: %d\tAddr: %s\tHome dir: %s",
79                                         vlpEntry->ID,
80                                         vlpEntry->addr,
81                                         vlpEntry->homedir
82                                 );
83
84                                 strcpy(vlpEntry->item, info.toStdString().c_str());
85
86                                 QListWidgetItem * listWidgetItem = new QListWidgetItem;
87                                 listWidgetItem->setText(info);
88                                 nodeList->addItem(listWidgetItem);
89                                 _nodes.append(vlpEntry);
90                         }
91                         else {
92                                 QMessageBox::warning(this,
93                                         "Error!",
94                                         "Only one VLP on a single computer!",
95                                         QMessageBox::Ok
96                                 );
97                         }
98                 }
99                 else {
100                         QMessageBox::warning(this,
101                                 "Error!",
102                                 "ID must be unique!",
103                                 QMessageBox::Ok
104                         );
105                 }
106         }
107 }
108
109 void NodeListDialog::removeNodes(QList<QListWidgetItem *> selectedNodes)
110 {
111         QListIterator<VLPEntry *> nodesIterator(_nodes);
112
113         fprintf(stderr, " > Removing nodes\n");
114         for (auto selectedItem : selectedNodes) {
115                 fprintf(stderr, " > Selected item\n");
116                 VLPEntry *node = NULL;
117                 QString selectedNodeInfo = selectedItem->text();
118
119                 while (nodesIterator.hasNext()) {
120                         node = nodesIterator.next();
121                         if (selectedNodeInfo == node->item) {
122                                 fprintf(stderr, "   - found\n");
123                                 break;
124                         }
125                 }
126                 if (node != NULL) {
127                         fprintf(stderr, "   - removed\n");
128                         int row = nodeList->row(selectedItem);
129                         delete nodeList->takeItem(row);
130                         _nodes.removeOne(node);
131                 }
132         }
133 }
134
135 void NodeListDialog::on_deleteButton_clicked()
136 {
137         if (nodeList->selectedItems().size() > 0) {
138                 if (QMessageBox::question(this,
139                         "Delete VLP",
140                         "Are you sure?",
141                         QMessageBox::Ok | QMessageBox::Cancel
142                 ) == QMessageBox::Ok) {
143                         removeNodes(nodeList->selectedItems());
144                 }
145         }
146 }
147
148 void NodeListDialog::on_saveButton_clicked()
149 {
150         
151 }
152
153 void NodeListDialog::on_cancelButton_clicked()
154 {
155 }
156
157
158 }
159 }
160 }