Skip to content

Commit

Permalink
Split QgsFileDownloader into separate core/gui classes
Browse files Browse the repository at this point in the history
So that the guts of this class can be used from non-gui code
  • Loading branch information
nyalldawson committed Oct 18, 2017
1 parent a406739 commit 587072c
Show file tree
Hide file tree
Showing 13 changed files with 311 additions and 53 deletions.
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -310,6 +310,7 @@
%Include qgsfieldformatterregistry.sip
%Include qgsfieldmodel.sip
%Include qgsfieldproxymodel.sip
%Include qgsfiledownloader.sip
%Include qgsgeometryvalidator.sip
%Include qgsgml.sip
%Include qgsgmlschema.sip
Expand Down
94 changes: 94 additions & 0 deletions python/core/qgsfiledownloader.sip
@@ -0,0 +1,94 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsfiledownloader.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsFileDownloader : QObject
{
%Docstring
QgsFileDownloader is a utility class for downloading files.

To use this class, it is necessary to pass the URL and an output file name as
arguments to the constructor, the download will start immediately.

The download is asynchronous.

The object will destroy itself when the request completes, errors or is canceled.
An optional authentication configuration can be specified.

.. versionadded:: 2.18.1
%End

%TypeHeaderCode
#include "qgsfiledownloader.h"
%End
public:

QgsFileDownloader( const QUrl &url, const QString &outputFileName, const QString &authcfg = QString(), bool delayStart = false );
%Docstring
QgsFileDownloader
\param url the download url
\param outputFileName file name where the downloaded content will be stored
\param authcfg optionally apply this authentication configuration
\param delayStart if true, the download will not be commenced immediately and must
be triggered by a later call to startDownload(). This can be useful if connections need
to be made to the downloader and there's a chance the download will emit
signals before these connections have been made.
%End

signals:
void downloadCompleted();
%Docstring
Emitted when the download has completed successfully
%End
void downloadExited();
%Docstring
Emitted always when the downloader exits
%End
void downloadCanceled();
%Docstring
Emitted when the download was canceled by the user
%End
void downloadError( QStringList errorMessages );
%Docstring
Emitted when an error makes the download fail
%End
void downloadProgress( qint64 bytesReceived, qint64 bytesTotal );
%Docstring
Emitted when data are ready to be processed
%End

public slots:

void onDownloadCanceled();
%Docstring
Called when a download is canceled by the user
this slot aborts the download and deletes
the object.
Never call this slot directly: this is meant to
be managed by the signal-slot system.
%End

void startDownload();
%Docstring
Called to start the download
%End

protected:
~QgsFileDownloader();

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsfiledownloader.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
2 changes: 1 addition & 1 deletion python/gui/gui_auto.sip
Expand Up @@ -18,7 +18,6 @@
%Include qgsuserinputdockwidget.sip
%Include qgsbrowserdockwidget.sip
%Include qgsvertexmarker.sip
%Include qgsfiledownloader.sip
%Include qgsabstractdatasourcewidget.sip
%Include qgssourceselectprovider.sip
%Include qgssourceselectproviderregistry.sip
Expand Down Expand Up @@ -201,6 +200,7 @@
%Include qgstreewidgetitem.sip
%Include qgsunitselectionwidget.sip
%Include qgsvariableeditorwidget.sip
%Include qgsfiledownloaderdialog.sip
%Include raster/qgsmultibandcolorrendererwidget.sip
%Include raster/qgspalettedrendererwidget.sip
%Include raster/qgsrasterbandcombobox.sip
Expand Down
68 changes: 68 additions & 0 deletions python/gui/qgsfiledownloaderdialog.sip
@@ -0,0 +1,68 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgsfiledownloaderdialog.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsFileDownloaderDialog : QProgressDialog
{
%Docstring
QgsFileDownloaderDialog is a QProgressDialog subclass which
handles file downloads and user feedback.

Internally, it uses QgsFileDownloader to handle the download,
while showing progress via a progress dialog and supporting
cancelation.

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsfiledownloaderdialog.h"
%End
public:

QgsFileDownloaderDialog( const QUrl &url, const QString &outputFileName, const QString &authcfg = QString() );
%Docstring
QgsFileDownloader
\param url the download url
\param outputFileName file name where the downloaded content will be stored
\param authcfg optionally apply this authentication configuration
%End

signals:
void downloadCompleted();
%Docstring
Emitted when the download has completed successfully
%End
void downloadExited();
%Docstring
Emitted always when the downloader exits
%End
void downloadCanceled();
%Docstring
Emitted when the download was canceled by the user
%End
void downloadError( QStringList errorMessages );
%Docstring
Emitted when an error makes the download fail
%End
void downloadProgress( qint64 bytesReceived, qint64 bytesTotal );
%Docstring
Emitted when data are ready to be processed
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgsfiledownloaderdialog.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
4 changes: 2 additions & 2 deletions src/app/qgsidentifyresultsdialog.cpp
Expand Up @@ -42,7 +42,7 @@
#include "qgswebframe.h"
#include "qgsstringutils.h"
#include "qgstreewidgetitem.h"
#include "qgsfiledownloader.h"
#include "qgsfiledownloaderdialog.h"
#include "qgsfieldformatterregistry.h"
#include "qgsfieldformatter.h"
#include "qgssettings.h"
Expand Down Expand Up @@ -132,7 +132,7 @@ void QgsIdentifyResultsWebView::handleDownload( QUrl url )
{
settings.setValue( DOWNLOADER_LAST_DIR_KEY, QFileInfo( targetFile ).dir().absolutePath() );
// Start the download
new QgsFileDownloader( url, targetFile );
new QgsFileDownloaderDialog( url, targetFile );
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt 100644 → 100755
Expand Up @@ -182,6 +182,7 @@ SET(QGIS_CORE_SRCS
qgsfieldmodel.cpp
qgsfieldproxymodel.cpp
qgsfields.cpp
qgsfiledownloader.cpp
qgsfontutils.cpp
qgsgeometrysimplifier.cpp
qgsgeometryvalidator.cpp
Expand Down Expand Up @@ -588,6 +589,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsfieldformatterregistry.h
qgsfieldmodel.h
qgsfieldproxymodel.h
qgsfiledownloader.h
qgsgeometryvalidator.h
qgsgml.h
qgsgmlschema.h
Expand Down
31 changes: 2 additions & 29 deletions src/gui/qgsfiledownloader.cpp → src/core/qgsfiledownloader.cpp 100644 → 100755
Expand Up @@ -21,18 +21,17 @@
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QMessageBox>
#ifndef QT_NO_SSL
#include <QSslError>
#endif

QgsFileDownloader::QgsFileDownloader( const QUrl &url, const QString &outputFileName, bool enableGuiNotifications, const QString &authcfg )
QgsFileDownloader::QgsFileDownloader(const QUrl &url, const QString &outputFileName, const QString &authcfg , bool delayStart)
: mUrl( url )
, mDownloadCanceled( false )
, mGuiNotificationsEnabled( enableGuiNotifications )
{
mFile.setFileName( outputFileName );
mAuthCfg = authcfg;
if ( !delayStart )
startDownload();
}

Expand All @@ -44,10 +43,6 @@ QgsFileDownloader::~QgsFileDownloader()
mReply->abort();
mReply->deleteLater();
}
if ( mProgressDialog )
{
mProgressDialog->deleteLater();
}
}


Expand Down Expand Up @@ -83,14 +78,6 @@ void QgsFileDownloader::startDownload()
#ifndef QT_NO_SSL
connect( nam, &QgsNetworkAccessManager::sslErrors, this, &QgsFileDownloader::onSslErrors, Qt::UniqueConnection );
#endif
if ( mGuiNotificationsEnabled )
{
mProgressDialog = new QProgressDialog();
mProgressDialog->setWindowTitle( tr( "Download" ) );
mProgressDialog->setLabelText( tr( "Downloading %1." ).arg( mFile.fileName() ) );
mProgressDialog->show();
connect( mProgressDialog, &QProgressDialog::canceled, this, &QgsFileDownloader::onDownloadCanceled );
}
}

void QgsFileDownloader::onDownloadCanceled()
Expand Down Expand Up @@ -126,11 +113,6 @@ void QgsFileDownloader::error( const QStringList &errorMessages )
{
mErrors << errorMessages[i];
}
// Show error
if ( mGuiNotificationsEnabled )
{
QMessageBox::warning( nullptr, tr( "Download failed" ), mErrors.join( QStringLiteral( "<br>" ) ) );
}
emit downloadError( mErrors );
}

Expand Down Expand Up @@ -161,14 +143,10 @@ void QgsFileDownloader::onFinished()
{
mFile.close();
mFile.remove();
if ( mGuiNotificationsEnabled )
mProgressDialog->hide();
}
else
{
// download finished normally
if ( mGuiNotificationsEnabled )
mProgressDialog->hide();
mFile.flush();
mFile.close();

Expand Down Expand Up @@ -206,11 +184,6 @@ void QgsFileDownloader::onDownloadProgress( qint64 bytesReceived, qint64 bytesTo
{
return;
}
if ( mGuiNotificationsEnabled )
{
mProgressDialog->setMaximum( bytesTotal );
mProgressDialog->setValue( bytesReceived );
}
emit downloadProgress( bytesReceived, bytesTotal );
}

30 changes: 15 additions & 15 deletions src/gui/qgsfiledownloader.h → src/core/qgsfiledownloader.h 100644 → 100755
Expand Up @@ -19,30 +19,27 @@
#include <QObject>
#include <QFile>
#include <QNetworkReply>
#include <QProgressDialog>
#include "qgis_gui.h"
#include "qgis_core.h"

#ifndef QT_NO_SSL
#include <QSslError>
#endif

/**
* \ingroup gui
* \ingroup core
* QgsFileDownloader is a utility class for downloading files.
*
* To use this class, it is necessary to pass the URL and an output file name as
* arguments to the constructor, the download will start immediately.
* The download is asynchronous and depending on the guiNotificationsEnabled
* parameter accepted by the constructor (default = true) the class will
* show a progress dialog and report all errors in a QMessageBox::warning dialog.
* If the guiNotificationsEnabled parameter is set to false, the class can still
* be used through the signals and slots mechanism.
*
* The download is asynchronous.
*
* The object will destroy itself when the request completes, errors or is canceled.
* An optional authentication configuration can be specified.
*
* \since QGIS 2.18.1
*/
class GUI_EXPORT QgsFileDownloader : public QObject
class CORE_EXPORT QgsFileDownloader : public QObject
{
Q_OBJECT
public:
Expand All @@ -51,10 +48,13 @@ class GUI_EXPORT QgsFileDownloader : public QObject
* QgsFileDownloader
* \param url the download url
* \param outputFileName file name where the downloaded content will be stored
* \param guiNotificationsEnabled if false, the downloader will not display any progress bar or error message
* \param authcfg optionally apply this authentication configuration
* \param delayStart if true, the download will not be commenced immediately and must
* be triggered by a later call to startDownload(). This can be useful if connections need
* to be made to the downloader and there's a chance the download will emit
* signals before these connections have been made.
*/
QgsFileDownloader( const QUrl &url, const QString &outputFileName, bool guiNotificationsEnabled = true, const QString &authcfg = QString() );
QgsFileDownloader( const QUrl &url, const QString &outputFileName, const QString &authcfg = QString(), bool delayStart = false );

signals:
//! Emitted when the download has completed successfully
Expand All @@ -79,6 +79,9 @@ class GUI_EXPORT QgsFileDownloader : public QObject
*/
void onDownloadCanceled();

//! Called to start the download
void startDownload();

private slots:
//! Called when the network reply data are ready
void onReadyRead();
Expand All @@ -88,8 +91,7 @@ class GUI_EXPORT QgsFileDownloader : public QObject
void onDownloadProgress( qint64 bytesReceived, qint64 bytesTotal );
//! Called when a network request times out
void onRequestTimedOut();
//! Called to start the download
void startDownload();

#ifndef QT_NO_SSL

/**
Expand All @@ -114,10 +116,8 @@ class GUI_EXPORT QgsFileDownloader : public QObject
QUrl mUrl;
QNetworkReply *mReply = nullptr;
QFile mFile;
QProgressDialog *mProgressDialog = nullptr;
bool mDownloadCanceled;
QStringList mErrors;
bool mGuiNotificationsEnabled;
QString mAuthCfg;
};

Expand Down

0 comments on commit 587072c

Please sign in to comment.