QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
server
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
18
#include "
qgscapabilitiescache.h
"
19
20
#include <QCoreApplication>
21
#include <QDir>
22
#include <QFileInfo>
23
#include <QString>
24
25
#include "moc_qgscapabilitiescache.cpp"
26
27
using namespace
Qt::StringLiterals;
28
29
#if defined( Q_OS_LINUX )
30
#include <sys/vfs.h>
31
#endif
32
33
#include "
qgslogger.h
"
34
#include "
qgsserversettings.h
"
35
#include "
qgsmessagelog.h
"
36
37
const
QString
cacheKey
(
const
QString &pathIn )
38
{
39
// Clean the given file path so that in the case where the cache inserts/searches a path like folder\sub\filename.qgs or
40
// folder/sub/filename.qgs they will both be cleaned and resolve to the same cache entry
41
return
QDir::cleanPath( pathIn );
42
}
43
44
45
QgsCapabilitiesCache::QgsCapabilitiesCache
(
int
size )
46
: mCacheSize( size )
47
{
48
QObject::connect( &mFileSystemWatcher, &QFileSystemWatcher::fileChanged,
this
, &QgsCapabilitiesCache::removeChangedEntry );
49
50
#if defined( Q_OS_LINUX )
51
QObject::connect( &mTimer, &QTimer::timeout,
this
, &QgsCapabilitiesCache::removeOutdatedEntries );
52
#endif
53
}
54
55
const
QDomDocument *
QgsCapabilitiesCache::searchCapabilitiesDocument
(
const
QString &configFilePathIn,
const
QString &key )
56
{
57
QCoreApplication::processEvents();
//get updates from file system watcher
58
59
const
QString configFilePath =
cacheKey
( configFilePathIn );
60
if
( mCachedCapabilities.contains( configFilePath ) && mCachedCapabilities[configFilePath].contains( key ) )
61
{
62
return
&mCachedCapabilities[configFilePath][key];
63
}
64
else
65
{
66
return
nullptr
;
67
}
68
}
69
70
void
QgsCapabilitiesCache::insertCapabilitiesDocument
(
const
QString &configFilePathIn,
const
QString &key,
const
QDomDocument *doc )
71
{
72
const
QString configFilePath =
cacheKey
( configFilePathIn );
73
if
( mCachedCapabilities.size() > mCacheSize )
74
{
75
//remove another cache entry to avoid memory problems
76
const
QHash<QString, QHash<QString, QDomDocument>>::iterator capIt = mCachedCapabilities.begin();
77
mFileSystemWatcher.removePath( capIt.key() );
78
mCachedCapabilities.erase( capIt );
79
80
QgsMessageLog::logMessage
( u
"Removed cached WMS capabilities document because all %1 cache slots were taken"
_s.arg( mCacheSize ), u
"Server"
_s );
81
}
82
83
if
( !mCachedCapabilities.contains( configFilePath ) )
84
{
85
mFileSystemWatcher.addPath( configFilePath );
86
mCachedCapabilities.insert( configFilePath, QHash<QString, QDomDocument>() );
87
}
88
89
mCachedCapabilities[configFilePath].insert( key, doc->cloneNode().toDocument() );
90
91
#if defined( Q_OS_LINUX )
92
struct
statfs sStatFS;
93
if
( statfs( configFilePath.toUtf8().constData(), &sStatFS ) == 0
94
&& ( sStatFS.f_type == 0x6969
/* NFS */
|| sStatFS.f_type == 0x517b
/* SMB */
|| sStatFS.f_type == 0xff534d42ul
/* CIFS */
|| sStatFS.f_type == 0xfe534d42ul
/* CIFS */
) )
95
{
96
const
QFileInfo fi( configFilePath );
97
mCachedCapabilitiesTimestamps[configFilePath] = fi.lastModified();
98
mTimer.start( 1000 );
99
}
100
#endif
101
}
102
103
void
QgsCapabilitiesCache::removeCapabilitiesDocument
(
const
QString &pathIn )
104
{
105
const
QString path =
cacheKey
( pathIn );
106
mCachedCapabilities.remove( path );
107
mCachedCapabilitiesTimestamps.remove( path );
108
mFileSystemWatcher.removePath( path );
109
}
110
111
void
QgsCapabilitiesCache::removeChangedEntry(
const
QString &pathIn )
112
{
113
const
QString path =
cacheKey
( pathIn );
114
QgsDebugMsgLevel
( u
"Remove capabilities cache entry because file changed"
_s, 2 );
115
removeCapabilitiesDocument
( path );
116
}
117
118
void
QgsCapabilitiesCache::removeOutdatedEntries()
119
{
120
QgsDebugMsgLevel
( u
"Checking for outdated entries"
_s, 2 );
121
for
(
auto
it = mCachedCapabilitiesTimestamps.constBegin(); it != mCachedCapabilitiesTimestamps.constEnd(); it++ )
122
{
123
const
QString configFilePath = it.key();
124
const
QFileInfo fi( configFilePath );
125
if
( !fi.exists() || it.value() < fi.lastModified() )
126
removeChangedEntry( configFilePath );
127
}
128
129
if
( !mCachedCapabilitiesTimestamps.isEmpty() )
130
{
131
mTimer.start( 1000 );
132
}
133
}
QgsCapabilitiesCache::removeCapabilitiesDocument
void removeCapabilitiesDocument(const QString &path)
Removes capabilities document.
Definition
qgscapabilitiescache.cpp:103
QgsCapabilitiesCache::searchCapabilitiesDocument
const QDomDocument * searchCapabilitiesDocument(const QString &configFilePath, const QString &key)
Returns cached capabilities document (or 0 if document for configuration file not in cache).
Definition
qgscapabilitiescache.cpp:55
QgsCapabilitiesCache::QgsCapabilitiesCache
QgsCapabilitiesCache(int size)
Definition
qgscapabilitiescache.cpp:45
QgsCapabilitiesCache::insertCapabilitiesDocument
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).
Definition
qgscapabilitiescache.cpp:70
QgsMessageLog::logMessage
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(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
Definition
qgsmessagelog.cpp:35
cacheKey
const QString cacheKey(const QString &pathIn)
Definition
qgscapabilitiescache.cpp:37
qgscapabilitiescache.h
qgslogger.h
QgsDebugMsgLevel
#define QgsDebugMsgLevel(str, level)
Definition
qgslogger.h:80
qgsmessagelog.h
qgsserversettings.h
Generated on
for QGIS API Documentation by
1.17.0