Skip to content
Permalink
Browse files
Support canceling network content fetching tasks
  • Loading branch information
nyalldawson committed Apr 1, 2018
1 parent 149ccf2 commit 83f5486
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
@@ -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:
@@ -50,6 +50,8 @@ the specified network ``request``.

virtual bool run();

virtual void cancel();


QNetworkReply *reply();
%Docstring
@@ -44,6 +44,7 @@ void QgsNetworkContentFetcher::fetchContent( const QUrl &url )
void QgsNetworkContentFetcher::fetchContent( const QNetworkRequest &request )
{
mContentLoaded = false;
mIsCanceled = false;

if ( mReply )
{
@@ -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
@@ -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() ) );
@@ -74,6 +74,12 @@ class CORE_EXPORT QgsNetworkContentFetcher : public QObject
*/
QString contentAsString() const;

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

signals:

/**
@@ -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
@@ -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
@@ -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;
@@ -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.

0 comments on commit 83f5486

Please sign in to comment.