QGIS API Documentation 3.41.0-Master (af5edcb665c)
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#include "moc_qgscapabilitiescache.cpp"
20
21#include <QCoreApplication>
22#include <QFileInfo>
23
24#if defined( Q_OS_LINUX )
25#include <sys/vfs.h>
26#endif
27
28#include "qgslogger.h"
29#include "qgsserversettings.h"
30#include "qgsmessagelog.h"
31
32
34 : mCacheSize( size )
35{
36 QObject::connect( &mFileSystemWatcher, &QFileSystemWatcher::fileChanged, this, &QgsCapabilitiesCache::removeChangedEntry );
37
38#if defined( Q_OS_LINUX )
39 QObject::connect( &mTimer, &QTimer::timeout, this, &QgsCapabilitiesCache::removeOutdatedEntries );
40#endif
41}
42
43const QDomDocument *QgsCapabilitiesCache::searchCapabilitiesDocument( const QString &configFilePath, const QString &key )
44{
45 QCoreApplication::processEvents(); //get updates from file system watcher
46
47 if ( mCachedCapabilities.contains( configFilePath ) && mCachedCapabilities[configFilePath].contains( key ) )
48 {
49 return &mCachedCapabilities[configFilePath][key];
50 }
51 else
52 {
53 return nullptr;
54 }
55}
56
57void QgsCapabilitiesCache::insertCapabilitiesDocument( const QString &configFilePath, const QString &key, const QDomDocument *doc )
58{
59 if ( mCachedCapabilities.size() > mCacheSize )
60 {
61 //remove another cache entry to avoid memory problems
62 const QHash<QString, QHash<QString, QDomDocument>>::iterator capIt = mCachedCapabilities.begin();
63 mFileSystemWatcher.removePath( capIt.key() );
64 mCachedCapabilities.erase( capIt );
65
66 QgsMessageLog::logMessage( QStringLiteral( "Removed cached WMS capabilities document because all %1 cache slots were taken" ).arg( mCacheSize ), QStringLiteral( "Server" ) );
67 }
68
69 if ( !mCachedCapabilities.contains( configFilePath ) )
70 {
71 mFileSystemWatcher.addPath( configFilePath );
72 mCachedCapabilities.insert( configFilePath, QHash<QString, QDomDocument>() );
73 }
74
75 mCachedCapabilities[configFilePath].insert( key, doc->cloneNode().toDocument() );
76
77#if defined( Q_OS_LINUX )
78 struct statfs sStatFS;
79 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 */ ) )
80 {
81 const QFileInfo fi( configFilePath );
82 mCachedCapabilitiesTimestamps[configFilePath] = fi.lastModified();
83 mTimer.start( 1000 );
84 }
85#endif
86}
87
89{
90 mCachedCapabilities.remove( path );
91 mCachedCapabilitiesTimestamps.remove( path );
92 mFileSystemWatcher.removePath( path );
93}
94
95void QgsCapabilitiesCache::removeChangedEntry( const QString &path )
96{
97 QgsDebugMsgLevel( QStringLiteral( "Remove capabilities cache entry because file changed" ), 2 );
99}
100
101void QgsCapabilitiesCache::removeOutdatedEntries()
102{
103 QgsDebugMsgLevel( QStringLiteral( "Checking for outdated entries" ), 2 );
104 for ( auto it = mCachedCapabilitiesTimestamps.constBegin(); it != mCachedCapabilitiesTimestamps.constEnd(); it++ )
105 {
106 const QString configFilePath = it.key();
107 const QFileInfo fi( configFilePath );
108 if ( !fi.exists() || it.value() < fi.lastModified() )
109 removeChangedEntry( configFilePath );
110 }
111
112 if ( !mCachedCapabilitiesTimestamps.isEmpty() )
113 {
114 mTimer.start( 1000 );
115 }
116}
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)
Adds a message to the log instance (and creates it if necessary).
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39