diff --git a/src/mapserver/CMakeLists.txt b/src/mapserver/CMakeLists.txt index 85bfd0a4721a..bdebbf3066e8 100644 --- a/src/mapserver/CMakeLists.txt +++ b/src/mapserver/CMakeLists.txt @@ -62,6 +62,8 @@ SET ( qgis_mapserv_SRCS SET (qgis_mapserv_MOC_HDRS qgsftptransaction.h + qgscapabilitiescache.h + qgsconfigcache.h ) SET (qgis_mapserv_RCCS diff --git a/src/mapserver/qgscapabilitiescache.cpp b/src/mapserver/qgscapabilitiescache.cpp index 3620606a586c..7253c94e838d 100644 --- a/src/mapserver/qgscapabilitiescache.cpp +++ b/src/mapserver/qgscapabilitiescache.cpp @@ -16,9 +16,12 @@ ***************************************************************************/ #include "qgscapabilitiescache.h" +#include "qgsmapserverlogger.h" +#include QgsCapabilitiesCache::QgsCapabilitiesCache() { + QObject::connect( &mFileSystemWatcher, SIGNAL( fileChanged( const QString& ) ), this, SLOT( removeChangedEntry( const QString& ) ) ); } QgsCapabilitiesCache::~QgsCapabilitiesCache() @@ -27,6 +30,7 @@ QgsCapabilitiesCache::~QgsCapabilitiesCache() const QDomDocument* QgsCapabilitiesCache::searchCapabilitiesDocument( const QString& configFilePath ) const { + QCoreApplication::processEvents(); //get updates from file system watcher QHash< QString, QDomDocument >::const_iterator it = mCachedCapabilities.find( configFilePath ); if( it == mCachedCapabilities.constEnd() ) { @@ -44,7 +48,20 @@ void QgsCapabilitiesCache::insertCapabilitiesDocument( const QString& configFile { //remove another cache entry to avoid memory problems QHash::iterator capIt = mCachedCapabilities.begin(); + mFileSystemWatcher.removePath( capIt.key() ); mCachedCapabilities.erase( capIt ); } mCachedCapabilities.insert( configFilePath, doc->cloneNode().toDocument() ); + mFileSystemWatcher.addPath( configFilePath ); +} + +void QgsCapabilitiesCache::removeChangedEntry( const QString& path ) +{ + QgsMSDebugMsg( "Remove capabilities cache entry because file changed" ); + QHash< QString, QDomDocument >::iterator it = mCachedCapabilities.find( path ); + if( it != mCachedCapabilities.end() ) + { + mCachedCapabilities.erase( it ); + } + mFileSystemWatcher.removePath( path ); } diff --git a/src/mapserver/qgscapabilitiescache.h b/src/mapserver/qgscapabilitiescache.h index ee2e5e2f15d1..086ba3f71210 100644 --- a/src/mapserver/qgscapabilitiescache.h +++ b/src/mapserver/qgscapabilitiescache.h @@ -19,11 +19,14 @@ #define QGSCAPABILITIESCACHE_H #include +#include #include +#include /**A cache for capabilities xml documents (by configuration file path)*/ -class QgsCapabilitiesCache +class QgsCapabilitiesCache: public QObject { + Q_OBJECT public: QgsCapabilitiesCache(); ~QgsCapabilitiesCache(); @@ -35,6 +38,11 @@ class QgsCapabilitiesCache private: QHash< QString, QDomDocument > mCachedCapabilities; + QFileSystemWatcher mFileSystemWatcher; + + private slots: + /**Removes changed entry from this cache*/ + void removeChangedEntry( const QString& path ); }; #endif // QGSCAPABILITIESCACHE_H diff --git a/src/mapserver/qgsconfigcache.cpp b/src/mapserver/qgsconfigcache.cpp index ef3e65b7a1a9..a0b4a21e4c0b 100644 --- a/src/mapserver/qgsconfigcache.cpp +++ b/src/mapserver/qgsconfigcache.cpp @@ -20,9 +20,11 @@ #include "qgsmslayercache.h" #include "qgsprojectparser.h" #include "qgssldparser.h" +#include QgsConfigCache::QgsConfigCache() { + QObject::connect( &mFileSystemWatcher, SIGNAL( fileChanged( const QString& ) ), this, SLOT( removeChangedEntry( const QString& ) ) ); } QgsConfigCache::~QgsConfigCache() @@ -36,6 +38,7 @@ QgsConfigCache::~QgsConfigCache() QgsConfigParser* QgsConfigCache::searchConfiguration( const QString& filePath ) { + QCoreApplication::processEvents(); //check for updates from file system watcher QgsConfigParser* p = 0; QHash::const_iterator configIt = mCachedConfigurations.find( filePath ); if ( configIt == mCachedConfigurations.constEnd() ) @@ -63,7 +66,12 @@ QgsConfigParser* QgsConfigCache::insertConfiguration( const QString& filePath ) { //remove a cache entry to avoid memory problems QHash::iterator configIt = mCachedConfigurations.begin(); - mCachedConfigurations.erase( configIt ); + if ( configIt != mCachedConfigurations.end() ) + { + mFileSystemWatcher.removePath( configIt.key() ); + delete configIt.value(); + mCachedConfigurations.erase( configIt ); + } } //first open file @@ -107,6 +115,19 @@ QgsConfigParser* QgsConfigCache::insertConfiguration( const QString& filePath ) } mCachedConfigurations.insert( filePath, configParser ); + mFileSystemWatcher.addPath( filePath ); delete configFile; return configParser; } + +void QgsConfigCache::removeChangedEntry( const QString& path ) +{ + QgsMSDebugMsg( "Remove config cache entry because file changed" ); + QHash::iterator configIt = mCachedConfigurations.find( path ); + if ( configIt != mCachedConfigurations.end() ) + { + delete configIt.value(); + mCachedConfigurations.erase( configIt ); + } + mFileSystemWatcher.removePath( path ); +} diff --git a/src/mapserver/qgsconfigcache.h b/src/mapserver/qgsconfigcache.h index a8e28011dd93..9685f1c84cf5 100644 --- a/src/mapserver/qgsconfigcache.h +++ b/src/mapserver/qgsconfigcache.h @@ -18,14 +18,17 @@ #ifndef QGSCONFIGCACHE_H #define QGSCONFIGCACHE_H +#include #include +#include #include class QgsConfigParser; /**A cache for configuration XML (useful because of the mapfile parameter)*/ -class QgsConfigCache +class QgsConfigCache: public QObject { + Q_OBJECT public: QgsConfigCache(); ~QgsConfigCache(); @@ -40,6 +43,12 @@ class QgsConfigCache QgsConfigParser* insertConfiguration( const QString& filePath ); /**Cached XML configuration documents. Key: file path, value: config parser. Default configuration has key '$default$'*/ QHash mCachedConfigurations; + /**Check for configuration file updates (remove entry from cache if file changes)*/ + QFileSystemWatcher mFileSystemWatcher; + + private slots: + /**Removes changed entry from this cache*/ + void removeChangedEntry( const QString& path ); }; #endif // QGSCONFIGCACHE_H