QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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
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
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
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
81  QStringLiteral( "Log file" ),
82  QString(),
83  QVariant::String,
84  QVariant( "" ),
85  QVariant()
86  };
87  mSettings[ sLogFile.envVar ] = sLogFile;
88 
89  // log to 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
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
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
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
147  QStringLiteral( "Override system locale" ),
148  QStringLiteral( "/locale/userLocale" ),
149  QVariant::String,
150  QVariant( "" ),
151  QVariant()
152  };
153  mSettings[ sOverrideSystemLocale.envVar ] = sOverrideSystemLocale;
154 
155  // show group separator
158  QStringLiteral( "Show group (thousands) separator" ),
159  QStringLiteral( "/locale/showGroupSeparator" ),
160  QVariant::String,
161  QVariant( false ),
162  QVariant()
163  };
164  mSettings[ sShowGroupSeparator.envVar ] = sShowGroupSeparator;
165 
166  // max height
169  QStringLiteral( "Maximum height for a WMS request. The lower one of this and the project configuration is used." ),
170  QStringLiteral( "/qgis/max_wms_height" ),
171  QVariant::LongLong,
172  QVariant( -1 ),
173  QVariant()
174  };
175  mSettings[ sMaxHeight.envVar ] = sMaxHeight;
176 
177  // max width
180  QStringLiteral( "Maximum width for a WMS request. The most conservative between this and the project one is used" ),
181  QStringLiteral( "/qgis/max_wms_width" ),
182  QVariant::LongLong,
183  QVariant( -1 ),
184  QVariant()
185  };
186  mSettings[ sMaxWidth.envVar ] = sMaxWidth;
187 
188  // API templates and static override directory
191  QStringLiteral( "Base directory where HTML templates and static assets (e.g. images, js and css files) are searched for" ),
192  QStringLiteral( "/qgis/server_api_resources_directory" ),
193  QVariant::String,
194  QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( QStringLiteral( "resources/server/api" ) ),
195  QString()
196  };
197 
198  mSettings[ sApiResourcesDirectory.envVar ] = sApiResourcesDirectory;
199 
200  // API WFS3 max limit
203  QStringLiteral( "Maximum value for \"limit\" in a features request, defaults to 10000" ),
204  QStringLiteral( "/qgis/server_api_wfs3_max_limit" ),
205  QVariant::LongLong,
206  QVariant( 10000 ),
207  QVariant()
208  };
209 
210  mSettings[ sApiWfs3MaxLimit.envVar ] = sApiWfs3MaxLimit;
211 }
212 
214 {
215  // init settings each time to take into account QgsApplication and
216  // QCoreApplication configuration for some default values
217  initSettings();
218 
219  // store environment variables
220  QMap<QgsServerSettingsEnv::EnvVar, QString> env = getEnv();
221 
222  // load QSettings if QGIS_OPTIONS_PATH is defined
223  loadQSettings( env[ QgsServerSettingsEnv::QGIS_OPTIONS_PATH ] );
224 
225  // prioritize values: 'env var' -> 'ini file' -> 'default value'
226  prioritize( env );
227 }
228 
229 bool QgsServerSettings::load( const QString &envVarName )
230 {
231  bool rc( false );
232  const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerSettingsEnv::EnvVar>() );
233  const int value = metaEnum.keyToValue( envVarName.toStdString().c_str() );
234 
235  if ( value >= 0 )
236  {
237  const QString envValue( getenv( envVarName.toStdString().c_str() ) );
238  prioritize( QMap<QgsServerSettingsEnv::EnvVar, QString> { {( QgsServerSettingsEnv::EnvVar ) value, envValue } } );
239  rc = true;
240  }
241 
242  return rc;
243 }
244 
245 QMap<QgsServerSettingsEnv::EnvVar, QString> QgsServerSettings::getEnv() const
246 {
247  QMap<QgsServerSettingsEnv::EnvVar, QString> env;
248 
249  const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerSettingsEnv::EnvVar>() );
250  for ( int i = 0; i < metaEnum.keyCount(); i++ )
251  {
252  env[( QgsServerSettingsEnv::EnvVar ) metaEnum.value( i )] = getenv( metaEnum.key( i ) );
253  }
254 
255  return env;
256 }
257 
258 QVariant QgsServerSettings::value( QgsServerSettingsEnv::EnvVar envVar ) const
259 {
260  if ( mSettings[ envVar ].src == QgsServerSettingsEnv::DEFAULT_VALUE )
261  {
262  return mSettings[ envVar ].defaultVal;
263  }
264  else
265  {
266  return mSettings[ envVar ].val;
267  }
268 }
269 
270 void QgsServerSettings::loadQSettings( const QString &envOptPath ) const
271 {
272  if ( ! envOptPath.isEmpty() )
273  {
274  QSettings::setDefaultFormat( QSettings::IniFormat );
275  QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, envOptPath );
276  }
277 }
278 
279 void QgsServerSettings::prioritize( const QMap<QgsServerSettingsEnv::EnvVar, QString> &env )
280 {
281  for ( QgsServerSettingsEnv::EnvVar e : env.keys() )
282  {
283  Setting s = mSettings[ e ];
284 
285  QVariant varValue;
286  if ( ! env.value( e ).isEmpty() )
287  {
288  varValue.setValue( env.value( e ) );
289  }
290 
291  if ( ! varValue.isNull() && varValue.canConvert( s.type ) )
292  {
293  s.val = varValue;
295  }
296  else if ( ! s.iniKey.isEmpty() && QSettings().contains( s.iniKey ) && QSettings().value( s.iniKey ).canConvert( s.type ) )
297  {
298  s.val = QSettings().value( s.iniKey );
300  }
301  else
302  {
303  s.val = QVariant();
305  }
306 
307  // an empty string can be returned from QSettings. In this case, we want
308  // to use the default value
309  if ( s.type == QVariant::String && s.val.toString().isEmpty() )
310  {
311  s.val = QVariant();
313  }
314 
315  mSettings[ e ] = s;
316  }
317 }
318 
320 {
321  const QMetaEnum metaEnumSrc( QMetaEnum::fromType<QgsServerSettingsEnv::Source>() );
322  const QMetaEnum metaEnumEnv( QMetaEnum::fromType<QgsServerSettingsEnv::EnvVar>() );
323 
324  QgsMessageLog::logMessage( "Qgis Server Settings: ", "Server", Qgis::Info );
325  for ( Setting s : mSettings )
326  {
327  const QString src = metaEnumSrc.valueToKey( s.src );
328  const QString var = metaEnumEnv.valueToKey( s.envVar );
329 
330  const QString msg = " - " + var + " / '" + s.iniKey + "' (" + s.descr + "): '" + value( s.envVar ).toString() + "' (read from " + src + ")";
331  QgsMessageLog::logMessage( msg, "Server", Qgis::Info );
332  }
333 
334  if ( ! iniFile().isEmpty() )
335  {
336  const QString msg = "Ini file used to initialize settings: " + iniFile();
337  QgsMessageLog::logMessage( msg, "Server", Qgis::Info );
338  }
339 }
340 
341 // getter
343 {
344  return QSettings().fileName();
345 }
346 
348 {
350 }
351 
353 {
354  return value( QgsServerSettingsEnv::QGIS_SERVER_MAX_THREADS ).toInt();
355 }
356 
358 {
359  return value( QgsServerSettingsEnv::QGIS_SERVER_LOG_FILE ).toString();
360 }
361 
363 {
364  return value( QgsServerSettingsEnv::QGIS_SERVER_LOG_STDERR ).toBool();
365 }
366 
368 {
369  return static_cast<Qgis::MessageLevel>( value( QgsServerSettingsEnv::QGIS_SERVER_LOG_LEVEL ).toInt() );
370 }
371 
373 {
374  return value( QgsServerSettingsEnv::MAX_CACHE_LAYERS ).toInt();
375 }
376 
378 {
379  return value( QgsServerSettingsEnv::QGIS_PROJECT_FILE ).toString();
380 }
381 
383 {
384  return value( QgsServerSettingsEnv::QGIS_SERVER_CACHE_SIZE ).toLongLong();
385 }
386 
388 {
389  return value( QgsServerSettingsEnv::QGIS_SERVER_CACHE_DIRECTORY ).toString();
390 }
391 
393 {
395 }
396 
398 {
400 }
401 
403 {
404  return value( QgsServerSettingsEnv::QGIS_SERVER_WMS_MAX_HEIGHT ).toInt();
405 }
406 
408 {
409  return value( QgsServerSettingsEnv::QGIS_SERVER_WMS_MAX_WIDTH ).toInt();
410 }
411 
413 {
415 }
416 
418 {
419  return value( QgsServerSettingsEnv::QGIS_SERVER_API_WFS3_MAX_LIMIT ).toLongLong();
420 }
QString cacheDirectory() const
Returns the cache directory.
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user&#39;s home dir.
Maximum width for a WMS request. The most conservative between this and the project one is used (sinc...
qlonglong apiWfs3MaxLimit() const
Returns the server-wide maximum allowed value for "limit" in a features request.
QString iniFile() const
Returns the ini file loaded by QSetting.
int wmsMaxWidth() const
Returns the server-wide max width of a WMS GetMap request.
QString projectFile() const
Returns the QGS project file to use.
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:67
Show group (thousands) separator when formatting numeric values, defaults to false (since QGIS 3...
Override system locale (since QGIS 3.8)
int maxCacheLayers() const
Returns the maximum number of cached layers.
qint64 cacheSize() const
Returns the cache size.
bool showGroupSeparator() const
Show group (thousand) separator.
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).
QString logFile() const
Returns the log file.
static QString pkgDataPath()
Returns the common root path of all application data directories.
int wmsMaxHeight() const
Returns the server-wide max height of a WMS GetMap request.
bool parallelRendering() const
Returns parallel rendering setting.
QString overrideSystemLocale() const
Overrides system locale.
Maximum height for a WMS request. The most conservative between this and the project one is used (sin...
QString apiResourcesDirectory() const
Returns the server-wide base directory where HTML templates and static assets (e.g.
void load()
Load settings according to current environment variables.
void logSummary() const
Log a summary of settings currently loaded.
QgsServerSettingsEnv::EnvVar envVar
QgsServerSettingsEnv::Source src
Qgis::MessageLevel logLevel() const
Returns the log level.
EnvVar
Environment variables to configure the server.
int maxThreads() const
Returns the maximum number of threads to use.
bool logStderr() const
Returns whether logging to stderr is activated.
Base directory where HTML templates and static assets (e.g. images, js and css files) are searched fo...
QgsServerSettings()
Constructor.