From ff0abb53065dba3bbeff223f9499fb22d7a27c37 Mon Sep 17 00:00:00 2001 From: shibakaneki Date: Mon, 7 Nov 2011 10:58:25 +0100 Subject: [PATCH] Added the download manager for the modal files --- src/board/UBBoardController.cpp | 12 ++++---- src/board/UBLibraryController.cpp | 10 +++---- src/core/UBDownloadManager.cpp | 50 +++++++++++++++++++++++++++++-- src/core/UBDownloadManager.h | 4 +++ src/gui/UBDownloadWidget.cpp | 4 ++- src/network/UBHttpGet.cpp | 5 ++-- src/network/UBHttpGet.h | 2 +- 7 files changed, 69 insertions(+), 18 deletions(-) diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index 7db6d551..429f129b 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -767,13 +767,13 @@ void UBBoardController::downloadURL(const QUrl& url, const QPointF& pPos, const desc.isBackground = isBackground; // INFO: DO NOT UNCOMMENT THE NEXT LINE! DEVELOPMENT IN PROGRESS -// UBDownloadManager::downloadManager()->addFileToDownload(desc); + UBDownloadManager::downloadManager()->addFileToDownload(desc); - UBHttpGet *http = new UBHttpGet(mActiveScene); - showMessage(tr("Downloading content from %1").arg(url.toString()), true); - connect(http, SIGNAL(downloadFinished(bool, QUrl, QString, QByteArray, QPointF, QSize, bool)), - this, SLOT(downloadFinished(bool, QUrl, QString, QByteArray, QPointF, QSize, bool))); - http->get(url, pPos, pSize, isBackground); +// UBHttpGet *http = new UBHttpGet(mActiveScene); +// showMessage(tr("Downloading content from %1").arg(url.toString()), true); +// connect(http, SIGNAL(downloadFinished(bool, QUrl, QString, QByteArray, QPointF, QSize, bool)), +// this, SLOT(downloadFinished(bool, QUrl, QString, QByteArray, QPointF, QSize, bool))); +// http->get(url, pPos, pSize, isBackground); } } diff --git a/src/board/UBLibraryController.cpp b/src/board/UBLibraryController.cpp index edd4bc2e..d6663ded 100644 --- a/src/board/UBLibraryController.cpp +++ b/src/board/UBLibraryController.cpp @@ -239,11 +239,11 @@ QList UBLibraryController::rootCategoriesList() categories << element; // Note : FEATURE IN DEVELOPMENT, DO NOT ERASE (or you will get problems) !!!! -// mSearchCategoryPath = QUrl::fromLocalFile(UBSettings::settings()->uniboardSearchDirectory()); -// element = new UBLibElement(eUBLibElementType_Folder, mSearchCategoryPath, tr("Web Search", "Web search category element")); -// element->setThumbnail(QImage(":images/libpalette/WebSearchCategory.svg")); -// element->setMoveable(false); -// categories << element; + mSearchCategoryPath = QUrl::fromLocalFile(UBSettings::settings()->uniboardSearchDirectory()); + element = new UBLibElement(eUBLibElementType_Folder, mSearchCategoryPath, tr("Web Search", "Web search category element")); + element->setThumbnail(QImage(":images/libpalette/WebSearchCategory.svg")); + element->setMoveable(false); + categories << element; element = new UBLibElement(eUBLibElementType_Folder, mAnimationUserDirectoryPath, tr("Animations", "Animations category element")); element->setThumbnail(QImage(":images/libpalette/FlashCategory.svg")); diff --git a/src/core/UBDownloadManager.cpp b/src/core/UBDownloadManager.cpp index 9c581eed..dc0595d1 100644 --- a/src/core/UBDownloadManager.cpp +++ b/src/core/UBDownloadManager.cpp @@ -84,7 +84,9 @@ void UBDownloadManager::init() { mCrntDL.clear(); mPendingDL.clear(); + mReplies.clear(); mLastID = 1; + mDLAvailability.clear(); for(int i=0; iget(QUrl(desc.url)); + + // We send here the request and store its reply in order to be able to cancel it if needed + mReplies[desc.id] = http->get(QUrl(desc.url)); } /** @@ -295,7 +302,7 @@ void UBDownloadManager::checkIfModalRemains() } } - if(bModal) + if(bModal || (mCrntDL.empty() && mPendingDL.empty())) { // Close the modal window UBApplication::mainWindow->hideDownloadWidget(); @@ -311,12 +318,40 @@ void UBDownloadManager::checkIfModalRemains() void UBDownloadManager::cancelDownloads() { // Stop the current downloads + QMap::iterator it = mReplies.begin(); + for(; it!=mReplies.end();it++) + { + dynamic_cast(it.value())->abort(); + } + + // Clear all the lists + init(); + checkIfModalRemains(); // Notify everyone that the downloads have been canceled. emit cancelAllDownloads(); } +void UBDownloadManager::onDownloadError(int id) +{ + QNetworkReply* pReply = mReplies.value(id); + if(NULL != pReply) + { + // Check which error occured: + switch(pReply->error()) + { + case QNetworkReply::OperationCanceledError: + // For futur developments: do something in case of download aborting (message? remove the download?) + break; + + default: + // Check the documentation of QNetworkReply in Qt Assistant for the different error cases + break; + } + } +} + // ------------------------------------------------------------------------------ /** * \brief Constructor @@ -361,6 +396,15 @@ void UBDownloadHttpFile::onDownloadProgress(qint64 bytesReceived, qint64 bytesTo */ void UBDownloadHttpFile::onDownloadFinished(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground) { - emit downloadFinished(mId, pSuccess, sourceUrl, pContentTypeHeader, pData, pPos, pSize, isBackground); + if(pSuccess) + { + // Notify the end of the download + emit downloadFinished(mId, pSuccess, sourceUrl, pContentTypeHeader, pData, pPos, pSize, isBackground); + } + else + { + // Notify the fact that and error occured during the download + emit downloadError(mId); + } } diff --git a/src/core/UBDownloadManager.h b/src/core/UBDownloadManager.h index bc273100..8cda63ce 100644 --- a/src/core/UBDownloadManager.h +++ b/src/core/UBDownloadManager.h @@ -49,6 +49,7 @@ public: signals: void downloadProgress(int id, qint64 current,qint64 total); void downloadFinished(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); + void downloadError(int id); private slots: void onDownloadFinished(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); @@ -83,6 +84,7 @@ private slots: void onUpdateDownloadLists(); void onDownloadProgress(int id, qint64 received, qint64 total); void onDownloadFinished(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); + void onDownloadError(int id); private: void init(); @@ -101,6 +103,8 @@ private: int mLastID; /** The current download availability (-1 = free, otherwise the file ID is recorded)*/ QVector mDLAvailability; + /** A map containing the replies of the GET operations */ + QMap mReplies; }; #endif // UBDOWNLOADMANAGER_H diff --git a/src/gui/UBDownloadWidget.cpp b/src/gui/UBDownloadWidget.cpp index 83b8b7bd..1fd126da 100644 --- a/src/gui/UBDownloadWidget.cpp +++ b/src/gui/UBDownloadWidget.cpp @@ -109,6 +109,7 @@ void UBDownloadWidget::onFileAddedToDownload() void UBDownloadWidget::addCurrentDownloads() { QVector actualDL = UBDownloadManager::downloadManager()->currentDownloads(); + qDebug() << "Actual downloads size: " << actualDL.size(); for(int i=0; i pendingDL = UBDownloadManager::downloadManager()->pendingDownloads(); + qDebug() << "Pending downloads size: " << pendingDL.size(); for(int i=0; icancelDownloads(); } // --------------------------------------------------------------------------------------------- diff --git a/src/network/UBHttpGet.cpp b/src/network/UBHttpGet.cpp index aeb02417..1edb712b 100644 --- a/src/network/UBHttpGet.cpp +++ b/src/network/UBHttpGet.cpp @@ -43,7 +43,7 @@ UBHttpGet::~UBHttpGet() } -void UBHttpGet::get(QUrl pUrl, QPointF pPos, QSize pSize, bool isBackground) +QNetworkReply* UBHttpGet::get(QUrl pUrl, QPointF pPos, QSize pSize, bool isBackground) { mPos = pPos; mSize = pSize; @@ -61,6 +61,7 @@ void UBHttpGet::get(QUrl pUrl, QPointF pPos, QSize pSize, bool isBackground) connect(mReply, SIGNAL(readyRead()), this, SLOT(readyRead())); connect(mReply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgressed(qint64, qint64))); + return mReply; } @@ -111,7 +112,7 @@ void UBHttpGet::requestFinished() void UBHttpGet::downloadProgressed(qint64 bytesReceived, qint64 bytesTotal) { - qDebug() << "received: " << bytesReceived << ", / " << bytesTotal << " bytes"; +// qDebug() << "received: " << bytesReceived << ", / " << bytesTotal << " bytes"; if (-1 != bytesTotal) { emit downloadProgress(bytesReceived, bytesTotal); diff --git a/src/network/UBHttpGet.h b/src/network/UBHttpGet.h index 15930f19..fd1eeef4 100644 --- a/src/network/UBHttpGet.h +++ b/src/network/UBHttpGet.h @@ -29,7 +29,7 @@ class UBHttpGet : public QObject UBHttpGet(QObject* parent = 0); virtual ~UBHttpGet(); - void get(QUrl pUrl, QPointF pPoint = QPointF(0, 0), QSize pSize = QSize(0, 0), bool isBackground = false); + QNetworkReply* get(QUrl pUrl, QPointF pPoint = QPointF(0, 0), QSize pSize = QSize(0, 0), bool isBackground = false); signals: