Skip to content

Commit cbb19e6

Browse files
committed
Support canceling network content fetching tasks
(cherry-picked from 83f548)
1 parent c1b6e8f commit cbb19e6

File tree

6 files changed

+49
-2
lines changed

6 files changed

+49
-2
lines changed

python/core/qgsnetworkcontentfetcher.sip.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ Returns a reference to the network reply
6363
Returns the fetched content as a string
6464

6565
:return: string containing network content
66+
%End
67+
68+
void cancel();
69+
%Docstring
70+
Cancels any ongoing request.
71+
72+
.. versionadded:: 3.2
6673
%End
6774

6875
signals:

python/core/qgsnetworkcontentfetchertask.sip.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ the specified network ``request``.
5050

5151
virtual bool run();
5252

53+
virtual void cancel();
54+
5355

5456
QNetworkReply *reply();
5557
%Docstring

src/core/qgsnetworkcontentfetcher.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void QgsNetworkContentFetcher::fetchContent( const QUrl &url )
4444
void QgsNetworkContentFetcher::fetchContent( const QNetworkRequest &request )
4545
{
4646
mContentLoaded = false;
47+
mIsCanceled = false;
4748

4849
if ( mReply )
4950
{
@@ -82,6 +83,19 @@ QString QgsNetworkContentFetcher::contentAsString() const
8283
return codec->toUnicode( array );
8384
}
8485

86+
void QgsNetworkContentFetcher::cancel()
87+
{
88+
mIsCanceled = true;
89+
90+
if ( mReply )
91+
{
92+
//cancel any in progress requests
93+
mReply->abort();
94+
mReply->deleteLater();
95+
mReply = nullptr;
96+
}
97+
}
98+
8599
QTextCodec *QgsNetworkContentFetcher::codecForHtml( QByteArray &array ) const
86100
{
87101
//QTextCodec::codecForHtml fails to detect "<meta charset="utf-8"/>" type tags
@@ -125,6 +139,12 @@ void QgsNetworkContentFetcher::contentLoaded( bool ok )
125139
{
126140
Q_UNUSED( ok );
127141

142+
if ( mIsCanceled )
143+
{
144+
emit finished();
145+
return;
146+
}
147+
128148
if ( mReply->error() != QNetworkReply::NoError )
129149
{
130150
QgsMessageLog::logMessage( tr( "HTTP fetch %1 failed with error %2" ).arg( mReply->url().toString(), mReply->errorString() ) );

src/core/qgsnetworkcontentfetcher.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ class CORE_EXPORT QgsNetworkContentFetcher : public QObject
7474
*/
7575
QString contentAsString() const;
7676

77+
/**
78+
* Cancels any ongoing request.
79+
* \since QGIS 3.2
80+
*/
81+
void cancel();
82+
7783
signals:
7884

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

94100
bool mContentLoaded = false;
95101

102+
bool mIsCanceled = false;
103+
96104
/**
97105
* Tries to create a text codec for decoding html content. Works around bugs in Qt's built in method.
98106
* \param array input html byte array

src/core/qgsnetworkcontentfetchertask.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ bool QgsNetworkContentFetcherTask::run()
4343
connect( mFetcher, &QgsNetworkContentFetcher::finished, &loop, &QEventLoop::quit );
4444
connect( mFetcher, &QgsNetworkContentFetcher::downloadProgress, this, [ = ]( qint64 bytesReceived, qint64 bytesTotal )
4545
{
46-
if ( bytesTotal > 0 )
46+
if ( !isCanceled() && bytesTotal > 0 )
4747
{
4848
int progress = ( bytesReceived * 100 ) / bytesTotal;
4949
// don't emit 100% progress reports until completely fetched - otherwise we get
@@ -54,11 +54,20 @@ bool QgsNetworkContentFetcherTask::run()
5454
} );
5555
mFetcher->fetchContent( mRequest );
5656
loop.exec();
57-
setProgress( 100 );
57+
if ( !isCanceled() )
58+
setProgress( 100 );
5859
emit fetched();
5960
return true;
6061
}
6162

63+
void QgsNetworkContentFetcherTask::cancel()
64+
{
65+
if ( mFetcher )
66+
mFetcher->cancel();
67+
68+
QgsTask::cancel();
69+
}
70+
6271
QNetworkReply *QgsNetworkContentFetcherTask::reply()
6372
{
6473
return mFetcher ? mFetcher->reply() : nullptr;

src/core/qgsnetworkcontentfetchertask.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class CORE_EXPORT QgsNetworkContentFetcherTask : public QgsTask
6565
~QgsNetworkContentFetcherTask();
6666

6767
bool run() override;
68+
void cancel() override;
6869

6970
/**
7071
* Returns the network reply. Ownership is not transferred.

0 commit comments

Comments
 (0)