28#include "moc_qgsconfigcache.cpp"
30using namespace Qt::StringLiterals;
42 u
"Initializing 'periodic' cache strategy"_s,
50 u
"Initializing 'off' cache strategy"_s,
58 u
"Initializing 'filesystem' cache strategy"_s,
72 u
"Project's cache is already initialized"_s,
85 qFatal(
"QgsConfigCache must be initialized before accessing QgsConfigCache instance." );
99 : mStrategy( strategy )
101 mStrategy->attach(
this );
111 if ( !mProjectCache[path] )
120 prj->setBadLayerHandler( badLayerHandler );
145 if ( prj->read( path, readFlags ) )
147 if ( !badLayerHandler->
badLayers().isEmpty() )
150 QStringList unrestrictedBadLayers;
152 const QStringList badLayerIds = badLayerHandler->
badLayers();
153 const QMap<QString, QString> badLayerNames = badLayerHandler->
badLayerNames();
155 for (
const QString &badLayerId : badLayerIds )
159 if ( badLayerNames.contains( badLayerId ) && restrictedLayers.contains( badLayerNames.value( badLayerId ) ) )
163 unrestrictedBadLayers.append( badLayerId );
165 if ( !unrestrictedBadLayers.isEmpty() )
171 u
"Error, Layer(s) %1 not valid in project %2"_s.arg( unrestrictedBadLayers.join(
", "_L1 ), path ),
179 u
"Warning, Layer(s) %1 not valid in project %2"_s.arg( unrestrictedBadLayers.join(
", "_L1 ), path ),
185 cacheProject( path, prj.release() );
190 u
"Error when loading project file '%1': %2 "_s.arg( path, prj->error() ),
196 auto entry = mProjectCache[path];
203 if ( ( settings && settings->
retryBadLayers() ) && ( entry->second->validCount() != entry->second->count() ) )
205 for (
const auto &l : entry->second->mapLayers() )
209 QString subsetString;
216 l->setDataSource( l->source(), l->name(), l->providerType(), options );
217 if ( vlayer && !subsetString.isEmpty() )
225 return entry->second.get();
232 const auto constKeys { mProjectCache.keys() };
233 for (
const auto &path : std::as_const( constKeys ) )
235 projects << mProjectCache[path]->second.get();
241QDomDocument *QgsConfigCache::xmlDocument(
const QString &filePath )
244 QFile configFile( filePath );
245 if ( !configFile.exists() )
251 if ( !configFile.open( QIODevice::ReadOnly ) )
258 QDomDocument *xmlDoc = mXmlDocumentCache.object( filePath );
262 xmlDoc =
new QDomDocument();
265 if ( !xmlDoc->setContent( &configFile,
true, &errorMsg, &line, &column ) )
271 mXmlDocumentCache.insert( filePath, xmlDoc );
272 xmlDoc = mXmlDocumentCache.object( filePath );
279void QgsConfigCache::cacheProject(
const QString &path,
QgsProject *project )
281 mProjectCache.insert( path,
new std::pair<QDateTime, std::unique_ptr<QgsProject>>(
project->lastModified(), std::unique_ptr<QgsProject>(
project ) ) );
283 mStrategy->entryInserted( path );
288 mProjectCache.remove( path );
291 mXmlDocumentCache.remove( path );
293 mStrategy->entryRemoved( path );
310 const auto constKeys { mProjectCache.keys() };
311 for (
const auto &path : std::as_const( constKeys ) )
313 const auto entry = mProjectCache[path];
314 if ( entry && entry->first < entry->second->lastModified() )
335 mFileSystemWatcher.removePath( path );
340 mFileSystemWatcher.addPath( path );
346 : mInterval( interval )
365 if ( !mTimer.isActive() )
367 mTimer.start( mInterval );
373 if ( mTimer.isActive() )
376 mTimer.start( msec );
@ DontLoad3DViews
Skip loading 3D views.
@ DontStoreOriginalStyles
Skip the initial XML style storage for layers. Useful for minimising project load times in non-intera...
@ ForceReadOnlyLayers
Open layers in a read-only mode.
@ TrustLayerMetadata
Trust layer metadata. Improves project read time. Do not use it if layers' extent is not fixed during...
@ DontUpgradeAnnotations
Don't upgrade old annotation items to QgsAnnotationItem.
@ DontLoadLayouts
Don't load print layouts. Improves project read time if layouts are not required, and allows projects...
QFlags< ProjectCapability > ProjectCapabilities
Flags which control project capabilities.
QFlags< ProjectReadFlag > ProjectReadFlags
Project load flags.
@ Warning
Warning message.
@ Critical
Critical/error message.
@ Info
Information message.
Abstract base class for implementing cache invalidation strategy.
Cache for server configuration.
QList< QgsProject * > projects() const
Returns projects currently in cache.
QgsConfigCache(QgsServerSettings *settings)
Initialize from settings.
void removeChangedEntry(const QString &path)
Remove cache entry.
void removeEntry(const QString &path)
Removes an entry from cache.
void removeChangedEntries()
Remove all changed cache entries.
static QgsConfigCache * instance()
Returns the current instance.
static void initialize(QgsServerSettings *settings)
Initialize from settings.
void projectRemovedFromCache(const QString &path)
Emitted whenever a project is removed from the cache.
const QgsProject * project(const QString &path, const QgsServerSettings *settings=nullptr)
If the project is not cached yet, then the project is read from the path.
File system cache strategy for server configuration.
void entryInserted(const QString &path) override
Called when an entry is inserted.
QgsFileSystemCacheStrategy()
Creates a new filesystem strategy.
void entryRemoved(const QString &path) override
Called when an entry is removed from cache.
void attach(QgsConfigCache *cache) override
Attach cache to this strategy.
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())
Adds a message to the log instance (and creates it if necessary).
Null system cache strategy for server configuration, completely disable cache invalidation invalidati...
void entryInserted(const QString &path) override
Called when an entry is inserted.
void entryRemoved(const QString &path) override
Called when an entry is removed from cache.
void attach(QgsConfigCache *owner) override
Attaches cache to this strategy.
Periodic system cache strategy for server configuration.
void entryInserted(const QString &path) override
Called when an entry is inserted.
void attach(QgsConfigCache *owner) override
Attaches cache to this strategy.
QgsPeriodicCacheStrategy(int interval=3000)
Creates a new periodic strategy.
void entryRemoved(const QString &path) override
Called when an entry is removed from cache.
void setCheckInterval(int msec)
Sets the invalidation check interval for PeriodicStrategy.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
static void setInstance(QgsProject *project)
Set the current project singleton instance to project.
Exception base class for server exceptions.
static QStringList wmsRestrictedLayers(const QgsProject &project)
Returns the restricted layer name list.
Provides a way to retrieve settings by prioritizing according to environment variables,...
bool getPrintDisabled() const
Returns true if WMS GetPrint request is disabled and the project's reading flag QgsProject::ReadFlag:...
int projectCacheSize() const
Returns the projects cache size The default value is 100, the value can be changed by setting the env...
int projectCacheCheckInterval() const
Returns the config cache check interval (in ms) for the 'periodic' strategy.
bool retryBadLayers() const
Returns true if bad layers should be re-checked after the project has been cached.
bool forceReadOnlyLayers() const
Returns true if the reading flag force layer read only is activated.
bool ignoreBadLayers() const
Returns true if the bad layers are ignored and false when the presence of a bad layers invalidates th...
QString projectCacheStrategy() const
Returns the project's cache strategy The default value is 'filesystem', the value can be changed by s...
bool trustLayerMetadata() const
Returns true if the reading flag trust layer metadata is activated.
Stores layer IDs for bad (broken) layers.
QMap< QString, QString > badLayerNames() const
Returns names of bad layers with ids.
QStringList badLayers() const
badLayers
Represents a vector layer which manages a vector based dataset.
virtual bool setSubsetString(const QString &subset)
Sets the string (typically sql) used to define a subset of the layer.
QgsAbstractCacheStrategy * getStrategyFromSettings(QgsServerSettings *settings)
Setting options for creating vector data providers.