From 6c15601a22b0d095c47e613a0e64f0f6e9c463e0 Mon Sep 17 00:00:00 2001 From: Flavio Tordini Date: Thu, 13 Aug 2009 19:18:47 +0200 Subject: [PATCH] Better error reporting --- src/ListModel.cpp | 20 +++++++++++++++++++- src/ListModel.h | 2 ++ src/loadingwidget.cpp | 6 ++++++ src/loadingwidget.h | 1 + src/networkaccess.cpp | 15 ++++++++++++--- src/networkaccess.h | 2 ++ src/videoareawidget.cpp | 5 +++++ src/videoareawidget.h | 1 + src/youtubesearch.cpp | 13 +++++++++---- src/youtubesearch.h | 2 ++ 10 files changed, 59 insertions(+), 8 deletions(-) diff --git a/src/ListModel.cpp b/src/ListModel.cpp index 61cdc4e..69aee7b 100755 --- a/src/ListModel.cpp +++ b/src/ListModel.cpp @@ -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())); diff --git a/src/ListModel.h b/src/ListModel.h index 1d6228b..c4c078f 100755 --- a/src/ListModel.h +++ b/src/ListModel.h @@ -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 diff --git a/src/loadingwidget.cpp b/src/loadingwidget.cpp index d9596dc..ef565b3 100644 --- a/src/loadingwidget.cpp +++ b/src/loadingwidget.cpp @@ -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; diff --git a/src/loadingwidget.h b/src/loadingwidget.h index 3117bad..77a3b1d 100644 --- a/src/loadingwidget.h +++ b/src/loadingwidget.h @@ -11,6 +11,7 @@ class LoadingWidget : public QWidget { public: LoadingWidget(QWidget *parent); void setVideo(Video *video); + void setError(QString message); void clear(); public slots: diff --git a/src/networkaccess.cpp b/src/networkaccess.cpp index b48d14a..921fa79 100644 --- a/src/networkaccess.cpp +++ b/src/networkaccess.cpp @@ -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(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(); } diff --git a/src/networkaccess.h b/src/networkaccess.h index 42bfa11..385b3ca 100644 --- a/src/networkaccess.h +++ b/src/networkaccess.h @@ -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: diff --git a/src/videoareawidget.cpp b/src/videoareawidget.cpp index ca38f67..9300b9e 100644 --- a/src/videoareawidget.cpp +++ b/src/videoareawidget.cpp @@ -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); diff --git a/src/videoareawidget.h b/src/videoareawidget.h index 2d6e763..5795374 100644 --- a/src/videoareawidget.h +++ b/src/videoareawidget.h @@ -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; } diff --git a/src/youtubesearch.cpp b/src/youtubesearch.cpp index b1ae0fe..a182099 100644 --- a/src/youtubesearch.cpp +++ b/src/youtubesearch.cpp @@ -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) { diff --git a/src/youtubesearch.h b/src/youtubesearch.h index 77ed7d1..dc2739d 100644 --- a/src/youtubesearch.h +++ b/src/youtubesearch.h @@ -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: -- 2.39.5