Skip to content

Commit

Permalink
Re-enable reloading of changed project files
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Apr 6, 2014
1 parent 9af8498 commit 4e28719
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 163 deletions.
136 changes: 11 additions & 125 deletions src/mapserver/qgsconfigcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ QgsConfigCache* QgsConfigCache::instance()
return &mInstance;
}

QgsConfigCache::QgsConfigCache()
{
QObject::connect( &mFileSystemWatcher, SIGNAL( fileChanged( const QString& ) ), this, SLOT( removeChangedEntry( const QString& ) ) );
}

QgsConfigCache::~QgsConfigCache()
{
}
Expand All @@ -45,6 +50,7 @@ QgsWCSProjectParser* QgsConfigCache::wcsConfiguration( const QString& filePath )
}
p = new QgsWCSProjectParser( doc, filePath );
mWCSConfigCache.insert( filePath, p );
mFileSystemWatcher.addPath( filePath );
return p;
}

Expand All @@ -64,6 +70,7 @@ QgsWFSProjectParser* QgsConfigCache::wfsConfiguration( const QString& filePath )

p = new QgsWFSProjectParser( doc, filePath );
mWFSConfigCache.insert( filePath, p );
mFileSystemWatcher.addPath( filePath );
return p;
}

Expand Down Expand Up @@ -93,6 +100,7 @@ QgsWMSConfigParser* QgsConfigCache::wmsConfiguration( const QString& filePath )
}

mWMSConfigCache.insert( filePath, p );
mFileSystemWatcher.addPath( filePath );
return p;
}

Expand Down Expand Up @@ -120,132 +128,10 @@ QDomDocument* QgsConfigCache::xmlDocument( const QString& filePath )
return xmlDoc;
}

#if 0

#include "qgslogger.h"
#include "qgsmslayercache.h"
#include "qgsprojectfiletransform.h"
#include "qgsprojectparser.h"
#include "qgssldparser.h"
#include <QCoreApplication>


QgsConfigCache* QgsConfigCache::instance()
{
static QgsConfigCache mInstance;
return &mInstance;
}

QgsConfigCache::QgsConfigCache()
{
QObject::connect( &mFileSystemWatcher, SIGNAL( fileChanged( const QString& ) ), this, SLOT( removeChangedEntry( const QString& ) ) );
}

QgsConfigCache::~QgsConfigCache()
{
foreach ( QgsConfigParser *parser, mCachedConfigurations.values() )
{
delete parser;
}
}

QgsConfigParser* QgsConfigCache::searchConfiguration( const QString& filePath )
{
QCoreApplication::processEvents(); //check for updates from file system watcher
QgsConfigParser* p = mCachedConfigurations.value( filePath, 0 );

if ( p )
{
QgsDebugMsg( "Return configuration from cache" );
}
else
{
QgsDebugMsg( "Create new configuration" );
p = insertConfiguration( filePath );
}

if ( p )
{
//there could be more layers in a project than allowed by the cache per default
QgsMSLayerCache::instance()->setProjectMaxLayers( p->numberOfLayers() );
}

return p;
}

QgsConfigParser* QgsConfigCache::insertConfiguration( const QString& filePath )
{
if ( mCachedConfigurations.size() > 40 )
{
//remove a cache entry to avoid memory problems
QHash<QString, QgsConfigParser*>::iterator configIt = mCachedConfigurations.begin();
if ( configIt != mCachedConfigurations.end() )
{
mFileSystemWatcher.removePath( configIt.key() );
delete configIt.value();
mCachedConfigurations.erase( configIt );
}
}

//first open file
QFile* configFile = new QFile( filePath );
if ( !configFile->exists() || !configFile->open( QIODevice::ReadOnly ) )
{
QgsDebugMsg( "File unreadable: " + filePath );
delete configFile;
return 0;
}

//then create xml document
QDomDocument* configDoc = new QDomDocument();
QString errorMsg;
int line, column;
if ( !configDoc->setContent( configFile, true, &errorMsg, &line, &column ) )
{
QgsDebugMsg( QString( "Parse error %1 at row %2, column %3 in %4 " )
.arg( errorMsg ).arg( line ).arg( column ).arg( filePath ) );
delete configFile;
delete configDoc;
return 0;
}

//is it an sld document or a qgis project file?
QDomElement documentElem = configDoc->documentElement();
QgsConfigParser* configParser = 0;
if ( documentElem.tagName() == "StyledLayerDescriptor" )
{
configParser = new QgsSLDParser( configDoc );
}
else if ( documentElem.tagName() == "qgis" )
{
//convert project file to current version first
QgsProjectFileTransform pt( *configDoc, QgsProjectVersion( documentElem.attribute( "version" ) ) );
pt.updateRevision( QgsProjectVersion( QGis::QGIS_VERSION ) );
configParser = new QgsProjectParser( configDoc, filePath );
}
else
{
QgsDebugMsg( "SLD or qgis expected in " + filePath );
delete configDoc;
return 0;
}

mCachedConfigurations.insert( filePath, configParser );
mFileSystemWatcher.addPath( filePath );
delete configFile;
return configParser;
}

void QgsConfigCache::removeChangedEntry( const QString& path )
{
QgsDebugMsg( "Remove config cache entry because file changed" );
QHash<QString, QgsConfigParser*>::iterator configIt = mCachedConfigurations.find( path );
if ( configIt != mCachedConfigurations.end() )
{
delete configIt.value();
mCachedConfigurations.erase( configIt );
}
mWMSConfigCache.remove( path );
mWFSConfigCache.remove( path );
mWCSConfigCache.remove( path );
mFileSystemWatcher.removePath( path );
}

#endif //0
44 changes: 6 additions & 38 deletions src/mapserver/qgsconfigcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define QGSCONFIGCACHE_H

#include <QCache>
#include <QFileSystemWatcher>
#include <QObject>

class QgsWCSProjectParser;
Expand All @@ -39,55 +40,22 @@ class QgsConfigCache: public QObject
QgsWMSConfigParser* wmsConfiguration( const QString& filePath );

private:
QgsConfigCache();
static QgsConfigCache* mInstance;

/**Check for configuration file updates (remove entry from cache if file changes)*/
QFileSystemWatcher mFileSystemWatcher;

/**Returns xml document for project file / sld or 0 in case of errors*/
QDomDocument* xmlDocument( const QString& filePath );

QCache<QString, QgsWMSConfigParser> mWMSConfigCache;
QCache<QString, QgsWFSProjectParser> mWFSConfigCache;
QCache<QString, QgsWCSProjectParser> mWCSConfigCache;
};


#if 0

#include <QFileSystemWatcher>
#include <QHash>
#include <QObject>
#include <QString>

class QgsConfigParser;

/**A cache for configuration XML (useful because of the mapfile parameter)*/
class QgsConfigCache: public QObject
{
Q_OBJECT
public:
static QgsConfigCache* instance();
~QgsConfigCache();

/**Returns configuration for given config file path. The calling function does _not_ take ownership*/
QgsConfigParser* searchConfiguration( const QString& filePath );

protected:
QgsConfigCache();

private:
static QgsConfigCache* mInstance;

/**Creates configuration parser depending on the file type and, if successfull, inserts it to the cached configuration map
@param filePath path of the configuration file
@return the inserted config parser or 0 in case of error*/
QgsConfigParser* insertConfiguration( const QString& filePath );
/**Cached XML configuration documents. Key: file path, value: config parser. Default configuration has key '$default$'*/
QHash<QString, QgsConfigParser*> 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 //0

#endif // QGSCONFIGCACHE_H

0 comments on commit 4e28719

Please sign in to comment.