25#include <QNetworkAccessManager>
26#include <QNetworkReply>
27#include <QNetworkRequest>
29#include "moc_qgsfiledownloader.cpp"
37 , mDownloadCanceled( false )
38 , mHttpMethod( httpMethod )
41 if ( !outputFileName.isEmpty() )
42 mFile.setFileName( outputFileName );
54 mReply->deleteLater();
62 QNetworkRequest request( mUrl );
63 request.setAttribute( QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::RedirectPolicy::NoLessSafeRedirectPolicy );
65 if ( !mAuthCfg.isEmpty() )
72 disconnect( mReply, &QNetworkReply::readyRead,
this, &QgsFileDownloader::onReadyRead );
73 disconnect( mReply, &QNetworkReply::finished,
this, &QgsFileDownloader::onFinished );
74 disconnect( mReply, &QNetworkReply::downloadProgress,
this, &QgsFileDownloader::onDownloadProgress );
76 mReply->deleteLater();
79 switch ( mHttpMethod )
83 mReply = nam->get( request );
88 mReply = nam->post( request, mData );
100 if ( !mAuthCfg.isEmpty() )
105 connect( mReply, &QNetworkReply::readyRead,
this, &QgsFileDownloader::onReadyRead );
106 connect( mReply, &QNetworkReply::finished,
this, &QgsFileDownloader::onFinished );
107 connect( mReply, &QNetworkReply::downloadProgress,
this, &QgsFileDownloader::onDownloadProgress );
110 connect( nam, &QgsNetworkAccessManager::sslErrors,
this, &QgsFileDownloader::onSslErrors, Qt::UniqueConnection );
116 mDownloadCanceled =
true;
121void QgsFileDownloader::onRequestTimedOut( QNetworkReply *reply )
123 if ( reply == mReply )
124 error( tr(
"Network request %1 timed out" ).arg( mUrl.toString() ) );
128void QgsFileDownloader::onSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors )
130 if ( reply == mReply )
132 QStringList errorMessages;
133 errorMessages.reserve( errors.size() + 1 );
134 errorMessages << QStringLiteral(
"SSL Errors: " );
136 for (
const QSslError &error : errors )
137 errorMessages << error.errorString();
139 error( errorMessages );
145void QgsFileDownloader::error(
const QStringList &errorMessages )
147 for (
const QString &error : errorMessages )
155void QgsFileDownloader::error(
const QString &errorMessage )
157 error( QStringList() << errorMessage );
160void QgsFileDownloader::onReadyRead()
163 if ( mFile.fileName().isEmpty() )
165 error( tr(
"No output filename specified" ) );
168 else if ( ! mFile.isOpen() && ! mFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
170 error( tr(
"Cannot open output file: %1" ).arg( mFile.fileName() ) );
175 const QByteArray data = mReply->readAll();
180void QgsFileDownloader::onFinished()
183 if ( ! mErrors.isEmpty() || mDownloadCanceled )
185 if ( mFile.isOpen() )
187 if ( mFile.exists() )
193 if ( mFile.isOpen() )
199 if ( mReply->error() )
202 error( tr(
"Download failed: %1" ).arg( mReply->errorString() ) );
214void QgsFileDownloader::onDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
216 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
QNetworkAccessManager with additional QGIS specific logic.
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.
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
#define QgsDebugError(str)
#define QgsSetRequestInitiatorClass(request, _class)