Repository navigation
Expand file tree
/
Copy pathdownloadmanager.cpp
More file actions
86 lines (72 loc) · 2.42 KB
/
Copy pathdownloadmanager.cpp
File metadata and controls
86 lines (72 loc) · 2.42 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
#include "downloadmanager.h"
#include <QByteArray>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
DownloadManager::DownloadManager(QObject *parent) : QObject(parent)
{
connect(&manager, SIGNAL(finished(QNetworkReply*)),SLOT(downloadFinished(QNetworkReply*)));
}
void DownloadManager::doDownload(const QUrl &url)
{
QNetworkRequest request(url);
request.setRawHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.12) Gecko/20080214 Firefox/2.0.0.12");
QNetworkReply *reply = manager.get(request);
#if QT_CONFIG(ssl)
connect(reply, SIGNAL(sslErrors(QList<QSslError>)),this,SLOT(sslErrors(QList<QSslError>)));
#endif
}
QString DownloadManager::saveFileName(const QUrl &url)
{
QString path = url.path();
QString basename = QFileInfo(path).fileName();
return basename;
}
bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data)
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
qDebug() << "Could not open %s for writing: %s\n" << qPrintable(filename) << qPrintable(file.errorString());
return false;
}
file.write(data->readAll());
file.close();
return true;
}
bool DownloadManager::isHttpRedirect(QNetworkReply *reply)
{
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
return statusCode == 301 || statusCode == 302 || statusCode == 303
|| statusCode == 305 || statusCode == 307 || statusCode == 308;
}
void DownloadManager::execute(QString lsLink)
{
QUrl url = QUrl::fromEncoded(lsLink.toLocal8Bit());
doDownload(url);
}
void DownloadManager::sslErrors(const QList<QSslError> &sslErrors)
{
#if QT_CONFIG(ssl)
for (const QSslError &error : sslErrors)
qDebug() << "SSL error: %s\n" << qPrintable(error.errorString());
#else
Q_UNUSED(sslErrors);
#endif
}
void DownloadManager::downloadFinished(QNetworkReply *reply)
{
QUrl url = reply->url();
if (reply->error()) {
qDebug() << "Failed" << " " << reply->errorString();
} else {
if (isHttpRedirect(reply)) {
qDebug() << "Redirected";
} else {
QString filename = saveFileName(url);
if (saveToDisk(filename, reply)) {
qDebug() << "File downloaded";
}
}
}
reply->deleteLater();
}