+#include <QtGui/QListWidgetItem>
+#include <QtGui/QMessageBox>
+
+#include "lgconfig.h" /* VLPEntry */
+#include "NodeListDialog.h"
+#include "AddNodeDialog.h"
+
+namespace loglan {
+namespace vlp {
+namespace dialog {
+
+NodeListDialog::NodeListDialog(QList<VLPEntry*> *nodes, QWidget * parent)
+ : QDialog(parent)
+{
+ setupUi(this);
+
+ _nodes = nodes;
+
+ connect(saveButton, SIGNAL(clicked()), this, SLOT(accept()));
+ connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
+}
+
+NodeListDialog::~NodeListDialog()
+{
+}
+
+bool NodeListDialog::check_id(int id)
+{
+ QListIterator<VLPEntry *> nodesIterator(*_nodes);
+ VLPEntry *node = NULL;
+
+ while (nodesIterator.hasNext()) {
+ node = nodesIterator.next();
+ if (node->ID == id)
+ return false;
+ }
+ return true;
+}
+
+bool NodeListDialog::check_addr(QString addr)
+{
+ QListIterator<VLPEntry *> nodesIterator(*_nodes);
+ VLPEntry *node = NULL;
+
+ while (nodesIterator.hasNext()) {
+ node = nodesIterator.next();
+ if (node->addr == addr)
+ return false;
+ }
+ return true;
+}
+
+void NodeListDialog::on_addButton_clicked()
+{
+ dialog::AddNodeDialog dialog(this);
+
+ if (dialog.exec()) {
+ int nodeId = dialog.getNodeNumber();
+ QString ipAddress = dialog.getIPAddress();
+ if (check_id(nodeId)) {
+ if (check_addr(ipAddress)) {
+
+ VLPEntry *vlpEntry = new VLPEntry;
+ vlpEntry->ID = nodeId;
+ if (dialog.getConnectionType() == "Explicit") {
+ vlpEntry->type = 0;
+ }
+ strcpy(vlpEntry->addr, ipAddress.toStdString().c_str());
+ strcpy(vlpEntry->progdir, dialog.getProgramsDirectory().toStdString().c_str());
+ strcpy(vlpEntry->homedir, dialog.getVLPDirectory().toStdString().c_str());
+
+ QString info;
+ info.sprintf("Node: %d\tAddr: %s\tHome dir: %s",
+ vlpEntry->ID,
+ vlpEntry->addr,
+ vlpEntry->homedir
+ );
+
+ strcpy(vlpEntry->item, info.toStdString().c_str());
+
+ QListWidgetItem * listWidgetItem = new QListWidgetItem;
+ listWidgetItem->setText(info);
+ nodeList->addItem(listWidgetItem);
+ _nodes->append(vlpEntry);
+ }
+ else {
+ QMessageBox::warning(this,
+ "Error!",
+ "Only one VLP on a single computer!",
+ QMessageBox::Ok
+ );
+ }
+ }
+ else {
+ QMessageBox::warning(this,
+ "Error!",
+ "ID must be unique!",
+ QMessageBox::Ok
+ );
+ }
+ }
+}
+
+void NodeListDialog::removeNodes(QList<QListWidgetItem *> selectedNodes)
+{
+ QListIterator<VLPEntry *> nodesIterator(*_nodes);
+
+ fprintf(stderr, " > Removing nodes\n");
+ for (auto selectedItem : selectedNodes) {
+ fprintf(stderr, " > Selected item\n");
+ VLPEntry *node = NULL;
+ QString selectedNodeInfo = selectedItem->text();
+
+ while (nodesIterator.hasNext()) {
+ node = nodesIterator.next();
+ if (selectedNodeInfo == node->item) {
+ fprintf(stderr, " - found\n");
+ break;
+ }
+ }
+ if (node != NULL) {
+ fprintf(stderr, " - removed\n");
+ int row = nodeList->row(selectedItem);
+ delete nodeList->takeItem(row);
+ _nodes->removeOne(node);
+ }
+ }
+}
+
+void NodeListDialog::on_deleteButton_clicked()
+{
+ if (nodeList->selectedItems().size() > 0) {
+ if (QMessageBox::question(this,
+ "Delete VLP",
+ "Are you sure?",
+ QMessageBox::Ok | QMessageBox::Cancel
+ ) == QMessageBox::Ok) {
+ removeNodes(nodeList->selectedItems());
+ }
+ }
+}
+
+void NodeListDialog::on_saveButton_clicked()
+{
+
+}
+
+void NodeListDialog::on_cancelButton_clicked()
+{
+}
+
+
+}
+}
+}