Playlist tweak - minimizing impact of refresh on the UI.
[vlc-remote] / playlistmainwindow.cpp
index 8a9d978..ca4ce3b 100644 (file)
 #include "ui_playlistmainwindow.h"
 #include <QPushButton>
 #include <QSettings>
+#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+#include <QMaemo5InformationBox>
+#endif
 #include "configdialog.h"
 #include "aboutdialog.h"
-#include "accountdialog.h"
+#include "appsettings.h"
+#include "vlcstatus.h"
 
 PlayListMainWindow::PlayListMainWindow(QWidget *parent) :
         QMainWindow(parent),
@@ -70,7 +74,7 @@ PlayListMainWindow::PlayListMainWindow(QWidget *parent) :
 }
 void PlayListMainWindow::init()  // CALL WHEN CONFIG CHANGES
 {
-    mIp = AccountDialog::currentIp();
+    mIp = AppSettings::getCurrentIp(); // AccountDialog::currentIp();
 }
 void PlayListMainWindow::showPlayList()  // CALL WHEN SHOWN
 {
@@ -134,16 +138,27 @@ void PlayListMainWindow::onClear() {
     connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(requestPlayList()));
 }
 void PlayListMainWindow::requestPlayList() {
-  mContents->clear();
-  ui->listWidget->clear();
   mResponse.clear();
   ui->removeButton->setDisabled(true);
   ui->playButton->setDisabled(true);
+#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+    this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
+#endif
   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(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(error(QNetworkReply::NetworkError)));
   connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
 }
+void PlayListMainWindow::error(QNetworkReply::NetworkError code) {
+#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+    this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
+#endif
+    qDebug() << code;
+#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+    QMaemo5InformationBox::information(this, tr("Playlist could not be retrieved."), QMaemo5InformationBox::DefaultTimeout);
+#endif
+}
 void PlayListMainWindow::readReady() {
   QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
   // append to buffer
@@ -154,15 +169,23 @@ void PlayListMainWindow::finished(QNetworkReply * reply) {
   this->parseXmlPlayList();
   // only interested in finished signals
   disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
+#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+    this->setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
+#endif
 }
 
 void PlayListMainWindow::parseXmlPlayList() {
+  mContents->clear();
   QDomDocument doc;
   doc.setContent(this->mResponse);
   QDomElement docElem = doc.documentElement();
   QDomNodeList nodes = docElem.elementsByTagName("node");
   int depth = 0;
 
+  int currentLeafId = 0;
+  bool hasArt = false;
+  QString extension = "";
+
   int ct = nodes.count();
   for (int idx = 0; idx < ct; ++idx) {
     QDomNode node = nodes.at(idx);
@@ -175,15 +198,18 @@ void PlayListMainWindow::parseXmlPlayList() {
         QDomNodeList leafs = node.childNodes();
         int leafct = leafs.count();
         if (0 < leafct) {
+          depth = 1;
           for (int jdx = 0; jdx < leafct; ++jdx) {
             QDomNode leaf = leafs.at(jdx);
+            parsePlayListItem(&leaf, &extension, &hasArt, &currentLeafId, 1);
+            /*
             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().replace("\\\\", "\\");
             current = leaf.attributes().namedItem("current").nodeValue();
             el->playing = (0 < current.length());
-            el->depth = 1;
+            el->depth = depth;
             if (0 == QString::compare(leaf.nodeName(), "node")) {
               el->type = "node";
               el->extension = getExtension(el->path, NULL);
@@ -193,6 +219,7 @@ void PlayListMainWindow::parseXmlPlayList() {
                 QDomNodeList items = leaf.childNodes();
                 int itemct = items.count();
                 if (0 < itemct) {
+                  depth = 2;
                   for (int kdx = 0; kdx < itemct; ++kdx) {
                     QDomNode item = items.at(kdx);
                     VlcPlayListElementSimple* it = new VlcPlayListElementSimple();
@@ -204,6 +231,12 @@ void PlayListMainWindow::parseXmlPlayList() {
                     it->type = "leaf";
                     current = item.attributes().namedItem("current").nodeValue();
                     it->playing = (0 < current.length());
+                    if (it->playing) {
+                        currentLeafId = it->id;
+                        QString art = item.toElement().namedItem("art_url").toElement().text();
+                        hasArt = (!art.isNull() && !art.isEmpty());
+                        extension = getExtension(it->path, NULL);
+                    }
                     this->mContents->append(*it);
                     delete it;
                   }
@@ -213,9 +246,16 @@ void PlayListMainWindow::parseXmlPlayList() {
             else {
               el->type = "leaf";
               el->extension = getExtension(el->path, NULL);
+              if (el->playing) {
+                  currentLeafId = el->id;
+                  QString art = leaf.toElement().namedItem("art_url").toElement().text();
+                  hasArt = (!art.isNull() && !art.isEmpty());
+                  extension = getExtension(el->path, NULL);
+              }
               this->mContents->append(*el);
             }
             delete el;
+            */
           }
         }
       }
@@ -225,8 +265,57 @@ void PlayListMainWindow::parseXmlPlayList() {
 
   mResponse.clear();
 
+  emit this->idUpdated(currentLeafId, hasArt, extension);
   this->updateList();
 
+
+}
+
+
+void PlayListMainWindow::parsePlayListItem(QDomNode *node, QString *extension, bool *hasArt, int *currentLeafId, int depth) {
+    if (NULL != node) {
+        QString current;
+        VlcPlayListElementSimple* el = new VlcPlayListElementSimple();
+        el->id = node->attributes().namedItem("id").nodeValue().toInt();
+        el->path = node->attributes().namedItem("uri").nodeValue();
+        el->name = node->attributes().namedItem("name").nodeValue().replace("\\\\", "\\");
+        current = node->attributes().namedItem("current").nodeValue();
+        el->playing = (0 < current.length());
+        el->depth = depth;
+        if (0 != QString::compare(node->nodeName(), "node")) {
+            el->type = "leaf";
+            el->extension = getExtension(el->path, NULL);
+            if (el->playing) {
+                *currentLeafId = el->id;
+                QString art = node->toElement().namedItem("art_url").toElement().text();
+                *hasArt = (!art.isNull() && !art.isEmpty());
+                *extension = getExtension(el->path, NULL);
+            }
+            this->mContents->append(*el);
+            delete el;
+        }
+        else {
+            el->type = "node";
+            el->extension = getExtension(el->path, NULL);
+            // empty nodes appear in the playlist when they can't be played!
+            if (node->hasChildNodes()) {
+                this->mContents->append(*el);
+            }
+            delete el;
+            // now parse the child nodes as leaf.
+            if (node->hasChildNodes()) {
+                QDomNodeList items = node->childNodes();
+                int itemct = items.count();
+                if (0 < itemct) {
+                  ++depth;
+                  for (int kdx = 0; kdx < itemct; ++kdx) {
+                    QDomNode item = items.at(kdx);
+                    parsePlayListItem(&item, extension, hasArt, currentLeafId, depth);
+                  }
+                }
+            }
+        }
+    }
 }
 
 QString PlayListMainWindow::getExtension(QString path, QString extension) {
@@ -256,6 +345,7 @@ VlcPlayListElementSimple PlayListMainWindow::getElementFromText(QString text) {
 }
 
 void PlayListMainWindow::updateList() {
+  ui->listWidget->clear();
   int ct = this->mContents->count();
   if (0 < ct) {
     QIcon icon_audio  = QIcon::fromTheme("general_audio_file");
@@ -263,6 +353,8 @@ void PlayListMainWindow::updateList() {
     QIcon icon_image  = QIcon::fromTheme("general_image");
     QIcon icon_flash  = QIcon::fromTheme("filemanager_flash_file");
     QIcon icon_media  = QIcon::fromTheme("filemanager_media_folder");
+    QIcon icon_real   = QIcon::fromTheme("filemanager_real_music");
+    QIcon icon_unknown= QIcon::fromTheme("filemanager_unknown_file");
     for (int idx = 0; idx < ct; ++idx) {
       VlcPlayListElementSimple el = mContents->at(idx);
       QListWidgetItem* item;
@@ -290,14 +382,23 @@ void PlayListMainWindow::updateList() {
                     0 == QString::compare(el.extension, "mpg")  ||
                     0 == QString::compare(el.extension, "mov")  ||
                     0 == QString::compare(el.extension, "mp4")  ||
+                    0 == QString::compare(el.extension, "m4v")  ||
                     0 == QString::compare(el.extension, "wmv")  ||
                     0 == QString::compare(el.extension, "mkv")  ||
                     0 == QString::compare(el.extension, "ogv")  ) {
-              item = new QListWidgetItem(icon_video, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .avi, .mpg, .mpeg, .mov, .mp4, .wmv, .mkv, .ogv
+              item = new QListWidgetItem(icon_video, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .avi, .mpg, .mpeg, .mov, .m4v, .mp4, .wmv, .mkv, .ogv
+          }
+          else if ( 0 == QString::compare(el.extension, "rm")  ||
+                    0 == QString::compare(el.extension, "ra")  ||
+                    0 == QString::compare(el.extension, "ram")  ) {
+              item = new QListWidgetItem(icon_real, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .ram, 'rm, 'ra
           }
           else if ( 0 == QString::compare(el.extension, "flv")  ) {
               item = new QListWidgetItem(icon_flash, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .flv
           }
+          //else if ( 0 == QString::compare(el.extension, "")  ) {
+          //    item = new QListWidgetItem(icon_unknown, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id); // .flv
+          //}
           else {
               if (el.name.contains("Flash")) {
                   item = new QListWidgetItem(icon_flash, el.name, ui->listWidget, LIST_ITEM_TYPE_OFFSET + el.id);
@@ -315,3 +416,8 @@ void PlayListMainWindow::updateList() {
     }
   }
 }
+void PlayListMainWindow::updateUiWithCurrentStatus(VlcStatus * status) {
+    ui->loopButton->setChecked(status->loop);
+    ui->repeatButton->setChecked(status->repeat);
+    ui->shuffleButton->setChecked(status->random);
+}