20#include <QCoreApplication>
25#include "moc_qgscapabilitiescache.cpp"
27using namespace Qt::StringLiterals;
29#if defined( Q_OS_LINUX )
41 return QDir::cleanPath( pathIn );
48 QObject::connect( &mFileSystemWatcher, &QFileSystemWatcher::fileChanged,
this, &QgsCapabilitiesCache::removeChangedEntry );
50#if defined( Q_OS_LINUX )
51 QObject::connect( &mTimer, &QTimer::timeout,
this, &QgsCapabilitiesCache::removeOutdatedEntries );
57 QCoreApplication::processEvents();
59 const QString configFilePath =
cacheKey( configFilePathIn );
60 if ( mCachedCapabilities.contains( configFilePath ) && mCachedCapabilities[configFilePath].contains( key ) )
62 return &mCachedCapabilities[configFilePath][key];
72 const QString configFilePath =
cacheKey( configFilePathIn );
73 if ( mCachedCapabilities.size() > mCacheSize )
76 const QHash<QString, QHash<QString, QDomDocument>>::iterator capIt = mCachedCapabilities.begin();
77 mFileSystemWatcher.removePath( capIt.key() );
78 mCachedCapabilities.erase( capIt );
80 QgsMessageLog::logMessage( u
"Removed cached WMS capabilities document because all %1 cache slots were taken"_s.arg( mCacheSize ), u
"Server"_s );
83 if ( !mCachedCapabilities.contains( configFilePath ) )
85 mFileSystemWatcher.addPath( configFilePath );
86 mCachedCapabilities.insert( configFilePath, QHash<QString, QDomDocument>() );
89 mCachedCapabilities[configFilePath].insert( key, doc->cloneNode().toDocument() );
91#if defined( Q_OS_LINUX )
92 struct statfs sStatFS;
93 if ( statfs( configFilePath.toUtf8().constData(), &sStatFS ) == 0 && ( sStatFS.f_type == 0x6969 || sStatFS.f_type == 0x517b || sStatFS.f_type == 0xff534d42ul || sStatFS.f_type == 0xfe534d42ul ) )
95 const QFileInfo fi( configFilePath );
96 mCachedCapabilitiesTimestamps[configFilePath] = fi.lastModified();
104 const QString path =
cacheKey( pathIn );
105 mCachedCapabilities.remove( path );
106 mCachedCapabilitiesTimestamps.remove( path );
107 mFileSystemWatcher.removePath( path );
110void QgsCapabilitiesCache::removeChangedEntry(
const QString &pathIn )
112 const QString path =
cacheKey( pathIn );
113 QgsDebugMsgLevel( u
"Remove capabilities cache entry because file changed"_s, 2 );
117void QgsCapabilitiesCache::removeOutdatedEntries()
120 for (
auto it = mCachedCapabilitiesTimestamps.constBegin(); it != mCachedCapabilitiesTimestamps.constEnd(); it++ )
122 const QString configFilePath = it.key();
123 const QFileInfo fi( configFilePath );
124 if ( !fi.exists() || it.value() < fi.lastModified() )
125 removeChangedEntry( configFilePath );
128 if ( !mCachedCapabilitiesTimestamps.isEmpty() )
130 mTimer.start( 1000 );
void removeCapabilitiesDocument(const QString &path)
Removes capabilities document.
const QDomDocument * searchCapabilitiesDocument(const QString &configFilePath, const QString &key)
Returns cached capabilities document (or 0 if document for configuration file not in cache).
QgsCapabilitiesCache(int size)
void insertCapabilitiesDocument(const QString &configFilePath, const QString &key, const QDomDocument *doc)
Inserts new capabilities document (creates a copy of the document, does not take ownership).
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
const QString cacheKey(const QString &pathIn)
#define QgsDebugMsgLevel(str, level)