Skip to content
Permalink
Browse files

[FEATURE] cache WMS capabililies

  • Loading branch information
jef-n committed Dec 6, 2015
1 parent bf79ecc commit 9c340d7b4b8fb7bf03182155d2e883b084e31e8d
@@ -262,6 +262,9 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
mNetworkTimeoutSpinBox->setValue( settings.value( "/qgis/networkAndProxy/networkTimeout", "60000" ).toInt() );
leUserAgent->setText( settings.value( "/qgis/networkAndProxy/userAgent", "Mozilla/5.0" ).toString() );

// WMS capabilities expiry time
mDefaultCapabilitiesExpirySpinBox->setValue( settings.value( "/qgis/defaultCapabilitiesExpiry", "24" ).toInt() );

This comment has been minimized.

Copy link
@strk

strk Dec 10, 2015

Contributor

which units is that number in ?

This comment has been minimized.

Copy link
@jef-n

jef-n Dec 10, 2015

Author Member

// WMS/WMS-C tile expiry time
mDefaultTileExpirySpinBox->setValue( settings.value( "/qgis/defaultTileExpiry", "24" ).toInt() );

@@ -1041,6 +1044,9 @@ void QgsOptions::saveOptions()
settings.setValue( "/qgis/networkAndProxy/networkTimeout", mNetworkTimeoutSpinBox->value() );
settings.setValue( "/qgis/networkAndProxy/userAgent", leUserAgent->text() );

// WMS capabiltiies expiry time
settings.setValue( "/qgis/defaultCapabilitiesExpiry", mDefaultCapabilitiesExpirySpinBox->value() );

// WMS/WMS-C tile expiry time
settings.setValue( "/qgis/defaultTileExpiry", mDefaultTileExpirySpinBox->value() );

@@ -4,6 +4,8 @@

#include <QFile>
#include <QDir>
#include <QNetworkCacheMetaData>
#include <QSettings>

#include "qgscoordinatetransform.h"
#include "qgsdatasourceuri.h"
@@ -709,11 +711,14 @@ void QgsWmsCapabilities::parseLayer( QDomElement const & e, QgsWmsLayerProperty&
//QgsDebugMsg( "entering." );

// TODO: Delete this stanza completely, depending on success of "Inherit things into the sublayer" below.
// // enforce WMS non-inheritance rules
// layerProperty.name = QString::null;
// layerProperty.title = QString::null;
// layerProperty.abstract = QString::null;
// layerProperty.keywordList.clear();
#if 0
// enforce WMS non-inheritance rules
layerProperty.name = QString::null;
layerProperty.title = QString::null;
layerProperty.abstract = QString::null;
layerProperty.keywordList.clear();
#endif

layerProperty.orderId = ++mLayerCount;
layerProperty.queryable = e.attribute( "queryable" ).toUInt();
layerProperty.cascaded = e.attribute( "cascaded" ).toUInt();
@@ -1860,20 +1865,22 @@ int QgsWmsCapabilities::identifyCapabilities() const

// -----------------

QgsWmsCapabilitiesDownload::QgsWmsCapabilitiesDownload( QObject *parent )
QgsWmsCapabilitiesDownload::QgsWmsCapabilitiesDownload( bool forceRefresh, QObject *parent )
: QObject( parent )
, mCapabilitiesReply( 0 )
, mIsAborted( false )
, mForceRefresh( forceRefresh )
{
connectManager();
}

QgsWmsCapabilitiesDownload::QgsWmsCapabilitiesDownload( const QString& baseUrl, const QgsWmsAuthorization& auth, QObject *parent )
QgsWmsCapabilitiesDownload::QgsWmsCapabilitiesDownload( const QString& baseUrl, const QgsWmsAuthorization& auth, bool forceRefresh, QObject *parent )
: QObject( parent )
, mBaseUrl( baseUrl )
, mAuth( auth )
, mCapabilitiesReply( 0 )
, mIsAborted( false )
, mForceRefresh( forceRefresh )
{
connectManager();
}
@@ -1902,7 +1909,7 @@ bool QgsWmsCapabilitiesDownload::downloadCapabilities( const QString& baseUrl, c

bool QgsWmsCapabilitiesDownload::downloadCapabilities()
{
QgsDebugMsg( "entering." );
QgsDebugMsg( QString( "entering: forceRefresh=%1" ).arg( mForceRefresh ) );
abort(); // cancel previous
mIsAborted = false;

@@ -1923,7 +1930,7 @@ bool QgsWmsCapabilitiesDownload::downloadCapabilities()
QgsMessageLog::logMessage( mError, tr( "WMS" ) );
return false;
}
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork );
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, mForceRefresh ? QNetworkRequest::PreferNetwork : QNetworkRequest::PreferCache );
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );

connect( QgsNetworkAccessManager::instance(), SIGNAL( requestSent( QNetworkReply *, QObject * ) ),
@@ -2014,13 +2021,13 @@ void QgsWmsCapabilitiesDownload::capabilitiesReplyFinished()
emit downloadFinished();
return;
}
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork );
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, mForceRefresh ? QNetworkRequest::PreferNetwork : QNetworkRequest::PreferCache );
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );

mCapabilitiesReply->deleteLater();
mCapabilitiesReply = 0;

QgsDebugMsg( QString( "redirected getcapabilities: %1" ).arg( redirect.toString() ) );
QgsDebugMsg( QString( "redirected getcapabilities: %1 forceRefresh=%2" ).arg( redirect.toString() ).arg( mForceRefresh ) );
//mCapabilitiesReply = QgsNetworkAccessManager::instance()->get( request );
connect( QgsNetworkAccessManager::instance(),
SIGNAL( requestSent( QNetworkReply *, QObject * ) ),
@@ -2031,6 +2038,37 @@ void QgsWmsCapabilitiesDownload::capabilitiesReplyFinished()
}
else
{
const QgsNetworkAccessManager *nam = QgsNetworkAccessManager::instance();

if ( nam->cache() )
{
QNetworkCacheMetaData cmd = nam->cache()->metaData( mCapabilitiesReply->request().url() );

QNetworkCacheMetaData::RawHeaderList hl;
Q_FOREACH ( const QNetworkCacheMetaData::RawHeader &h, cmd.rawHeaders() )
{
if ( h.first != "Cache-Control" )
hl.append( h );
}
cmd.setRawHeaders( hl );

QgsDebugMsg( QString( "expirationDate:%1" ).arg( cmd.expirationDate().toString() ) );
if ( cmd.expirationDate().isNull() )
{
QSettings s;
cmd.setExpirationDate( QDateTime::currentDateTime().addSecs( s.value( "/qgis/defaultCapabilitiesExpiry", "24" ).toInt() * 60 * 60 ) );
}

nam->cache()->updateMetaData( cmd );
}
else
{
QgsDebugMsg( "No cache for capabilites!" );
}

bool fromCache = mCapabilitiesReply->attribute( QNetworkRequest::SourceIsFromCacheAttribute ).toBool();
QgsDebugMsg( QString( "Capabilities reply was cached: %1" ).arg( fromCache ) );

mHttpCapabilitiesResponse = mCapabilitiesReply->readAll();

if ( mHttpCapabilitiesResponse.isEmpty() )
@@ -696,9 +696,9 @@ class QgsWmsCapabilitiesDownload : public QObject
Q_OBJECT

public:
explicit QgsWmsCapabilitiesDownload( QObject* parent = 0 );
explicit QgsWmsCapabilitiesDownload( bool forceRefresh, QObject* parent = 0 );

QgsWmsCapabilitiesDownload( const QString& baseUrl, const QgsWmsAuthorization& auth, QObject* parent = 0 );
QgsWmsCapabilitiesDownload( const QString& baseUrl, const QgsWmsAuthorization& auth, bool forceRefresh, QObject* parent = 0 );

virtual ~QgsWmsCapabilitiesDownload();

@@ -751,6 +751,7 @@ class QgsWmsCapabilitiesDownload : public QObject
QByteArray mHttpCapabilitiesResponse;

bool mIsAborted;
bool mForceRefresh;

private:
void connectManager();
@@ -30,7 +30,7 @@ QgsWMSConnectionItem::QgsWMSConnectionItem( QgsDataItem* parent, QString name, Q
, mCapabilitiesDownload( 0 )
{
mIconName = "mIconConnect.png";
mCapabilitiesDownload = new QgsWmsCapabilitiesDownload();
mCapabilitiesDownload = new QgsWmsCapabilitiesDownload( false );
}

QgsWMSConnectionItem::~QgsWMSConnectionItem()
@@ -874,13 +874,13 @@ void QgsWmsProvider::readBlock( int bandNo, QgsRectangle const & viewExtent, in
}


bool QgsWmsProvider::retrieveServerCapabilities( bool /*forceRefresh*/ )
bool QgsWmsProvider::retrieveServerCapabilities( bool forceRefresh )
{
QgsDebugMsg( "entering." );
QgsDebugMsg( QString( "entering: forceRefresh=%1" ).arg( forceRefresh ) );

if ( !mCaps.isValid() )
{
QgsWmsCapabilitiesDownload downloadCaps( mSettings.baseUrl(), mSettings.authorization() );
QgsWmsCapabilitiesDownload downloadCaps( mSettings.baseUrl(), mSettings.authorization(), forceRefresh );
if ( !downloadCaps.downloadCapabilities() )
{
mErrorFormat = "text/plain";
@@ -3693,7 +3693,7 @@ QgsWmsLegendDownloadHandler::startUrl( const QUrl& url )

QNetworkRequest request( url );
mSettings.authorization().setAuthorization( request );
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork );
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );

mReply = mNetworkAccessManager.get( request );
@@ -446,7 +446,7 @@ void QgsWMSSourceSelect::on_btnConnect_clicked()
return;
}

QgsWmsCapabilitiesDownload capDownload( wmsSettings.baseUrl(), wmsSettings.authorization() );
QgsWmsCapabilitiesDownload capDownload( wmsSettings.baseUrl(), wmsSettings.authorization(), true );
connect( &capDownload, SIGNAL( statusChanged( QString ) ), this, SLOT( showStatusMessage( QString ) ) );

QApplication::setOverrideCursor( Qt::WaitCursor );
@@ -4440,6 +4440,24 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_40">
<item>
<widget class="QLabel" name="label_45">
<property name="text">
<string>Default expiration period for WMS capabilities (hours)</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="mDefaultCapabilitiesExpirySpinBox">
<property name="maximum">
<number>100000000</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_17">
<item>

1 comment on commit 9c340d7

@nyalldawson

This comment has been minimized.

Copy link
Contributor

@nyalldawson nyalldawson commented on 9c340d7 Dec 6, 2015

@jef-n thank you! has anyone told you you're a legend lately? 👍

Please sign in to comment.
You can’t perform that action at this time.