Add static currentIP() in AccountDialog and change all
[vlc-remote] / browsemainwindow.cpp
index 7bb6cbb..f4bcef5 100644 (file)
@@ -21,7 +21,7 @@
 #include "configdialog.h"
 #include "aboutdialog.h"
 #include "vlcbrowseelement.h"
-
+#include "accountdialog.h"
 
 BrowseMainWindow::BrowseMainWindow(QWidget *parent) :
         QMainWindow(parent),
@@ -32,13 +32,13 @@ BrowseMainWindow::BrowseMainWindow(QWidget *parent) :
     mCurrentDir = "~/"; // This works on win as well as linux, would guess mac too.
     setWindowTitle("Vlc remote");
 
-    QSettings settings;
-    mIp = settings.value("ip").toString();
 
     mNetManager = new QNetworkAccessManager(this);
 
     mContents = new QList<VlcBrowseElement>();
 
+    //mResponse = new QByteArray();
+
     ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
     ui->addButton->setIcon(QIcon::fromTheme("general_add"));
     ui->browseButton->setIcon(QIcon::fromTheme("filemanager_media_folder"));
@@ -51,7 +51,14 @@ 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();
+    browseDirectory(mCurrentDir);
 }
 
 BrowseMainWindow::~BrowseMainWindow()
@@ -99,13 +106,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);
-            }
+    for (int idx = 0; idx < mContents->count(); ++idx) {
+        if (0 == QString::compare(text, mContents->at(idx).name)) {
+            return mContents->at(idx);
         }
-    //}
+    }
     return *(new VlcBrowseElement());
 }
 
@@ -122,27 +127,38 @@ void BrowseMainWindow::onBrowse() {
 
 void BrowseMainWindow::onAddToPlaylist() {
     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_enqueue&input=" + mCurrentElement.path)));
-}
+                             }
 
 void BrowseMainWindow::onPlay() {
     /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_play&input=" + mCurrentElement.path)));
-}
+                             }
 
 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()));
+    //reply->setReadBufferSize(1024 * 500);
+    connect(reply,SIGNAL(readyRead()),this,SLOT(readReady()));
+    connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
 }
-void BrowseMainWindow::parseXmlDirectory() {
-    qDebug() << "got called!";
+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->clear();
-    //mContents = new QList<VlcBrowseElement>();
-    // we can sort by filders then files alphabetically by running to lists and appending them at the end
+    // 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()) {
@@ -170,8 +186,7 @@ void BrowseMainWindow::parseXmlDirectory() {
         }
     }
     delete files;
-    //reply->deleteLater();
-    delete reply;
+    mResponse.clear();
 
     // Update UI
     this->updateList();
@@ -179,11 +194,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() {
@@ -256,7 +271,6 @@ void BrowseMainWindow::updateList() {
                 ui->listWidget->addItem(item);
             }
             // other types ignored
-            //if (item) delete item;
         }
     }
 }