19 #include <QNetworkAccessManager> 20 #include <QNetworkRequest> 21 #include <QNetworkReply> 22 #include <QMessageBox> 30 , mProgressDialog( nullptr )
31 , mDownloadCanceled( false )
33 , mGuiNotificationsEnabled( enableGuiNotifications )
40 QgsFileDownloader::~QgsFileDownloader()
47 if ( mProgressDialog )
54 void QgsFileDownloader::startDownload()
60 mReply = nam->
get( request );
62 connect( mReply, SIGNAL( readyRead() ),
this, SLOT( onReadyRead() ) );
63 connect( mReply, SIGNAL( error( QNetworkReply::NetworkError ) ),
this, SLOT( onNetworkError( QNetworkReply::NetworkError ) ) );
64 connect( mReply, SIGNAL( finished() ),
this, SLOT( onFinished() ) );
65 connect( mReply, SIGNAL(
downloadProgress( qint64, qint64 ) ),
this, SLOT( onDownloadProgress( qint64, qint64 ) ) );
70 if ( mGuiNotificationsEnabled )
75 mProgressDialog->
show();
82 mDownloadCanceled =
true;
87 void QgsFileDownloader::onRequestTimedOut()
89 error(
tr(
"Network request %1 timed out" ).arg( mUrl.
toString() ) );
97 errorMessages <<
"SSL Errors: ";
98 for (
int end = errors.
size(), i = 0; i != end; ++i )
100 errorMessages << errors[i].errorString();
102 error( errorMessages );
107 void QgsFileDownloader::error(
QStringList errorMessages )
109 for (
int end = errorMessages.
size(), i = 0; i != end; ++i )
111 mErrors.
append( errorMessages[i] );
114 if ( mGuiNotificationsEnabled )
121 void QgsFileDownloader::error(
QString errorMessage )
126 void QgsFileDownloader::onReadyRead()
129 if ( ! mFile.
isOpen() && ! mFile.
open( QIODevice::WriteOnly | QIODevice::Truncate ) )
131 error(
tr(
"Cannot open output file: %1" ).arg( mFile.
fileName() ) );
141 void QgsFileDownloader::onFinished()
144 if ( ! mErrors.
isEmpty() || mDownloadCanceled )
148 if ( mGuiNotificationsEnabled )
149 mProgressDialog->
hide();
154 if ( mGuiNotificationsEnabled )
155 mProgressDialog->
hide();
160 QVariant redirectionTarget = mReply->
attribute( QNetworkRequest::RedirectionTargetAttribute );
161 if ( mReply->
error() )
164 error(
tr(
"Download failed: %1." ).arg( mReply->
errorString() ) );
166 else if ( !redirectionTarget.
isNull() )
171 mFile.
open( QIODevice::WriteOnly );
184 void QgsFileDownloader::onNetworkError( QNetworkReply::NetworkError err )
190 void QgsFileDownloader::onDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
192 if ( mDownloadCanceled )
196 if ( mGuiNotificationsEnabled )
199 mProgressDialog->
setValue( bytesReceived );
void setMaximum(int maximum)
QString errorString() const
void setLabelText(const QString &text)
void setFileName(const QString &name)
QString join(const QString &separator) const
QString toString(QFlags< QUrl::FormattingOption > options) const
QString tr(const char *sourceText, const char *disambiguation, int n)
void setValue(int progress)
void append(const T &value)
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QgsFileDownloader(QUrl url, QString outputFileName, bool guiNotificationsEnabled=true)
QgsFileDownloader.
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
Emitted when data ready to be processed.
void downloadCanceled()
Emitted when the download was canceled by the user.
QUrl resolved(const QUrl &relative) const
QVariant attribute(QNetworkRequest::Attribute code) const
qint64 write(const char *data, qint64 maxSize)
NetworkError error() const
void onDownloadCanceled()
Called when a download is canceled by the user this slot aborts the download and deletes the object...
StandardButton warning(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
QNetworkReply * get(const QNetworkRequest &request)
void downloadCompleted()
Emitted when the download has completed successfully.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void downloadError(QStringList errorMessages)
Emitted when an error makes the download fail.
void downloadExited()
Emitted always when the downloader exits.