QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsconfigcache.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsconfigcache.cpp
3  ------------------
4  begin : July 24th, 2010
5  copyright : (C) 2010 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 "qgsconfigcache.h"
19 #include "qgsmessagelog.h"
20 #include "qgsserverexception.h"
21 #include "qgsstorebadlayerinfo.h"
22 
23 #include <QFile>
24 
26 {
27  static QgsConfigCache *sInstance = nullptr;
28 
29  if ( !sInstance )
30  sInstance = new QgsConfigCache();
31 
32  return sInstance;
33 }
34 
35 QgsConfigCache::QgsConfigCache()
36 {
37  QObject::connect( &mFileSystemWatcher, &QFileSystemWatcher::fileChanged, this, &QgsConfigCache::removeChangedEntry );
38 }
39 
40 const QgsProject *QgsConfigCache::project( const QString &path )
41 {
42  if ( ! mProjectCache[ path ] )
43  {
44  std::unique_ptr<QgsProject> prj( new QgsProject() );
45  QgsStoreBadLayerInfo *badLayerHandler = new QgsStoreBadLayerInfo();
46  prj->setBadLayerHandler( badLayerHandler );
47  if ( prj->read( path ) )
48  {
49  if ( !badLayerHandler->badLayers().isEmpty() )
50  {
51  QString errorMsg = QStringLiteral( "Layer(s) %1 not valid" ).arg( badLayerHandler->badLayers().join( ',' ) );
52  QgsMessageLog::logMessage( errorMsg, QStringLiteral( "Server" ), Qgis::Critical );
53  throw QgsServerException( QStringLiteral( "Layer(s) not valid" ) );
54  }
55  mProjectCache.insert( path, prj.release() );
56  mFileSystemWatcher.addPath( path );
57  }
58  else
59  {
61  tr( "Error when loading project file '%1': %2 " ).arg( path, prj->error() ),
62  QStringLiteral( "Server" ), Qgis::Critical );
63  }
64  }
65  QgsProject::setInstance( mProjectCache[ path ] );
66  return mProjectCache[ path ];
67 }
68 
69 QDomDocument *QgsConfigCache::xmlDocument( const QString &filePath )
70 {
71  //first open file
72  QFile configFile( filePath );
73  if ( !configFile.exists() )
74  {
75  QgsMessageLog::logMessage( "Error, configuration file '" + filePath + "' does not exist", QStringLiteral( "Server" ), Qgis::Critical );
76  return nullptr;
77  }
78 
79  if ( !configFile.open( QIODevice::ReadOnly ) )
80  {
81  QgsMessageLog::logMessage( "Error, cannot open configuration file '" + filePath + "'", QStringLiteral( "Server" ), Qgis::Critical );
82  return nullptr;
83  }
84 
85  // first get cache
86  QDomDocument *xmlDoc = mXmlDocumentCache.object( filePath );
87  if ( !xmlDoc )
88  {
89  //then create xml document
90  xmlDoc = new QDomDocument();
91  QString errorMsg;
92  int line, column;
93  if ( !xmlDoc->setContent( &configFile, true, &errorMsg, &line, &column ) )
94  {
95  QgsMessageLog::logMessage( "Error parsing file '" + filePath +
96  QStringLiteral( "': parse error %1 at row %2, column %3" ).arg( errorMsg ).arg( line ).arg( column ), QStringLiteral( "Server" ), Qgis::Critical );
97  delete xmlDoc;
98  return nullptr;
99  }
100  mXmlDocumentCache.insert( filePath, xmlDoc );
101  mFileSystemWatcher.addPath( filePath );
102  xmlDoc = mXmlDocumentCache.object( filePath );
103  Q_ASSERT( xmlDoc );
104  }
105  return xmlDoc;
106 }
107 
108 void QgsConfigCache::removeChangedEntry( const QString &path )
109 {
110  mProjectCache.remove( path );
111 
112  //xml document must be removed last, as other config cache destructors may require it
113  mXmlDocumentCache.remove( path );
114 
115  mFileSystemWatcher.removePath( path );
116 }
117 
118 
119 void QgsConfigCache::removeEntry( const QString &path )
120 {
121  removeChangedEntry( path );
122 }
123 
QStringList badLayers() const
badLayers
void removeEntry(const QString &path)
Removes an entry from cache.
Stores layer ids of bad layers.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
Definition: qgsproject.h:89
const QgsProject * project(const QString &path)
If the project is not cached yet, then the project is read thanks to the path.
Exception base class for server exceptions.
Cache for server configuration.
static QgsConfigCache * instance()
Returns the current instance.