metaforce/hecl-gui/DownloadManager.hpp

65 lines
2.0 KiB
C++
Raw Normal View History

2018-10-07 03:41:18 +00:00
#pragma once
#include <QObject>
#include <QtNetwork>
#include <QNetworkAccessManager>
#include <QProgressBar>
#include <QLabel>
2018-01-10 06:19:48 +00:00
class QuaZip;
2018-12-08 05:19:40 +00:00
class DownloadManager : public QObject {
Q_OBJECT
QNetworkAccessManager m_netManager;
QNetworkReply* m_indexInProgress = nullptr;
QNetworkReply* m_binaryInProgress = nullptr;
QString m_outPath;
bool m_hasError = false;
QProgressBar* m_progBar = nullptr;
QLabel* m_errorLabel = nullptr;
std::function<void(const QStringList& index)> m_indexCompletionHandler;
std::function<void(QuaZip& file)> m_completionHandler;
std::function<void()> m_failedHandler;
2018-12-08 05:19:40 +00:00
void resetError() {
m_hasError = false;
if (m_errorLabel)
m_errorLabel->setText(QString());
}
2018-12-08 05:19:40 +00:00
void setError(QNetworkReply::NetworkError error, const QString& errStr) {
if (m_hasError && error == QNetworkReply::OperationCanceledError)
return;
m_hasError = true;
if (m_errorLabel)
m_errorLabel->setText(errStr);
}
2018-12-08 05:19:40 +00:00
void _validateCert(QNetworkReply* reply);
public:
2018-12-08 05:19:40 +00:00
explicit DownloadManager(QObject* parent = Q_NULLPTR) : QObject(parent), m_netManager(this) {}
void connectWidgets(QProgressBar* progBar, QLabel* errorLabel,
std::function<void(const QStringList& index)>&& indexCompletionHandler,
std::function<void(QuaZip& file)>&& completionHandler, std::function<void()>&& failedHandler) {
m_progBar = progBar;
m_errorLabel = errorLabel;
m_indexCompletionHandler = std::move(indexCompletionHandler);
m_completionHandler = std::move(completionHandler);
m_failedHandler = std::move(failedHandler);
}
void fetchIndex();
void fetchBinary(const QString& str, const QString& outPath);
bool hasError() const { return m_hasError; }
public slots:
2018-12-08 05:19:40 +00:00
void indexFinished();
void indexError(QNetworkReply::NetworkError error);
void indexValidateCert();
2018-12-08 05:19:40 +00:00
void binaryFinished();
void binaryError(QNetworkReply::NetworkError error);
void binaryValidateCert();
void binaryDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
};