18#include "moc_qgsconfigcache.cpp"
36 QStringLiteral(
"Initializing 'periodic' cache strategy" ),
43 QStringLiteral(
"Initializing 'off' cache strategy" ),
50 QStringLiteral(
"Initializing 'filesystem' cache strategy" ),
63 QStringLiteral(
"Project's cache is already initialized" ),
75 qFatal(
"QgsConfigCache must be initialized before accessing QgsConfigCache instance." );
88 : mStrategy( strategy )
90 mStrategy->attach(
this );
100 if ( !mProjectCache[ path ] )
109 prj->setBadLayerHandler( badLayerHandler );
134 if ( prj->read( path, readFlags ) )
136 if ( !badLayerHandler->
badLayers().isEmpty() )
139 QStringList unrestrictedBadLayers;
141 const QStringList badLayerIds = badLayerHandler->
badLayers();
142 const QMap<QString, QString> badLayerNames = badLayerHandler->
badLayerNames();
144 for (
const QString &badLayerId : badLayerIds )
148 if ( badLayerNames.contains( badLayerId ) &&
149 resctrictedLayers.contains( badLayerNames.value( badLayerId ) ) )
153 unrestrictedBadLayers.append( badLayerId );
155 if ( !unrestrictedBadLayers.isEmpty() )
161 QStringLiteral(
"Error, Layer(s) %1 not valid in project %2" ).arg( unrestrictedBadLayers.join( QLatin1String(
", " ) ), path ),
168 QStringLiteral(
"Warning, Layer(s) %1 not valid in project %2" ).arg( unrestrictedBadLayers.join( QLatin1String(
", " ) ), path ),
173 cacheProject( path, prj.release() );
178 QStringLiteral(
"Error when loading project file '%1': %2 " ).arg( path, prj->error() ),
183 auto entry = mProjectCache[ path ];
184 return entry ? entry->second.get() :
nullptr;
191 const auto constKeys { mProjectCache.keys() };
192 for (
const auto &path : std::as_const( constKeys ) )
194 projects << mProjectCache[path]->second.get();
200QDomDocument *QgsConfigCache::xmlDocument(
const QString &filePath )
203 QFile configFile( filePath );
204 if ( !configFile.exists() )
210 if ( !configFile.open( QIODevice::ReadOnly ) )
217 QDomDocument *xmlDoc = mXmlDocumentCache.object( filePath );
221 xmlDoc =
new QDomDocument();
224 if ( !xmlDoc->setContent( &configFile,
true, &errorMsg, &line, &column ) )
227 QStringLiteral(
"': parse error %1 at row %2, column %3" ).arg( errorMsg ).arg( line ).arg( column ), QStringLiteral(
"Server" ),
Qgis::MessageLevel::Critical );
231 mXmlDocumentCache.insert( filePath, xmlDoc );
232 xmlDoc = mXmlDocumentCache.object( filePath );
239void QgsConfigCache::cacheProject(
const QString &path,
QgsProject *project )
241 mProjectCache.insert( path,
new std::pair<QDateTime, std::unique_ptr<QgsProject> >(
project->
lastModified(), std::unique_ptr<QgsProject>(
project ) ) );
243 mStrategy->entryInserted( path );
248 mProjectCache.remove( path );
251 mXmlDocumentCache.remove( path );
253 mStrategy->entryRemoved( path );
270 const auto constKeys { mProjectCache.keys() };
271 for (
const auto &path : std::as_const( constKeys ) )
273 const auto entry = mProjectCache[ path ];
274 if ( entry && entry->first < entry->second->lastModified() )
296 mFileSystemWatcher.removePath( path );
301 mFileSystemWatcher.addPath( path );
307 : mInterval( interval )
327 if ( !mTimer.isActive() )
329 mTimer.start( mInterval );
335 if ( mTimer.isActive() )
338 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)
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.
QDateTime lastModified() const
Returns last modified time of the project file as returned by the file system (or other project stora...
Exception base class for server exceptions.
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 projectCacheCheckInterval() const
Returns the config cache check interval (in ms) for the 'periodic' strategy.
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 of bad layers.
QMap< QString, QString > badLayerNames() const
Returns names of bad layers with ids.
QStringList badLayers() const
badLayers
SERVER_EXPORT QStringList wmsRestrictedLayers(const QgsProject &project)
Returns the restricted layer name list.
QgsAbstractCacheStrategy * getStrategyFromSettings(QgsServerSettings *settings)