Browser working correctly from a functional perspective.
authordruid23 <usr@dru-id.co.uk>
Mon, 16 Aug 2010 23:42:09 +0000 (00:42 +0100)
committerdruid23 <usr@dru-id.co.uk>
Mon, 16 Aug 2010 23:42:09 +0000 (00:42 +0100)
modified:   browsemainwindow.cpp
modified:   browsemainwindow.h

browsemainwindow.cpp
browsemainwindow.h

index 7bb6cbb..599b632 100644 (file)
@@ -39,6 +39,8 @@ BrowseMainWindow::BrowseMainWindow(QWidget *parent) :
 
     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"));
@@ -99,13 +101,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());
 }
 
@@ -129,20 +129,31 @@ 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()));
+    //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 +181,7 @@ void BrowseMainWindow::parseXmlDirectory() {
         }
     }
     delete files;
-    //reply->deleteLater();
-    delete reply;
+    mResponse.clear();
 
     // Update UI
     this->updateList();
@@ -256,7 +266,6 @@ void BrowseMainWindow::updateList() {
                 ui->listWidget->addItem(item);
             }
             // other types ignored
-            //if (item) delete item;
         }
     }
 }
index 5f91467..d287019 100644 (file)
@@ -40,6 +40,8 @@ public slots:
     void onPlay();
     void onAddToPlaylist();
     void onListSelectionChanged();
+    void finished(QNetworkReply * reply);
+    void readReady();
 
 protected slots:
     void parseXmlDirectory();
@@ -57,6 +59,7 @@ private:
     QString mIp;
     QList<VlcBrowseElement>* mContents;
     VlcBrowseElement mCurrentElement;
+    QByteArray mResponse;
 };
 
 #endif // BROWSEMAINWINDOW_H