QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsprojectservervalidator.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprojectservervalidator.cpp
3 ---------------------------
4 begin : March 2020
5 copyright : (C) 2020 by Etienne Trimaille
6 email : etienne dot trimaille at gmail dot com
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
20
21#include "qgsapplication.h"
22#include "qgslayertreelayer.h"
23#include "qgsvectorlayer.h"
24
25#include <QRegularExpression>
26
28{
29 switch ( error )
30 {
32 return QObject::tr( "Encoding is not correctly set. A non 'System' encoding is required" );
34 return QObject::tr( "Layer short name is not valid. It must start with an unaccented alphabetical letter, followed by any alphanumeric letters, dot, dash or underscore" );
36 return QObject::tr( "One or more layers or groups have the same name or short name. Both the 'name' and 'short name' for layers and groups must be unique" );
38 return QObject::tr( "The project root name (either the project short name or project title) is not valid. It must start with an unaccented alphabetical letter, followed by any alphanumeric letters, dot, dash or underscore" );
40 return QObject::tr( "The project root name (either the project short name or project title) is already used by a layer or a group" );
42 return QObject::tr( "Use only maptip for HTML GetFeatureInfo response is enabled but the HTML maptip is empty" );
43 }
44 return QString();
45}
46
48template <class T>
49QString getShortName( T *node )
50{
51 const QString shortName = node->serverProperties()->shortName();
52 return shortName.isEmpty() ? node->name() : shortName;
53}
54
55void QgsProjectServerValidator::browseLayerTree( QgsLayerTreeGroup *treeGroup, QStringList &owsNames, QStringList &encodingMessages, QStringList &layerNames, QStringList &maptipTemplates )
56{
57 const QList< QgsLayerTreeNode * > treeGroupChildren = treeGroup->children();
58 for ( int i = 0; i < treeGroupChildren.size(); ++i )
59 {
60 QgsLayerTreeNode *treeNode = treeGroupChildren.at( i );
61 if ( treeNode->nodeType() == QgsLayerTreeNode::NodeGroup )
62 {
63 QgsLayerTreeGroup *treeGroupChild = static_cast<QgsLayerTreeGroup *>( treeNode );
64 owsNames << getShortName( treeGroupChild );
65 browseLayerTree( treeGroupChild, owsNames, encodingMessages, layerNames, maptipTemplates );
66 }
67 else
68 {
69 QgsLayerTreeLayer *treeLayer = static_cast<QgsLayerTreeLayer *>( treeNode );
70 QgsMapLayer *layer = treeLayer->layer();
71 if ( layer )
72 {
73 owsNames << getShortName( layer );
74 if ( layer->type() == Qgis::LayerType::Vector )
75 {
76 QgsVectorLayer *vl = static_cast<QgsVectorLayer *>( layer );
77 if ( vl->dataProvider() && vl->dataProvider()->encoding() == QLatin1String( "System" ) )
78 encodingMessages << layer->name();
79 }
80 layerNames << treeLayer->name();
81 maptipTemplates << layer->mapTipTemplate();
82 }
83 }
84 }
85}
86
87bool QgsProjectServerValidator::isOnlyMaptipEnabled( QgsProject *project )
88{
89 return project->readBoolEntry(
90 QStringLiteral( "WMSHTMLFeatureInfoUseOnlyMaptip" ),
91 QString(),
92 false
93 );
94}
95
96bool QgsProjectServerValidator::validate( QgsProject *project, QList<QgsProjectServerValidator::ValidationResult> &results )
97{
98 results.clear();
99 bool result = true;
100
101 if ( !project )
102 return false;
103
104 if ( !project->layerTreeRoot() )
105 return false;
106
107 QStringList owsNames, encodingMessages, layerNames, maptipTemplates;
108 browseLayerTree( project->layerTreeRoot(), owsNames, encodingMessages, layerNames, maptipTemplates );
109
110 QStringList duplicateNames, regExpMessages;
111 const thread_local QRegularExpression snRegExp = QgsApplication::shortNameRegularExpression();
112 const auto constOwsNames = owsNames;
113 for ( const QString &name : constOwsNames )
114 {
115 if ( !snRegExp.match( name ).hasMatch() )
116 {
117 regExpMessages << name;
118 }
119
120 if ( duplicateNames.contains( name ) )
121 {
122 continue;
123 }
124
125 if ( owsNames.count( name ) > 1 )
126 {
127 duplicateNames << name;
128 }
129 }
130
131 if ( !duplicateNames.empty() )
132 {
133 result = false;
134 results << ValidationResult( QgsProjectServerValidator::DuplicatedNames, duplicateNames.join( QLatin1String( ", " ) ) );
135 }
136
137 if ( !regExpMessages.empty() )
138 {
139 result = false;
140 results << ValidationResult( QgsProjectServerValidator::LayerShortName, regExpMessages.join( QLatin1String( ", " ) ) );
141 }
142
143 if ( !encodingMessages.empty() )
144 {
145 result = false;
146 results << ValidationResult( QgsProjectServerValidator::LayerEncoding, encodingMessages.join( QLatin1String( ", " ) ) );
147 }
148
149 // Determine the root layername
150 QString rootLayerName = project->readEntry( QStringLiteral( "WMSRootName" ), QStringLiteral( "/" ), "" );
151 if ( rootLayerName.isEmpty() && !project->title().isEmpty() )
152 {
153 rootLayerName = project->title();
154 }
155 if ( !rootLayerName.isEmpty() )
156 {
157 if ( owsNames.count( rootLayerName ) >= 1 )
158 {
159 result = false;
161 }
162
163 if ( !snRegExp.match( rootLayerName ).hasMatch() )
164 {
165 result = false;
167 }
168 }
169 if ( isOnlyMaptipEnabled( project ) )
170 {
171 QStringList emptyLayers;
172 for ( int i = 0; i < maptipTemplates.size(); ++i )
173 {
174 if ( maptipTemplates[i].trimmed().isEmpty() )
175 emptyLayers << layerNames[i];
176 }
177
178 if ( !emptyLayers.isEmpty() )
179 {
180 result = false;
181 QString details = emptyLayers.join( QLatin1String( ", " ) ).toHtmlEscaped();
182 results << ValidationResult(
184 details );
185 }
186 }
187
188 return result;
189}
@ Vector
Vector layer.
Definition qgis.h:191
static QRegularExpression shortNameRegularExpression()
Returns the short name regular expression for line edit validator.
Layer tree group node serves as a container for layers and further groups.
QString name() const override
Returns the layer's name.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
@ NodeGroup
Container of other groups and layers.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
NodeType nodeType() const
Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree...
QString name
Definition qgsmaplayer.h:84
Qgis::LayerType type
Definition qgsmaplayer.h:90
QString mapTipTemplate
Definition qgsmaplayer.h:93
static QString displayValidationError(QgsProjectServerValidator::ValidationError error)
Returns a human readable string for a given error.
ValidationError
Errors that might be raised by the validation process.
@ LayerEncoding
Encoding is not correctly set on a vector layer.
@ ProjectRootNameConflict
The project root name is already used by a layer or a group.
@ ProjectShortName
The project short name is not valid.
@ DuplicatedNames
A duplicated layer/group name in the layer tree.
@ OnlyMaptipTrueButEmptyMaptip
Use only maptip for HTML GetFeatureInfo response is enabled but HTML maptip is empty.
@ LayerShortName
Layer/group short name is not valid.
static bool validate(QgsProject *project, QList< QgsProjectServerValidator::ValidationResult > &results)
Validates a project to detect problems on QGIS Server, and returns true if it's considered valid.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:109
QString title
Definition qgsproject.h:112
QString readEntry(const QString &scope, const QString &key, const QString &def=QString(), bool *ok=nullptr) const
Reads a string from the specified scope and key.
QgsLayerTree * layerTreeRoot() const
Returns pointer to the root (invisible) node of the project's layer tree.
bool readBoolEntry(const QString &scope, const QString &key, bool def=false, bool *ok=nullptr) const
Reads a boolean from the specified scope and key.
QString encoding() const
Returns the encoding which is used for accessing data.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.
QString getShortName(T *node)
helper method to retrieve a layer or layer tree group short name
Contains the parameters describing a project validation failure.