44
55#include < QFile>
66#include < QDir>
7+ #include < QNetworkCacheMetaData>
8+ #include < QSettings>
79
810#include " qgscoordinatetransform.h"
911#include " qgsdatasourceuri.h"
@@ -709,11 +711,14 @@ void QgsWmsCapabilities::parseLayer( QDomElement const & e, QgsWmsLayerProperty&
709711 // QgsDebugMsg( "entering." );
710712
711713// TODO: Delete this stanza completely, depending on success of "Inherit things into the sublayer" below.
712- // // enforce WMS non-inheritance rules
713- // layerProperty.name = QString::null;
714- // layerProperty.title = QString::null;
715- // layerProperty.abstract = QString::null;
716- // layerProperty.keywordList.clear();
714+ #if 0
715+ // enforce WMS non-inheritance rules
716+ layerProperty.name = QString::null;
717+ layerProperty.title = QString::null;
718+ layerProperty.abstract = QString::null;
719+ layerProperty.keywordList.clear();
720+ #endif
721+
717722 layerProperty.orderId = ++mLayerCount ;
718723 layerProperty.queryable = e.attribute ( " queryable" ).toUInt ();
719724 layerProperty.cascaded = e.attribute ( " cascaded" ).toUInt ();
@@ -1860,20 +1865,22 @@ int QgsWmsCapabilities::identifyCapabilities() const
18601865
18611866// -----------------
18621867
1863- QgsWmsCapabilitiesDownload::QgsWmsCapabilitiesDownload ( QObject *parent )
1868+ QgsWmsCapabilitiesDownload::QgsWmsCapabilitiesDownload ( bool forceRefresh, QObject *parent )
18641869 : QObject( parent )
18651870 , mCapabilitiesReply( 0 )
18661871 , mIsAborted( false )
1872+ , mForceRefresh( forceRefresh )
18671873{
18681874 connectManager ();
18691875}
18701876
1871- QgsWmsCapabilitiesDownload::QgsWmsCapabilitiesDownload ( const QString& baseUrl, const QgsWmsAuthorization& auth, QObject *parent )
1877+ QgsWmsCapabilitiesDownload::QgsWmsCapabilitiesDownload ( const QString& baseUrl, const QgsWmsAuthorization& auth, bool forceRefresh, QObject *parent )
18721878 : QObject( parent )
18731879 , mBaseUrl( baseUrl )
18741880 , mAuth( auth )
18751881 , mCapabilitiesReply( 0 )
18761882 , mIsAborted( false )
1883+ , mForceRefresh( forceRefresh )
18771884{
18781885 connectManager ();
18791886}
@@ -1902,7 +1909,7 @@ bool QgsWmsCapabilitiesDownload::downloadCapabilities( const QString& baseUrl, c
19021909
19031910bool QgsWmsCapabilitiesDownload::downloadCapabilities ()
19041911{
1905- QgsDebugMsg ( " entering. " );
1912+ QgsDebugMsg ( QString ( " entering: forceRefresh=%1 " ). arg ( mForceRefresh ) );
19061913 abort (); // cancel previous
19071914 mIsAborted = false ;
19081915
@@ -1923,7 +1930,7 @@ bool QgsWmsCapabilitiesDownload::downloadCapabilities()
19231930 QgsMessageLog::logMessage ( mError , tr ( " WMS" ) );
19241931 return false ;
19251932 }
1926- request.setAttribute ( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork );
1933+ request.setAttribute ( QNetworkRequest::CacheLoadControlAttribute, mForceRefresh ? QNetworkRequest::PreferNetwork : QNetworkRequest::PreferCache );
19271934 request.setAttribute ( QNetworkRequest::CacheSaveControlAttribute, true );
19281935
19291936 connect ( QgsNetworkAccessManager::instance (), SIGNAL ( requestSent ( QNetworkReply *, QObject * ) ),
@@ -2014,13 +2021,13 @@ void QgsWmsCapabilitiesDownload::capabilitiesReplyFinished()
20142021 emit downloadFinished ();
20152022 return ;
20162023 }
2017- request.setAttribute ( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork );
2024+ request.setAttribute ( QNetworkRequest::CacheLoadControlAttribute, mForceRefresh ? QNetworkRequest::PreferNetwork : QNetworkRequest::PreferCache );
20182025 request.setAttribute ( QNetworkRequest::CacheSaveControlAttribute, true );
20192026
20202027 mCapabilitiesReply ->deleteLater ();
20212028 mCapabilitiesReply = 0 ;
20222029
2023- QgsDebugMsg ( QString ( " redirected getcapabilities: %1" ).arg ( redirect.toString () ) );
2030+ QgsDebugMsg ( QString ( " redirected getcapabilities: %1 forceRefresh=%2 " ).arg ( redirect.toString () ). arg ( mForceRefresh ) );
20242031 // mCapabilitiesReply = QgsNetworkAccessManager::instance()->get( request );
20252032 connect ( QgsNetworkAccessManager::instance (),
20262033 SIGNAL ( requestSent ( QNetworkReply *, QObject * ) ),
@@ -2031,6 +2038,37 @@ void QgsWmsCapabilitiesDownload::capabilitiesReplyFinished()
20312038 }
20322039 else
20332040 {
2041+ const QgsNetworkAccessManager *nam = QgsNetworkAccessManager::instance ();
2042+
2043+ if ( nam->cache () )
2044+ {
2045+ QNetworkCacheMetaData cmd = nam->cache ()->metaData ( mCapabilitiesReply ->request ().url () );
2046+
2047+ QNetworkCacheMetaData::RawHeaderList hl;
2048+ Q_FOREACH ( const QNetworkCacheMetaData::RawHeader &h, cmd.rawHeaders () )
2049+ {
2050+ if ( h.first != " Cache-Control" )
2051+ hl.append ( h );
2052+ }
2053+ cmd.setRawHeaders ( hl );
2054+
2055+ QgsDebugMsg ( QString ( " expirationDate:%1" ).arg ( cmd.expirationDate ().toString () ) );
2056+ if ( cmd.expirationDate ().isNull () )
2057+ {
2058+ QSettings s;
2059+ cmd.setExpirationDate ( QDateTime::currentDateTime ().addSecs ( s.value ( " /qgis/defaultCapabilitiesExpiry" , " 24" ).toInt () * 60 * 60 ) );
2060+ }
2061+
2062+ nam->cache ()->updateMetaData ( cmd );
2063+ }
2064+ else
2065+ {
2066+ QgsDebugMsg ( " No cache for capabilites!" );
2067+ }
2068+
2069+ bool fromCache = mCapabilitiesReply ->attribute ( QNetworkRequest::SourceIsFromCacheAttribute ).toBool ();
2070+ QgsDebugMsg ( QString ( " Capabilities reply was cached: %1" ).arg ( fromCache ) );
2071+
20342072 mHttpCapabilitiesResponse = mCapabilitiesReply ->readAll ();
20352073
20362074 if ( mHttpCapabilitiesResponse .isEmpty () )
0 commit comments