class TaskDownload
: public QObject
{
Q_OBJECT
QNetworkAccessManager NetworkAccessManager;
QNetworkReply* Reply;
QByteArray Content;
qint64 Attemps;
QUrl Url;
void AttempDownload();
public:
bool IsSuccessfully() const
{
return Attemps < 3 && Content.size() > 0;
}
QByteArray& GetContent()
{
return Content;
}
const QByteArray& GetContent() const
{
return Content;
}
TaskDownload(QUrl url);
~TaskDownload();
signals:
void ReadyRead();
void DownloadFinished();
void DownloadProgress(qint64, qint64);
private slots:
void onReadyRead();
virtual void onDownloadFinished();
void onDownloadError(QNetworkReply::NetworkError err);
void onDownloadProgress(qint64, qint64);
};
#include "TaskDownload.h"
TaskDownload::TaskDownload(QUrl url)
: Reply(nullptr),
Attemps(1)
, Url(url)
{
AttempDownload();
}
TaskDownload::~TaskDownload()
{
}
void TaskDownload::AttempDownload()
{
if (Attemps > 3)
{
//throw new std::exception(QString("Failed to download file: " + Url.toString()).toStdString().c_str());
exit(100001);
}
QNetworkRequest request(Url);
Reply = NetworkAccessManager.get(request);
qInfo() << QString("Begin download, attemp: %1 \nurl: %2").arg(QString::number(Attemps), Url.toString());
connect(Reply, SIGNAL(readyRead()),
this, SLOT(onReadyRead()));
connect(Reply, SIGNAL(downloadProgress(qint64, qint64)),
this, SLOT(onDownloadProgress(qint64, qint64)));
connect(Reply, SIGNAL(finished()),
this, SLOT(onDownloadFinished()));
connect(Reply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(onDownloadError(QNetworkReply::NetworkError)));
this->Attemps++;
}
void TaskDownload::onReadyRead()
{
// downloadTime.start();
emit ReadyRead();
}
void TaskDownload::onDownloadError(QNetworkReply::NetworkError err)
{
auto url = Url.toString().toStdString();
qInfo() << "onDownloadError: " << Url.toString() << " | error: " << err;
AttempDownload();
}
void TaskDownload::onDownloadFinished()
{
qInfo() << "complete download: " << Url.toString();
if (Reply->error() > 0)
{
qDebug() << "onDownloadError: " << Url.toString() << " | error: " << Reply->errorString();
AttempDownload();
return;
}
Content = Reply->readAll();
Reply->deleteLater();
emit DownloadFinished();
}
void TaskDownload::onDownloadProgress(qint64 cur, qint64 max)
{
emit DownloadProgress(cur, max);
}
Просто делаете редирект на маркет