Update async methods! Now valid ip is detected
[vlc-remote] / accountdialog.cpp
index b64f7e8..c87215a 100644 (file)
@@ -6,13 +6,32 @@
 #include <QSettings>
 #include <QTcpSocket>
 #include <QFuture>
-#include <QtConcurrentRun>
+#include <QtConcurrentMap>
+
+
+
+QListWidgetItem asyncTestItem(const QListWidgetItem &item)
+{
+
+    QListWidgetItem item2= item;
+    item2.setText("boby");
+
+    return item;
+
+}
+
+
+
+
 AccountDialog::AccountDialog(QWidget *parent) :
         QDialog(parent),
         ui(new Ui::AccountDialog)
 {
     ui->setupUi(this);
 
+    mFuturWatcher = new QFutureWatcher<QListWidgetItem>(this);
+    connect(mFuturWatcher,SIGNAL(resultReadyAt(int)),this,SLOT(setAsyncItem(int)));
+
     connect(ui->addButton,SIGNAL(clicked()),this,SLOT(add()));
     connect(ui->editButton,SIGNAL(clicked()),this,SLOT(edit()));
     connect(ui->remButton,SIGNAL(clicked()),this,SLOT(rem()));
@@ -60,6 +79,7 @@ void AccountDialog::load()
 
     QString useKey = settings.value("config/currentKey").toString();
 
+    QList <QListWidgetItem> asycItems;
 
     settings.beginGroup("account");
     foreach ( QString key, settings.allKeys())
@@ -74,42 +94,58 @@ void AccountDialog::load()
             item->setFont(font);
         }
         ui->listWidget->addItem(item);
+        asycItems.append(*item);
     }
     settings.endGroup();    
 
 
+    // ... create and add in the list widget
+
+    //  QFuture<QListWidgetItem> itemFutur = QtConcurrent::mapped(asycItems, asyncTestItem);
 
-}
 
+    mFuturWatcher->setFuture(QtConcurrent::mapped(asycItems, asyncTestItem));
+}
 
-void AccountDialog::asyncTestConnection()
+QListWidgetItem AccountDialog::asyncTestItem(const QListWidgetItem& item)
 {
+    QListWidgetItem newItem = item;
+
+    QTcpSocket * socket = new QTcpSocket;
     QSettings settings;
-    settings.beginGroup("account");
+    QString host = settings.value("account/"+item.text()).toString();
+    QStringList hostSplit = host.split(":");
+    QString ip   = hostSplit.at(0);
+    QString port = hostSplit.at(1);
 
-    for ( int i=0; i<ui->listWidget->count(); ++i)
-    {
 
-        QListWidgetItem * item = ui->listWidget->item(i);
-        QString key = item->text();
-        QString hostIp = settings.value(key).toString();
-        qDebug()<<hostIp;
-        QTcpSocket * socket = new QTcpSocket(this);
-        socket->connectToHost(hostIp, 80);
+    socket->connectToHost(ip,port.toInt());
 
-        if (socket->waitForConnected(1000))
-            item->setBackgroundColor(Qt::green);
-        else
-            item->setBackgroundColor(Qt::red);
+    if (socket->waitForConnected(1000))
+        newItem.setBackgroundColor(Qt::green);
+    else
+        newItem.setBackgroundColor(Qt::red);
 
+    return newItem;
+
+
+}
+
+
+
+void AccountDialog::setAsyncItem(int row)  // EDIT THE ROW AFTER ASYNC FUNCTION FINISHED
+{
+    QListWidgetItem newItem = mFuturWatcher->resultAt(row);
+    QListWidgetItem * item = ui->listWidget->item(row);
+
+    item->setBackgroundColor(newItem.backgroundColor());
 
-    }
 
-    settings.endGroup();
 
 }
 
 
+
 void AccountDialog::edit()
 {
     QString currentIp = ui->listWidget->currentItem()->data(Qt::UserRole).toString();