17#include "moc_qgsfiledownloader.cpp"
24#include <QNetworkAccessManager>
25#include <QNetworkRequest>
26#include <QNetworkReply>
33 , mDownloadCanceled( false )
34 , mHttpMethod( httpMethod )
37 if ( !outputFileName.isEmpty() )
38 mFile.setFileName( outputFileName );
50 mReply->deleteLater();
58 QNetworkRequest request( mUrl );
59 request.setAttribute( QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::RedirectPolicy::NoLessSafeRedirectPolicy );
61 if ( !mAuthCfg.isEmpty() )
68 disconnect( mReply, &QNetworkReply::readyRead,
this, &QgsFileDownloader::onReadyRead );
69 disconnect( mReply, &QNetworkReply::finished,
this, &QgsFileDownloader::onFinished );
70 disconnect( mReply, &QNetworkReply::downloadProgress,
this, &QgsFileDownloader::onDownloadProgress );
72 mReply->deleteLater();
75 switch ( mHttpMethod )
79 mReply = nam->get( request );
84 mReply = nam->post( request, mData );
89 if ( !mAuthCfg.isEmpty() )
94 connect( mReply, &QNetworkReply::readyRead,
this, &QgsFileDownloader::onReadyRead );
95 connect( mReply, &QNetworkReply::finished,
this, &QgsFileDownloader::onFinished );
96 connect( mReply, &QNetworkReply::downloadProgress,
this, &QgsFileDownloader::onDownloadProgress );
99 connect( nam, &QgsNetworkAccessManager::sslErrors,
this, &QgsFileDownloader::onSslErrors, Qt::UniqueConnection );
105 mDownloadCanceled =
true;
110void QgsFileDownloader::onRequestTimedOut( QNetworkReply *reply )
112 if ( reply == mReply )
113 error( tr(
"Network request %1 timed out" ).arg( mUrl.toString() ) );
117void QgsFileDownloader::onSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors )
119 if ( reply == mReply )
121 QStringList errorMessages;
122 errorMessages.reserve( errors.size() + 1 );
123 errorMessages << QStringLiteral(
"SSL Errors: " );
125 for (
const QSslError &error : errors )
126 errorMessages << error.errorString();
128 error( errorMessages );
134void QgsFileDownloader::error(
const QStringList &errorMessages )
136 for (
const QString &error : errorMessages )
144void QgsFileDownloader::error(
const QString &errorMessage )
146 error( QStringList() << errorMessage );
149void QgsFileDownloader::onReadyRead()
152 if ( mFile.fileName().isEmpty() )
154 error( tr(
"No output filename specified" ) );
157 else if ( ! mFile.isOpen() && ! mFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
159 error( tr(
"Cannot open output file: %1" ).arg( mFile.fileName() ) );
164 const QByteArray data = mReply->readAll();
169void QgsFileDownloader::onFinished()
172 if ( ! mErrors.isEmpty() || mDownloadCanceled )
174 if ( mFile.isOpen() )
176 if ( mFile.exists() )
182 if ( mFile.isOpen() )
188 if ( mReply->error() )
191 error( tr(
"Download failed: %1" ).arg( mReply->errorString() ) );
203void QgsFileDownloader::onDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
205 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)