MauiKit File Browsing

downloader.h
1#pragma once
2
3#include <QObject>
4#include <QString>
5#include <QMap>
6
7#include "filebrowsing_export.h"
8
10class QNetworkReply;
11class QFile;
12
13namespace FMH
14{
15 /**
16 * @brief The Downloader class
17 * This is a quick helper to download remote content and save it as local files.
18 *
19 * @note Allows to make get request using optional headers.
20 *
21 * @code
22 * #include <QCoreApplication>
23 * #include <QDebug>
24 * #include <QUrl>
25 * #include <QObject>
26 * #include <iostream>
27 *
28 * #include <MauiKit4/FileBrowsing/downloader.h>
29 * #include <MauiKit4/FileBrowsing/fmstatic.h>
30 *
31 * int main(int argc, char *argv[])
32 * {
33 * QCoreApplication a(argc, argv);
34 *
35 * QUrl url = u"https://mauikit.org/wp-content/uploads/2021/08/index-1.png"_qs; //The remote file image URL to download.
36 * QString fileName = "/IndexAppDemo.png"; //The new name of the image to save it locally.
37 * QUrl savePath = FMStatic::DownloadsPath+fileName; //The local downloads path where to save the image.
38 * FMH::Downloader downloader;
39 *
40 * QObject::connect(&downloader, &FMH::Downloader::progress, [=](int percent) {
41 * qDebug() << "Downloading " << percent << "%"; });
42 *
43 * QObject::connect(&downloader, &FMH::Downloader::done, [&a]() {
44 * qDebug() << "Download finished";
45 * a.exit(); });
46 *
47 * QObject::connect(&downloader, &FMH::Downloader::fileSaved, [=](QString path) {
48 * qDebug() << "Downloaded file has been saved as " << path; });
49 *
50 * downloader.downloadFile(url, savePath);
51 *
52 * return a.exec();
53 * }
54 * @endcode
55 */
56 class FILEBROWSING_EXPORT Downloader : public QObject
57 {
58 Q_OBJECT
59
60 public:
61 Downloader(QObject *parent = nullptr);
62
63 virtual ~Downloader();
64
65 /**
66 * @brief Given a source URL to a file it downloads it to a given local destination
67 * @param source the remote file URL
68 * @param destination the local file URL destination with a name an suffix extension type
69 */
70 void downloadFile(const QUrl &source, const QUrl &destination);
71
72 /**
73 * @brief Given a URL make a get request and once the reply is ready emits a signal with the retrieved array data.
74 * @note Usually this is used to retrieved structured information from a web API, such a a JSON or a XML file, to later be parsed.
75 * @see dataReady
76 * @param fileURL the end point URL to make the request
77 * @param headers the optional set of HTTP request headers structured as a QMap of a key value pair of plain strings.
78 */
79 void getArray(const QUrl &fileURL, const QMap<QString, QString> &headers = {});
80
81 /**
82 * @brief Force to stop the process of the data request or the downloading of the file.
83 */
84 void stop();
85
86 /**
87 * @brief Whether the downloading or current request is still in progress.
88 */
89 bool isRunning() const;
90
91 /**
92 * @brief Whether the process has finished successfully.
93 * @see aborted
94 */
95 bool isFinished() const;
96
97 private:
98 QNetworkAccessManager *manager;
99 QNetworkReply *reply;
100 QFile *file;
101
102 QByteArray *array;
103
104 bool m_saveToFile = false;
105 void setConnections();
106
107 Q_SIGNALS:
108
109 /**
110 * @brief Emitted while the process is ongoing
111 * @param percent the process value from 0 to 100
112 */
113 void progress(int percent);
114
115 /**
116 * @brief Emitted when the downloading has finished.
117 */
119
120 /**
121 * @brief Emitted when the download or data request has been aborted manually by calling the `stop` method.
122 */
123 void aborted();
124
125 /**
126 * @brief Emitted after the downloading has finished and the file has been saved successfully.
127 * @param path the location path of the new saved file
128 */
129 void fileSaved(QString path);
130
131 /**
132 * @brief Emitted when there is a warning message, for example when an error has occurred during the process or at the end.
133 */
134 void warning(QString warning);
135
136 /**
137 * @brief Emitted when the data is ready and can be parsed.
138 * @param array the array raw data
139 */
141
142 /**
143 * @brief Emitted when the process has been finished. Emitted as the `downloadReady` signal.
144 */
145 void done();
146
147 private Q_SLOTS:
148 /**
149 * @private
150 */
151 void onDownloadProgress(qint64 bytesRead, qint64 bytesTotal);
152
153 /**
154 * @private
155 */
156 void onReadyRead();
157
158 /**
159 * @private
160 */
161 void onReplyFinished();
162 };
163}
The Downloader class This is a quick helper to download remote content and save it as local files.
Definition downloader.h:57
void warning(QString warning)
Emitted when there is a warning message, for example when an error has occurred during the process or...
void progress(int percent)
Emitted while the process is ongoing.
void done()
Emitted when the process has been finished.
void aborted()
Emitted when the download or data request has been aborted manually by calling the stop method.
void downloadReady()
Emitted when the downloading has finished.
void fileSaved(QString path)
Emitted after the downloading has finished and the file has been saved successfully.
void dataReady(QByteArray array)
Emitted when the data is ready and can be parsed.
void stop(Ekos::AlignState mode)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:53:26 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.