QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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
29
31{
32 QObject::connect( &mFileSystemWatcher, &QFileSystemWatcher::fileChanged, this, &QgsCapabilitiesCache::removeChangedEntry );
33
34#if defined(Q_OS_LINUX)
35 QObject::connect( &mTimer, &QTimer::timeout, this, &QgsCapabilitiesCache::removeOutdatedEntries );
36#endif
37}
38
39const QDomDocument *QgsCapabilitiesCache::searchCapabilitiesDocument( const QString &configFilePath, const QString &key )
40{
41 QCoreApplication::processEvents(); //get updates from file system watcher
42
43 if ( mCachedCapabilities.contains( configFilePath ) && mCachedCapabilities[ configFilePath ].contains( key ) )
44 {
45 return &mCachedCapabilities[ configFilePath ][ key ];
46 }
47 else
48 {
49 return nullptr;
50 }
51}
52
53void QgsCapabilitiesCache::insertCapabilitiesDocument( const QString &configFilePath, const QString &key, const QDomDocument *doc )
54{
55 if ( mCachedCapabilities.size() > 40 )
56 {
57 //remove another cache entry to avoid memory problems
58 const QHash<QString, QHash<QString, QDomDocument> >::iterator capIt = mCachedCapabilities.begin();
59 mFileSystemWatcher.removePath( capIt.key() );
60 mCachedCapabilities.erase( capIt );
61 }
62
63 if ( !mCachedCapabilities.contains( configFilePath ) )
64 {
65 mFileSystemWatcher.addPath( configFilePath );
66 mCachedCapabilities.insert( configFilePath, QHash<QString, QDomDocument>() );
67 }
68
69 mCachedCapabilities[ configFilePath ].insert( key, doc->cloneNode().toDocument() );
70
71#if defined(Q_OS_LINUX)
72 struct statfs sStatFS;
73 if ( statfs( configFilePath.toUtf8().constData(), &sStatFS ) == 0 &&
74 ( sStatFS.f_type == 0x6969 /* NFS */ ||
75 sStatFS.f_type == 0x517b /* SMB */ ||
76 sStatFS.f_type == 0xff534d42ul /* CIFS */ ||
77 sStatFS.f_type == 0xfe534d42ul /* CIFS */ ) )
78 {
79 const QFileInfo fi( configFilePath );
80 mCachedCapabilitiesTimestamps[ configFilePath ] = fi.lastModified();
81 mTimer.start( 1000 );
82 }
83#endif
84}
85
87{
88 mCachedCapabilities.remove( path );
89 mCachedCapabilitiesTimestamps.remove( path );
90 mFileSystemWatcher.removePath( path );
91}
92
93void QgsCapabilitiesCache::removeChangedEntry( const QString &path )
94{
95 QgsDebugMsg( QStringLiteral( "Remove capabilities cache entry because file changed" ) );
97}
98
99void QgsCapabilitiesCache::removeOutdatedEntries()
100{
101 QgsDebugMsg( QStringLiteral( "Checking for outdated entries" ) );
102 for ( const QString &configFilePath : mCachedCapabilitiesTimestamps.keys() )
103 {
104 const QFileInfo fi( configFilePath );
105 if ( !fi.exists() || mCachedCapabilitiesTimestamps[ configFilePath ] < fi.lastModified() )
106 removeChangedEntry( configFilePath );
107 }
108
109 if ( !mCachedCapabilitiesTimestamps.isEmpty() )
110 {
111 mTimer.start( 1000 );
112 }
113}
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)
#define QgsDebugMsg(str)
Definition: qgslogger.h:38