QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsserversettings.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsserversettings.cpp
3  ---------------------
4  begin : December 19, 2016
5  copyright : (C) 2016 by Paul Blottiere
6  email : paul dot blottiere at oslandia dot com
7 
8 ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #include "qgsserversettings.h"
20 #include "qgsapplication.h"
21 
22 #include <QSettings>
23 #include <QDir>
24 
26 {
27  load();
28 }
29 
30 void QgsServerSettings::initSettings()
31 {
32  mSettings.clear();
33 
34  // options path
35  const Setting sOptPath = { QgsServerSettingsEnv::QGIS_OPTIONS_PATH,
37  QStringLiteral( "Override the default path for user configuration" ),
38  QString(),
39  QVariant::String,
40  QVariant( "" ),
41  QVariant()
42  };
43  mSettings[ sOptPath.envVar ] = sOptPath;
44 
45  // parallel rendering
48  QStringLiteral( "Activate/Deactivate parallel rendering for WMS getMap request" ),
49  QStringLiteral( "/qgis/parallel_rendering" ),
50  QVariant::Bool,
51  QVariant( false ),
52  QVariant()
53  };
54  mSettings[ sParRend.envVar ] = sParRend;
55 
56  // max threads
57  const Setting sMaxThreads = { QgsServerSettingsEnv::QGIS_SERVER_MAX_THREADS,
59  QStringLiteral( "Number of threads to use when parallel rendering is activated" ),
60  QStringLiteral( "/qgis/max_threads" ),
61  QVariant::Int,
62  QVariant( -1 ),
63  QVariant()
64  };
65  mSettings[ sMaxThreads.envVar ] = sMaxThreads;
66 
67  // log level
68  const Setting sLogLevel = { QgsServerSettingsEnv::QGIS_SERVER_LOG_LEVEL,
70  QStringLiteral( "Log level" ),
71  QString(),
72  QVariant::Int,
73  QVariant( Qgis::None ),
74  QVariant()
75  };
76  mSettings[ sLogLevel.envVar ] = sLogLevel;
77 
78  // log file
79  const Setting sLogFile = { QgsServerSettingsEnv::QGIS_SERVER_LOG_FILE,
81  QStringLiteral( "Log file" ),
82  QString(),
83  QVariant::String,
84  QVariant( "" ),
85  QVariant()
86  };
87  mSettings[ sLogFile.envVar ] = sLogFile;
88 
89  // log to stderr
90  const Setting sLogStderr = { QgsServerSettingsEnv::QGIS_SERVER_LOG_STDERR,
92  QStringLiteral( "Activate/Deactivate logging to stderr" ),
93  QString(),
94  QVariant::Bool,
95  QVariant( false ),
96  QVariant()
97  };
98  mSettings[ sLogStderr.envVar ] = sLogStderr;
99 
100  // project file
101  const Setting sProject = { QgsServerSettingsEnv::QGIS_PROJECT_FILE,
103  QStringLiteral( "QGIS project file" ),
104  QString(),
105  QVariant::String,
106  QVariant( "" ),
107  QVariant()
108  };
109  mSettings[ sProject.envVar ] = sProject;
110 
111  // max cache layers
112  const Setting sMaxCacheLayers = { QgsServerSettingsEnv::MAX_CACHE_LAYERS,
114  QStringLiteral( "Specify the maximum number of cached layers" ),
115  QString(),
116  QVariant::Int,
117  QVariant( 100 ),
118  QVariant()
119  };
120  mSettings[ sMaxCacheLayers.envVar ] = sMaxCacheLayers;
121 
122  // cache directory
123  const Setting sCacheDir = { QgsServerSettingsEnv::QGIS_SERVER_CACHE_DIRECTORY,
125  QStringLiteral( "Specify the cache directory" ),
126  QStringLiteral( "/cache/directory" ),
127  QVariant::String,
128  QVariant( QgsApplication::qgisSettingsDirPath() + "cache" ),
129  QVariant()
130  };
131  mSettings[ sCacheDir.envVar ] = sCacheDir;
132 
133  // cache size
134  const Setting sCacheSize = { QgsServerSettingsEnv::QGIS_SERVER_CACHE_SIZE,
136  QStringLiteral( "Specify the cache size" ),
137  QStringLiteral( "/cache/size" ),
138  QVariant::LongLong,
139  QVariant( 50 * 1024 * 1024 ),
140  QVariant()
141  };
142  mSettings[ sCacheSize.envVar ] = sCacheSize;
143 
144  // system locale override
145  const Setting sOverrideSystemLocale = { QgsServerSettingsEnv::QGIS_SERVER_OVERRIDE_SYSTEM_LOCALE,
147  QStringLiteral( "Override system locale" ),
148  QStringLiteral( "/locale/userLocale" ),
149  QVariant::String,
150  QVariant( "" ),
151  QVariant()
152  };
153  mSettings[ sOverrideSystemLocale.envVar ] = sOverrideSystemLocale;
154 
155  // bad layers handling
156  const Setting sIgnoreBadLayers = { QgsServerSettingsEnv::QGIS_SERVER_IGNORE_BAD_LAYERS,
158  QStringLiteral( "Ignore bad layers" ),
159  QString(),
160  QVariant::Bool,
161  QVariant( false ),
162  QVariant()
163  };
164  mSettings[ sIgnoreBadLayers.envVar ] = sIgnoreBadLayers;
165 
166  // show group separator
167  const Setting sShowGroupSeparator = { QgsServerSettingsEnv::QGIS_SERVER_SHOW_GROUP_SEPARATOR,
169  QStringLiteral( "Show group (thousands) separator" ),
170  QStringLiteral( "/locale/showGroupSeparator" ),
171  QVariant::String,
172  QVariant( false ),
173  QVariant()
174  };
175  mSettings[ sShowGroupSeparator.envVar ] = sShowGroupSeparator;
176 
177  // max height
178  const Setting sMaxHeight = { QgsServerSettingsEnv::QGIS_SERVER_WMS_MAX_HEIGHT,
180  QStringLiteral( "Maximum height for a WMS request. The lower one of this and the project configuration is used." ),
181  QStringLiteral( "/qgis/max_wms_height" ),
182  QVariant::LongLong,
183  QVariant( -1 ),
184  QVariant()
185  };
186  mSettings[ sMaxHeight.envVar ] = sMaxHeight;
187 
188  // max width
189  const Setting sMaxWidth = { QgsServerSettingsEnv::QGIS_SERVER_WMS_MAX_WIDTH,
191  QStringLiteral( "Maximum width for a WMS request. The most conservative between this and the project one is used" ),
192  QStringLiteral( "/qgis/max_wms_width" ),
193  QVariant::LongLong,
194  QVariant( -1 ),
195  QVariant()
196  };
197  mSettings[ sMaxWidth.envVar ] = sMaxWidth;
198 
199  // API templates and static override directory
200  const Setting sApiResourcesDirectory = { QgsServerSettingsEnv::QGIS_SERVER_API_RESOURCES_DIRECTORY,
202  QStringLiteral( "Base directory where HTML templates and static assets (e.g. images, js and css files) are searched for" ),
203  QStringLiteral( "/qgis/server_api_resources_directory" ),
204  QVariant::String,
205  QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( QStringLiteral( "resources/server/api" ) ),
206  QString()
207  };
208 
209  mSettings[ sApiResourcesDirectory.envVar ] = sApiResourcesDirectory;
210 
211  // API WFS3 max limit
212  const Setting sApiWfs3MaxLimit = { QgsServerSettingsEnv::QGIS_SERVER_API_WFS3_MAX_LIMIT,
214  QStringLiteral( "Maximum value for \"limit\" in a features request, defaults to 10000" ),
215  QStringLiteral( "/qgis/server_api_wfs3_max_limit" ),
216  QVariant::LongLong,
217  QVariant( 10000 ),
218  QVariant()
219  };
220 
221  mSettings[ sApiWfs3MaxLimit.envVar ] = sApiWfs3MaxLimit;
222 }
223 
225 {
226  // init settings each time to take into account QgsApplication and
227  // QCoreApplication configuration for some default values
228  initSettings();
229 
230  // store environment variables
231  QMap<QgsServerSettingsEnv::EnvVar, QString> env = getEnv();
232 
233  // load QSettings if QGIS_OPTIONS_PATH is defined
234  loadQSettings( env[ QgsServerSettingsEnv::QGIS_OPTIONS_PATH ] );
235 
236  // prioritize values: 'env var' -> 'ini file' -> 'default value'
237  prioritize( env );
238 }
239 
240 bool QgsServerSettings::load( const QString &envVarName )
241 {
242  bool rc( false );
243  const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerSettingsEnv::EnvVar>() );
244  const int value = metaEnum.keyToValue( envVarName.toStdString().c_str() );
245 
246  if ( value >= 0 )
247  {
248  const QString envValue( getenv( envVarName.toStdString().c_str() ) );
249  prioritize( QMap<QgsServerSettingsEnv::EnvVar, QString> { {( QgsServerSettingsEnv::EnvVar ) value, envValue } } );
250  rc = true;
251  }
252 
253  return rc;
254 }
255 
256 QMap<QgsServerSettingsEnv::EnvVar, QString> QgsServerSettings::getEnv() const
257 {
258  QMap<QgsServerSettingsEnv::EnvVar, QString> env;
259 
260  const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerSettingsEnv::EnvVar>() );
261  for ( int i = 0; i < metaEnum.keyCount(); i++ )
262  {
263  env[( QgsServerSettingsEnv::EnvVar ) metaEnum.value( i )] = getenv( metaEnum.key( i ) );
264  }
265 
266  return env;
267 }
268 
269 QVariant QgsServerSettings::value( QgsServerSettingsEnv::EnvVar envVar ) const
270 {
271  if ( mSettings[ envVar ].src == QgsServerSettingsEnv::DEFAULT_VALUE )
272  {
273  return mSettings[ envVar ].defaultVal;
274  }
275  else
276  {
277  return mSettings[ envVar ].val;
278  }
279 }
280 
281 void QgsServerSettings::loadQSettings( const QString &envOptPath ) const
282 {
283  if ( ! envOptPath.isEmpty() )
284  {
285  QSettings::setDefaultFormat( QSettings::IniFormat );
286  QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, envOptPath );
287  }
288 }
289 
290 void QgsServerSettings::prioritize( const QMap<QgsServerSettingsEnv::EnvVar, QString> &env )
291 {
292  for ( QgsServerSettingsEnv::EnvVar e : env.keys() )
293  {
294  Setting s = mSettings[ e ];
295 
296  QVariant varValue;
297  if ( ! env.value( e ).isEmpty() )
298  {
299  varValue.setValue( env.value( e ) );
300  }
301 
302  if ( ! varValue.isNull() && varValue.canConvert( s.type ) )
303  {
304  s.val = varValue;
306  }
307  else if ( ! s.iniKey.isEmpty() && QSettings().contains( s.iniKey ) && QSettings().value( s.iniKey ).canConvert( s.type ) )
308  {
309  s.val = QSettings().value( s.iniKey );
311  }
312  else
313  {
314  s.val = QVariant();
316  }
317 
318  // an empty string can be returned from QSettings. In this case, we want
319  // to use the default value
320  if ( s.type == QVariant::String && s.val.toString().isEmpty() )
321  {
322  s.val = QVariant();
324  }
325 
326  mSettings[ e ] = s;
327  }
328 }
329 
331 {
332  const QMetaEnum metaEnumSrc( QMetaEnum::fromType<QgsServerSettingsEnv::Source>() );
333  const QMetaEnum metaEnumEnv( QMetaEnum::fromType<QgsServerSettingsEnv::EnvVar>() );
334 
335  QgsMessageLog::logMessage( "QGIS Server Settings: ", "Server", Qgis::Info );
336  for ( Setting s : mSettings )
337  {
338  const QString src = metaEnumSrc.valueToKey( s.src );
339  const QString var = metaEnumEnv.valueToKey( s.envVar );
340 
341  const QString msg = " - " + var + " / '" + s.iniKey + "' (" + s.descr + "): '" + value( s.envVar ).toString() + "' (read from " + src + ")";
342  QgsMessageLog::logMessage( msg, "Server", Qgis::Info );
343  }
344 
345  if ( ! iniFile().isEmpty() )
346  {
347  const QString msg = "Ini file used to initialize settings: " + iniFile();
348  QgsMessageLog::logMessage( msg, "Server", Qgis::Info );
349  }
350 }
351 
352 // getter
354 {
355  return QSettings().fileName();
356 }
357 
359 {
361 }
362 
364 {
365  return value( QgsServerSettingsEnv::QGIS_SERVER_MAX_THREADS ).toInt();
366 }
367 
369 {
370  return value( QgsServerSettingsEnv::QGIS_SERVER_LOG_FILE ).toString();
371 }
372 
374 {
375  return value( QgsServerSettingsEnv::QGIS_SERVER_LOG_STDERR ).toBool();
376 }
377 
379 {
380  return static_cast<Qgis::MessageLevel>( value( QgsServerSettingsEnv::QGIS_SERVER_LOG_LEVEL ).toInt() );
381 }
382 
384 {
385  return value( QgsServerSettingsEnv::MAX_CACHE_LAYERS ).toInt();
386 }
387 
389 {
390  return value( QgsServerSettingsEnv::QGIS_PROJECT_FILE ).toString();
391 }
392 
394 {
395  return value( QgsServerSettingsEnv::QGIS_SERVER_CACHE_SIZE ).toLongLong();
396 }
397 
399 {
400  return value( QgsServerSettingsEnv::QGIS_SERVER_CACHE_DIRECTORY ).toString();
401 }
402 
404 {
406 }
407 
409 {
411 }
412 
414 {
415  return value( QgsServerSettingsEnv::QGIS_SERVER_WMS_MAX_HEIGHT ).toInt();
416 }
417 
419 {
420  return value( QgsServerSettingsEnv::QGIS_SERVER_WMS_MAX_WIDTH ).toInt();
421 }
422 
424 {
426 }
427 
429 {
430  return value( QgsServerSettingsEnv::QGIS_SERVER_API_WFS3_MAX_LIMIT ).toLongLong();
431 }
432 
434 {
436 }
qgsserversettings.h
QgsServerSettings::QgsServerSettings
QgsServerSettings()
Constructor.
Definition: qgsserversettings.cpp:25
QgsServerSettings::iniFile
QString iniFile() const
Returns the ini file loaded by QSetting.
Definition: qgsserversettings.cpp:353
QgsServerSettings::wmsMaxWidth
int wmsMaxWidth() const
Returns the server-wide max width of a WMS GetMap request.
Definition: qgsserversettings.cpp:418
QgsServerSettingsEnv::MAX_CACHE_LAYERS
@ MAX_CACHE_LAYERS
Definition: qgsserversettings.h:61
QgsServerSettings::apiWfs3MaxLimit
qlonglong apiWfs3MaxLimit() const
Returns the server-wide maximum allowed value for "limit" in a features request.
Definition: qgsserversettings.cpp:428
QgsServerSettingsEnv::QGIS_SERVER_CACHE_DIRECTORY
@ QGIS_SERVER_CACHE_DIRECTORY
Definition: qgsserversettings.h:63
QgsServerSettingsEnv::ENVIRONMENT_VARIABLE
@ ENVIRONMENT_VARIABLE
Definition: qgsserversettings.h:46
QgsServerSettingsEnv::DEFAULT_VALUE
@ DEFAULT_VALUE
Definition: qgsserversettings.h:45
QgsServerSettings::cacheDirectory
QString cacheDirectory() const
Returns the cache directory.
Definition: qgsserversettings.cpp:398
QgsServerSettingsEnv::QGIS_SERVER_CACHE_SIZE
@ QGIS_SERVER_CACHE_SIZE
Definition: qgsserversettings.h:64
QgsServerSettings::maxCacheLayers
int maxCacheLayers() const
Returns the maximum number of cached layers.
Definition: qgsserversettings.cpp:383
QgsServerSettingsEnv::QGIS_SERVER_LOG_LEVEL
@ QGIS_SERVER_LOG_LEVEL
Definition: qgsserversettings.h:57
qgsapplication.h
QgsApplication::qgisSettingsDirPath
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user's home dir.
Definition: qgsapplication.cpp:973
QgsServerSettings::showGroupSeparator
bool showGroupSeparator() const
Show group (thousand) separator.
Definition: qgsserversettings.cpp:408
QgsServerSettings::projectFile
QString projectFile() const
Returns the QGS project file to use.
Definition: qgsserversettings.cpp:388
QgsServerSettings::logFile
QString logFile() const
Returns the log file.
Definition: qgsserversettings.cpp:368
Qgis::Info
@ Info
Definition: qgis.h:103
QgsServerSettingsEnv::QGIS_SERVER_PARALLEL_RENDERING
@ QGIS_SERVER_PARALLEL_RENDERING
Definition: qgsserversettings.h:55
QgsServerSettingsEnv::QGIS_SERVER_WMS_MAX_HEIGHT
@ QGIS_SERVER_WMS_MAX_HEIGHT
Maximum height for a WMS request. The most conservative between this and the project one is used (sin...
Definition: qgsserversettings.h:67
QgsServerSettings::parallelRendering
bool parallelRendering() const
Returns parallel rendering setting.
Definition: qgsserversettings.cpp:358
QgsServerSettings::cacheSize
qint64 cacheSize() const
Returns the cache size.
Definition: qgsserversettings.cpp:393
QgsServerSettingsEnv::QGIS_SERVER_API_RESOURCES_DIRECTORY
@ QGIS_SERVER_API_RESOURCES_DIRECTORY
Base directory where HTML templates and static assets (e.g. images, js and css files) are searched fo...
Definition: qgsserversettings.h:69
QgsServerSettingsEnv::QGIS_SERVER_LOG_STDERR
@ QGIS_SERVER_LOG_STDERR
Definition: qgsserversettings.h:59
QgsServerSettingsEnv::INI_FILE
@ INI_FILE
Definition: qgsserversettings.h:47
QgsApplication::pkgDataPath
static QString pkgDataPath()
Returns the common root path of all application data directories.
Definition: qgsapplication.cpp:557
QgsServerSettingsEnv::QGIS_SERVER_API_WFS3_MAX_LIMIT
@ QGIS_SERVER_API_WFS3_MAX_LIMIT
Maximum value for "limit" in a features request, defaults to 10000 (since QGIS 3.10).
Definition: qgsserversettings.h:70
QgsServerSettingsEnv::QGIS_SERVER_OVERRIDE_SYSTEM_LOCALE
@ QGIS_SERVER_OVERRIDE_SYSTEM_LOCALE
Override system locale (since QGIS 3.8)
Definition: qgsserversettings.h:66
QgsServerSettings::ignoreBadLayers
bool ignoreBadLayers() const
Returns true if the bad layers are ignored and false when the presence of a bad layers invalidates th...
Definition: qgsserversettings.cpp:433
QgsServerSettings::overrideSystemLocale
QString overrideSystemLocale() const
Overrides system locale.
Definition: qgsserversettings.cpp:403
QgsServerSettingsEnv::QGIS_SERVER_MAX_THREADS
@ QGIS_SERVER_MAX_THREADS
Definition: qgsserversettings.h:56
Qgis::MessageLevel
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:101
QgsServerSettingsEnv::QGIS_OPTIONS_PATH
@ QGIS_OPTIONS_PATH
Definition: qgsserversettings.h:54
QgsServerSettings::logSummary
void logSummary() const
Log a summary of settings currently loaded.
Definition: qgsserversettings.cpp:330
QgsServerSettings::maxThreads
int maxThreads() const
Returns the maximum number of threads to use.
Definition: qgsserversettings.cpp:363
QgsServerSettings::logStderr
bool logStderr() const
Returns whether logging to stderr is activated.
Definition: qgsserversettings.cpp:373
QgsServerSettings::wmsMaxHeight
int wmsMaxHeight() const
Returns the server-wide max height of a WMS GetMap request.
Definition: qgsserversettings.cpp:413
QgsMessageLog::logMessage
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Definition: qgsmessagelog.cpp:27
QgsServerSettings::apiResourcesDirectory
QString apiResourcesDirectory() const
Returns the server-wide base directory where HTML templates and static assets (e.g.
Definition: qgsserversettings.cpp:423
QgsServerSettings::Setting
Definition: qgsserversettings.h:85
QgsServerSettingsEnv::QGIS_SERVER_LOG_FILE
@ QGIS_SERVER_LOG_FILE
Definition: qgsserversettings.h:58
QgsServerSettingsEnv::QGIS_SERVER_WMS_MAX_WIDTH
@ QGIS_SERVER_WMS_MAX_WIDTH
Maximum width for a WMS request. The most conservative between this and the project one is used (sinc...
Definition: qgsserversettings.h:68
QgsServerSettings::logLevel
Qgis::MessageLevel logLevel() const
Returns the log level.
Definition: qgsserversettings.cpp:378
QgsServerSettingsEnv::QGIS_PROJECT_FILE
@ QGIS_PROJECT_FILE
Definition: qgsserversettings.h:60
QgsServerSettingsEnv::EnvVar
EnvVar
Environment variables to configure the server.
Definition: qgsserversettings.h:52
QgsServerSettingsEnv::QGIS_SERVER_IGNORE_BAD_LAYERS
@ QGIS_SERVER_IGNORE_BAD_LAYERS
Do not consider the whole project unavailable if it contains bad layers.
Definition: qgsserversettings.h:62
Qgis::None
@ None
Definition: qgis.h:107
QgsServerSettings::load
void load()
Load settings according to current environment variables.
Definition: qgsserversettings.cpp:224
QgsServerSettingsEnv::QGIS_SERVER_SHOW_GROUP_SEPARATOR
@ QGIS_SERVER_SHOW_GROUP_SEPARATOR
Show group (thousands) separator when formatting numeric values, defaults to false (since QGIS 3....
Definition: qgsserversettings.h:65