Skip to content

Commit

Permalink
Support canceling network content fetching tasks
Browse files Browse the repository at this point in the history
(cherry-picked from 83f548)
  • Loading branch information
nyalldawson committed Apr 6, 2018
1 parent c1b6e8f commit cbb19e6
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
7 changes: 7 additions & 0 deletions python/core/qgsnetworkcontentfetcher.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ Returns a reference to the network reply
Returns the fetched content as a string

:return: string containing network content
%End

void cancel();
%Docstring
Cancels any ongoing request.

.. versionadded:: 3.2
%End

signals:
Expand Down
2 changes: 2 additions & 0 deletions python/core/qgsnetworkcontentfetchertask.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ the specified network ``request``.

virtual bool run();

virtual void cancel();


QNetworkReply *reply();
%Docstring
Expand Down
20 changes: 20 additions & 0 deletions src/core/qgsnetworkcontentfetcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void QgsNetworkContentFetcher::fetchContent( const QUrl &url )
void QgsNetworkContentFetcher::fetchContent( const QNetworkRequest &request )
{
mContentLoaded = false;
mIsCanceled = false;

if ( mReply )
{
Expand Down Expand Up @@ -82,6 +83,19 @@ QString QgsNetworkContentFetcher::contentAsString() const
return codec->toUnicode( array );
}

void QgsNetworkContentFetcher::cancel()
{
mIsCanceled = true;

if ( mReply )
{
//cancel any in progress requests
mReply->abort();
mReply->deleteLater();
mReply = nullptr;
}
}

QTextCodec *QgsNetworkContentFetcher::codecForHtml( QByteArray &array ) const
{
//QTextCodec::codecForHtml fails to detect "<meta charset="utf-8"/>" type tags
Expand Down Expand Up @@ -125,6 +139,12 @@ void QgsNetworkContentFetcher::contentLoaded( bool ok )
{
Q_UNUSED( ok );

if ( mIsCanceled )
{
emit finished();
return;
}

if ( mReply->error() != QNetworkReply::NoError )
{
QgsMessageLog::logMessage( tr( "HTTP fetch %1 failed with error %2" ).arg( mReply->url().toString(), mReply->errorString() ) );
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsnetworkcontentfetcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ class CORE_EXPORT QgsNetworkContentFetcher : public QObject
*/
QString contentAsString() const;

/**
* Cancels any ongoing request.
* \since QGIS 3.2
*/
void cancel();

signals:

/**
Expand All @@ -93,6 +99,8 @@ class CORE_EXPORT QgsNetworkContentFetcher : public QObject

bool mContentLoaded = false;

bool mIsCanceled = false;

/**
* Tries to create a text codec for decoding html content. Works around bugs in Qt's built in method.
* \param array input html byte array
Expand Down
13 changes: 11 additions & 2 deletions src/core/qgsnetworkcontentfetchertask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bool QgsNetworkContentFetcherTask::run()
connect( mFetcher, &QgsNetworkContentFetcher::finished, &loop, &QEventLoop::quit );
connect( mFetcher, &QgsNetworkContentFetcher::downloadProgress, this, [ = ]( qint64 bytesReceived, qint64 bytesTotal )
{
if ( bytesTotal > 0 )
if ( !isCanceled() && bytesTotal > 0 )
{
int progress = ( bytesReceived * 100 ) / bytesTotal;
// don't emit 100% progress reports until completely fetched - otherwise we get
Expand All @@ -54,11 +54,20 @@ bool QgsNetworkContentFetcherTask::run()
} );
mFetcher->fetchContent( mRequest );
loop.exec();
setProgress( 100 );
if ( !isCanceled() )
setProgress( 100 );
emit fetched();
return true;
}

void QgsNetworkContentFetcherTask::cancel()
{
if ( mFetcher )
mFetcher->cancel();

QgsTask::cancel();
}

QNetworkReply *QgsNetworkContentFetcherTask::reply()
{
return mFetcher ? mFetcher->reply() : nullptr;
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsnetworkcontentfetchertask.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class CORE_EXPORT QgsNetworkContentFetcherTask : public QgsTask
~QgsNetworkContentFetcherTask();

bool run() override;
void cancel() override;

/**
* Returns the network reply. Ownership is not transferred.
Expand Down

0 comments on commit cbb19e6

Please sign in to comment.