23#include <QNetworkAccessManager>
24#include <QNetworkRequest>
25#include <QNetworkReply>
32 , mDownloadCanceled( false )
33 , mHttpMethod( httpMethod )
36 if ( !outputFileName.isEmpty() )
37 mFile.setFileName( outputFileName );
49 mReply->deleteLater();
57 QNetworkRequest request( mUrl );
58 request.setAttribute( QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::RedirectPolicy::NoLessSafeRedirectPolicy );
60 if ( !mAuthCfg.isEmpty() )
67 disconnect( mReply, &QNetworkReply::readyRead,
this, &QgsFileDownloader::onReadyRead );
68 disconnect( mReply, &QNetworkReply::finished,
this, &QgsFileDownloader::onFinished );
69 disconnect( mReply, &QNetworkReply::downloadProgress,
this, &QgsFileDownloader::onDownloadProgress );
71 mReply->deleteLater();
74 switch ( mHttpMethod )
78 mReply = nam->get( request );
83 mReply = nam->post( request, mData );
88 if ( !mAuthCfg.isEmpty() )
93 connect( mReply, &QNetworkReply::readyRead,
this, &QgsFileDownloader::onReadyRead );
94 connect( mReply, &QNetworkReply::finished,
this, &QgsFileDownloader::onFinished );
95 connect( mReply, &QNetworkReply::downloadProgress,
this, &QgsFileDownloader::onDownloadProgress );
98 connect( nam, &QgsNetworkAccessManager::sslErrors,
this, &QgsFileDownloader::onSslErrors, Qt::UniqueConnection );
104 mDownloadCanceled =
true;
109void QgsFileDownloader::onRequestTimedOut( QNetworkReply *reply )
111 if ( reply == mReply )
112 error( tr(
"Network request %1 timed out" ).arg( mUrl.toString() ) );
116void QgsFileDownloader::onSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors )
118 if ( reply == mReply )
120 QStringList errorMessages;
121 errorMessages.reserve( errors.size() + 1 );
122 errorMessages << QStringLiteral(
"SSL Errors: " );
124 for (
const QSslError &error : errors )
125 errorMessages << error.errorString();
127 error( errorMessages );
133void QgsFileDownloader::error(
const QStringList &errorMessages )
135 for (
const QString &error : errorMessages )
143void QgsFileDownloader::error(
const QString &errorMessage )
145 error( QStringList() << errorMessage );
148void QgsFileDownloader::onReadyRead()
151 if ( mFile.fileName().isEmpty() )
153 error( tr(
"No output filename specified" ) );
156 else if ( ! mFile.isOpen() && ! mFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
158 error( tr(
"Cannot open output file: %1" ).arg( mFile.fileName() ) );
163 const QByteArray data = mReply->readAll();
168void QgsFileDownloader::onFinished()
171 if ( ! mErrors.isEmpty() || mDownloadCanceled )
173 if ( mFile.isOpen() )
175 if ( mFile.exists() )
181 if ( mFile.isOpen() )
187 if ( mReply->error() )
190 error( tr(
"Download failed: %1" ).arg( mReply->errorString() ) );
202void QgsFileDownloader::onDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
204 if ( mDownloadCanceled )
HttpMethod
Different methods of HTTP requests.
static QgsAuthManager * authManager()
Returns the application's authentication manager instance.
bool updateNetworkRequest(QNetworkRequest &request, const QString &authcfg, const QString &dataprovider=QString())
Provider call to update a QNetworkRequest with an authentication config.
bool updateNetworkReply(QNetworkReply *reply, const QString &authcfg, const QString &dataprovider=QString())
Provider call to update a QNetworkReply with an authentication config (used to skip known SSL errors,...
void cancelDownload()
Call to abort the download and delete this object after the cancellation has been processed.
void downloadExited()
Emitted always when the downloader exits.
void downloadCanceled()
Emitted when the download was canceled by the user.
void downloadError(QStringList errorMessages)
Emitted when an error makes the download fail.
void startDownload()
Called to start the download.
QgsFileDownloader(const QUrl &url, const QString &outputFileName, const QString &authcfg=QString(), bool delayStart=false, Qgis::HttpMethod httpMethod=Qgis::HttpMethod::Get, const QByteArray &data=QByteArray())
QgsFileDownloader.
void downloadCompleted(const QUrl &url)
Emitted when the download has completed successfully.
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
Emitted when data are ready to be processed.
~QgsFileDownloader() override
network access manager for QGIS
static QgsNetworkAccessManager * instance(Qt::ConnectionType connectionType=Qt::BlockingQueuedConnection)
Returns a pointer to the active QgsNetworkAccessManager for the current thread.
void requestTimedOut(QgsNetworkRequestParameters request)
Emitted when a network request has timed out.
#define QgsSetRequestInitiatorClass(request, _class)