Update webpage content.
[vlc-remote] / browsemainwindow.cpp
index 7bb6cbb..b410e31 100644 (file)
@@ -1,27 +1,27 @@
 /*   VLC-REMOTE for MAEMO 5
- *   Copyright (C) 2010 Schutz Sacha <istdasklar@gmail.com>
- *   This program is free software; you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License version 2,
- *   or (at your option) any later version, as published by the Free
- *   Software Foundation
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details
- *
- *   You should have received a copy of the GNU General Public
- *   License along with this program; if not, write to the
- *   Free Software Foundation, Inc.,
- *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
+*   Copyright (C) 2010 Schutz Sacha <istdasklar@gmail.com>, Dru Moore <usr@dru-id.co.uk>, Yann Nave <yannux@onbebop.net>
+*   This program is free software; you can redistribute it and/or modify
+*   it under the terms of the GNU General Public License version 2,
+*   or (at your option) any later version, as published by the Free
+*   Software Foundation
+*
+*   This program is distributed in the hope that it will be useful,
+*   but WITHOUT ANY WARRANTY; without even the implied warranty of
+*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*   GNU General Public License for more details
+*
+*   You should have received a copy of the GNU General Public
+*   License along with this program; if not, write to the
+*   Free Software Foundation, Inc.,
+*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
 #include "browsemainwindow.h"
 #include "ui_browsemainwindow.h"
 #include <QSettings>
 #include "configdialog.h"
 #include "aboutdialog.h"
 #include "vlcbrowseelement.h"
-
+#include "accountdialog.h"
 
 BrowseMainWindow::BrowseMainWindow(QWidget *parent) :
         QMainWindow(parent),
@@ -32,13 +32,14 @@ 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>();
 
+    ui->listWidget->setTextElideMode(Qt::ElideMiddle);
+    ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+
     ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
     ui->addButton->setIcon(QIcon::fromTheme("general_add"));
     ui->browseButton->setIcon(QIcon::fromTheme("filemanager_media_folder"));
@@ -51,7 +52,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()
@@ -80,7 +91,8 @@ void BrowseMainWindow::onListSelectionChanged() {
             ui->browseButton->setDisabled(true);
             ui->playButton->setDisabled(true);
             ui->addButton->setDisabled(true);
-            browseDirectory(mCurrentElement.path);
+            mCurrentDir = mCurrentElement.path;
+            browseDirectory(mCurrentDir);
         }
         else {
             // can we browse?
@@ -99,13 +111,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());
 }
 
@@ -113,7 +123,8 @@ void BrowseMainWindow::onBrowse() {
     // check for directory
     if (0 == QString::compare("directory", mCurrentElement.type)) {
         // call browseDirectory
-        this->browseDirectory(mCurrentElement.path);
+        mCurrentDir = mCurrentElement.path;
+        browseDirectory(mCurrentDir);
     }
     else {
         ui->browseButton->setDisabled(true);
@@ -121,28 +132,47 @@ void BrowseMainWindow::onBrowse() {
 }
 
 void BrowseMainWindow::onAddToPlaylist() {
-    /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_enqueue&input=" + mCurrentElement.path)));
+    QUrl url = QUrl("http://"+mIp+"/requests/status.xml?command=in_enqueue");
+    url.addEncodedQueryItem(QByteArray("input"), QUrl::toPercentEncoding(mCurrentElement.path.replace("\\", "\\\\")));
+    mNetManager->get(QNetworkRequest(url));
+    //mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_enqueue&input=" + mCurrentElement.path.replace("\\", "\\\\"))));
 }
 
 void BrowseMainWindow::onPlay() {
-    /*QNetworkReply * reply = */ mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_play&input=" + mCurrentElement.path)));
+    QUrl url = QUrl("http://"+mIp+"/requests/status.xml?command=in_play");
+    url.addEncodedQueryItem(QByteArray("input"), QUrl::toPercentEncoding(mCurrentElement.path.replace("\\", "\\\\")));
+    mNetManager->get(QNetworkRequest(url));
+    //mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/status.xml?command=in_play&input=" + mCurrentElement.path.replace("\\", "\\\\"))));
 }
 
 void BrowseMainWindow::browseDirectory(QString dir) {
+    mContents->clear();
     ui->listWidget->clear();
-    QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/browse.xml?dir=" + dir)));
-    connect(reply,SIGNAL(readyRead()),this,SLOT(parseXmlDirectory()));
+    mResponse.clear();
+    QUrl url = QUrl("http://"+mIp+"/requests/browse.xml");
+    url.addEncodedQueryItem(QByteArray("dir"), QUrl::toPercentEncoding(dir));
+    QNetworkReply * reply = mNetManager->get(QNetworkRequest(url));
+    //QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/browse.xml?dir=" + dir.replace("&", "%26").replace("\\", "\\\\"))));
+    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()) {
@@ -155,11 +185,14 @@ void BrowseMainWindow::parseXmlDirectory() {
             dir->date = QDate::fromString(node.attributes().namedItem("date").nodeValue());
             dir->path = node.attributes().namedItem("path").nodeValue();
             dir->name = node.attributes().namedItem("name").nodeValue();
-            dir->extension = node.attributes().namedItem("extension").nodeValue();
+            dir->extension = getExtension(dir->path, node.attributes().namedItem("extension").nodeValue());
             ++idx;
             if (0 != QString::compare("directory", dir->type)) {
                 files->append(*dir);
             }
+            else if (0 == QString::compare("..", dir->name)) {
+                this->mContents->prepend(*dir);
+            }
             else {
                 this->mContents->append(*dir);
             }
@@ -170,20 +203,34 @@ void BrowseMainWindow::parseXmlDirectory() {
         }
     }
     delete files;
-    //reply->deleteLater();
-    delete reply;
+    mResponse.clear();
 
     // Update UI
     this->updateList();
 }
 
+QString BrowseMainWindow::getExtension(QString path, QString extension) {
+    // return extension if exists
+    if (!extension.isNull() && !extension.isEmpty()) return extension;
+    // return blank if no path
+    if (path.isNull() || path.isEmpty()) return "";
+    // otherwise extract the extension
+    int dot_pos = path.lastIndexOf('.');
+    if (0 < dot_pos) {
+        return path.right(path.length() - (dot_pos + 1));
+    }
+    else { // no dot
+        return "";
+    }
+}
+
 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 +303,6 @@ void BrowseMainWindow::updateList() {
                 ui->listWidget->addItem(item);
             }
             // other types ignored
-            //if (item) delete item;
         }
     }
 }