From f0d67a082a0f28d7b4380d3b6a88f1041bfae8bb Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 14 Feb 2020 11:44:38 +0200 Subject: staging: add filterlist parallel downloader --- staging/filterlist/downloadmanager.cpp | 53 ++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 staging/filterlist/downloadmanager.cpp (limited to 'staging/filterlist/downloadmanager.cpp') diff --git a/staging/filterlist/downloadmanager.cpp b/staging/filterlist/downloadmanager.cpp new file mode 100644 index 0000000..bbc9287 --- /dev/null +++ b/staging/filterlist/downloadmanager.cpp @@ -0,0 +1,53 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "downloadmanager.h" + +QString saveFileName(const QUrl &url, const QString &filename) +{ + if(!filename.isEmpty()) + return filename; + + QString path = url.path(); + QString basename = QFileInfo(path).fileName(); + + if(basename.isEmpty()) + basename = "download"; + + return basename; +} + +DownloadManager::DownloadManager(QObject *parent) + : QObject(parent) +{ +} + +QNetworkReply *DownloadManager::download(const QUrl &url, const QString &filename) +{ + auto *file = new QFile(saveFileName(url, filename), this); + if(!file->open(QIODevice::WriteOnly)) { + delete file; + return nullptr; + } + + QNetworkRequest request(url); + QNetworkReply *reply = manager.get(request); + + connect(reply, &QNetworkReply::readyRead, this, [reply, file]() { + file->write(reply->readAll()); + }); + + connect(reply, &QNetworkReply::finished, this, [reply, file]() { + file->write(reply->readAll()); + file->close(); + file->deleteLater(); + reply->deleteLater(); + }); + + return reply; +} -- cgit v1.2.1