21 #include <QNetworkAccessManager> 22 #include <QNetworkRequest> 23 #include <QNetworkReply> 30 , mDownloadCanceled( false )
32 mFile.setFileName( outputFileName );
44 mReply->deleteLater();
53 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()
92 error( tr(
"Network request %1 timed out" ).arg( mUrl.toString() ) );
96 void QgsFileDownloader::onSslErrors( QNetworkReply *reply,
const QList<QSslError> &errors )
99 QStringList errorMessages;
100 errorMessages << QStringLiteral(
"SSL Errors: " );
101 for (
auto end = errors.size(), i = 0; i !=
end; ++i )
103 errorMessages << errors[i].errorString();
105 error( errorMessages );
110 void QgsFileDownloader::error(
const QStringList &errorMessages )
112 for (
auto end = errorMessages.size(), i = 0; i !=
end; ++i )
114 mErrors << errorMessages[i];
121 void QgsFileDownloader::error(
const QString &errorMessage )
123 error( QStringList() << errorMessage );
126 void QgsFileDownloader::onReadyRead()
129 if ( mFile.fileName().isEmpty() )
131 error( tr(
"No output filename specified" ) );
134 else if ( ! mFile.isOpen() && ! mFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
136 error( tr(
"Cannot open output file: %1" ).arg( mFile.fileName() ) );
141 QByteArray data = mReply->readAll();
146 void QgsFileDownloader::onFinished()
149 if ( ! mErrors.isEmpty() || mDownloadCanceled )
151 if ( mFile.isOpen() )
153 if ( mFile.exists() )
159 if ( mFile.isOpen() )
166 QVariant redirectionTarget = mReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
167 if ( mReply->error() )
170 error( tr(
"Download failed: %1" ).arg( mReply->errorString() ) );
172 else if ( !redirectionTarget.isNull() )
174 QUrl newUrl = mUrl.resolved( redirectionTarget.toUrl() );
176 mReply->deleteLater();
177 if ( !mFile.open( QIODevice::WriteOnly ) )
180 error( tr(
"Cannot open output file: %1" ).arg( mFile.fileName() ) );
200 void QgsFileDownloader::onDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
202 if ( mDownloadCanceled )
void startDownload()
Called to start the download.
~QgsFileDownloader() override
void requestTimedOut(QNetworkReply *)
QgsFileDownloader(const QUrl &url, const QString &outputFileName, const QString &authcfg=QString(), bool delayStart=false)
QgsFileDownloader.
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.
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
Emitted when data are ready to be processed.
void downloadCanceled()
Emitted when the download was canceled by the user.
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 downloadCompleted()
Emitted when the download has completed successfully.
void downloadError(QStringList errorMessages)
Emitted when an error makes the download fail.
void cancelDownload()
Call to abort the download and delete this object after the cancelation has been processed.
void downloadExited()
Emitted always when the downloader exits.
network access manager for QGIS