Warning, file /maui/mauikit-filebrowsing/src/code/downloader.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 #pragma once 0002 0003 #include <QObject> 0004 #include <QString> 0005 #include <QMap> 0006 0007 #include "filebrowsing_export.h" 0008 0009 class QNetworkAccessManager; 0010 class QNetworkReply; 0011 class QFile; 0012 0013 namespace FMH 0014 { 0015 /** 0016 * @brief The Downloader class 0017 * This is a quick helper to download remote content and save it as local files. 0018 * 0019 * @note Allows to make get request using optional headers. 0020 * 0021 * @code 0022 * #include <QCoreApplication> 0023 * #include <QDebug> 0024 * #include <QUrl> 0025 * #include <QObject> 0026 * #include <iostream> 0027 * 0028 * #include <MauiKit4/FileBrowsing/downloader.h> 0029 * #include <MauiKit4/FileBrowsing/fmstatic.h> 0030 * 0031 * int main(int argc, char *argv[]) 0032 * { 0033 * QCoreApplication a(argc, argv); 0034 * 0035 * QUrl url = u"https://mauikit.org/wp-content/uploads/2021/08/index-1.png"_qs; //The remote file image URL to download. 0036 * QString fileName = "/IndexAppDemo.png"; //The new name of the image to save it locally. 0037 * QUrl savePath = FMStatic::DownloadsPath+fileName; //The local downloads path where to save the image. 0038 * FMH::Downloader downloader; 0039 * 0040 * QObject::connect(&downloader, &FMH::Downloader::progress, [=](int percent) { 0041 * qDebug() << "Downloading " << percent << "%"; }); 0042 * 0043 * QObject::connect(&downloader, &FMH::Downloader::done, [&a]() { 0044 * qDebug() << "Download finished"; 0045 * a.exit(); }); 0046 * 0047 * QObject::connect(&downloader, &FMH::Downloader::fileSaved, [=](QString path) { 0048 * qDebug() << "Downloaded file has been saved as " << path; }); 0049 * 0050 * downloader.downloadFile(url, savePath); 0051 * 0052 * return a.exec(); 0053 * } 0054 * @endcode 0055 */ 0056 class FILEBROWSING_EXPORT Downloader : public QObject 0057 { 0058 Q_OBJECT 0059 0060 public: 0061 Downloader(QObject *parent = nullptr); 0062 0063 virtual ~Downloader(); 0064 0065 /** 0066 * @brief Given a source URL to a file it downloads it to a given local destination 0067 * @param source the remote file URL 0068 * @param destination the local file URL destination with a name an suffix extension type 0069 */ 0070 void downloadFile(const QUrl &source, const QUrl &destination); 0071 0072 /** 0073 * @brief Given a URL make a get request and once the reply is ready emits a signal with the retrieved array data. 0074 * @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. 0075 * @see dataReady 0076 * @param fileURL the end point URL to make the request 0077 * @param headers the optional set of HTTP request headers structured as a QMap of a key value pair of plain strings. 0078 */ 0079 void getArray(const QUrl &fileURL, const QMap<QString, QString> &headers = {}); 0080 0081 /** 0082 * @brief Force to stop the process of the data request or the downloading of the file. 0083 */ 0084 void stop(); 0085 0086 /** 0087 * @brief Whether the downloading or current request is still in progress. 0088 */ 0089 bool isRunning() const; 0090 0091 /** 0092 * @brief Whether the process has finished successfully. 0093 * @see aborted 0094 */ 0095 bool isFinished() const; 0096 0097 private: 0098 QNetworkAccessManager *manager; 0099 QNetworkReply *reply; 0100 QFile *file; 0101 0102 QByteArray *array; 0103 0104 bool m_saveToFile = false; 0105 void setConnections(); 0106 0107 Q_SIGNALS: 0108 0109 /** 0110 * @brief Emitted while the process is ongoing 0111 * @param percent the process value from 0 to 100 0112 */ 0113 void progress(int percent); 0114 0115 /** 0116 * @brief Emitted when the downloading has finished. 0117 */ 0118 void downloadReady(); 0119 0120 /** 0121 * @brief Emitted when the download or data request has been aborted manually by calling the `stop` method. 0122 */ 0123 void aborted(); 0124 0125 /** 0126 * @brief Emitted after the downloading has finished and the file has been saved successfully. 0127 * @param path the location path of the new saved file 0128 */ 0129 void fileSaved(QString path); 0130 0131 /** 0132 * @brief Emitted when there is a warning message, for example when an error has occurred during the process or at the end. 0133 */ 0134 void warning(QString warning); 0135 0136 /** 0137 * @brief Emitted when the data is ready and can be parsed. 0138 * @param array the array raw data 0139 */ 0140 void dataReady(QByteArray array); 0141 0142 /** 0143 * @brief Emitted when the process has been finished. Emitted as the `downloadReady` signal. 0144 */ 0145 void done(); 0146 0147 private Q_SLOTS: 0148 /** 0149 * @private 0150 */ 0151 void onDownloadProgress(qint64 bytesRead, qint64 bytesTotal); 0152 0153 /** 0154 * @private 0155 */ 0156 void onReadyRead(); 0157 0158 /** 0159 * @private 0160 */ 0161 void onReplyFinished(); 0162 }; 0163 }