22#include <QNetworkAccessManager> 
   23#include <QNetworkRequest> 
   24#include <QNetworkReply> 
   31  , mDownloadCanceled( false )
 
   32  , mHttpMethod( httpMethod )
 
   35  mFile.setFileName( outputFileName );
 
   47    mReply->deleteLater();
 
   55  QNetworkRequest request( mUrl );
 
   57  if ( !mAuthCfg.isEmpty() )
 
   64    disconnect( mReply, &QNetworkReply::readyRead, 
this, &QgsFileDownloader::onReadyRead );
 
   65    disconnect( mReply, &QNetworkReply::finished, 
this, &QgsFileDownloader::onFinished );
 
   66    disconnect( mReply, &QNetworkReply::downloadProgress, 
this, &QgsFileDownloader::onDownloadProgress );
 
   68    mReply->deleteLater();
 
   71  switch ( mHttpMethod )
 
   75      mReply = nam->get( request );
 
   80      mReply = nam->post( request, mData );
 
   85  if ( !mAuthCfg.isEmpty() )
 
   90  connect( mReply, &QNetworkReply::readyRead, 
this, &QgsFileDownloader::onReadyRead );
 
   91  connect( mReply, &QNetworkReply::finished, 
this, &QgsFileDownloader::onFinished );
 
   92  connect( mReply, &QNetworkReply::downloadProgress, 
this, &QgsFileDownloader::onDownloadProgress );
 
   95  connect( nam, &QgsNetworkAccessManager::sslErrors, 
this, &QgsFileDownloader::onSslErrors, Qt::UniqueConnection );
 
  101  mDownloadCanceled = 
true;
 
  106void QgsFileDownloader::onRequestTimedOut( QNetworkReply *reply )
 
  108  if ( reply == mReply )
 
  109    error( tr( 
"Network request %1 timed out" ).arg( mUrl.toString() ) );
 
  113void QgsFileDownloader::onSslErrors( QNetworkReply *reply, 
const QList<QSslError> &errors )
 
  115  if ( reply == mReply )
 
  117    QStringList errorMessages;
 
  118    errorMessages.reserve( errors.size() + 1 );
 
  119    errorMessages <<  QStringLiteral( 
"SSL Errors: " );
 
  121    for ( 
const QSslError &error : errors )
 
  122      errorMessages << error.errorString();
 
  124    error( errorMessages );
 
  130void QgsFileDownloader::error( 
const QStringList &errorMessages )
 
  132  for ( 
const QString &error : errorMessages )
 
  140void QgsFileDownloader::error( 
const QString &errorMessage )
 
  142  error( QStringList() << errorMessage );
 
  145void QgsFileDownloader::onReadyRead()
 
  148  if ( mFile.fileName().isEmpty() )
 
  150    error( tr( 
"No output filename specified" ) );
 
  153  else if ( ! mFile.isOpen() && ! mFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
 
  155    error( tr( 
"Cannot open output file: %1" ).arg( mFile.fileName() ) );
 
  160    const QByteArray data = mReply->readAll();
 
  165void QgsFileDownloader::onFinished()
 
  168  if ( ! mErrors.isEmpty() || mDownloadCanceled )
 
  170    if ( mFile.isOpen() )
 
  172    if ( mFile.exists() )
 
  178    if ( mFile.isOpen() )
 
  185    const QVariant redirectionTarget = mReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
 
  186    if ( mReply->error() )
 
  189      error( tr( 
"Download failed: %1" ).arg( mReply->errorString() ) );
 
  193      const QUrl newUrl = mUrl.resolved( redirectionTarget.toUrl() );
 
  195      mReply->deleteLater();
 
  196      if ( !mFile.open( QIODevice::WriteOnly ) )
 
  199        error( tr( 
"Cannot open output file: %1" ).arg( mFile.fileName() ) );
 
  219void QgsFileDownloader::onDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
 
  221  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.
 
static bool isNull(const QVariant &variant)
Returns true if the specified variant should be considered a NULL value.
 
#define QgsSetRequestInitiatorClass(request, _class)