Added the download manager for the modal files

preferencesAboutTextFull
shibakaneki 13 years ago
parent b422398d37
commit ff0abb5306
  1. 12
      src/board/UBBoardController.cpp
  2. 10
      src/board/UBLibraryController.cpp
  3. 50
      src/core/UBDownloadManager.cpp
  4. 4
      src/core/UBDownloadManager.h
  5. 4
      src/gui/UBDownloadWidget.cpp
  6. 5
      src/network/UBHttpGet.cpp
  7. 2
      src/network/UBHttpGet.h

@ -767,13 +767,13 @@ void UBBoardController::downloadURL(const QUrl& url, const QPointF& pPos, const
desc.isBackground = isBackground; desc.isBackground = isBackground;
// INFO: DO NOT UNCOMMENT THE NEXT LINE! DEVELOPMENT IN PROGRESS // INFO: DO NOT UNCOMMENT THE NEXT LINE! DEVELOPMENT IN PROGRESS
// UBDownloadManager::downloadManager()->addFileToDownload(desc); UBDownloadManager::downloadManager()->addFileToDownload(desc);
UBHttpGet *http = new UBHttpGet(mActiveScene); // UBHttpGet *http = new UBHttpGet(mActiveScene);
showMessage(tr("Downloading content from %1").arg(url.toString()), true); // showMessage(tr("Downloading content from %1").arg(url.toString()), true);
connect(http, SIGNAL(downloadFinished(bool, QUrl, QString, QByteArray, QPointF, QSize, bool)), // connect(http, SIGNAL(downloadFinished(bool, QUrl, QString, QByteArray, QPointF, QSize, bool)),
this, SLOT(downloadFinished(bool, QUrl, QString, QByteArray, QPointF, QSize, bool))); // this, SLOT(downloadFinished(bool, QUrl, QString, QByteArray, QPointF, QSize, bool)));
http->get(url, pPos, pSize, isBackground); // http->get(url, pPos, pSize, isBackground);
} }
} }

@ -239,11 +239,11 @@ QList<UBLibElement*> UBLibraryController::rootCategoriesList()
categories << element; categories << element;
// Note : FEATURE IN DEVELOPMENT, DO NOT ERASE (or you will get problems) !!!! // Note : FEATURE IN DEVELOPMENT, DO NOT ERASE (or you will get problems) !!!!
// mSearchCategoryPath = QUrl::fromLocalFile(UBSettings::settings()->uniboardSearchDirectory()); mSearchCategoryPath = QUrl::fromLocalFile(UBSettings::settings()->uniboardSearchDirectory());
// element = new UBLibElement(eUBLibElementType_Folder, mSearchCategoryPath, tr("Web Search", "Web search category element")); element = new UBLibElement(eUBLibElementType_Folder, mSearchCategoryPath, tr("Web Search", "Web search category element"));
// element->setThumbnail(QImage(":images/libpalette/WebSearchCategory.svg")); element->setThumbnail(QImage(":images/libpalette/WebSearchCategory.svg"));
// element->setMoveable(false); element->setMoveable(false);
// categories << element; categories << element;
element = new UBLibElement(eUBLibElementType_Folder, mAnimationUserDirectoryPath, tr("Animations", "Animations category element")); element = new UBLibElement(eUBLibElementType_Folder, mAnimationUserDirectoryPath, tr("Animations", "Animations category element"));
element->setThumbnail(QImage(":images/libpalette/FlashCategory.svg")); element->setThumbnail(QImage(":images/libpalette/FlashCategory.svg"));

@ -84,7 +84,9 @@ void UBDownloadManager::init()
{ {
mCrntDL.clear(); mCrntDL.clear();
mPendingDL.clear(); mPendingDL.clear();
mReplies.clear();
mLastID = 1; mLastID = 1;
mDLAvailability.clear();
for(int i=0; i<SIMULTANEOUS_DOWNLOAD; i++) for(int i=0; i<SIMULTANEOUS_DOWNLOAD; i++)
{ {
mDLAvailability.append(-1); mDLAvailability.append(-1);
@ -228,6 +230,9 @@ void UBDownloadManager::updateFileCurrentSize(int id, qint64 received, qint64 to
// Remove the finished file from the current download list // Remove the finished file from the current download list
mCrntDL.remove(i); mCrntDL.remove(i);
// Here we don't forget to remove the reply related to the finished download
mReplies.remove(id);
// Free the download slot used by the finished file // Free the download slot used by the finished file
for(int j=0; j<mDLAvailability.size();j++) for(int j=0; j<mDLAvailability.size();j++)
{ {
@ -265,7 +270,9 @@ void UBDownloadManager::startFileDownload(sDownloadFileDesc desc)
UBDownloadHttpFile* http = new UBDownloadHttpFile(desc.id, this); UBDownloadHttpFile* http = new UBDownloadHttpFile(desc.id, this);
connect(http, SIGNAL(downloadProgress(int, qint64,qint64)), this, SLOT(onDownloadProgress(int,qint64,qint64))); connect(http, SIGNAL(downloadProgress(int, qint64,qint64)), this, SLOT(onDownloadProgress(int,qint64,qint64)));
connect(http, SIGNAL(downloadFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool)), this, SLOT(onDownloadFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool))); connect(http, SIGNAL(downloadFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool)), this, SLOT(onDownloadFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool)));
http->get(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 // Close the modal window
UBApplication::mainWindow->hideDownloadWidget(); UBApplication::mainWindow->hideDownloadWidget();
@ -311,12 +318,40 @@ void UBDownloadManager::checkIfModalRemains()
void UBDownloadManager::cancelDownloads() void UBDownloadManager::cancelDownloads()
{ {
// Stop the current downloads // Stop the current downloads
QMap<int, QNetworkReply*>::iterator it = mReplies.begin();
for(; it!=mReplies.end();it++)
{
dynamic_cast<QNetworkReply*>(it.value())->abort();
}
// Clear all the lists
init();
checkIfModalRemains();
// Notify everyone that the downloads have been canceled. // Notify everyone that the downloads have been canceled.
emit cancelAllDownloads(); 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 * \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) 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);
}
} }

@ -49,6 +49,7 @@ public:
signals: signals:
void downloadProgress(int id, qint64 current,qint64 total); 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 downloadFinished(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground);
void downloadError(int id);
private slots: private slots:
void onDownloadFinished(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); 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 onUpdateDownloadLists();
void onDownloadProgress(int id, qint64 received, qint64 total); 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 onDownloadFinished(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground);
void onDownloadError(int id);
private: private:
void init(); void init();
@ -101,6 +103,8 @@ private:
int mLastID; int mLastID;
/** The current download availability (-1 = free, otherwise the file ID is recorded)*/ /** The current download availability (-1 = free, otherwise the file ID is recorded)*/
QVector<int> mDLAvailability; QVector<int> mDLAvailability;
/** A map containing the replies of the GET operations */
QMap<int, QNetworkReply*> mReplies;
}; };
#endif // UBDOWNLOADMANAGER_H #endif // UBDOWNLOADMANAGER_H

@ -109,6 +109,7 @@ void UBDownloadWidget::onFileAddedToDownload()
void UBDownloadWidget::addCurrentDownloads() void UBDownloadWidget::addCurrentDownloads()
{ {
QVector<sDownloadFileDesc> actualDL = UBDownloadManager::downloadManager()->currentDownloads(); QVector<sDownloadFileDesc> actualDL = UBDownloadManager::downloadManager()->currentDownloads();
qDebug() << "Actual downloads size: " << actualDL.size();
for(int i=0; i<actualDL.size();i++) for(int i=0; i<actualDL.size();i++)
{ {
mpItem = new QTreeWidgetItem(mpTree); mpItem = new QTreeWidgetItem(mpTree);
@ -130,6 +131,7 @@ void UBDownloadWidget::addCurrentDownloads()
void UBDownloadWidget::addPendingDownloads() void UBDownloadWidget::addPendingDownloads()
{ {
QVector<sDownloadFileDesc> pendingDL = UBDownloadManager::downloadManager()->pendingDownloads(); QVector<sDownloadFileDesc> pendingDL = UBDownloadManager::downloadManager()->pendingDownloads();
qDebug() << "Pending downloads size: " << pendingDL.size();
for(int i=0; i<pendingDL.size(); i++) for(int i=0; i<pendingDL.size(); i++)
{ {
mpItem = new QTreeWidgetItem(mpTree); mpItem = new QTreeWidgetItem(mpTree);
@ -182,7 +184,7 @@ void UBDownloadWidget::onDownloadFinished(int id)
*/ */
void UBDownloadWidget::onCancelClicked() void UBDownloadWidget::onCancelClicked()
{ {
UBDownloadManager::downloadManager()->cancelDownloads();
} }
// --------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------

@ -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; mPos = pPos;
mSize = pSize; 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(readyRead()), this, SLOT(readyRead()));
connect(mReply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgressed(qint64, qint64))); 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) void UBHttpGet::downloadProgressed(qint64 bytesReceived, qint64 bytesTotal)
{ {
qDebug() << "received: " << bytesReceived << ", / " << bytesTotal << " bytes"; // qDebug() << "received: " << bytesReceived << ", / " << bytesTotal << " bytes";
if (-1 != bytesTotal) if (-1 != bytesTotal)
{ {
emit downloadProgress(bytesReceived, bytesTotal); emit downloadProgress(bytesReceived, bytesTotal);

@ -29,7 +29,7 @@ class UBHttpGet : public QObject
UBHttpGet(QObject* parent = 0); UBHttpGet(QObject* parent = 0);
virtual ~UBHttpGet(); 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: signals:

Loading…
Cancel
Save