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( finished() ),
this, SLOT( onFinished() ) );
64 connect( mReply, SIGNAL(
downloadProgress( qint64, qint64 ) ),
this, SLOT( onDownloadProgress( qint64, qint64 ) ) );
69 if ( mGuiNotificationsEnabled )
74 mProgressDialog->
show();
81 mDownloadCanceled =
true;
86 void QgsFileDownloader::onRequestTimedOut()
88 error(
tr(
"Network request %1 timed out" ).arg( mUrl.
toString() ) );
96 errorMessages <<
"SSL Errors: ";
97 for (
int end = errors.
size(), i = 0; i != end; ++i )
99 errorMessages << errors[i].errorString();
101 error( errorMessages );
106 void QgsFileDownloader::error(
QStringList errorMessages )
108 for (
int end = errorMessages.
size(), i = 0; i != end; ++i )
110 mErrors.
append( errorMessages[i] );
113 if ( mGuiNotificationsEnabled )
120 void QgsFileDownloader::error(
QString errorMessage )
125 void QgsFileDownloader::onReadyRead()
128 if ( ! mFile.
isOpen() && ! mFile.
open( QIODevice::WriteOnly | QIODevice::Truncate ) )
130 error(
tr(
"Cannot open output file: %1" ).arg( mFile.
fileName() ) );
140 void QgsFileDownloader::onFinished()
143 if ( ! mErrors.
isEmpty() || mDownloadCanceled )
147 if ( mGuiNotificationsEnabled )
148 mProgressDialog->
hide();
153 if ( mGuiNotificationsEnabled )
154 mProgressDialog->
hide();
159 QVariant redirectionTarget = mReply->
attribute( QNetworkRequest::RedirectionTargetAttribute );
160 if ( mReply->
error() )
163 error(
tr(
"Download failed: %1" ).arg( mReply->
errorString() ) );
165 else if ( !redirectionTarget.
isNull() )
170 mFile.
open( QIODevice::WriteOnly );
186 void QgsFileDownloader::onDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
188 if ( mDownloadCanceled )
192 if ( mGuiNotificationsEnabled )
195 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.