Update async methods! Now valid ip is detected
[vlc-remote] / browsemainwindow.cpp
index c561533..90f09a7 100644 (file)
 #include "browsemainwindow.h"
 #include "ui_browsemainwindow.h"
 #include <QSettings>
-#include <QDebug>
 #include "configdialog.h"
 #include "aboutdialog.h"
 #include "vlcbrowseelement.h"
-
+#include "accountdialog.h"
 
 BrowseMainWindow::BrowseMainWindow(QWidget *parent) :
         QMainWindow(parent),
@@ -33,13 +32,12 @@ BrowseMainWindow::BrowseMainWindow(QWidget *parent) :
     mCurrentDir = "~/"; // This works on win as well as linux, would guess mac too.
     setWindowTitle("Vlc remote");
 
-    QSettings settings;
 
-    QString currentKey = settings.value("config/currentKey").toString();
-    mIp = settings.value("account/"+currentKey).toString();
+    mNetManager = new QNetworkAccessManager(this);
 
+    mContents = new QList<VlcBrowseElement>();
 
-    mNetManager = new QNetworkAccessManager(this);
+    //mResponse = new QByteArray();
 
     ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
     ui->addButton->setIcon(QIcon::fromTheme("general_add"));
@@ -53,7 +51,17 @@ BrowseMainWindow::BrowseMainWindow(QWidget *parent) :
     connect(ui->playButton,SIGNAL(clicked()),this,SLOT(onPlay()));
     connect(ui->listWidget, SIGNAL(itemSelectionChanged()), this, SLOT(onListSelectionChanged()));
 
-    this->browseDirectory(mCurrentDir);
+    init();
+
+
+}
+void BrowseMainWindow::init()  // THIS METHOD IS CALLED WHEN CONFIG CHANGED...
+{
+    mIp = AccountDialog::currentIp();
+}
+void BrowseMainWindow::showCurrentDirectory()  // THIS METHOD IS CALLED WHEN WINDOW IS OPENED...
+{
+    browseDirectory(mCurrentDir);
 }
 
 BrowseMainWindow::~BrowseMainWindow()
@@ -101,13 +109,11 @@ void BrowseMainWindow::onListSelectionChanged() {
 }
 
 VlcBrowseElement BrowseMainWindow::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);
         }
     }
-    //}
     return *(new VlcBrowseElement());
 }
 
@@ -117,6 +123,9 @@ void BrowseMainWindow::onBrowse() {
         // call browseDirectory
         this->browseDirectory(mCurrentElement.path);
     }
+    else {
+        ui->browseButton->setDisabled(true);
+    }
 }
 
 void BrowseMainWindow::onAddToPlaylist() {
@@ -128,17 +137,32 @@ void BrowseMainWindow::onPlay() {
                              }
 
 void BrowseMainWindow::browseDirectory(QString dir) {
+    mContents->clear();
     ui->listWidget->clear();
+    mResponse.clear();
     QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/browse.xml?dir=" + dir)));
-    connect(reply,SIGNAL(readyRead()),this,SLOT(parseXmlDirectory()));
+    connect(reply,SIGNAL(readyRead()),this,SLOT(readReady()));
+    connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
 }
-void BrowseMainWindow::parseXmlDirectory() {
+void BrowseMainWindow::readReady() {
     QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
+    // append to buffer
+    mResponse += reply->readAll();
+}
+void BrowseMainWindow::finished(QNetworkReply * reply) {
+    // now we can call parseXmlDirectory to process the full buffers
+    this->parseXmlDirectory();
+    // only interested in finished signals
+    disconnect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
+}
+void BrowseMainWindow::parseXmlDirectory() {
     QDomDocument doc;
-    doc.setContent(reply->readAll());
+    doc.setContent(this->mResponse);
     QDomElement docElem = doc.documentElement();
     QDomNodeList elements = docElem.elementsByTagName("element");
-    mContents = new QList<VlcBrowseElement>();
+    // we can sort by folders then files alphabetically by running to lists and appending them at the end
+    // vlc alpha sorts everything in the incoming stream, we just need to seperate files from folders.
+    QList<VlcBrowseElement>* files = new QList<VlcBrowseElement>();
     if (0 < elements.count()) {
         int idx = 0;
         do {
@@ -151,10 +175,20 @@ void BrowseMainWindow::parseXmlDirectory() {
             dir->name = node.attributes().namedItem("name").nodeValue();
             dir->extension = node.attributes().namedItem("extension").nodeValue();
             ++idx;
-            this->mContents->append(*dir);
+            if (0 != QString::compare("directory", dir->type)) {
+                files->append(*dir);
+            }
+            else {
+                this->mContents->append(*dir);
+            }
+            delete dir;
         } while (idx < elements.count());
+        if (0 < files->count()) {
+            mContents->append(*files);
+        }
     }
-    delete reply;
+    delete files;
+    mResponse.clear();
 
     // Update UI
     this->updateList();
@@ -162,11 +196,11 @@ void BrowseMainWindow::parseXmlDirectory() {
 
 void BrowseMainWindow::writeFile(QString path, QByteArray text) {
     QFile file(path);
-         if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
-             return;
+    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
+        return;
 
-         QTextStream out(&file);
-         out << text;
+    QTextStream out(&file);
+    out << text;
 }
 
 void BrowseMainWindow::updateList() {
@@ -239,7 +273,6 @@ void BrowseMainWindow::updateList() {
                 ui->listWidget->addItem(item);
             }
             // other types ignored
-            //if (item) delete item;
         }
     }
 }