17#include "moc_qgsfiledownloader.cpp" 
   25#include <QNetworkAccessManager> 
   26#include <QNetworkRequest> 
   27#include <QNetworkReply> 
   34  , mDownloadCanceled( false )
 
   35  , mHttpMethod( httpMethod )
 
   38  if ( !outputFileName.isEmpty() )
 
   39    mFile.setFileName( outputFileName );
 
 
   51    mReply->deleteLater();
 
 
   59  QNetworkRequest request( mUrl );
 
   60  request.setAttribute( QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::RedirectPolicy::NoLessSafeRedirectPolicy );
 
   62  if ( !mAuthCfg.isEmpty() )
 
   69    disconnect( mReply, &QNetworkReply::readyRead, 
this, &QgsFileDownloader::onReadyRead );
 
   70    disconnect( mReply, &QNetworkReply::finished, 
this, &QgsFileDownloader::onFinished );
 
   71    disconnect( mReply, &QNetworkReply::downloadProgress, 
this, &QgsFileDownloader::onDownloadProgress );
 
   73    mReply->deleteLater();
 
   76  switch ( mHttpMethod )
 
   80      mReply = nam->get( request );
 
   85      mReply = nam->post( request, mData );
 
   97  if ( !mAuthCfg.isEmpty() )
 
  102  connect( mReply, &QNetworkReply::readyRead, 
this, &QgsFileDownloader::onReadyRead );
 
  103  connect( mReply, &QNetworkReply::finished, 
this, &QgsFileDownloader::onFinished );
 
  104  connect( mReply, &QNetworkReply::downloadProgress, 
this, &QgsFileDownloader::onDownloadProgress );
 
  107  connect( nam, &QgsNetworkAccessManager::sslErrors, 
this, &QgsFileDownloader::onSslErrors, Qt::UniqueConnection );
 
 
  113  mDownloadCanceled = 
true;
 
 
  118void QgsFileDownloader::onRequestTimedOut( QNetworkReply *reply )
 
  120  if ( reply == mReply )
 
  121    error( tr( 
"Network request %1 timed out" ).arg( mUrl.toString() ) );
 
  125void QgsFileDownloader::onSslErrors( QNetworkReply *reply, 
const QList<QSslError> &errors )
 
  127  if ( reply == mReply )
 
  129    QStringList errorMessages;
 
  130    errorMessages.reserve( errors.size() + 1 );
 
  131    errorMessages <<  QStringLiteral( 
"SSL Errors: " );
 
  133    for ( 
const QSslError &error : errors )
 
  134      errorMessages << error.errorString();
 
  136    error( errorMessages );
 
  142void QgsFileDownloader::error( 
const QStringList &errorMessages )
 
  144  for ( 
const QString &error : errorMessages )
 
  152void QgsFileDownloader::error( 
const QString &errorMessage )
 
  154  error( QStringList() << errorMessage );
 
  157void QgsFileDownloader::onReadyRead()
 
  160  if ( mFile.fileName().isEmpty() )
 
  162    error( tr( 
"No output filename specified" ) );
 
  165  else if ( ! mFile.isOpen() && ! mFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
 
  167    error( tr( 
"Cannot open output file: %1" ).arg( mFile.fileName() ) );
 
  172    const QByteArray data = mReply->readAll();
 
  177void QgsFileDownloader::onFinished()
 
  180  if ( ! mErrors.isEmpty() || mDownloadCanceled )
 
  182    if ( mFile.isOpen() )
 
  184    if ( mFile.exists() )
 
  190    if ( mFile.isOpen() )
 
  196    if ( mReply->error() )
 
  199      error( tr( 
"Download failed: %1" ).arg( mReply->errorString() ) );
 
  211void QgsFileDownloader::onDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
 
  213  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)