parent
2f9367c39a
commit
af5b99cfb3
@ -0,0 +1,366 @@ |
|||||||
|
/*
|
||||||
|
* This program is free software: you can redistribute it and/or modify |
||||||
|
* it under the terms of the GNU General Public License as published by |
||||||
|
* the Free Software Foundation, either version 3 of the License, or |
||||||
|
* (at your option) any later version. |
||||||
|
* |
||||||
|
* This program is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
* GNU General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU General Public License |
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/ |
||||||
|
#include "UBDownloadManager.h" |
||||||
|
#include "core/UBApplication.h" |
||||||
|
#include "gui/UBMainWindow.h" |
||||||
|
|
||||||
|
/** The unique instance of the download manager */ |
||||||
|
static UBDownloadManager* pInstance = NULL; |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Constructor |
||||||
|
* @param parent as the parent widget |
||||||
|
* @param name as the object name |
||||||
|
*/ |
||||||
|
UBDownloadManager::UBDownloadManager(QObject *parent, const char *name):QObject(parent) |
||||||
|
{ |
||||||
|
setObjectName(name); |
||||||
|
init(); |
||||||
|
|
||||||
|
connect(this, SIGNAL(fileAddedToDownload()), this, SLOT(onUpdateDownloadLists())); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Destructor |
||||||
|
*/ |
||||||
|
UBDownloadManager::~UBDownloadManager() |
||||||
|
{ |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Get the download manager |
||||||
|
* @return a pointer on the download manager |
||||||
|
*/ |
||||||
|
UBDownloadManager* UBDownloadManager::downloadManager() |
||||||
|
{ |
||||||
|
if(NULL == pInstance) |
||||||
|
{ |
||||||
|
pInstance = new UBDownloadManager(); |
||||||
|
} |
||||||
|
return pInstance; |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Add a file to the download list |
||||||
|
* @param desc as the given file description |
||||||
|
*/ |
||||||
|
void UBDownloadManager::addFileToDownload(sDownloadFileDesc desc) |
||||||
|
{ |
||||||
|
// Set the ID for this download
|
||||||
|
desc.id = mLastID; |
||||||
|
mLastID++; |
||||||
|
|
||||||
|
// Add the file to the pending download list
|
||||||
|
mPendingDL.append(desc); |
||||||
|
|
||||||
|
// If the download is modal, show the download dialog
|
||||||
|
if(desc.modal) |
||||||
|
{ |
||||||
|
// Update the download order (priority to modal files)
|
||||||
|
updateDownloadOrder(); |
||||||
|
UBApplication::mainWindow->showDownloadWidget(); |
||||||
|
} |
||||||
|
|
||||||
|
emit fileAddedToDownload(); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Initialize the download manager |
||||||
|
*/ |
||||||
|
void UBDownloadManager::init() |
||||||
|
{ |
||||||
|
mCrntDL.clear(); |
||||||
|
mPendingDL.clear(); |
||||||
|
mLastID = 1; |
||||||
|
for(int i=0; i<SIMULTANEOUS_DOWNLOAD; i++) |
||||||
|
{ |
||||||
|
mDLAvailability.append(-1); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Update the download order. The modal downloads will be put in priority. |
||||||
|
*/ |
||||||
|
void UBDownloadManager::updateDownloadOrder() |
||||||
|
{ |
||||||
|
QVector<sDownloadFileDesc> modalFiles; |
||||||
|
QVector<sDownloadFileDesc> nonModalfiles; |
||||||
|
|
||||||
|
for(int i=0; i<mPendingDL.size(); i++) |
||||||
|
{ |
||||||
|
sDownloadFileDesc crnt = mPendingDL.at(i); |
||||||
|
if(crnt.modal) |
||||||
|
{ |
||||||
|
modalFiles.append(crnt); |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
nonModalfiles.append(crnt); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
mPendingDL = modalFiles + nonModalfiles; |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Update the download list. If a current download is finished, we take a |
||||||
|
* file from the pending download list and add it to the download list. |
||||||
|
*/ |
||||||
|
void UBDownloadManager::onUpdateDownloadLists() |
||||||
|
{ |
||||||
|
for(int i=0; i<SIMULTANEOUS_DOWNLOAD; i++) |
||||||
|
{ |
||||||
|
if(mPendingDL.empty()) |
||||||
|
{ |
||||||
|
// If we fall here that means that there is no pending download
|
||||||
|
break; |
||||||
|
} |
||||||
|
if(-1 == mDLAvailability.at(i)) |
||||||
|
{ |
||||||
|
// Pending downloads exist and a download 'slot' is available
|
||||||
|
// Let's move the first pending download to the current download
|
||||||
|
// list and fill the slot
|
||||||
|
sDownloadFileDesc desc = mPendingDL.at(0); |
||||||
|
mCrntDL.append(desc); |
||||||
|
mPendingDL.remove(0); |
||||||
|
mDLAvailability.remove(i); |
||||||
|
mDLAvailability.insert(i, desc.id); |
||||||
|
|
||||||
|
// Start the download of this file
|
||||||
|
startFileDownload(desc); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Get the list of the current downloads |
||||||
|
* @return a QVector of current downloads |
||||||
|
*/ |
||||||
|
QVector<sDownloadFileDesc> UBDownloadManager::currentDownloads() |
||||||
|
{ |
||||||
|
return mCrntDL; |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Get the list of the pending downloads |
||||||
|
* @return a QVector of pending downloads |
||||||
|
*/ |
||||||
|
QVector<sDownloadFileDesc> UBDownloadManager::pendingDownloads() |
||||||
|
{ |
||||||
|
return mPendingDL; |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Update the file transfer information |
||||||
|
* @param desc as the current downloaded file description |
||||||
|
*/ |
||||||
|
void UBDownloadManager::onDownloadProgress(int id, qint64 received, qint64 total) |
||||||
|
{ |
||||||
|
updateFileCurrentSize(id, received, total); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Called when the download of the given file is finished |
||||||
|
* @param desc as the current downloaded file description |
||||||
|
*/ |
||||||
|
void UBDownloadManager::onDownloadFinished(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground) |
||||||
|
{ |
||||||
|
for(int i=0; i<mCrntDL.size(); i++) |
||||||
|
{ |
||||||
|
sDownloadFileDesc desc = mCrntDL.at(i); |
||||||
|
if(id == desc.id) |
||||||
|
{ |
||||||
|
if(desc.modal) |
||||||
|
{ |
||||||
|
// The downloaded file is modal so we must put it on the board
|
||||||
|
emit addDownloadedFileToBoard(pSuccess, sourceUrl, pContentTypeHeader, pData, pPos, pSize, isBackground); |
||||||
|
} |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Then do this
|
||||||
|
updateFileCurrentSize(id); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Update the description of the given current downloaded file |
||||||
|
* @param desc as the current downloaded file description |
||||||
|
*/ |
||||||
|
void UBDownloadManager::updateFileCurrentSize(int id, qint64 received, qint64 total) |
||||||
|
{ |
||||||
|
for(int i=0; i<mCrntDL.size();i++) |
||||||
|
{ |
||||||
|
if(mCrntDL.at(i).id == id) |
||||||
|
{ |
||||||
|
sDownloadFileDesc desc = mCrntDL.at(i); |
||||||
|
if(received >= 0 && total >= 0) |
||||||
|
{ |
||||||
|
// -------------------------------------
|
||||||
|
// [=============== x% ==== ]
|
||||||
|
// -------------------------------------
|
||||||
|
desc.currentSize = received; |
||||||
|
desc.totalSize = total; |
||||||
|
|
||||||
|
emit downloadUpdated(id, received, total); |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
// -------------------------------------
|
||||||
|
// [=============== 100% ==============]
|
||||||
|
// -------------------------------------
|
||||||
|
// received and total are negative. That means that the download is finished
|
||||||
|
desc.currentSize = mCrntDL.at(i).totalSize; |
||||||
|
|
||||||
|
// Remove the finished file from the current download list
|
||||||
|
mCrntDL.remove(i); |
||||||
|
|
||||||
|
// Free the download slot used by the finished file
|
||||||
|
for(int j=0; j<mDLAvailability.size();j++) |
||||||
|
{ |
||||||
|
if(id == mDLAvailability.at(j)) |
||||||
|
{ |
||||||
|
mDLAvailability.remove(j); |
||||||
|
mDLAvailability.insert(j, -1); |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Here we check if some modal downloads remain
|
||||||
|
checkIfModalRemains(); |
||||||
|
|
||||||
|
// Then we update the list of downloads
|
||||||
|
onUpdateDownloadLists(); |
||||||
|
|
||||||
|
emit downloadFinished(id); |
||||||
|
break; |
||||||
|
} |
||||||
|
|
||||||
|
mCrntDL.remove(i); |
||||||
|
mCrntDL.insert(i,desc); |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Start the download of a file |
||||||
|
* @param desc as the given file description |
||||||
|
*/ |
||||||
|
void UBDownloadManager::startFileDownload(sDownloadFileDesc desc) |
||||||
|
{ |
||||||
|
UBDownloadHttpFile* http = new UBDownloadHttpFile(desc.id, this); |
||||||
|
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))); |
||||||
|
http->get(QUrl(desc.url)); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Verify if modal downloads remains and notify everyone if it is not the case. |
||||||
|
*/ |
||||||
|
void UBDownloadManager::checkIfModalRemains() |
||||||
|
{ |
||||||
|
bool bModal = false; |
||||||
|
for(int i=0; i<mCrntDL.size();i++) |
||||||
|
{ |
||||||
|
if(mCrntDL.at(i).modal) |
||||||
|
{ |
||||||
|
bModal = true; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if(!bModal) |
||||||
|
{ |
||||||
|
for(int j=0; j<mPendingDL.size(); j++) |
||||||
|
{ |
||||||
|
if(mPendingDL.at(j).modal) |
||||||
|
{ |
||||||
|
bModal = true; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if(bModal) |
||||||
|
{ |
||||||
|
// Close the modal window
|
||||||
|
UBApplication::mainWindow->hideDownloadWidget(); |
||||||
|
|
||||||
|
// Notify that no modal downloads are pending
|
||||||
|
emit downloadModalFinished(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Cancel all downloads |
||||||
|
*/ |
||||||
|
void UBDownloadManager::cancelDownloads() |
||||||
|
{ |
||||||
|
// Stop the current downloads
|
||||||
|
|
||||||
|
|
||||||
|
// Notify everyone that the downloads have been canceled.
|
||||||
|
emit cancelAllDownloads(); |
||||||
|
} |
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------
|
||||||
|
/**
|
||||||
|
* \brief Constructor |
||||||
|
* @param parent as the parent widget |
||||||
|
* @param name as the object name |
||||||
|
*/ |
||||||
|
UBDownloadHttpFile::UBDownloadHttpFile(int fileId, QObject *parent):UBHttpGet(parent) |
||||||
|
{ |
||||||
|
mId = fileId; |
||||||
|
|
||||||
|
connect(this, SIGNAL(downloadFinished(bool,QUrl,QString,QByteArray,QPointF,QSize,bool)), this, SLOT(onDownloadFinished(bool,QUrl,QString,QByteArray,QPointF,QSize,bool))); |
||||||
|
connect(this, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(onDownloadProgress(qint64,qint64))); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Destructor |
||||||
|
*/ |
||||||
|
UBDownloadHttpFile::~UBDownloadHttpFile() |
||||||
|
{ |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Handles the download progress notification |
||||||
|
* @param bytesReceived as the number of received bytes |
||||||
|
* @param bytesTotal as the total number of bytes |
||||||
|
*/ |
||||||
|
void UBDownloadHttpFile::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) |
||||||
|
{ |
||||||
|
emit downloadProgress(mId, bytesReceived, bytesTotal); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Handles the download finished notification |
||||||
|
* @param pSuccess as the success indicator |
||||||
|
* @param sourceUrl as the source URL |
||||||
|
* @param pContentTypeHeader as the response content type header |
||||||
|
* @param pData as the packet data |
||||||
|
* @param pPos as the item position in the board |
||||||
|
* @param psize as the item size (GUI) |
||||||
|
* @param isBackground as the background mdoe indicator |
||||||
|
*/ |
||||||
|
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); |
||||||
|
} |
||||||
|
|
@ -0,0 +1,106 @@ |
|||||||
|
/*
|
||||||
|
* This program is free software: you can redistribute it and/or modify |
||||||
|
* it under the terms of the GNU General Public License as published by |
||||||
|
* the Free Software Foundation, either version 3 of the License, or |
||||||
|
* (at your option) any later version. |
||||||
|
* |
||||||
|
* This program is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
* GNU General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU General Public License |
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/ |
||||||
|
#ifndef UBDOWNLOADMANAGER_H |
||||||
|
#define UBDOWNLOADMANAGER_H |
||||||
|
|
||||||
|
#include <QObject> |
||||||
|
#include <QString> |
||||||
|
#include <QVector> |
||||||
|
#include <QMutex> |
||||||
|
|
||||||
|
#include "UBDownloadThread.h" |
||||||
|
|
||||||
|
#include "network/UBHttpGet.h" |
||||||
|
|
||||||
|
#define SIMULTANEOUS_DOWNLOAD 2 // Maximum 5 because of QNetworkAccessManager limitation!!!
|
||||||
|
|
||||||
|
typedef struct |
||||||
|
{ |
||||||
|
QString name; |
||||||
|
int id; |
||||||
|
int totalSize; |
||||||
|
int currentSize; |
||||||
|
QString url; |
||||||
|
bool modal; |
||||||
|
QPointF pos; // For board drop only
|
||||||
|
QSize size; // For board drop only
|
||||||
|
bool isBackground; // For board drop only
|
||||||
|
}sDownloadFileDesc; |
||||||
|
|
||||||
|
class UBDownloadHttpFile : public UBHttpGet |
||||||
|
{ |
||||||
|
Q_OBJECT |
||||||
|
public: |
||||||
|
UBDownloadHttpFile(int fileId, QObject* parent=0); |
||||||
|
~UBDownloadHttpFile(); |
||||||
|
|
||||||
|
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); |
||||||
|
|
||||||
|
private slots: |
||||||
|
void onDownloadFinished(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); |
||||||
|
void onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal); |
||||||
|
|
||||||
|
private: |
||||||
|
int mId; |
||||||
|
}; |
||||||
|
|
||||||
|
class UBDownloadManager : public QObject |
||||||
|
{ |
||||||
|
Q_OBJECT |
||||||
|
public: |
||||||
|
UBDownloadManager(QObject* parent=0, const char* name="UBDownloadManager"); |
||||||
|
~UBDownloadManager(); |
||||||
|
|
||||||
|
static UBDownloadManager* downloadManager(); |
||||||
|
void addFileToDownload(sDownloadFileDesc desc); |
||||||
|
QVector<sDownloadFileDesc> currentDownloads(); |
||||||
|
QVector<sDownloadFileDesc> pendingDownloads(); |
||||||
|
void cancelDownloads(); |
||||||
|
|
||||||
|
signals: |
||||||
|
void fileAddedToDownload(); |
||||||
|
void downloadUpdated(int id, qint64 crnt, qint64 total); |
||||||
|
void downloadFinished(int id); |
||||||
|
void downloadModalFinished(); |
||||||
|
void addDownloadedFileToBoard(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); |
||||||
|
void cancelAllDownloads(); |
||||||
|
|
||||||
|
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); |
||||||
|
|
||||||
|
private: |
||||||
|
void init(); |
||||||
|
void updateDownloadOrder(); |
||||||
|
void updateFileCurrentSize(int id, qint64 received=-1, qint64 total=-1); |
||||||
|
void startFileDownload(sDownloadFileDesc desc); |
||||||
|
void checkIfModalRemains(); |
||||||
|
|
||||||
|
/** The current downloads */ |
||||||
|
QVector<sDownloadFileDesc> mCrntDL; |
||||||
|
/** The pending downloads */ |
||||||
|
QVector<sDownloadFileDesc> mPendingDL; |
||||||
|
/** Pending download mutex */ |
||||||
|
QMutex mMutex; |
||||||
|
/** The last file ID */ |
||||||
|
int mLastID; |
||||||
|
/** The current download availability (-1 = free, otherwise the file ID is recorded)*/ |
||||||
|
QVector<int> mDLAvailability; |
||||||
|
}; |
||||||
|
|
||||||
|
#endif // UBDOWNLOADMANAGER_H
|
@ -0,0 +1,115 @@ |
|||||||
|
/*
|
||||||
|
* This program is free software: you can redistribute it and/or modify |
||||||
|
* it under the terms of the GNU General Public License as published by |
||||||
|
* the Free Software Foundation, either version 3 of the License, or |
||||||
|
* (at your option) any later version. |
||||||
|
* |
||||||
|
* This program is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
* GNU General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU General Public License |
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/ |
||||||
|
#include <QDebug> |
||||||
|
#include <QNetworkProxy> |
||||||
|
#include <QNetworkDiskCache> |
||||||
|
|
||||||
|
#include "core/UBSettings.h" |
||||||
|
|
||||||
|
#include "UBDownloadThread.h" |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Constructor |
||||||
|
* @param parent as the parent object |
||||||
|
* @param name as the object name |
||||||
|
*/ |
||||||
|
UBDownloadThread::UBDownloadThread(QObject *parent, const char *name):QThread(parent) |
||||||
|
, mbRun(false) |
||||||
|
,mpReply(NULL) |
||||||
|
{ |
||||||
|
setObjectName(name); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Destructor |
||||||
|
*/ |
||||||
|
UBDownloadThread::~UBDownloadThread() |
||||||
|
{ |
||||||
|
if(NULL != mpReply) |
||||||
|
{ |
||||||
|
delete mpReply; |
||||||
|
mpReply = NULL; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Run the thread |
||||||
|
*/ |
||||||
|
void UBDownloadThread::run() |
||||||
|
{ |
||||||
|
qDebug() << mUrl; |
||||||
|
// We start the download
|
||||||
|
QNetworkAccessManager* pNam = new QNetworkAccessManager(); |
||||||
|
|
||||||
|
mpReply = pNam->get(QNetworkRequest(QUrl(mUrl))); |
||||||
|
qDebug() << " -- Http GET reply ---------------------- "; |
||||||
|
qDebug() << mpReply->readAll(); |
||||||
|
qDebug() << " ---------------------------------------- "; |
||||||
|
|
||||||
|
connect(mpReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(onDownloadProgress(qint64,qint64))); |
||||||
|
connect(mpReply, SIGNAL(finished()), this, SLOT(onDownloadFinished())); |
||||||
|
|
||||||
|
while(mbRun) |
||||||
|
{ |
||||||
|
// Wait here until the end of the download
|
||||||
|
sleep(100); |
||||||
|
} |
||||||
|
|
||||||
|
disconnect(mpReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(onDownloadProgress(qint64,qint64))); |
||||||
|
disconnect(mpReply, SIGNAL(finished()), this, SLOT(onDownloadFinished())); |
||||||
|
if(NULL != mpReply) |
||||||
|
{ |
||||||
|
delete mpReply; |
||||||
|
mpReply = NULL; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Stop the current download |
||||||
|
*/ |
||||||
|
void UBDownloadThread::stopDownload() |
||||||
|
{ |
||||||
|
mbRun = false; |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Start the download |
||||||
|
*/ |
||||||
|
void UBDownloadThread::startDownload(int id, QString url) |
||||||
|
{ |
||||||
|
mID = id; |
||||||
|
mUrl = url; |
||||||
|
mbRun = true; |
||||||
|
start(); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Notify the download progression |
||||||
|
* @param received as the number of bytes received |
||||||
|
* @param total as the total number of bytes of the file |
||||||
|
*/ |
||||||
|
void UBDownloadThread::onDownloadProgress(qint64 received, qint64 total) |
||||||
|
{ |
||||||
|
qDebug() << received << " on " << total; |
||||||
|
emit downloadProgress(mID, received, total); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Notify the end of the download |
||||||
|
*/ |
||||||
|
void UBDownloadThread::onDownloadFinished() |
||||||
|
{ |
||||||
|
emit downloadFinised(mID); |
||||||
|
} |
@ -0,0 +1,56 @@ |
|||||||
|
/*
|
||||||
|
* This program is free software: you can redistribute it and/or modify |
||||||
|
* it under the terms of the GNU General Public License as published by |
||||||
|
* the Free Software Foundation, either version 3 of the License, or |
||||||
|
* (at your option) any later version. |
||||||
|
* |
||||||
|
* This program is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
* GNU General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU General Public License |
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/ |
||||||
|
#ifndef UBDOWNLOADTHREAD_H |
||||||
|
#define UBDOWNLOADTHREAD_H |
||||||
|
|
||||||
|
#include <QThread> |
||||||
|
#include <QNetworkAccessManager> |
||||||
|
#include <QNetworkRequest> |
||||||
|
#include <QNetworkReply> |
||||||
|
|
||||||
|
class UBDownloadThread : public QThread |
||||||
|
{ |
||||||
|
Q_OBJECT |
||||||
|
public: |
||||||
|
UBDownloadThread(QObject* parent=0, const char* name="UBDownloadThread"); |
||||||
|
~UBDownloadThread(); |
||||||
|
void stopDownload(); |
||||||
|
void startDownload(int id, QString url); |
||||||
|
|
||||||
|
signals: |
||||||
|
void downloadFinised(int id); |
||||||
|
void downloadProgress(int id, qint64 current, qint64 total); |
||||||
|
|
||||||
|
protected: |
||||||
|
virtual void run(); |
||||||
|
|
||||||
|
private slots: |
||||||
|
void onDownloadProgress(qint64 received, qint64 total); |
||||||
|
void onDownloadFinished(); |
||||||
|
|
||||||
|
private: |
||||||
|
/** Flag used to stop the thread */ |
||||||
|
bool mbRun; |
||||||
|
/** The downloaded file id */ |
||||||
|
int mID; |
||||||
|
/** The downloaded file url */ |
||||||
|
QString mUrl; |
||||||
|
/** The network access manager */ |
||||||
|
QNetworkAccessManager* mpNam; |
||||||
|
/** The network reply */ |
||||||
|
QNetworkReply* mpReply; |
||||||
|
}; |
||||||
|
|
||||||
|
#endif // UBDOWNLOADTHREAD_H
|
@ -0,0 +1,203 @@ |
|||||||
|
/*
|
||||||
|
* This program is free software: you can redistribute it and/or modify |
||||||
|
* it under the terms of the GNU General Public License as published by |
||||||
|
* the Free Software Foundation, either version 3 of the License, or |
||||||
|
* (at your option) any later version. |
||||||
|
* |
||||||
|
* This program is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
* GNU General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU General Public License |
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/ |
||||||
|
#include <QDebug> |
||||||
|
#include <QHeaderView> |
||||||
|
#include <QStyleOptionProgressBarV2> |
||||||
|
#include <QApplication> |
||||||
|
|
||||||
|
#include "UBDownloadWidget.h" |
||||||
|
#include "core/UBApplication.h" |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Constructor |
||||||
|
* @param parent as the parent widget |
||||||
|
* @param name as the widget object name |
||||||
|
*/ |
||||||
|
UBDownloadWidget::UBDownloadWidget(QWidget *parent, const char *name):QWidget(parent) |
||||||
|
, mpLayout(NULL) |
||||||
|
, mpBttnLayout(NULL) |
||||||
|
, mpTree(NULL) |
||||||
|
, mpCancelBttn(NULL) |
||||||
|
, mpItem(NULL) |
||||||
|
{ |
||||||
|
setObjectName(name); |
||||||
|
setWindowTitle(tr("Downloading files")); |
||||||
|
setAttribute(Qt::WA_StyledBackground, true); |
||||||
|
setStyleSheet(UBApplication::globalStyleSheet()); |
||||||
|
resize(400, 300); |
||||||
|
|
||||||
|
mpLayout = new QVBoxLayout(this); |
||||||
|
setLayout(mpLayout); |
||||||
|
|
||||||
|
mpTree = new QTreeWidget(this); |
||||||
|
mpTree->setRootIsDecorated(false); |
||||||
|
mpTree->header()->close(); |
||||||
|
mpLayout->addWidget(mpTree, 1); |
||||||
|
|
||||||
|
mpBttnLayout = new QHBoxLayout(); |
||||||
|
mpBttnLayout->addStretch(1); |
||||||
|
mpCancelBttn = new QPushButton(tr("Cancel"), this); |
||||||
|
mpBttnLayout->addWidget(mpCancelBttn, 0); |
||||||
|
mpLayout->addLayout(mpBttnLayout); |
||||||
|
|
||||||
|
connect(UBDownloadManager::downloadManager(), SIGNAL(fileAddedToDownload()), this, SLOT(onFileAddedToDownload())); |
||||||
|
connect(UBDownloadManager::downloadManager(), SIGNAL(downloadUpdated(int,qint64,qint64)), this, SLOT(onDownloadUpdated(int,qint64,qint64))); |
||||||
|
connect(UBDownloadManager::downloadManager(), SIGNAL(downloadFinished(int)), this, SLOT(onDownloadFinished(int))); |
||||||
|
connect(mpCancelBttn, SIGNAL(clicked()), this, SLOT(onCancelClicked())); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Destructor |
||||||
|
*/ |
||||||
|
UBDownloadWidget::~UBDownloadWidget() |
||||||
|
{ |
||||||
|
if(NULL != mpItem) |
||||||
|
{ |
||||||
|
delete mpItem; |
||||||
|
mpItem = NULL; |
||||||
|
} |
||||||
|
if(NULL != mpCancelBttn) |
||||||
|
{ |
||||||
|
delete mpCancelBttn; |
||||||
|
mpCancelBttn = NULL; |
||||||
|
} |
||||||
|
if(NULL != mpTree) |
||||||
|
{ |
||||||
|
delete mpTree; |
||||||
|
mpTree = NULL; |
||||||
|
} |
||||||
|
if(NULL != mpBttnLayout) |
||||||
|
{ |
||||||
|
delete mpBttnLayout; |
||||||
|
mpBttnLayout = NULL; |
||||||
|
} |
||||||
|
if(NULL != mpLayout) |
||||||
|
{ |
||||||
|
delete mpLayout; |
||||||
|
mpLayout = NULL; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Refresh the tree of downloaded files |
||||||
|
*/ |
||||||
|
void UBDownloadWidget::onFileAddedToDownload() |
||||||
|
{ |
||||||
|
if(NULL != mpTree) |
||||||
|
{ |
||||||
|
mpTree->clear(); |
||||||
|
addCurrentDownloads(); |
||||||
|
addPendingDownloads(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Add the current downloads |
||||||
|
*/ |
||||||
|
void UBDownloadWidget::addCurrentDownloads() |
||||||
|
{ |
||||||
|
QVector<sDownloadFileDesc> actualDL = UBDownloadManager::downloadManager()->currentDownloads(); |
||||||
|
for(int i=0; i<actualDL.size();i++) |
||||||
|
{ |
||||||
|
mpItem = new QTreeWidgetItem(mpTree); |
||||||
|
mpItem->setText(0, actualDL.at(i).name); |
||||||
|
mpItem->setData(0, Qt::UserRole, QVariant(actualDL.at(i).id)); |
||||||
|
mpTree->addTopLevelItem(mpItem); |
||||||
|
mpItem = new QTreeWidgetItem(mpTree); |
||||||
|
mpItem->setData(0, Qt::UserRole, actualDL.at(i).currentSize); |
||||||
|
mpItem->setData(0, Qt::UserRole + 1, actualDL.at(i).totalSize); |
||||||
|
mpItem->setData(0, Qt::UserRole + 2, actualDL.at(i).id); |
||||||
|
mpTree->addTopLevelItem(mpItem); |
||||||
|
mpTree->setItemDelegateForRow(((i+1)*2)-1, &mProgressBarDelegate); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Add the pending downloads |
||||||
|
*/ |
||||||
|
void UBDownloadWidget::addPendingDownloads() |
||||||
|
{ |
||||||
|
QVector<sDownloadFileDesc> pendingDL = UBDownloadManager::downloadManager()->pendingDownloads(); |
||||||
|
for(int i=0; i<pendingDL.size(); i++) |
||||||
|
{ |
||||||
|
mpItem = new QTreeWidgetItem(mpTree); |
||||||
|
mpItem->setText(0, pendingDL.at(i).name); |
||||||
|
mpItem->setData(0, Qt::UserRole, QVariant(pendingDL.at(i).id)); |
||||||
|
mpTree->addTopLevelItem(mpItem); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Update the progress bar |
||||||
|
* @param id as the downloaded file id |
||||||
|
* @param crnt as the current transfered size |
||||||
|
* @param total as the total size of the file |
||||||
|
*/ |
||||||
|
void UBDownloadWidget::onDownloadUpdated(int id, qint64 crnt, qint64 total) |
||||||
|
{ |
||||||
|
if(NULL != mpTree) |
||||||
|
{ |
||||||
|
QAbstractItemModel* model = mpTree->model(); |
||||||
|
if(NULL != model) |
||||||
|
{ |
||||||
|
for(int i=0; i< model->rowCount(); i++) |
||||||
|
{ |
||||||
|
QModelIndex currentIndex = model->index(i, 0); |
||||||
|
if(id == currentIndex.data(Qt::UserRole + 2)) |
||||||
|
{ |
||||||
|
// We found the right item, now we update the progress bar
|
||||||
|
model->setData(currentIndex, crnt, Qt::UserRole); |
||||||
|
model->setData(currentIndex, total, Qt::UserRole + 1); |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Handles the download finish notification |
||||||
|
* @param id as the downloaded file id |
||||||
|
*/ |
||||||
|
void UBDownloadWidget::onDownloadFinished(int id) |
||||||
|
{ |
||||||
|
// Refresh the file's list
|
||||||
|
onFileAddedToDownload(); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Handles the Cancel button action |
||||||
|
*/ |
||||||
|
void UBDownloadWidget::onCancelClicked() |
||||||
|
{ |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------------------
|
||||||
|
UBDownloadProgressDelegate::UBDownloadProgressDelegate(QObject *parent):QItemDelegate(parent) |
||||||
|
{ |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
void UBDownloadProgressDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
||||||
|
{ |
||||||
|
QStyleOptionProgressBarV2 opt; |
||||||
|
opt.rect = option.rect; |
||||||
|
opt.minimum = 0; |
||||||
|
opt.maximum = index.data(Qt::UserRole + 1).toInt(); |
||||||
|
opt.progress = index.data(Qt::UserRole).toInt(); |
||||||
|
|
||||||
|
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &opt, painter, 0); |
||||||
|
} |
@ -0,0 +1,69 @@ |
|||||||
|
/*
|
||||||
|
* This program is free software: you can redistribute it and/or modify |
||||||
|
* it under the terms of the GNU General Public License as published by |
||||||
|
* the Free Software Foundation, either version 3 of the License, or |
||||||
|
* (at your option) any later version. |
||||||
|
* |
||||||
|
* This program is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
* GNU General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU General Public License |
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/ |
||||||
|
#ifndef UBDOWNLOADWIDGET_H |
||||||
|
#define UBDOWNLOADWIDGET_H |
||||||
|
|
||||||
|
#include <QWidget> |
||||||
|
#include <QHBoxLayout> |
||||||
|
#include <QVBoxLayout> |
||||||
|
#include <QPushButton> |
||||||
|
#include <QTreeWidget> |
||||||
|
#include <QTreeWidgetItem> |
||||||
|
#include <QItemDelegate> |
||||||
|
|
||||||
|
#include "core/UBDownloadManager.h" |
||||||
|
|
||||||
|
class UBDownloadProgressDelegate : public QItemDelegate |
||||||
|
{ |
||||||
|
Q_OBJECT |
||||||
|
public: |
||||||
|
UBDownloadProgressDelegate(QObject* parent=0); |
||||||
|
|
||||||
|
protected: |
||||||
|
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; |
||||||
|
}; |
||||||
|
|
||||||
|
class UBDownloadWidget : public QWidget |
||||||
|
{ |
||||||
|
Q_OBJECT |
||||||
|
public: |
||||||
|
UBDownloadWidget(QWidget* parent=0, const char* name="UBDownloadWidget"); |
||||||
|
~UBDownloadWidget(); |
||||||
|
|
||||||
|
private slots: |
||||||
|
void onFileAddedToDownload(); |
||||||
|
void onDownloadUpdated(int id, qint64 crnt, qint64 total); |
||||||
|
void onDownloadFinished(int id); |
||||||
|
void onCancelClicked(); |
||||||
|
|
||||||
|
private: |
||||||
|
void addCurrentDownloads(); |
||||||
|
void addPendingDownloads(); |
||||||
|
|
||||||
|
/** The general layout of this widget */ |
||||||
|
QVBoxLayout* mpLayout; |
||||||
|
/** The button layout */ |
||||||
|
QHBoxLayout* mpBttnLayout; |
||||||
|
/** The treeview that will display the files list */ |
||||||
|
QTreeWidget* mpTree; |
||||||
|
/** The 'Cancel' button */ |
||||||
|
QPushButton* mpCancelBttn; |
||||||
|
/** A temporary tree widget item */ |
||||||
|
QTreeWidgetItem* mpItem; |
||||||
|
/** The delegate that will draw the progressbars */ |
||||||
|
UBDownloadProgressDelegate mProgressBarDelegate; |
||||||
|
}; |
||||||
|
|
||||||
|
#endif // UBDOWNLOADWIDGET_H
|
Loading…
Reference in new issue