QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgscapabilitiescache.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscapabilitiescache.h
3 ----------------------
4 begin : May 11th, 2011
5 copyright : (C) 2011 by Marco Hugentobler
6 email : marco dot hugentobler at sourcepole dot ch
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include <QCoreApplication>
21#include <QDir>
22#include <QFileInfo>
23#include <QString>
24
25#include "moc_qgscapabilitiescache.cpp"
26
27using namespace Qt::StringLiterals;
28
29#if defined( Q_OS_LINUX )
30#include <sys/vfs.h>
31#endif
32
33#include "qgslogger.h"
34#include "qgsserversettings.h"
35#include "qgsmessagelog.h"
36
37const QString cacheKey( const QString &pathIn )
38{
39 // Clean the given file path so that in the case where the cache inserts/searches a path like folder\sub\filename.qgs or
40 // folder/sub/filename.qgs they will both be cleaned and resolve to the same cache entry
41 return QDir::cleanPath( pathIn );
42}
43
44
46 : mCacheSize( size )
47{
48 QObject::connect( &mFileSystemWatcher, &QFileSystemWatcher::fileChanged, this, &QgsCapabilitiesCache::removeChangedEntry );
49
50#if defined( Q_OS_LINUX )
51 QObject::connect( &mTimer, &QTimer::timeout, this, &QgsCapabilitiesCache::removeOutdatedEntries );
52#endif
53}
54
55const QDomDocument *QgsCapabilitiesCache::searchCapabilitiesDocument( const QString &configFilePathIn, const QString &key )
56{
57 QCoreApplication::processEvents(); //get updates from file system watcher
58
59 const QString configFilePath = cacheKey( configFilePathIn );
60 if ( mCachedCapabilities.contains( configFilePath ) && mCachedCapabilities[configFilePath].contains( key ) )
61 {
62 return &mCachedCapabilities[configFilePath][key];
63 }
64 else
65 {
66 return nullptr;
67 }
68}
69
70void QgsCapabilitiesCache::insertCapabilitiesDocument( const QString &configFilePathIn, const QString &key, const QDomDocument *doc )
71{
72 const QString configFilePath = cacheKey( configFilePathIn );
73 if ( mCachedCapabilities.size() > mCacheSize )
74 {
75 //remove another cache entry to avoid memory problems
76 const QHash<QString, QHash<QString, QDomDocument>>::iterator capIt = mCachedCapabilities.begin();
77 mFileSystemWatcher.removePath( capIt.key() );
78 mCachedCapabilities.erase( capIt );
79
80 QgsMessageLog::logMessage( u"Removed cached WMS capabilities document because all %1 cache slots were taken"_s.arg( mCacheSize ), u"Server"_s );
81 }
82
83 if ( !mCachedCapabilities.contains( configFilePath ) )
84 {
85 mFileSystemWatcher.addPath( configFilePath );
86 mCachedCapabilities.insert( configFilePath, QHash<QString, QDomDocument>() );
87 }
88
89 mCachedCapabilities[configFilePath].insert( key, doc->cloneNode().toDocument() );
90
91#if defined( Q_OS_LINUX )
92 struct statfs sStatFS;
93 if ( statfs( configFilePath.toUtf8().constData(), &sStatFS ) == 0 && ( sStatFS.f_type == 0x6969 /* NFS */ || sStatFS.f_type == 0x517b /* SMB */ || sStatFS.f_type == 0xff534d42ul /* CIFS */ || sStatFS.f_type == 0xfe534d42ul /* CIFS */ ) )
94 {
95 const QFileInfo fi( configFilePath );
96 mCachedCapabilitiesTimestamps[configFilePath] = fi.lastModified();
97 mTimer.start( 1000 );
98 }
99#endif
100}
101
103{
104 const QString path = cacheKey( pathIn );
105 mCachedCapabilities.remove( path );
106 mCachedCapabilitiesTimestamps.remove( path );
107 mFileSystemWatcher.removePath( path );
108}
109
110void QgsCapabilitiesCache::removeChangedEntry( const QString &pathIn )
111{
112 const QString path = cacheKey( pathIn );
113 QgsDebugMsgLevel( u"Remove capabilities cache entry because file changed"_s, 2 );
115}
116
117void QgsCapabilitiesCache::removeOutdatedEntries()
118{
119 QgsDebugMsgLevel( u"Checking for outdated entries"_s, 2 );
120 for ( auto it = mCachedCapabilitiesTimestamps.constBegin(); it != mCachedCapabilitiesTimestamps.constEnd(); it++ )
121 {
122 const QString configFilePath = it.key();
123 const QFileInfo fi( configFilePath );
124 if ( !fi.exists() || it.value() < fi.lastModified() )
125 removeChangedEntry( configFilePath );
126 }
127
128 if ( !mCachedCapabilitiesTimestamps.isEmpty() )
129 {
130 mTimer.start( 1000 );
131 }
132}
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).
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)
Definition qgslogger.h:63