Update async methods! Now valid ip is detected
[vlc-remote] / playlistmainwindow.cpp
index cefca98..8b1729a 100644 (file)
@@ -21,7 +21,7 @@
 #include <QSettings>
 #include "configdialog.h"
 #include "aboutdialog.h"
-
+#include "accountdialog.h"
 
 PlayListMainWindow::PlayListMainWindow(QWidget *parent) :
         QMainWindow(parent),
@@ -35,14 +35,11 @@ PlayListMainWindow::PlayListMainWindow(QWidget *parent) :
     mCurrentDepth = 0;
     mCurrentVlcIndex = 0;
 
-    QSettings settings;
-    QString currentKey = settings.value("config/currentKey").toString();
-    mIp = settings.value("account/"+currentKey).toString();
-
-    //mIp = settings.value("ip").toString();
 
     mNetManager = new QNetworkAccessManager(this);
 
+    mContents = new QList<VlcPlayListElementSimple>();
+
     ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
     ui->clearButton->setIcon(QIcon::fromTheme("general_delete"));
     ui->shuffleButton->setIcon(QIcon::fromTheme("mediaplayer_default_shuffle"));
@@ -65,7 +62,16 @@ PlayListMainWindow::PlayListMainWindow(QWidget *parent) :
     connect(ui->clearButton,SIGNAL(clicked()),this,SLOT(onClear()));
     connect(ui->listWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
 
-    this->requestPlayList();
+    init();
+
+}
+void PlayListMainWindow::init()  // CALL WHEN CONFIG CHANGES
+{
+    mIp = AccountDialog::currentIp();
+}
+void PlayListMainWindow::showPlayList()  // CALL WHEN SHOWN
+{
+    requestPlayList();
 }
 
 PlayListMainWindow::~PlayListMainWindow()
@@ -103,7 +109,7 @@ void PlayListMainWindow::onListSelectionChanged() {
 void PlayListMainWindow::onRemove() {
     if (0 < this->mCurrentVlcIndex) {
         /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_delete&id=" + QString::number(this->mCurrentVlcIndex))));
-        this->requestPlayList();
+        connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(requestPlayList()));
     }
 }
 void PlayListMainWindow::onPlay() {
@@ -122,82 +128,123 @@ void PlayListMainWindow::onShuffle() {
 }
 void PlayListMainWindow::onClear() {
     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=pl_empty")));
-    this->requestPlayList();
+    connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(requestPlayList()));
 }
 void PlayListMainWindow::requestPlayList() {
-    ui->listWidget->clear();
-    ui->removeButton->setDisabled(true);
-    ui->playButton->setDisabled(true);
-    QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/playlist.xml")));
-    connect(reply,SIGNAL(readyRead()),this,SLOT(parseXmlPlayList()));
+  mContents->clear();
+  ui->listWidget->clear();
+  mResponse.clear();
+  ui->removeButton->setDisabled(true);
+  ui->playButton->setDisabled(true);
+  QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/playlist.xml")));
+  disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(requestPlayList()));
+  connect(reply,SIGNAL(readyRead()),this,SLOT(readReady()));
+  connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
+}
+void PlayListMainWindow::readReady() {
+  QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
+  // append to buffer
+  mResponse += reply->readAll();
+}
+void PlayListMainWindow::finished(QNetworkReply * reply) {
+  // now we can call parseXmlList to process the full buffers
+  this->parseXmlPlayList();
+  // only interested in finished signals
+  disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
 }
 
 void PlayListMainWindow::parseXmlPlayList() {
-    QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
-    QDomDocument doc;
-    doc.setContent(reply->readAll());
-    QDomElement docElem = doc.documentElement();
-    QDomNodeList nodes = docElem.elementsByTagName("node");
-    mContents = new QList<VlcPlayListElementSimple>();
-
-    int depth = 0;
-
-    int ct = nodes.count();
-    qDebug() << "elements " << ct;
-    for (int idx = 0; idx < ct; ++idx) {
-        QDomNode node = nodes.at(idx);
-        QString name = node.attributes().namedItem("name").nodeValue();
-        if (0 == QString::compare("Playlist", name)) {
-            // got the main playlist, let's build it up
-            if (node.hasChildNodes()) {
-                QDomNodeList leafs = node.childNodes();
-                int leafct = leafs.count();
-                if (0 < leafct) {
-                    for (int jdx = 0; jdx < leafct; ++jdx) {
-                        QDomNode leaf = leafs.at(jdx);
-                        VlcPlayListElementSimple* el = new VlcPlayListElementSimple();
-                        el->depth = 1;
-                        el->id = leaf.attributes().namedItem("id").nodeValue().toInt();
-                        el->type = "leaf";
-                        el->path = leaf.attributes().namedItem("uri").nodeValue();
-                        el->name = leaf.attributes().namedItem("name").nodeValue();
-                        this->mContents->append(*el);
-                    }
+  QDomDocument doc;
+  doc.setContent(this->mResponse);
+  QDomElement docElem = doc.documentElement();
+  QDomNodeList nodes = docElem.elementsByTagName("node");
+
+  int depth = 0;
+
+  int ct = nodes.count();
+  for (int idx = 0; idx < ct; ++idx) {
+    QDomNode node = nodes.at(idx);
+    QString name = node.attributes().namedItem("name").nodeValue();
+    int id = node.attributes().namedItem("id").nodeValue().toInt();
+    if (3 == id) {
+      // got the main playlist, let's build it up
+      if (node.hasChildNodes()) {
+        QDomNodeList leafs = node.childNodes();
+        int leafct = leafs.count();
+        if (0 < leafct) {
+          for (int jdx = 0; jdx < leafct; ++jdx) {
+            QDomNode leaf = leafs.at(jdx);
+            VlcPlayListElementSimple* el = new VlcPlayListElementSimple();
+            el->id = leaf.attributes().namedItem("id").nodeValue().toInt();
+            //el->path = leaf.attributes().namedItem("uri").nodeValue();
+            el->name = leaf.attributes().namedItem("name").nodeValue();
+            if (0 == QString::compare(leaf.nodeName(), "node")) {
+              el->depth = 1;
+              el->type = "node";
+              this->mContents->append(*el);
+              // now parse the child nodes as leafs.
+              if (leaf.hasChildNodes()) {
+                QDomNodeList items = leaf.childNodes();
+                int itemct = items.count();
+                if (0 < itemct) {
+                  for (int kdx = 0; kdx < itemct; ++kdx) {
+                    QDomNode item = items.at(kdx);
+                    VlcPlayListElementSimple* it = new VlcPlayListElementSimple();
+                    it->id = item.attributes().namedItem("id").nodeValue().toInt();
+                    //it->path = item.attributes().namedItem("uri").nodeValue();
+                    it->name = item.attributes().namedItem("name").nodeValue();
+                    it->depth = 2;
+                    it->type = "leaf";
+                    this->mContents->append(*it);
+                    delete it;
+                  }
                 }
+              }
             }
-
+            else {
+              el->depth = 1;
+              el->type = "leaf";
+              this->mContents->append(*el);
+            }
+            delete el;
+          }
         }
-    }
-
+      }
 
+    }
+  }
 
-    delete reply;
+  mResponse.clear();
 
-    this->updateList();
+  this->updateList();
 
 }
 
 VlcPlayListElementSimple PlayListMainWindow::getElementFromText(QString text) {
-    //if (0 != QString::compare("", text)) {
-        for (int idx = 0; idx < mContents->count(); ++idx) {
-            if (0 == QString::compare(text, mContents->at(idx).name)) {
-                return mContents->at(idx);
-            }
-        }
+  //if (0 != QString::compare("", text)) {
+    for (int idx = 0; idx < mContents->count(); ++idx) {
+      if (0 == QString::compare(text, mContents->at(idx).name)) {
+        return mContents->at(idx);
+      }
+    }
     //}
     return *(new VlcPlayListElementSimple());
 }
 
 void PlayListMainWindow::updateList() {
-    int ct = this->mContents->count();
-    if (0 < ct) {
-        for (int idx = 0; idx < ct; ++idx) {
-            VlcPlayListElementSimple el = mContents->at(idx);
-            QListWidgetItem* item;
-            item = new QListWidgetItem(QIcon::fromTheme("general_video_file"), el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
-            ui->listWidget->addItem(item);
-            /// TODO - Work out the file / media type and use an appropriate icon instead of the default.
-        }
+  int ct = this->mContents->count();
+  if (0 < ct) {
+    for (int idx = 0; idx < ct; ++idx) {
+      VlcPlayListElementSimple el = mContents->at(idx);
+      QListWidgetItem* item;//
+      if (0 == QString::compare("node", el.type)) {
+        item = new QListWidgetItem(QIcon::fromTheme("filemanager_media_folder"), el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
+      }
+      else {
+        item = new QListWidgetItem(QIcon::fromTheme("general_video_file"), el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
+      }
+      ui->listWidget->addItem(item);
+      /// TODO - Work out the file / media type and use an appropriate icon instead of the default.
     }
+  }
 }
-