QGIS API Documentation 3.43.0-Master (32433f7016e)
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
19#include "qgsapplication.h"
20#include "qgslayertreelayer.h"
22#include "qgsvectorlayer.h"
23
24#include <QRegularExpression>
25
27{
28 switch ( error )
29 {
31 return QObject::tr( "Encoding is not correctly set. A non 'System' encoding is required" );
33 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" );
35 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" );
37 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" );
39 return QObject::tr( "The project root name (either the project short name or project title) is already used by a layer or a group" );
40 }
41 return QString();
42}
43
45template <class T>
46QString getShortName( T *node )
47{
48 const QString shortName = node->serverProperties()->shortName();
49 return shortName.isEmpty() ? node->name() : shortName;
50}
51
52void QgsProjectServerValidator::browseLayerTree( QgsLayerTreeGroup *treeGroup, QStringList &owsNames, QStringList &encodingMessages )
53{
54 const QList< QgsLayerTreeNode * > treeGroupChildren = treeGroup->children();
55 for ( int i = 0; i < treeGroupChildren.size(); ++i )
56 {
57 QgsLayerTreeNode *treeNode = treeGroupChildren.at( i );
58 if ( treeNode->nodeType() == QgsLayerTreeNode::NodeGroup )
59 {
60 QgsLayerTreeGroup *treeGroupChild = static_cast<QgsLayerTreeGroup *>( treeNode );
61 owsNames << getShortName( treeGroupChild );
62 browseLayerTree( treeGroupChild, owsNames, encodingMessages );
63 }
64 else
65 {
66 QgsLayerTreeLayer *treeLayer = static_cast<QgsLayerTreeLayer *>( treeNode );
67 QgsMapLayer *layer = treeLayer->layer();
68 if ( layer )
69 {
70 owsNames << getShortName( layer );
71 if ( layer->type() == Qgis::LayerType::Vector )
72 {
73 QgsVectorLayer *vl = static_cast<QgsVectorLayer *>( layer );
74 if ( vl->dataProvider() && vl->dataProvider()->encoding() == QLatin1String( "System" ) )
75 encodingMessages << layer->name();
76 }
77 }
78 }
79 }
80}
81
82bool QgsProjectServerValidator::validate( QgsProject *project, QList<QgsProjectServerValidator::ValidationResult> &results )
83{
84 results.clear();
85 bool result = true;
86
87 if ( !project )
88 return false;
89
90 if ( !project->layerTreeRoot() )
91 return false;
92
93 QStringList owsNames, encodingMessages;
94 browseLayerTree( project->layerTreeRoot(), owsNames, encodingMessages );
95
96 QStringList duplicateNames, regExpMessages;
97 const thread_local QRegularExpression snRegExp = QgsApplication::shortNameRegularExpression();
98 const auto constOwsNames = owsNames;
99 for ( const QString &name : constOwsNames )
100 {
101 if ( !snRegExp.match( name ).hasMatch() )
102 {
103 regExpMessages << name;
104 }
105
106 if ( duplicateNames.contains( name ) )
107 {
108 continue;
109 }
110
111 if ( owsNames.count( name ) > 1 )
112 {
113 duplicateNames << name;
114 }
115 }
116
117 if ( !duplicateNames.empty() )
118 {
119 result = false;
120 results << ValidationResult( QgsProjectServerValidator::DuplicatedNames, duplicateNames.join( QLatin1String( ", " ) ) );
121 }
122
123 if ( !regExpMessages.empty() )
124 {
125 result = false;
126 results << ValidationResult( QgsProjectServerValidator::LayerShortName, regExpMessages.join( QLatin1String( ", " ) ) );
127 }
128
129 if ( !encodingMessages.empty() )
130 {
131 result = false;
132 results << ValidationResult( QgsProjectServerValidator::LayerEncoding, encodingMessages.join( QLatin1String( ", " ) ) );
133 }
134
135 // Determine the root layername
136 QString rootLayerName = project->readEntry( QStringLiteral( "WMSRootName" ), QStringLiteral( "/" ), "" );
137 if ( rootLayerName.isEmpty() && !project->title().isEmpty() )
138 {
139 rootLayerName = project->title();
140 }
141 if ( !rootLayerName.isEmpty() )
142 {
143 if ( owsNames.count( rootLayerName ) >= 1 )
144 {
145 result = false;
147 }
148
149 if ( !snRegExp.match( rootLayerName ).hasMatch() )
150 {
151 result = false;
153 }
154 }
155
156 return result;
157}
@ Vector
Vector layer.
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.
Layer tree node points to a map layer.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
Base class for nodes in a layer tree.
@ 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...
Base class for all map layer types.
Definition qgsmaplayer.h:77
QString name
Definition qgsmaplayer.h:81
Qgis::LayerType type
Definition qgsmaplayer.h:87
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.
@ 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:107
QString title() const
Returns the project's title.
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.
QString encoding() const
Returns the encoding which is used for accessing data.
Represents a vector layer which manages a vector based dataset.
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.