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