Kstars

filedownloader.h
1/*
2 SPDX-FileCopyrightText: 2016 Jasem Mutlaq <mutlaqja@ikarustech.com>
3
4 Adapted from https://wiki.qt.io/Download_Data_from_URL
5
6 SPDX-License-Identifier: GPL-2.0-or-later
7*/
8
9#pragma once
10
11#include <QByteArray>
12#include <QFile>
13#include <QNetworkAccessManager>
14#include <QNetworkRequest>
15#include <QNetworkReply>
16#include <QObject>
17#include <QTemporaryFile>
18
19#include <functional>
20
21class QProgressDialog;
22
23class FileDownloader : public QObject
24{
26 public:
27 explicit FileDownloader(QObject *parent = nullptr);
28 ~FileDownloader() override = default;
29
30 void get(const QUrl &fileUrl);
31 void post(const QUrl &fileUrl, QByteArray &data);
32 void post(const QUrl &fileUrl, QHttpMultiPart *parts);
33
34 QByteArray downloadedData() const;
35
36 QUrl getDownloadedFileURL() const;
37 bool setDownloadedFileURL(const QUrl &DownloadedFile);
38
39 void setProgressDialogEnabled(bool ShowProgressDialog, const QString &textTitle = QString(),
40 const QString &textLabel = QString());
41
42 // Callbacks to verify data before being accepted
43 void registerDataVerification(std::function<bool(const QByteArray &data)> verifyFunc) { m_verifyData = verifyFunc; }
44 void registerFileVerification(std::function<bool(const QString &filename)> verifyFile){ m_verifyFile = verifyFile; }
45
46 signals:
47 void downloaded();
48 void canceled();
49 void error(const QString &errorString);
50 void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
51
52 private slots:
53 void dataFinished(QNetworkReply *pReply);
54 void dataReady();
55 void slotError();
56 void setDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
57
58 private:
59 QNetworkAccessManager m_WebCtrl;
60 QByteArray m_DownloadedData;
61
62 // Downloaded file
63 QUrl m_DownloadedFileURL;
64
65 // Temporary file used until download is successful
66 QTemporaryFile m_downloadTemporaryFile;
67
68 // Network reply
69 QNetworkReply *m_Reply { nullptr };
70
71 // Optional Progress dialog
72 bool m_ShowProgressDialog { false };
73
74#ifndef KSTARS_LITE
75 QProgressDialog *progressDialog { nullptr };
76#endif
77 bool isCancelled { false };
78 QString label;
79 QString title;
80
81 std::function<bool(const QByteArray &data)> m_verifyData;
82 std::function<bool(const QString &filename)> m_verifyFile;
83};
Q_OBJECTQ_OBJECT
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:39 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.