/* * 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; }