Tweaks to how status updates are handled in prepartion for UI changes.
[vlc-remote] / browsemainwindow.cpp
index f4bcef5..b410e31 100644 (file)
@@ -1,20 +1,20 @@
 /*   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>
@@ -37,7 +37,8 @@ BrowseMainWindow::BrowseMainWindow(QWidget *parent) :
 
     mContents = new QList<VlcBrowseElement>();
 
-    //mResponse = new QByteArray();
+    ui->listWidget->setTextElideMode(Qt::ElideMiddle);
+    ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
     ui->playButton->setIcon(QIcon::fromTheme("camera_playback"));
     ui->addButton->setIcon(QIcon::fromTheme("general_add"));
@@ -58,6 +59,9 @@ BrowseMainWindow::BrowseMainWindow(QWidget *parent) :
 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);
 }
 
@@ -87,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?
@@ -118,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);
@@ -126,19 +132,27 @@ 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();
     mResponse.clear();
-    QNetworkReply * reply =  mNetManager->get(QNetworkRequest(QUrl("http://"+mIp+"/requests/browse.xml?dir=" + dir)));
-    //reply->setReadBufferSize(1024 * 500);
+    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 *)));
 }
@@ -171,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);
             }
@@ -192,6 +209,21 @@ void BrowseMainWindow::parseXmlDirectory() {
     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))