Skip to content

Commit

Permalink
qgis server: track timestamps of remote projects
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed May 15, 2019
1 parent 5f5340a commit 3017c7a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions python/server/auto_generated/qgscapabilitiescache.sip.in
Expand Up @@ -9,6 +9,7 @@




class QgsCapabilitiesCache : QObject
{
%Docstring
Expand Down
47 changes: 44 additions & 3 deletions src/server/qgscapabilitiescache.cpp
Expand Up @@ -16,12 +16,24 @@
***************************************************************************/

#include "qgscapabilitiescache.h"
#include "qgslogger.h"

#include <QCoreApplication>
#include <QFileInfo>

#if defined(Q_OS_LINUX)
#include <sys/vfs.h>
#endif

#include "qgslogger.h"


QgsCapabilitiesCache::QgsCapabilitiesCache()
{
QObject::connect( &mFileSystemWatcher, &QFileSystemWatcher::fileChanged, this, &QgsCapabilitiesCache::removeChangedEntry );

#if defined(Q_OS_LINUX)
QObject::connect( &mTimer, &QTimer::timeout, this, &QgsCapabilitiesCache::removeOutdatedEntries );
#endif
}

const QDomDocument *QgsCapabilitiesCache::searchCapabilitiesDocument( const QString &configFilePath, const QString &key )
Expand Down Expand Up @@ -55,17 +67,46 @@ void QgsCapabilitiesCache::insertCapabilitiesDocument( const QString &configFile
}

mCachedCapabilities[ configFilePath ].insert( key, doc->cloneNode().toDocument() );

#if defined(Q_OS_LINUX)
struct statfs sStatFS;
if ( statfs( configFilePath.toUtf8().constData(), &sStatFS ) == 0 &&
( sStatFS.f_type == 0x6969 /* NFS */ ||
sStatFS.f_type == 0x517b /* SMB */ ||
sStatFS.f_type == 0xff534d42 /* CIFS */ ) )
{
QFileInfo fi( configFilePath );
mCachedCapabilitiesTimestamps[ configFilePath ] = fi.lastModified();
mTimer.start( 1000 );
}
#endif
}

void QgsCapabilitiesCache::removeCapabilitiesDocument( const QString &path )
{
mCachedCapabilities.remove( path );
mCachedCapabilitiesTimestamps.remove( path );
mFileSystemWatcher.removePath( path );
}

void QgsCapabilitiesCache::removeChangedEntry( const QString &path )
{
QgsDebugMsg( QStringLiteral( "Remove capabilities cache entry because file changed" ) );
mCachedCapabilities.remove( path );
mFileSystemWatcher.removePath( path );
removeCapabilitiesDocument( path );
}

void QgsCapabilitiesCache::removeOutdatedEntries()
{
QgsDebugMsg( QStringLiteral( "Checking for outdated entries" ) );
for ( const QString &configFilePath : mCachedCapabilitiesTimestamps.keys() )
{
QFileInfo fi( configFilePath );
if ( !fi.exists() || mCachedCapabilitiesTimestamps[ configFilePath ] < fi.lastModified() )
removeChangedEntry( configFilePath );
}

if ( !mCachedCapabilitiesTimestamps.isEmpty() )
{
mTimer.start( 1000 );
}
}
7 changes: 7 additions & 0 deletions src/server/qgscapabilitiescache.h
Expand Up @@ -22,6 +22,9 @@
#include <QFileSystemWatcher>
#include <QHash>
#include <QObject>
#include <QDateTime>
#include <QTimer>

#include "qgis_server.h"

/**
Expand Down Expand Up @@ -58,11 +61,15 @@ class SERVER_EXPORT QgsCapabilitiesCache : public QObject

private:
QHash< QString, QHash< QString, QDomDocument > > mCachedCapabilities;
QHash< QString, QDateTime> mCachedCapabilitiesTimestamps;
QFileSystemWatcher mFileSystemWatcher;
QTimer mTimer;

private slots:
//! Removes changed entry from this cache
void removeChangedEntry( const QString &path );
//! Remove outdated enties
void removeOutdatedEntries();
};

#endif // QGSCAPABILITIESCACHE_H

0 comments on commit 3017c7a

Please sign in to comment.