Changed: Use IOperationProgressListener for CDownloader

--HG--
branch : develop
This commit is contained in:
kervala 2016-06-12 14:11:58 +02:00
parent d77db6ce27
commit 9116ec1c44
4 changed files with 34 additions and 63 deletions

View file

@ -15,6 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "stdpch.h" #include "stdpch.h"
#include "operation.h"
#include "downloader.h" #include "downloader.h"
#include "nel/misc/system_info.h" #include "nel/misc/system_info.h"
@ -24,9 +25,9 @@
#define new DEBUG_NEW #define new DEBUG_NEW
#endif #endif
CDownloader::CDownloader(QObject *parent):QObject(parent), m_manager(NULL), m_reply(NULL), m_timer(NULL), CDownloader::CDownloader(QObject *parent, IOperationProgressListener *listener):QObject(parent), m_listener(listener), m_manager(NULL), m_reply(NULL), m_timer(NULL),
m_offset(0), m_size(0), m_supportsAcceptRanges(false), m_supportsContentRange(false), m_offset(0), m_size(0), m_supportsAcceptRanges(false), m_supportsContentRange(false),
m_downloadAfterHead(false), m_aborted(false), m_file(NULL) m_downloadAfterHead(false), m_file(NULL)
{ {
m_manager = new QNetworkAccessManager(this); m_manager = new QNetworkAccessManager(this);
m_timer = new QTimer(this); m_timer = new QTimer(this);
@ -60,7 +61,7 @@ bool CDownloader::prepareFile(const QString &url, const QString &fullPath)
m_downloadAfterHead = false; m_downloadAfterHead = false;
emit downloadPrepare(); if (m_listener) m_listener->operationPrepare();
m_fullPath = fullPath; m_fullPath = fullPath;
m_url = url; m_url = url;
@ -86,15 +87,6 @@ bool CDownloader::getFile()
return true; return true;
} }
bool CDownloader::stop()
{
if (!m_reply) return false;
m_reply->abort();
return true;
}
void CDownloader::startTimer() void CDownloader::startTimer()
{ {
stopTimer(); stopTimer();
@ -154,12 +146,12 @@ void CDownloader::getFileHead()
if (checkDownloadedFile()) if (checkDownloadedFile())
{ {
// file is already downloaded // file is already downloaded
emit downloadSuccess(m_size); if (m_listener) m_listener->operationSuccess(m_size);
} }
else else
{ {
// or has wrong size // or has wrong size
emit downloadFail(tr("File (%1B) is larger than expected (%2B)").arg(m_offset).arg(m_size)); if (m_listener) m_listener->operationFail(tr("File (%1B) is larger than expected (%2B)").arg(m_offset).arg(m_size));
} }
return; return;
@ -189,13 +181,13 @@ void CDownloader::downloadFile()
if (freeSpace < m_size - m_offset) if (freeSpace < m_size - m_offset)
{ {
// we have not enough free disk space to continue download // we have not enough free disk space to continue download
emit downloadFail(tr("You only have %1 bytes left on device, but %2 bytes are required.").arg(freeSpace).arg(m_size - m_offset)); if (m_listener) m_listener->operationFail(tr("You only have %1 bytes left on device, but %2 bytes are required.").arg(freeSpace).arg(m_size - m_offset));
return; return;
} }
if (!openFile()) if (!openFile())
{ {
emit downloadFail(tr("Unable to write file")); if (m_listener) m_listener->operationFail(tr("Unable to write file"));
return; return;
} }
@ -214,7 +206,7 @@ void CDownloader::downloadFile()
connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)), SLOT(onDownloadProgress(qint64, qint64))); connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)), SLOT(onDownloadProgress(qint64, qint64)));
connect(m_reply, SIGNAL(readyRead()), SLOT(onDownloadRead())); connect(m_reply, SIGNAL(readyRead()), SLOT(onDownloadRead()));
emit downloadStart(); if (m_listener) m_listener->operationStart();
startTimer(); startTimer();
} }
@ -230,7 +222,7 @@ void CDownloader::onTimeout()
{ {
qDebug() << "Timeout"; qDebug() << "Timeout";
emit downloadFail(tr("Timeout")); if (m_listener) m_listener->operationFail(tr("Timeout"));
} }
void CDownloader::onHtmlPageFinished() void CDownloader::onHtmlPageFinished()
@ -267,7 +259,7 @@ void CDownloader::onHeadFinished()
{ {
if (redirection.isEmpty()) if (redirection.isEmpty())
{ {
emit downloadFail(tr("Redirection URL is not defined")); if (m_listener) m_listener->operationFail(tr("Redirection URL is not defined"));
return; return;
} }
@ -289,7 +281,7 @@ void CDownloader::onHeadFinished()
else if (status == 200) else if (status == 200)
{ {
// update size // update size
emit downloadInit(0, m_size); if (m_listener) m_listener->operationInit(0, m_size);
if (!m_supportsAcceptRanges && acceptRanges == "bytes") if (!m_supportsAcceptRanges && acceptRanges == "bytes")
{ {
@ -321,7 +313,7 @@ void CDownloader::onHeadFinished()
m_size = regexp.cap(3).toLongLong(); m_size = regexp.cap(3).toLongLong();
// update offset and size // update offset and size
emit downloadInit(m_offset, m_size); if (m_listener) m_listener->operationInit(m_offset, m_size);
} }
else else
{ {
@ -332,7 +324,7 @@ void CDownloader::onHeadFinished()
// other status // other status
else else
{ {
emit downloadFail(tr("Wrong status code: %1").arg(status)); if (m_listener) m_listener->operationFail(tr("Wrong status code: %1").arg(status));
return; return;
} }
@ -356,28 +348,29 @@ void CDownloader::onDownloadFinished()
closeFile(); closeFile();
if (m_aborted) if (m_listener && m_listener->operationShouldStop())
{ {
m_aborted = false; m_listener->operationStop();
emit downloadStop();
} }
else else
{ {
bool ok = NLMISC::CFile::setFileModificationDate(m_fullPath.toUtf8().constData(), m_lastModified.toTime_t()); bool ok = NLMISC::CFile::setFileModificationDate(m_fullPath.toUtf8().constData(), m_lastModified.toTime_t());
emit downloadSuccess(m_size); if (m_listener) m_listener->operationSuccess(m_size);
} }
} }
void CDownloader::onError(QNetworkReply::NetworkError error) void CDownloader::onError(QNetworkReply::NetworkError error)
{ {
if (!m_listener) return;
if (error == QNetworkReply::OperationCanceledError) if (error == QNetworkReply::OperationCanceledError)
{ {
m_aborted = true; m_listener->operationStop();
} }
else else
{ {
emit downloadFail(tr("Network error: %1").arg(error)); m_listener->operationFail(tr("Network error: %1").arg(error));
} }
} }
@ -385,7 +378,12 @@ void CDownloader::onDownloadProgress(qint64 current, qint64 total)
{ {
stopTimer(); stopTimer();
emit downloadProgress(m_offset + current); if (!m_listener) return;
m_listener->operationProgress(m_offset + current, ""); // TODO: put file
// abort download
if (m_listener->operationShouldStop() && m_reply) m_reply->abort();
} }
void CDownloader::onDownloadRead() void CDownloader::onDownloadRead()

View file

@ -17,6 +17,8 @@
#ifndef DOWNLOADER_H #ifndef DOWNLOADER_H
#define DOWNLOADER_H #define DOWNLOADER_H
class IOperationProgressListener;
/** /**
* Files downloader, please note that only one file can be downloaded at once. * Files downloader, please note that only one file can be downloaded at once.
* *
@ -28,41 +30,19 @@ class CDownloader : public QObject
Q_OBJECT Q_OBJECT
public: public:
CDownloader(QObject *parent); CDownloader(QObject *parent, IOperationProgressListener *listener);
virtual ~CDownloader(); virtual ~CDownloader();
bool getHtmlPageContent(const QString &url); bool getHtmlPageContent(const QString &url);
bool prepareFile(const QString &url, const QString &fullPath); bool prepareFile(const QString &url, const QString &fullPath);
bool getFile(); bool getFile();
bool stop();
bool supportsResume() const { return m_supportsAcceptRanges && m_supportsContentRange; } bool supportsResume() const { return m_supportsAcceptRanges && m_supportsContentRange; }
bool isDownloading() const { return m_file != NULL; } bool isDownloading() const { return m_file != NULL; }
signals: signals:
// emitted when requesting real URL
void downloadPrepare();
// emitted when we got the initial (local) and total (remote) size of file
void downloadInit(qint64 current, qint64 total);
// emitted when we begin to download
void downloadStart();
// emitted when the download stopped
void downloadStop();
// emittd when downloading
void downloadProgress(qint64 current);
// emitted when the whole file is downloaded
void downloadSuccess(qint64 total);
// emitted when an error occurs
void downloadFail(const QString &error);
void htmlPageContent(const QString &html); void htmlPageContent(const QString &html);
private slots: private slots:
@ -75,6 +55,8 @@ private slots:
void onDownloadRead(); void onDownloadRead();
protected: protected:
IOperationProgressListener *m_listener;
void startTimer(); void startTimer();
void stopTimer(); void stopTimer();
@ -102,7 +84,6 @@ protected:
bool m_supportsContentRange; bool m_supportsContentRange;
bool m_downloadAfterHead; bool m_downloadAfterHead;
bool m_aborted;
QFile *m_file; QFile *m_file;
}; };

View file

@ -35,7 +35,7 @@ CMainWindow::CMainWindow():QMainWindow()
setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint); setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);
// downloader // downloader
m_downloader = new CDownloader(this); m_downloader = new CDownloader(this, NULL);
connect(m_downloader, SIGNAL(htmlPageContent(QString)), SLOT(onHtmlPageContent(QString))); connect(m_downloader, SIGNAL(htmlPageContent(QString)), SLOT(onHtmlPageContent(QString)));

View file

@ -55,15 +55,7 @@ COperationDialog::COperationDialog(QWidget *parent):QDialog(parent), m_aborting(
// connect(stopButton, SIGNAL(clicked()), SLOT(onStopClicked())); // connect(stopButton, SIGNAL(clicked()), SLOT(onStopClicked()));
// downloader // downloader
m_downloader = new CDownloader(this); m_downloader = new CDownloader(this, this);
connect(m_downloader, SIGNAL(downloadPrepare()), SLOT(onProgressPrepare()));
connect(m_downloader, SIGNAL(downloadInit(qint64, qint64)), SLOT(onProgressInit(qint64, qint64)));
connect(m_downloader, SIGNAL(downloadStart()), SLOT(onProgressStart()));
connect(m_downloader, SIGNAL(downloadStop()), SLOT(onProgressStop()));
connect(m_downloader, SIGNAL(downloadProgress(qint64, QString)), SLOT(onProgressProgress(qint64, QString)));
connect(m_downloader, SIGNAL(downloadSuccess(qint64)), SLOT(onProgressSuccess(qint64)));
connect(m_downloader, SIGNAL(downloadFail(QString)), SLOT(onProgressFail(QString)));
connect(operationButtonBox, SIGNAL(clicked(QAbstractButton*)), SLOT(onAbortClicked())); connect(operationButtonBox, SIGNAL(clicked(QAbstractButton*)), SLOT(onAbortClicked()));