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