]> git.sur5r.net Git - minitube/commitdiff
Better error reporting
authorFlavio Tordini <flavio.tordini@gmail.com>
Thu, 13 Aug 2009 17:18:47 +0000 (19:18 +0200)
committerFlavio Tordini <flavio.tordini@gmail.com>
Thu, 13 Aug 2009 17:18:47 +0000 (19:18 +0200)
src/ListModel.cpp
src/ListModel.h
src/loadingwidget.cpp
src/loadingwidget.h
src/networkaccess.cpp
src/networkaccess.h
src/videoareawidget.cpp
src/videoareawidget.h
src/youtubesearch.cpp
src/youtubesearch.h

index 61cdc4ee9d35b0bd77dc2176f41eac21c4a6eb67..69aee7ba33fbbaead4b19bc5d599fce120469160 100755 (executable)
@@ -42,6 +42,7 @@ QVariant ListModel::data(const QModelIndex &index, int role) const {
             return ItemTypeShowMore;
         case Qt::DisplayRole:
         case Qt::StatusTipRole:
+            if (!errorMessage.isEmpty()) return errorMessage;
             if (searching) return tr("Searching...");
             if (canSearchMore) return tr("Show %1 More").arg(MAX_ITEMS);
             if (videos.isEmpty()) return tr("No videos");
@@ -49,7 +50,15 @@ QVariant ListModel::data(const QModelIndex &index, int role) const {
         case Qt::TextAlignmentRole:
             return QVariant(int(Qt::AlignHCenter | Qt::AlignVCenter));
         case Qt::ForegroundRole:
-            return palette.color(QPalette::Dark);
+            if (!errorMessage.isEmpty())
+                return palette.color(QPalette::ToolTipText);
+            else
+                return palette.color(QPalette::Dark);
+        case Qt::BackgroundColorRole:
+            if (!errorMessage.isEmpty())
+                return palette.color(QPalette::ToolTipBase);
+            else
+                return QVariant();
         case Qt::FontRole:
             return boldFont;
         default:
@@ -145,6 +154,7 @@ void ListModel::search(SearchParams *searchParams) {
     m_activeVideo = 0;
     m_activeRow = -1;
     skip = 1;
+    errorMessage.clear();
     reset();
 
     // (re)initialize the YouTubeSearch
@@ -152,6 +162,7 @@ void ListModel::search(SearchParams *searchParams) {
     youtubeSearch = new YouTubeSearch();
     connect(youtubeSearch, SIGNAL(gotVideo(Video*)), this, SLOT(addVideo(Video*)));
     connect(youtubeSearch, SIGNAL(finished(int)), this, SLOT(searchFinished(int)));
+    connect(youtubeSearch, SIGNAL(error(QString)), this, SLOT(searchError(QString)));
 
     this->searchParams = searchParams;
     searching = true;
@@ -162,6 +173,7 @@ void ListModel::search(SearchParams *searchParams) {
 void ListModel::searchMore(int max) {
     if (searching) return;
     searching = true;
+    errorMessage.clear();
     youtubeSearch->search(searchParams, max, skip);
     skip += max;
 }
@@ -193,6 +205,12 @@ void ListModel::searchFinished(int total) {
     emit dataChanged( createIndex( MAX_ITEMS, 0 ), createIndex( MAX_ITEMS, columnCount() - 1 ) );
 }
 
+void ListModel::searchError(QString message) {
+    errorMessage = message;
+    // update the message item
+    emit dataChanged( createIndex( MAX_ITEMS, 0 ), createIndex( MAX_ITEMS, columnCount() - 1 ) );
+}
+
 void ListModel::addVideo(Video* video) {
     
     connect(video, SIGNAL(gotThumbnail()), this, SLOT(updateThumbnail()));
index 1d6228be42725bc829df27d6e20a4eedaecbfbce..c4c078f41451afadc1d862c9a1c0a843426e59ea 100755 (executable)
@@ -63,6 +63,7 @@ public slots:
     void searchNeeded();
     void addVideo(Video* video);
     void searchFinished(int total);
+    void searchError(QString message);
     void updateThumbnail();
 
 signals:
@@ -84,6 +85,7 @@ private:
     int m_activeRow;
     Video *m_activeVideo;
 
+    QString errorMessage;
 };
 
 #endif
index d9596dc34789ce9d99146d568b43f8c90eb279b5..ef565b391223982b7d7198531bfafc79c293b3a5 100644 (file)
@@ -60,6 +60,12 @@ void LoadingWidget::setVideo(Video *video) {
     // progressBar->hide();
 }
 
+void LoadingWidget::setError(QString message) {
+    titleLabel->setText(tr("Error"));
+    descriptionLabel->setText(message);
+    // progressBar->hide();
+}
+
 void LoadingWidget::bufferStatus(int percent) {
     /*
     qDebug() << percent;
index 3117bad578ee7ddd46aa722aa83519184f5f34e8..77a3b1dfce1bea6c507572c603863014f97c3376 100644 (file)
@@ -11,6 +11,7 @@ class LoadingWidget : public QWidget {
 public:
     LoadingWidget(QWidget *parent);
     void setVideo(Video *video);
+    void setError(QString message);
     void clear();
 
 public slots:
index b48d14a5374a1da225f1c8941a968899907927f7..921fa79f378ceb2f0198817bcbc3f6a923dab52f 100644 (file)
@@ -40,7 +40,6 @@ void NetworkReply::finished() {
     // get the HTTP response body
     QByteArray bytes = networkReply->readAll();
 
-
     emit data(bytes);
 
     // bye bye my reply
@@ -48,6 +47,10 @@ void NetworkReply::finished() {
     networkReply->deleteLater();
 }
 
+void NetworkReply::requestError(QNetworkReply::NetworkError code) {
+    emit error(networkReply);
+}
+
 /* --- NetworkAccess --- */
 
 NetworkAccess::NetworkAccess( QObject* parent) : QObject( parent ) {}
@@ -79,6 +82,10 @@ NetworkReply* NetworkAccess::get(const QUrl url) {
     connect(networkReply, SIGNAL(metaDataChanged()),
             reply, SLOT(metaDataChanged()), Qt::QueuedConnection);
 
+    // error signal
+    connect(networkReply, SIGNAL(error(QNetworkReply::NetworkError)),
+            reply, SLOT(requestError(QNetworkReply::NetworkError)));
+
     // when the request is finished we'll invoke the target method
     connect(networkReply, SIGNAL(finished()), reply, SLOT(finished()), Qt::QueuedConnection);
 
@@ -146,9 +153,11 @@ void NetworkAccess::error(QNetworkReply::NetworkError code) {
 
     // report the error in the status bar
     QMainWindow* mainWindow = dynamic_cast<QMainWindow*>(qApp->topLevelWidgets().first());
-    if (mainWindow) mainWindow->statusBar()->showMessage(networkReply->errorString());
+    if (mainWindow) mainWindow->statusBar()->showMessage(
+            tr("Network error: %1").arg(networkReply->errorString()));
+
+    qDebug() << "Network error:" << networkReply->errorString() << code;
 
-    qDebug() << "Network error" << networkReply->errorString() << code;
     networkReply->deleteLater();
 }
 
index 42bfa117ac9f6402534843f98ac2aa432c78cd00..385b3cac4f00a8404a4f445d964f0c2bd61b565d 100644 (file)
@@ -17,9 +17,11 @@ public:
 public slots:
     void finished();
     void metaDataChanged();
+    void requestError(QNetworkReply::NetworkError);
 
 signals:
     void data(QByteArray);
+    void error(QNetworkReply*);
     void finished(QNetworkReply*);
 
 private:
index ca38f67ed5da67ba000e543bd9a3abeaac9fb948..9300b9ea4af7eb6b98647e4c544757c539749b0c 100644 (file)
@@ -21,6 +21,11 @@ void VideoAreaWidget::showVideo() {
     stackedLayout->setCurrentWidget(videoWidget);
 }
 
+void VideoAreaWidget::showError(QString message) {
+    loadingWidget->setError(message);
+    stackedLayout->setCurrentWidget(loadingWidget);
+}
+
 void VideoAreaWidget::showLoading(Video *video) {
     this->loadingWidget->setVideo(video);
     stackedLayout->setCurrentWidget(loadingWidget);
index 2d6e7639950147077ee8fa4d853ce2b75626b734..5795374a6f4682360e44456a3640fa2112653626 100644 (file)
@@ -16,6 +16,7 @@ public:
     void setLoadingWidget(LoadingWidget *loadingWidget);
     void showLoading(Video* video);
     void showVideo();
+    void showError(QString message);
     void setListModel(ListModel *listModel) {
         this->listModel = listModel;
     }
index b1ae0fe088c390a3e6b36f8318bf8b32757db58b..a1820993d7c4a7f4b731bc1dc44b7f37eba7cf7c 100644 (file)
@@ -16,6 +16,10 @@ void YouTubeSearch::search(SearchParams *searchParams, int max, int skip) {
             "http://gdata.youtube.com/feeds/api/videos?q=%1&max-results=%2&start-index=%3")
             .arg(searchParams->keywords(), QString::number(max), QString::number(skip));
 
+    // Useful to test with a local webserver
+    urlString = QString("http://localhost/oringo/video.xml?q=%1&max-results=%2&start-index=%3")
+                .arg(searchParams->keywords(), QString::number(max), QString::number(skip));
+
     switch (searchParams->sortBy()) {
     case SearchParams::SortByNewest:
         urlString.append("&orderby=published");
@@ -25,15 +29,16 @@ void YouTubeSearch::search(SearchParams *searchParams, int max, int skip) {
         break;
     }
 
-    // QString urlString = QString("http://localhost/oringo/video.xml?q=%1&max-results=%2&start-index=%3")
-    // .arg(query, QString::number(max), QString::number(skip));
-
     QUrl url(urlString);
 
     QObject *reply = The::http()->get(url);
-    // connect(reply, SIGNAL(ready(QNetworkReply*)), SLOT(searchReady(QNetworkReply*)));
     connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
+    connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(error(QNetworkReply*)));
+
+}
 
+void YouTubeSearch::error(QNetworkReply *reply) {
+    emit error(reply->errorString());
 }
 
 void YouTubeSearch::parseResults(QByteArray data) {
index 77ed7d185d2f48d4d8a3ef9e628b0388c50a00c5..dc2739dfa5dcdd3b92e5c75d7740f2b27335a2fc 100644 (file)
@@ -17,9 +17,11 @@ public:
 signals:
     void gotVideo(Video*);
     void finished(int total);
+    void error(QString message);
 
 private slots:
     void parseResults(QByteArray data);
+    void error(QNetworkReply *reply);
 
 private: