QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 <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 
39 const 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 
53 void 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  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 == 0xff534d42 /* CIFS */ ) )
77  {
78  QFileInfo fi( configFilePath );
79  mCachedCapabilitiesTimestamps[ configFilePath ] = fi.lastModified();
80  mTimer.start( 1000 );
81  }
82 #endif
83 }
84 
86 {
87  mCachedCapabilities.remove( path );
88  mCachedCapabilitiesTimestamps.remove( path );
89  mFileSystemWatcher.removePath( path );
90 }
91 
92 void QgsCapabilitiesCache::removeChangedEntry( const QString &path )
93 {
94  QgsDebugMsg( QStringLiteral( "Remove capabilities cache entry because file changed" ) );
96 }
97 
98 void QgsCapabilitiesCache::removeOutdatedEntries()
99 {
100  QgsDebugMsg( QStringLiteral( "Checking for outdated entries" ) );
101  for ( const QString &configFilePath : mCachedCapabilitiesTimestamps.keys() )
102  {
103  QFileInfo fi( configFilePath );
104  if ( !fi.exists() || mCachedCapabilitiesTimestamps[ configFilePath ] < fi.lastModified() )
105  removeChangedEntry( configFilePath );
106  }
107 
108  if ( !mCachedCapabilitiesTimestamps.isEmpty() )
109  {
110  mTimer.start( 1000 );
111  }
112 }
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
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) ...
const QDomDocument * searchCapabilitiesDocument(const QString &configFilePath, const QString &key)
Returns cached capabilities document (or 0 if document for configuration file not in cache) ...
void removeCapabilitiesDocument(const QString &path)
Remove capabilities document.