19 #include <QNetworkAccessManager> 20 #include <QNetworkRequest> 21 #include <QNetworkReply> 22 #include <QMessageBox> 30 , mProgressDialog( nullptr )
31 , mDownloadCanceled( false )
33 , mGuiNotificationsEnabled( enableGuiNotifications )
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 )
122 void QgsFileDownloader::error(
QString errorMessage )
127 void QgsFileDownloader::onReadyRead()
130 if ( ! mFile.
isOpen() && ! mFile.
open( QIODevice::WriteOnly | QIODevice::Truncate ) )
132 error(
tr(
"Cannot open output file: %1" ).arg( mFile.
fileName() ) );
142 void QgsFileDownloader::onFinished()
145 if ( ! mErrors.
isEmpty() || mDownloadCanceled )
149 if ( mGuiNotificationsEnabled )
150 mProgressDialog->
hide();
155 if ( mGuiNotificationsEnabled )
156 mProgressDialog->
hide();
161 QVariant redirectionTarget = mReply->
attribute( QNetworkRequest::RedirectionTargetAttribute );
162 if ( mReply->
error() )
165 error(
tr(
"Download failed: %1" ).arg( mReply->
errorString() ) );
167 else if ( !redirectionTarget.
isNull() )
172 mFile.
open( QIODevice::WriteOnly );
188 void QgsFileDownloader::onDownloadProgress( qint64 bytesReceived, qint64 bytesTotal )
190 if ( mDownloadCanceled )
194 if ( mGuiNotificationsEnabled )
197 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.
network access manager for QGIS