21 #include <QNetworkAccessManager>
22 #include <QNetworkRequest>
23 #include <QNetworkReply>
30 , mDownloadCanceled( false )
32 mFile.setFileName( outputFileName );
44 mReply->deleteLater();
52 QNetworkRequest request( mUrl );
54 if ( !mAuthCfg.isEmpty() )
61 disconnect( mReply, &QNetworkReply::readyRead,
this, &QgsFileDownloader::onReadyRead );
62 disconnect( mReply, &QNetworkReply::finished,
this, &QgsFileDownloader::onFinished );
63 disconnect( mReply, &QNetworkReply::downloadProgress,
this, &QgsFileDownloader::onDownloadProgress );
65 mReply->deleteLater();
68 mReply = nam->get( request );
69 if ( !mAuthCfg.isEmpty() )
74 connect( mReply, &QNetworkReply::readyRead,
this, &QgsFileDownloader::onReadyRead );
75 connect( mReply, &QNetworkReply::finished,
this, &QgsFileDownloader::onFinished );
76 connect( mReply, &QNetworkReply::downloadProgress,
this, &QgsFileDownloader::onDownloadProgress );
79 connect( nam, &QgsNetworkAccessManager::sslErrors,
this, &QgsFileDownloader::onSslErrors, Qt::UniqueConnection );
85 mDownloadCanceled =
true;
90 void QgsFileDownloader::onRequestTimedOut( QNetworkReply *reply )
92 if ( reply == mReply )
93 error( tr(
"Network request %1 timed out" ).arg( mUrl.toString() ) );
97 void QgsFileDownloader::onSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors )
99 if ( reply == mReply )
101 QStringList errorMessages;
102 errorMessages.reserve( errors.size() + 1 );
103 errorMessages << QStringLiteral(
"SSL Errors: " );
105 for (
const QSslError &error : errors )
106 errorMessages << error.errorString();
108 error( errorMessages );
114 void QgsFileDownloader::error(
const QStringList &errorMessages )
116 for (
const QString &error : errorMessages )
124 void QgsFileDownloader::error(
const QString &errorMessage )
126 error( QStringList() << errorMessage );
129 void QgsFileDownloader::onReadyRead()
132 if ( mFile.fileName().isEmpty() )
134 error( tr(
"No output filename specified" ) );
137 else if ( ! mFile.isOpen() && ! mFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
139 error( tr(
"Cannot open output file: %1" ).arg( mFile.fileName() ) );
144 QByteArray data = mReply->readAll();
149 void QgsFileDownloader::onFinished()
152 if ( ! mErrors.isEmpty() || mDownloadCanceled )
154 if ( mFile.isOpen() )
156 if ( mFile.exists() )
162 if ( mFile.isOpen() )
169 QVariant redirectionTarget = mReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
170 if ( mReply->error() )
173 error( tr(
"Download failed: %1" ).arg( mReply->errorString() ) );
175 else if ( !redirectionTarget.isNull() )
177 QUrl newUrl = mUrl.resolved( redirectionTarget.toUrl() );
179 mReply->deleteLater();
180 if ( !mFile.open( QIODevice::WriteOnly ) )
183 error( tr(
"Cannot open output file: %1" ).arg( mFile.fileName() ) );
203 void QgsFileDownloader::onDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
205 if ( mDownloadCanceled )
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,...
QgsFileDownloader(const QUrl &url, const QString &outputFileName, const QString &authcfg=QString(), bool delayStart=false)
QgsFileDownloader.
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.
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)