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