From 38d50cd5ec0a2490c263b05388193dac4d29d79b Mon Sep 17 00:00:00 2001 From: kervala Date: Sun, 12 Jun 2016 14:11:58 +0200 Subject: [PATCH] Changed: Use IOperationProgressListener for CDownloader --- .../client/ryzom_installer/src/downloader.cpp | 56 +++++++++---------- .../client/ryzom_installer/src/downloader.h | 29 ++-------- .../client/ryzom_installer/src/mainwindow.cpp | 2 +- .../ryzom_installer/src/operationdialog.cpp | 10 +--- 4 files changed, 34 insertions(+), 63 deletions(-) diff --git a/code/ryzom/tools/client/ryzom_installer/src/downloader.cpp b/code/ryzom/tools/client/ryzom_installer/src/downloader.cpp index f129416ad..3c85a8031 100644 --- a/code/ryzom/tools/client/ryzom_installer/src/downloader.cpp +++ b/code/ryzom/tools/client/ryzom_installer/src/downloader.cpp @@ -15,6 +15,7 @@ // along with this program. If not, see . #include "stdpch.h" +#include "operation.h" #include "downloader.h" #include "nel/misc/system_info.h" @@ -24,9 +25,9 @@ #define new DEBUG_NEW #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_downloadAfterHead(false), m_aborted(false), m_file(NULL) + m_downloadAfterHead(false), m_file(NULL) { m_manager = new QNetworkAccessManager(this); m_timer = new QTimer(this); @@ -60,7 +61,7 @@ bool CDownloader::prepareFile(const QString &url, const QString &fullPath) m_downloadAfterHead = false; - emit downloadPrepare(); + if (m_listener) m_listener->operationPrepare(); m_fullPath = fullPath; m_url = url; @@ -86,15 +87,6 @@ bool CDownloader::getFile() return true; } -bool CDownloader::stop() -{ - if (!m_reply) return false; - - m_reply->abort(); - - return true; -} - void CDownloader::startTimer() { stopTimer(); @@ -154,12 +146,12 @@ void CDownloader::getFileHead() if (checkDownloadedFile()) { // file is already downloaded - emit downloadSuccess(m_size); + if (m_listener) m_listener->operationSuccess(m_size); } else { // 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; @@ -189,13 +181,13 @@ void CDownloader::downloadFile() if (freeSpace < m_size - m_offset) { // 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; } if (!openFile()) { - emit downloadFail(tr("Unable to write file")); + if (m_listener) m_listener->operationFail(tr("Unable to write file")); return; } @@ -214,7 +206,7 @@ void CDownloader::downloadFile() connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)), SLOT(onDownloadProgress(qint64, qint64))); connect(m_reply, SIGNAL(readyRead()), SLOT(onDownloadRead())); - emit downloadStart(); + if (m_listener) m_listener->operationStart(); startTimer(); } @@ -230,7 +222,7 @@ void CDownloader::onTimeout() { qDebug() << "Timeout"; - emit downloadFail(tr("Timeout")); + if (m_listener) m_listener->operationFail(tr("Timeout")); } void CDownloader::onHtmlPageFinished() @@ -267,7 +259,7 @@ void CDownloader::onHeadFinished() { if (redirection.isEmpty()) { - emit downloadFail(tr("Redirection URL is not defined")); + if (m_listener) m_listener->operationFail(tr("Redirection URL is not defined")); return; } @@ -289,7 +281,7 @@ void CDownloader::onHeadFinished() else if (status == 200) { // update size - emit downloadInit(0, m_size); + if (m_listener) m_listener->operationInit(0, m_size); if (!m_supportsAcceptRanges && acceptRanges == "bytes") { @@ -321,7 +313,7 @@ void CDownloader::onHeadFinished() m_size = regexp.cap(3).toLongLong(); // update offset and size - emit downloadInit(m_offset, m_size); + if (m_listener) m_listener->operationInit(m_offset, m_size); } else { @@ -332,7 +324,7 @@ void CDownloader::onHeadFinished() // other status else { - emit downloadFail(tr("Wrong status code: %1").arg(status)); + if (m_listener) m_listener->operationFail(tr("Wrong status code: %1").arg(status)); return; } @@ -356,28 +348,29 @@ void CDownloader::onDownloadFinished() closeFile(); - if (m_aborted) + if (m_listener && m_listener->operationShouldStop()) { - m_aborted = false; - emit downloadStop(); + m_listener->operationStop(); } else { 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) { + if (!m_listener) return; + if (error == QNetworkReply::OperationCanceledError) { - m_aborted = true; + m_listener->operationStop(); } 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(); - 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() diff --git a/code/ryzom/tools/client/ryzom_installer/src/downloader.h b/code/ryzom/tools/client/ryzom_installer/src/downloader.h index 7d9090700..4d3163723 100644 --- a/code/ryzom/tools/client/ryzom_installer/src/downloader.h +++ b/code/ryzom/tools/client/ryzom_installer/src/downloader.h @@ -17,6 +17,8 @@ #ifndef DOWNLOADER_H #define DOWNLOADER_H +class IOperationProgressListener; + /** * Files downloader, please note that only one file can be downloaded at once. * @@ -28,41 +30,19 @@ class CDownloader : public QObject Q_OBJECT public: - CDownloader(QObject *parent); + CDownloader(QObject *parent, IOperationProgressListener *listener); virtual ~CDownloader(); bool getHtmlPageContent(const QString &url); bool prepareFile(const QString &url, const QString &fullPath); bool getFile(); - bool stop(); bool supportsResume() const { return m_supportsAcceptRanges && m_supportsContentRange; } bool isDownloading() const { return m_file != NULL; } 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); private slots: @@ -75,6 +55,8 @@ private slots: void onDownloadRead(); protected: + IOperationProgressListener *m_listener; + void startTimer(); void stopTimer(); @@ -102,7 +84,6 @@ protected: bool m_supportsContentRange; bool m_downloadAfterHead; - bool m_aborted; QFile *m_file; }; diff --git a/code/ryzom/tools/client/ryzom_installer/src/mainwindow.cpp b/code/ryzom/tools/client/ryzom_installer/src/mainwindow.cpp index 568bde2ca..03a576574 100644 --- a/code/ryzom/tools/client/ryzom_installer/src/mainwindow.cpp +++ b/code/ryzom/tools/client/ryzom_installer/src/mainwindow.cpp @@ -35,7 +35,7 @@ CMainWindow::CMainWindow():QMainWindow() setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint); // downloader - m_downloader = new CDownloader(this); + m_downloader = new CDownloader(this, NULL); connect(m_downloader, SIGNAL(htmlPageContent(QString)), SLOT(onHtmlPageContent(QString))); diff --git a/code/ryzom/tools/client/ryzom_installer/src/operationdialog.cpp b/code/ryzom/tools/client/ryzom_installer/src/operationdialog.cpp index 3355f6aeb..cbe363f77 100644 --- a/code/ryzom/tools/client/ryzom_installer/src/operationdialog.cpp +++ b/code/ryzom/tools/client/ryzom_installer/src/operationdialog.cpp @@ -55,15 +55,7 @@ COperationDialog::COperationDialog(QWidget *parent):QDialog(parent), m_aborting( // connect(stopButton, SIGNAL(clicked()), SLOT(onStopClicked())); // downloader - m_downloader = new CDownloader(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))); + m_downloader = new CDownloader(this, this); connect(operationButtonBox, SIGNAL(clicked(QAbstractButton*)), SLOT(onAbortClicked()));