Added error handling for unbrowsable directories
authordruid23 <usr@dru-id.co.uk>
Sat, 21 Aug 2010 16:44:28 +0000 (17:44 +0100)
committerdruid23 <usr@dru-id.co.uk>
Sat, 21 Aug 2010 16:44:28 +0000 (17:44 +0100)
modified:   browsemainwindow.cpp
modified:   browsemainwindow.h

browsemainwindow.cpp
browsemainwindow.h

index d429502..d4c034f 100644 (file)
@@ -18,6 +18,9 @@
 #include "browsemainwindow.h"
 #include "ui_browsemainwindow.h"
 #include <QSettings>
+#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+#include <QMaemo5InformationBox>
+#endif
 #include "configdialog.h"
 #include "aboutdialog.h"
 #include "vlcbrowseelement.h"
@@ -170,16 +173,18 @@ void BrowseMainWindow::onPlay() {
 }
 
 void BrowseMainWindow::browseDirectory(QString dir) {
-    mContents->clear();
-    ui->listWidget->clear();
     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(reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(error(QNetworkReply::NetworkError)));
     connect(mNetManager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finished(QNetworkReply *)));
 }
+void BrowseMainWindow::error(QNetworkReply::NetworkError code) {
+    qDebug() << code;
+}
 void BrowseMainWindow::readReady() {
     QNetworkReply * reply = qobject_cast<QNetworkReply*>(sender());
     // append to buffer
@@ -198,9 +203,10 @@ void BrowseMainWindow::parseXmlDirectory() {
     QDomNodeList elements = docElem.elementsByTagName("element");
     // 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()) {
+        QList<VlcBrowseElement>* files = new QList<VlcBrowseElement>();
         int idx = 0;
+        mContents->clear();
         do {
             QDomNode node = elements.at(idx);
             VlcBrowseElement* dir = new VlcBrowseElement();
@@ -225,12 +231,19 @@ void BrowseMainWindow::parseXmlDirectory() {
         if (0 < files->count()) {
             mContents->append(*files);
         }
+        delete files;
+        // Update UI
+        this->updateList();
+    }
+    else {
+        // alert user of error / empty directory
+        qDebug() << "can't browse dir: " << mCurrentDir;
+#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5)
+        QMaemo5InformationBox::information(this, tr("Directory could not be browsed!"), QMaemo5InformationBox::DefaultTimeout);
+#endif
     }
-    delete files;
     mResponse.clear();
 
-    // Update UI
-    this->updateList();
 }
 
 QString BrowseMainWindow::getExtension(QString path, QString extension) {
@@ -258,6 +271,7 @@ void BrowseMainWindow::writeFile(QString path, QByteArray text) {
 }
 
 void BrowseMainWindow::updateList() {
+    ui->listWidget->clear();
     int ct = this->mContents->count();
     if (0 < ct) {
         QIcon icon_up     = QIcon::fromTheme("filemanager_folder_up");
index dc802da..86cd057 100644 (file)
@@ -43,6 +43,7 @@ public slots:
     void onListSelectionChanged();
     void finished(QNetworkReply * reply);
     void readReady();
+    void error(QNetworkReply::NetworkError code);
     void showCurrentDirectory();
     void setHomeFolder();
     void showHomeFolder();