-
Notifications
You must be signed in to change notification settings - Fork 1
/
filedownloader.cpp
108 lines (90 loc) · 2.44 KB
/
filedownloader.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
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
#include "filedownloader.h"
#include <QtGui>
#include <QtNetwork>
FileDownloader::FileDownloader(QObject *parent) :
QObject(parent)
{
network = new QNetworkAccessManager;
}
FileDownloader::~FileDownloader()
{
foreach(QNetworkReply *th, res.keys())
th->abort();
}
QNetworkReply * FileDownloader::getURL(const QUrl &url, QProgressBar *prb)
{
QNetworkReply *control = network->get(QNetworkRequest(url));
connect(control, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(updateProgress(qint64, qint64)));
connect(control, SIGNAL(finished()), this, SLOT(operationFinished()));
connect(control, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(errorOccured(QNetworkReply::NetworkError)));
connect(control, SIGNAL(readyRead()), this, SLOT(readData()));
timers[control] = new QTimer;
connect(timers[control], SIGNAL(timeout()), this, SLOT(timedOut()));
timers2[timers[control]] = control;
timers[control]->start(10000);
err[control] = false;
done[control] = false;
pb[control] = prb;
res[control] = QByteArray();
return control;
}
void FileDownloader::updateProgress(qint64 cur, qint64 tot)
{
QNetworkReply *nr = qobject_cast<QNetworkReply *>(sender());
if (nr && pb[nr])
{
pb[nr]->setMaximum(tot);
pb[nr]->setValue(cur);
}
}
void FileDownloader::operationFinished()
{
QNetworkReply *nr = qobject_cast<QNetworkReply *>(sender());
if (nr)
{
nr->close();
done[nr] = true;
}
}
void FileDownloader::errorOccured(QNetworkReply::NetworkError error)
{
QNetworkReply *nr = qobject_cast<QNetworkReply *>(sender());
if (nr && error)
{
err[nr] = true;
nr->abort();
done[nr] = true;
}
}
void FileDownloader::readData()
{
QNetworkReply *nr = qobject_cast<QNetworkReply *>(sender());
if (nr)
res[nr] += nr->readAll();
}
QByteArray FileDownloader::popResult(QNetworkReply *nr)
{
if (res.find(nr) == res.end())
(new NoResultException)->raise();
QByteArray ret = res[nr];
res.remove(nr);
err.remove(nr);
done.remove(nr);
pb.remove(nr);
timers2.remove(timers[nr]);
delete timers[nr];
timers.remove(nr);
delete nr;
return ret;
}
void FileDownloader::timedOut()
{
QTimer *tr = qobject_cast<QTimer *>(sender());
QNetworkReply *nr = timers2[tr];
if (nr)
{
err[nr] = true;
nr->abort();
done[nr] = true;
}
}