]> git.sur5r.net Git - minitube/blob - lib/updater/src/impl/downloader.cpp
New upstream version 3.5
[minitube] / lib / updater / src / impl / downloader.cpp
1 #include "downloader.h"
2
3 #include <QDesktopServices>
4
5 namespace updater {
6
7 void Downloader::download(const QUrl &url) {
8     const QString tempDir = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
9     QString filename = tempDir + "/" +
10                        QByteArray::number(QRandomGenerator::global()->generate()).toHex() +
11                        url.fileName();
12     file.setFileName(filename);
13
14     qDebug() << "Downloading" << url << "to" << filename;
15     HttpRequest req;
16     req.url = url;
17     reply = Http::instance().networkReply(req);
18
19     connect(reply, &QNetworkReply::readyRead, this, [this] {
20         if (!reply) return;
21
22         if (!file.isOpen()) {
23             if (!file.open(QIODevice::ReadWrite)) {
24                 emit error(QString("Error opening file: %1").arg(file.errorString()));
25                 reply->disconnect();
26                 reply->abort();
27                 reply->deleteLater();
28                 reply = nullptr;
29                 return;
30             }
31         }
32         if (-1 == file.write(reply->readAll())) {
33             emit error(file.errorString());
34             reply->disconnect();
35             reply->abort();
36             reply->deleteLater();
37             reply = nullptr;
38         }
39     });
40
41     connect(reply,
42             static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(
43                     &QNetworkReply::error),
44             this, [this] {
45                 emit error(reply->errorString());
46                 reply->disconnect();
47                 reply->abort();
48                 reply->deleteLater();
49                 reply = nullptr;
50             });
51
52     connect(reply, &QNetworkReply::downloadProgress, this,
53             [this](qint64 bytesReceived, qint64 bytesTotal) {
54                 if (bytesTotal <= 0) return;
55                 int percent = bytesReceived * 100 / bytesTotal;
56                 emit progress(percent);
57             });
58
59     connect(reply, &QNetworkReply::finished, this, [this] {
60         if (!reply) return;
61         int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
62         if (status != 200) {
63             QString message =
64                     reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
65             if (message.isEmpty()) message = reply->errorString();
66             emit error(message);
67             return;
68         }
69
70         file.close();
71
72         reply->deleteLater();
73         reply = nullptr;
74
75         emit fileReady(file.fileName());
76     });
77 }
78
79 void Downloader::stop() {
80     if (!reply) return;
81     reply->disconnect();
82     reply->abort();
83     reply->deleteLater();
84     reply = nullptr;
85     emit error("Canceled");
86 }
87
88 } // namespace updater