21 #include <QNetworkAccessManager>
22 #include <QNetworkRequest>
23 #include <QNetworkReply>
30 , mDownloadCanceled( false )
31 , mHttpMethod( httpMethod )
34 mFile.setFileName( outputFileName );
46 mReply->deleteLater();
54 QNetworkRequest request( mUrl );
56 if ( !mAuthCfg.isEmpty() )
63 disconnect( mReply, &QNetworkReply::readyRead,
this, &QgsFileDownloader::onReadyRead );
64 disconnect( mReply, &QNetworkReply::finished,
this, &QgsFileDownloader::onFinished );
65 disconnect( mReply, &QNetworkReply::downloadProgress,
this, &QgsFileDownloader::onDownloadProgress );
67 mReply->deleteLater();
70 switch ( mHttpMethod )
74 mReply = nam->get( request );
79 mReply = nam->post( request, mData );
84 if ( !mAuthCfg.isEmpty() )
89 connect( mReply, &QNetworkReply::readyRead,
this, &QgsFileDownloader::onReadyRead );
90 connect( mReply, &QNetworkReply::finished,
this, &QgsFileDownloader::onFinished );
91 connect( mReply, &QNetworkReply::downloadProgress,
this, &QgsFileDownloader::onDownloadProgress );
94 connect( nam, &QgsNetworkAccessManager::sslErrors,
this, &QgsFileDownloader::onSslErrors, Qt::UniqueConnection );
100 mDownloadCanceled =
true;
105 void QgsFileDownloader::onRequestTimedOut( QNetworkReply *reply )
107 if ( reply == mReply )
108 error( tr(
"Network request %1 timed out" ).arg( mUrl.toString() ) );
112 void QgsFileDownloader::onSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors )
114 if ( reply == mReply )
116 QStringList errorMessages;
117 errorMessages.reserve( errors.size() + 1 );
118 errorMessages << QStringLiteral(
"SSL Errors: " );
120 for (
const QSslError &error : errors )
121 errorMessages << error.errorString();
123 error( errorMessages );
129 void QgsFileDownloader::error(
const QStringList &errorMessages )
131 for (
const QString &error : errorMessages )
139 void QgsFileDownloader::error(
const QString &errorMessage )
141 error( QStringList() << errorMessage );
144 void QgsFileDownloader::onReadyRead()
147 if ( mFile.fileName().isEmpty() )
149 error( tr(
"No output filename specified" ) );
152 else if ( ! mFile.isOpen() && ! mFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
154 error( tr(
"Cannot open output file: %1" ).arg( mFile.fileName() ) );
159 const QByteArray data = mReply->readAll();
164 void QgsFileDownloader::onFinished()
167 if ( ! mErrors.isEmpty() || mDownloadCanceled )
169 if ( mFile.isOpen() )
171 if ( mFile.exists() )
177 if ( mFile.isOpen() )
184 const QVariant redirectionTarget = mReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
185 if ( mReply->error() )
188 error( tr(
"Download failed: %1" ).arg( mReply->errorString() ) );
190 else if ( !redirectionTarget.isNull() )
192 const QUrl newUrl = mUrl.resolved( redirectionTarget.toUrl() );
194 mReply->deleteLater();
195 if ( !mFile.open( QIODevice::WriteOnly ) )
198 error( tr(
"Cannot open output file: %1" ).arg( mFile.fileName() ) );
218 void QgsFileDownloader::onDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
220 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)