Skip to content

Commit c285ed1

Browse files
committedAug 27, 2015
abort browser wms item get capabilities request on deleteLater, fix for #13271
1 parent e0d5a0b commit c285ed1

File tree

4 files changed

+93
-42
lines changed

4 files changed

+93
-42
lines changed
 

‎src/providers/wms/qgswmscapabilities.cpp

Lines changed: 66 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,20 +1854,33 @@ int QgsWmsCapabilities::identifyCapabilities() const
18541854

18551855
// -----------------
18561856

1857+
QgsWmsCapabilitiesDownload::QgsWmsCapabilitiesDownload( QObject *parent )
1858+
: QObject( parent )
1859+
, mCapabilitiesReply( 0 )
1860+
, mIsAborted( false )
1861+
{
1862+
}
18571863

18581864
QgsWmsCapabilitiesDownload::QgsWmsCapabilitiesDownload( const QString& baseUrl, const QgsWmsAuthorization& auth, QObject *parent )
18591865
: QObject( parent )
18601866
, mBaseUrl( baseUrl )
18611867
, mAuth( auth )
1862-
, mCapabilitiesReply( NULL )
1868+
, mCapabilitiesReply( 0 )
1869+
, mIsAborted( false )
18631870
{
18641871
}
18651872

1873+
bool QgsWmsCapabilitiesDownload::downloadCapabilities( const QString& baseUrl, const QgsWmsAuthorization& auth )
1874+
{
1875+
mBaseUrl = baseUrl;
1876+
mAuth = auth;
1877+
return downloadCapabilities();
1878+
}
18661879

18671880
bool QgsWmsCapabilitiesDownload::downloadCapabilities()
18681881
{
18691882
QgsDebugMsg( "entering." );
1870-
1883+
mIsAborted = false;
18711884
QString url = mBaseUrl;
18721885
QgsDebugMsg( "url = " + url );
18731886
if ( !url.contains( "SERVICE=WMTS" ) &&
@@ -1886,6 +1899,10 @@ bool QgsWmsCapabilitiesDownload::downloadCapabilities()
18861899
QgsDebugMsg( QString( "getcapabilities: %1" ).arg( url ) );
18871900
// This is causing Qt warning: "Cannot create children for a parent that is in a different thread."
18881901
// but it only means that the reply will have no parent
1902+
if ( mIsAborted )
1903+
{
1904+
return false;
1905+
}
18891906
mCapabilitiesReply = QgsNetworkAccessManager::instance()->get( request );
18901907

18911908
connect( mCapabilitiesReply, SIGNAL( finished() ), this, SLOT( capabilitiesReplyFinished() ), Qt::DirectConnection );
@@ -1898,7 +1915,15 @@ bool QgsWmsCapabilitiesDownload::downloadCapabilities()
18981915
return mError.isEmpty();
18991916
}
19001917

1901-
1918+
void QgsWmsCapabilitiesDownload::abort()
1919+
{
1920+
QgsDebugMsg( "Entered" );
1921+
mIsAborted = true;
1922+
if ( mCapabilitiesReply )
1923+
{
1924+
mCapabilitiesReply->abort();
1925+
}
1926+
}
19021927

19031928
void QgsWmsCapabilitiesDownload::capabilitiesReplyProgress( qint64 bytesReceived, qint64 bytesTotal )
19041929
{
@@ -1910,54 +1935,57 @@ void QgsWmsCapabilitiesDownload::capabilitiesReplyProgress( qint64 bytesReceived
19101935
void QgsWmsCapabilitiesDownload::capabilitiesReplyFinished()
19111936
{
19121937
QgsDebugMsg( "entering." );
1913-
if ( mCapabilitiesReply->error() == QNetworkReply::NoError )
1938+
if ( !mIsAborted )
19141939
{
1915-
QgsDebugMsg( "reply ok" );
1916-
QVariant redirect = mCapabilitiesReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
1917-
if ( !redirect.isNull() )
1940+
if ( mCapabilitiesReply->error() == QNetworkReply::NoError )
19181941
{
1919-
emit statusChanged( tr( "Capabilities request redirected." ) );
1920-
1921-
const QUrl& toUrl = redirect.toUrl();
1922-
mCapabilitiesReply->request();
1923-
if ( toUrl == mCapabilitiesReply->url() )
1942+
QgsDebugMsg( "reply ok" );
1943+
QVariant redirect = mCapabilitiesReply->attribute( QNetworkRequest::RedirectionTargetAttribute );
1944+
if ( !redirect.isNull() )
19241945
{
1925-
mError = tr( "Redirect loop detected: %1" ).arg( toUrl.toString() );
1926-
QgsMessageLog::logMessage( mError, tr( "WMS" ) );
1927-
mHttpCapabilitiesResponse.clear();
1946+
emit statusChanged( tr( "Capabilities request redirected." ) );
1947+
1948+
const QUrl& toUrl = redirect.toUrl();
1949+
mCapabilitiesReply->request();
1950+
if ( toUrl == mCapabilitiesReply->url() )
1951+
{
1952+
mError = tr( "Redirect loop detected: %1" ).arg( toUrl.toString() );
1953+
QgsMessageLog::logMessage( mError, tr( "WMS" ) );
1954+
mHttpCapabilitiesResponse.clear();
1955+
}
1956+
else
1957+
{
1958+
QNetworkRequest request( toUrl );
1959+
mAuth.setAuthorization( request );
1960+
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork );
1961+
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
1962+
1963+
mCapabilitiesReply->deleteLater();
1964+
QgsDebugMsg( QString( "redirected getcapabilities: %1" ).arg( redirect.toString() ) );
1965+
mCapabilitiesReply = QgsNetworkAccessManager::instance()->get( request );
1966+
1967+
connect( mCapabilitiesReply, SIGNAL( finished() ), this, SLOT( capabilitiesReplyFinished() ) );
1968+
connect( mCapabilitiesReply, SIGNAL( downloadProgress( qint64, qint64 ) ), this, SLOT( capabilitiesReplyProgress( qint64, qint64 ) ) );
1969+
return;
1970+
}
19281971
}
19291972
else
19301973
{
1931-
QNetworkRequest request( toUrl );
1932-
mAuth.setAuthorization( request );
1933-
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork );
1934-
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
1974+
mHttpCapabilitiesResponse = mCapabilitiesReply->readAll();
19351975

1936-
mCapabilitiesReply->deleteLater();
1937-
QgsDebugMsg( QString( "redirected getcapabilities: %1" ).arg( redirect.toString() ) );
1938-
mCapabilitiesReply = QgsNetworkAccessManager::instance()->get( request );
1939-
1940-
connect( mCapabilitiesReply, SIGNAL( finished() ), this, SLOT( capabilitiesReplyFinished() ) );
1941-
connect( mCapabilitiesReply, SIGNAL( downloadProgress( qint64, qint64 ) ), this, SLOT( capabilitiesReplyProgress( qint64, qint64 ) ) );
1942-
return;
1976+
if ( mHttpCapabilitiesResponse.isEmpty() )
1977+
{
1978+
mError = tr( "empty of capabilities: %1" ).arg( mCapabilitiesReply->errorString() );
1979+
}
19431980
}
19441981
}
19451982
else
19461983
{
1947-
mHttpCapabilitiesResponse = mCapabilitiesReply->readAll();
1948-
1949-
if ( mHttpCapabilitiesResponse.isEmpty() )
1950-
{
1951-
mError = tr( "empty of capabilities: %1" ).arg( mCapabilitiesReply->errorString() );
1952-
}
1984+
mError = tr( "Download of capabilities failed: %1" ).arg( mCapabilitiesReply->errorString() );
1985+
QgsMessageLog::logMessage( mError, tr( "WMS" ) );
1986+
mHttpCapabilitiesResponse.clear();
19531987
}
19541988
}
1955-
else
1956-
{
1957-
mError = tr( "Download of capabilities failed: %1" ).arg( mCapabilitiesReply->errorString() );
1958-
QgsMessageLog::logMessage( mError, tr( "WMS" ) );
1959-
mHttpCapabilitiesResponse.clear();
1960-
}
19611989

19621990
mCapabilitiesReply->deleteLater();
19631991
mCapabilitiesReply = 0;

‎src/providers/wms/qgswmscapabilities.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,14 +683,20 @@ class QgsWmsCapabilitiesDownload : public QObject
683683
Q_OBJECT
684684

685685
public:
686+
QgsWmsCapabilitiesDownload( QObject* parent = 0 );
687+
686688
QgsWmsCapabilitiesDownload( const QString& baseUrl, const QgsWmsAuthorization& auth, QObject* parent = 0 );
687689

688690
bool downloadCapabilities();
689691

692+
bool downloadCapabilities( const QString& baseUrl, const QgsWmsAuthorization& auth );
693+
690694
QString lastError() const { return mError; }
691695

692696
QByteArray response() const { return mHttpCapabilitiesResponse; }
693697

698+
/** Abort network request immediately */
699+
void abort();
694700
signals:
695701
/** \brief emit a signal to be caught by qgisapp and display a msg on status bar */
696702
void statusChanged( QString const & theStatusQString );
@@ -720,6 +726,7 @@ class QgsWmsCapabilitiesDownload : public QObject
720726
/** Capabilities of the WMS (raw) */
721727
QByteArray mHttpCapabilitiesResponse;
722728

729+
bool mIsAborted;
723730
};
724731

725732

‎src/providers/wms/qgswmsdataitems.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,26 @@
2727
QgsWMSConnectionItem::QgsWMSConnectionItem( QgsDataItem* parent, QString name, QString path, QString uri )
2828
: QgsDataCollectionItem( parent, name, path )
2929
, mUri( uri )
30+
, mCapabilitiesDownload( 0 )
3031
{
3132
mIconName = "mIconConnect.png";
33+
mCapabilitiesDownload = new QgsWmsCapabilitiesDownload();
3234
}
3335

3436
QgsWMSConnectionItem::~QgsWMSConnectionItem()
3537
{
38+
QgsDebugMsg( "Entered" );
39+
delete mCapabilitiesDownload;
40+
}
41+
42+
void QgsWMSConnectionItem::deleteLater()
43+
{
44+
QgsDebugMsg( "Entered" );
45+
if ( mCapabilitiesDownload )
46+
{
47+
mCapabilitiesDownload->abort();
48+
}
49+
QgsDataCollectionItem::deleteLater();
3650
}
3751

3852
QVector<QgsDataItem*> QgsWMSConnectionItem::createChildren()
@@ -52,9 +66,7 @@ QVector<QgsDataItem*> QgsWMSConnectionItem::createChildren()
5266
return children;
5367
}
5468

55-
QgsWmsCapabilitiesDownload capDownload( wmsSettings.baseUrl(), wmsSettings.authorization() );
56-
57-
bool res = capDownload.downloadCapabilities();
69+
bool res = mCapabilitiesDownload->downloadCapabilities( wmsSettings.baseUrl(), wmsSettings.authorization() );
5870

5971
if ( !res )
6072
{
@@ -63,7 +75,7 @@ QVector<QgsDataItem*> QgsWMSConnectionItem::createChildren()
6375
}
6476

6577
QgsWmsCapabilities caps;
66-
if ( !caps.parseResponse( capDownload.response(), wmsSettings.parserSettings() ) )
78+
if ( !caps.parseResponse( mCapabilitiesDownload->response(), wmsSettings.parserSettings() ) )
6779
{
6880
children.append( new QgsErrorItem( this, tr( "Failed to parse capabilities" ), mPath + "/error" ) );
6981
return children;

‎src/providers/wms/qgswmsdataitems.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "qgsdatasourceuri.h"
2020
#include "qgswmsprovider.h"
2121

22+
class QgsWmsCapabilitiesDownload;
23+
2224
class QgsWMSConnectionItem : public QgsDataCollectionItem
2325
{
2426
Q_OBJECT
@@ -34,9 +36,11 @@ class QgsWMSConnectionItem : public QgsDataCollectionItem
3436
public slots:
3537
void editConnection();
3638
void deleteConnection();
39+
virtual void deleteLater() override;
3740

3841
private:
3942
QString mUri;
43+
QgsWmsCapabilitiesDownload *mCapabilitiesDownload;
4044
};
4145

4246
// WMS Layers may be nested, so that they may be both QgsDataCollectionItem and QgsLayerItem

0 commit comments

Comments
 (0)
Please sign in to comment.