Branch data Line data Source code
1 : : /*************************************************************************** 2 : : qgsfiledownloader.h 3 : : -------------------------------------- 4 : : Date : November 2016 5 : : Copyright : (C) 2016 by Alessandro Pasotti 6 : : Email : apasotti at boundlessgeo dot com 7 : : *************************************************************************** 8 : : * * 9 : : * This program is free software; you can redistribute it and/or modify * 10 : : * it under the terms of the GNU General Public License as published by * 11 : : * the Free Software Foundation; either version 2 of the License, or * 12 : : * (at your option) any later version. * 13 : : * * 14 : : ***************************************************************************/ 15 : : 16 : : #ifndef QGSFILEDOWNLOADER_H 17 : : #define QGSFILEDOWNLOADER_H 18 : : 19 : : #include <QObject> 20 : : #include <QFile> 21 : : #include <QNetworkReply> 22 : : #include <QUrl> 23 : : 24 : : #include "qgis_core.h" 25 : : 26 : : #ifndef QT_NO_SSL 27 : : #include <QSslError> 28 : : #endif 29 : : 30 : : /** 31 : : * \ingroup core 32 : : * \brief QgsFileDownloader is a utility class for downloading files. 33 : : * 34 : : * To use this class, it is necessary to pass the URL and an output file name as 35 : : * arguments to the constructor, the download will start immediately. 36 : : * 37 : : * The download is asynchronous. 38 : : * 39 : : * The object will destroy itself when the request completes, errors or is canceled. 40 : : * An optional authentication configuration can be specified. 41 : : * 42 : : * \note This class was part of the GUI library from QGIS 2.18.1 until QGIS 3.0 43 : : * \since QGIS 3.0 44 : : */ 45 : : class CORE_EXPORT QgsFileDownloader : public QObject 46 : : { 47 : 0 : Q_OBJECT 48 : : public: 49 : : 50 : : /** 51 : : * QgsFileDownloader 52 : : * \param url the download URL 53 : : * \param outputFileName file name where the downloaded content will be stored 54 : : * \param authcfg optionally apply this authentication configuration 55 : : * \param delayStart if TRUE, the download will not be commenced immediately and must 56 : : * be triggered by a later call to startDownload(). This can be useful if connections need 57 : : * to be made to the downloader and there's a chance the download will emit 58 : : * signals before these connections have been made. 59 : : */ 60 : : QgsFileDownloader( const QUrl &url, const QString &outputFileName, const QString &authcfg = QString(), bool delayStart = false ); 61 : : 62 : : signals: 63 : : //! Emitted when the download has completed successfully 64 : : void downloadCompleted( const QUrl &url ); 65 : : //! Emitted always when the downloader exits 66 : : void downloadExited(); 67 : : 68 : : /** 69 : : * Emitted when the download was canceled by the user. 70 : : * \see cancelDownload() 71 : : */ 72 : : void downloadCanceled(); 73 : : 74 : : //! Emitted when an error makes the download fail 75 : : void downloadError( QStringList errorMessages ); 76 : : //! Emitted when data are ready to be processed 77 : : void downloadProgress( qint64 bytesReceived, qint64 bytesTotal ); 78 : : 79 : : public slots: 80 : : 81 : : /** 82 : : * Call to abort the download and delete this object after the cancellation 83 : : * has been processed. 84 : : * \see downloadCanceled() 85 : : */ 86 : : void cancelDownload(); 87 : : 88 : : //! Called to start the download 89 : : void startDownload(); 90 : : 91 : : private slots: 92 : : //! Called when the network reply data are ready 93 : : void onReadyRead(); 94 : : //! Called when the network reply has finished 95 : : void onFinished(); 96 : : //! Called on data ready to be processed 97 : : void onDownloadProgress( qint64 bytesReceived, qint64 bytesTotal ); 98 : : //! Called when a network request times out 99 : : void onRequestTimedOut( QNetworkReply *reply ); 100 : : 101 : : #ifndef QT_NO_SSL 102 : : 103 : : /** 104 : : * Called on SSL network Errors 105 : : * \param reply 106 : : * \param errors 107 : : */ 108 : : void onSslErrors( QNetworkReply *reply, const QList<QSslError> &errors ); 109 : : #endif 110 : : 111 : : protected: 112 : : ~QgsFileDownloader() override; 113 : : 114 : : private: 115 : : 116 : : /** 117 : : * Abort current request and show an error if the instance has GUI 118 : : * notifications enabled. 119 : : */ 120 : : void error( const QStringList &errorMessages ); 121 : : void error( const QString &errorMessage ); 122 : : QUrl mUrl; 123 : : QNetworkReply *mReply = nullptr; 124 : : QFile mFile; 125 : : bool mDownloadCanceled; 126 : : QStringList mErrors; 127 : : QString mAuthCfg; 128 : : }; 129 : : 130 : : #endif // QGSFILEDOWNLOADER_H