QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 #include "qgslogger.h"
20 #include <QCoreApplication>
21 
23 {
24  QObject::connect( &mFileSystemWatcher, &QFileSystemWatcher::fileChanged, this, &QgsCapabilitiesCache::removeChangedEntry );
25 }
26 
27 const QDomDocument *QgsCapabilitiesCache::searchCapabilitiesDocument( const QString &configFilePath, const QString &key )
28 {
29  QCoreApplication::processEvents(); //get updates from file system watcher
30 
31  if ( mCachedCapabilities.contains( configFilePath ) && mCachedCapabilities[ configFilePath ].contains( key ) )
32  {
33  return &mCachedCapabilities[ configFilePath ][ key ];
34  }
35  else
36  {
37  return nullptr;
38  }
39 }
40 
41 void QgsCapabilitiesCache::insertCapabilitiesDocument( const QString &configFilePath, const QString &key, const QDomDocument *doc )
42 {
43  if ( mCachedCapabilities.size() > 40 )
44  {
45  //remove another cache entry to avoid memory problems
46  QHash<QString, QHash<QString, QDomDocument> >::iterator capIt = mCachedCapabilities.begin();
47  mFileSystemWatcher.removePath( capIt.key() );
48  mCachedCapabilities.erase( capIt );
49  }
50 
51  if ( !mCachedCapabilities.contains( configFilePath ) )
52  {
53  mFileSystemWatcher.addPath( configFilePath );
54  mCachedCapabilities.insert( configFilePath, QHash<QString, QDomDocument>() );
55  }
56 
57  mCachedCapabilities[ configFilePath ].insert( key, doc->cloneNode().toDocument() );
58 }
59 
61 {
62  mCachedCapabilities.remove( path );
63  mFileSystemWatcher.removePath( path );
64 }
65 
66 void QgsCapabilitiesCache::removeChangedEntry( const QString &path )
67 {
68  QgsDebugMsg( QStringLiteral( "Remove capabilities cache entry because file changed" ) );
69  mCachedCapabilities.remove( path );
70  mFileSystemWatcher.removePath( path );
71 }
#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.