-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdownloader.cpp
More file actions
159 lines (147 loc) · 3.84 KB
/
downloader.cpp
File metadata and controls
159 lines (147 loc) · 3.84 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "downloader.h"
int Downloader::getMaxConnections() const
{
return maxConnections;
}
void Downloader::setMaxConnections(int value)
{
maxConnections = value;
}
void Downloader::abort(const QUrl &url)
{
for(int i=0;i<downloadQueue.size();i++)
{
Download download = downloadQueue[i];
if(download.url==url)
{
download.reply->deleteLater();
downloadQueue.removeAt(i);
i--;
}
}
}
void Downloader::continueQueue()
{
while(currentDownloads.size()<maxConnections && !downloadQueue.isEmpty())
{
Download dl = downloadQueue.first();
downloadQueue.removeAt(0);
load(dl.url,dl.description);
}
if(downloadQueue.isEmpty())
{
emit queueEmpty();
}
}
void Downloader::printProgress()
{
if(currentDownloads.size()>0)
{
foreach(Download dl,currentDownloads)
{
if(!dl.reply->header(QNetworkRequest::ContentLengthHeader).isNull())
{
int part = dl.reply->bytesAvailable();
int total = dl.reply->header(QNetworkRequest::ContentLengthHeader).toInt();
int percent = 100*((float)part)/total;
QString percentString = QString::number(percent) + "% ";
out << percentString;
for(int i=0;i<5-percentString.length();i++)
out << " ";
out << dl.description << endl;
}
else
{
int part = dl.reply->bytesAvailable();
QString sizeString = QString::number(part/1000000) + "MB ";
out << sizeString;
for(int i=0;i<5-sizeString.length();i++)
out << " ";
out << dl.description << endl;
}
}
out << downloadQueue.size() << " queued" << "\n\n";
out.flush();
}
}
QUrl Downloader::getUnredirectedUrl(QUrl url)
{
while(redirectMapping.contains(url))
{
url = redirectMapping[url];
}
return url;
}
int Downloader::getDownloadIndexByReply(QNetworkReply *reply)
{
for(int i=0;i<currentDownloads.size();i++)
{
if(currentDownloads[i].reply==reply)
return i;
}
return -1;
}
Downloader::Downloader(QObject *parent) : QObject(parent)
{
lastLength = 0;
connect(&timer,&QTimer::timeout,this,&Downloader::printProgress);
timer.setInterval(1000);
timer.setSingleShot(false);
manager = new QNetworkAccessManager(this);
//manager->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
connect(manager,&QNetworkAccessManager::finished,this,&Downloader::downloadFinished);
maxConnections = 5;
timer.start();
}
void Downloader::load(const QUrl &url, const QString& description)
{
if(currentDownloads.size()>=maxConnections)
{
downloadQueue.append(Download{url,description,nullptr});
return;
}
QNetworkRequest request;
request.setUrl(QUrl(url));
QNetworkReply* reply = manager->get(request);
currentDownloads.append(Download{url,description,reply});
}
void Downloader::downloadFinished(QNetworkReply *reply)
{
QNetworkReply::NetworkError error = reply->error();
int downloadIndex = getDownloadIndexByReply(reply);
if(downloadIndex>=0)
{
Download& dl = currentDownloads[downloadIndex];
if(!reply->attribute(QNetworkRequest::RedirectionTargetAttribute).isNull())
{
QUrl newUrl = QUrl(reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toString());
redirectMapping.insert(newUrl,reply->url());
load(newUrl,dl.description);
}
else
{
QUrl url = reply->url();
while(redirectMapping.contains(url))
{
url = redirectMapping[url];
}
if(error == QNetworkReply::NoError)
{
emit downloadSuccess(url,reply->readAll());
}
else
{
emit downloadFailed(url,error);
}
}
currentDownloads.removeAll(dl);
}
reply->deleteLater();
continueQueue();
}
bool Download::operator ==(const Download &other)
{
return (url == other.url) &&
(description == other.description) &&
(reply == other.reply);
}