35 QStringLiteral(
"Initializing 'periodic' cache strategy" ),
36 QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
42 QStringLiteral(
"Initializing 'off' cache strategy" ),
43 QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
49 QStringLiteral(
"Initializing 'filesystem' cache strategy" ),
50 QStringLiteral(
"Server" ), Qgis::MessageLevel::Info );
62 QStringLiteral(
"Project's cache is already initialized" ),
63 QStringLiteral(
"Server" ), Qgis::MessageLevel::Warning );
74 qFatal(
"QgsConfigCache must be initialized before accessing QgsConfigCache instance." );
87 : mStrategy( strategy )
89 mStrategy->attach(
this );
99 if ( !mProjectCache[ path ] )
102 std::unique_ptr<QgsProject> prj(
new QgsProject(
nullptr, Qgis::ProjectCapabilities() ) );
108 prj->setBadLayerHandler( badLayerHandler );
111 Qgis::ProjectReadFlags readFlags = Qgis::ProjectReadFlag::DontStoreOriginalStyles
112 | Qgis::ProjectReadFlag::DontLoad3DViews;
118 readFlags |= Qgis::ProjectReadFlag::TrustLayerMetadata;
123 readFlags |= Qgis::ProjectReadFlag::DontLoadLayouts;
127 if ( prj->read( path, readFlags ) )
129 if ( !badLayerHandler->
badLayers().isEmpty() )
132 QStringList unrestrictedBadLayers;
134 const QStringList badLayerIds = badLayerHandler->
badLayers();
135 const QMap<QString, QString> badLayerNames = badLayerHandler->
badLayerNames();
137 for (
const QString &badLayerId : badLayerIds )
141 if ( badLayerNames.contains( badLayerId ) &&
142 resctrictedLayers.contains( badLayerNames.value( badLayerId ) ) )
146 unrestrictedBadLayers.append( badLayerId );
148 if ( !unrestrictedBadLayers.isEmpty() )
154 QStringLiteral(
"Error, Layer(s) %1 not valid in project %2" ).arg( unrestrictedBadLayers.join( QLatin1String(
", " ) ), path ),
155 QStringLiteral(
"Server" ), Qgis::MessageLevel::Critical );
161 QStringLiteral(
"Warning, Layer(s) %1 not valid in project %2" ).arg( unrestrictedBadLayers.join( QLatin1String(
", " ) ), path ),
162 QStringLiteral(
"Server" ), Qgis::MessageLevel::Warning );
166 cacheProject( path, prj.release() );
171 QStringLiteral(
"Error when loading project file '%1': %2 " ).arg( path, prj->error() ),
172 QStringLiteral(
"Server" ), Qgis::MessageLevel::Critical );
176 auto entry = mProjectCache[ path ];
177 return entry ? entry->second.get() :
nullptr;
180 QDomDocument *QgsConfigCache::xmlDocument(
const QString &filePath )
183 QFile configFile( filePath );
184 if ( !configFile.exists() )
186 QgsMessageLog::logMessage(
"Error, configuration file '" + filePath +
"' does not exist", QStringLiteral(
"Server" ), Qgis::MessageLevel::Critical );
190 if ( !configFile.open( QIODevice::ReadOnly ) )
192 QgsMessageLog::logMessage(
"Error, cannot open configuration file '" + filePath +
"'", QStringLiteral(
"Server" ), Qgis::MessageLevel::Critical );
197 QDomDocument *xmlDoc = mXmlDocumentCache.object( filePath );
201 xmlDoc =
new QDomDocument();
204 if ( !xmlDoc->setContent( &configFile,
true, &errorMsg, &line, &column ) )
207 QStringLiteral(
"': parse error %1 at row %2, column %3" ).arg( errorMsg ).arg( line ).arg( column ), QStringLiteral(
"Server" ), Qgis::MessageLevel::Critical );
211 mXmlDocumentCache.insert( filePath, xmlDoc );
212 xmlDoc = mXmlDocumentCache.object( filePath );
219 void QgsConfigCache::cacheProject(
const QString &path,
QgsProject *project )
221 mProjectCache.insert( path,
new std::pair<QDateTime, std::unique_ptr<QgsProject> >(
project->
lastModified(), std::unique_ptr<QgsProject>(
project ) ) );
223 mStrategy->entryInserted( path );
228 mProjectCache.remove( path );
231 mXmlDocumentCache.remove( path );
233 mStrategy->entryRemoved( path );
248 const auto constKeys { mProjectCache.keys() };
249 for (
const auto &path : std::as_const( constKeys ) )
251 const auto entry = mProjectCache[ path ];
252 if ( entry && entry->first < entry->second->lastModified() )
274 mFileSystemWatcher.removePath( path );
279 mFileSystemWatcher.addPath( path );
285 : mInterval( interval )
305 if ( !mTimer.isActive() )
307 mTimer.start( mInterval );
313 if ( mTimer.isActive() )
316 mTimer.start( msec );