aboutsummaryrefslogtreecommitdiff
path: root/staging/filterlist/downloadmanager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'staging/filterlist/downloadmanager.cpp')
-rw-r--r--staging/filterlist/downloadmanager.cpp53
1 files changed, 53 insertions, 0 deletions
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;
+}