-
Notifications
You must be signed in to change notification settings - Fork 0
/
epidownloader.cpp
76 lines (66 loc) · 2.39 KB
/
epidownloader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "epidownloader.h"
#include <QDateTime>
#include <QUrl>
#include <QNetworkRequest>
#include <QDebug>
#include <QApplication>
EpiDownloader::EpiDownloader(QString dlurl, QObject *parent):QNetworkAccessManager(parent)
{
QUrl url(dlurl);
connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(fin(QNetworkReply*)));
QNetworkRequest req(url);
qDebug() << url;
req.setRawHeader("User-Agent", USERAGENT);
req.setRawHeader("Accept-Encoding","identity");
filename = dlurl;
mCurrentReply = this->get(req);
connect(mCurrentReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(dlProgress(qint64,qint64)));
connect(mCurrentReply, SIGNAL(readyRead()), this, SLOT(readingReadyBytes()));
downloadTime.start();
}
void EpiDownloader::fin(QNetworkReply * pReply)
{
if(pReply->error() != QNetworkReply::NoError)
qDebug() << "Download Error: " <<pReply->error();
if(pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 307 || pReply->rawHeaderList().contains("Location"))
{
QNetworkRequest req(pReply->header(QNetworkRequest::LocationHeader).toString());
req.setRawHeader("User-Agent", USERAGENT);
filename = pReply->header(QNetworkRequest::LocationHeader).toString();
mCurrentReply = this->get(req);
connect(mCurrentReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(dlProgress(qint64,qint64)));
connect(mCurrentReply, SIGNAL(readyRead()), this, SLOT(readingReadyBytes()));
return;
}
m_DownloadedData = pReply->readAll();
reply = pReply;
//emit a signal
pReply->deleteLater();
emit downloaded();
}
void EpiDownloader::readingReadyBytes()
{
//qDebug() << mCurrentReply->read(mCurrentReply->bytesAvailable());
QByteArray bytes = mCurrentReply->read(mCurrentReply->bytesAvailable());
emit readReady(bytes);
}
void EpiDownloader::dlProgress(qint64 bytesReceived, qint64 bytesToal)
{
// calculate the download speed
double speed = bytesReceived * 1000.0 / downloadTime.elapsed();
QString unit;
if (speed < 1024) {
unit = "bytes/sec";
} else if (speed < 1024*1024) {
speed /= 1024;
unit = "kB/s";
} else {
speed /= 1024*1024;
unit = "MB/s";
}
emit downloadProgress(bytesReceived, bytesToal, speed, unit);
}
QByteArray EpiDownloader::downloadedData() const
{
return m_DownloadedData;
}