QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
25 {
26  load();
27 }
28 
29 void QgsServerSettings::initSettings()
30 {
31  mSettings.clear();
32 
33  // options path
36  QStringLiteral( "Override the default path for user configuration" ),
37  QString(),
38  QVariant::String,
39  QVariant( "" ),
40  QVariant()
41  };
42  mSettings[ sOptPath.envVar ] = sOptPath;
43 
44  // parallel rendering
47  QStringLiteral( "Activate/Deactivate parallel rendering for WMS getMap request" ),
48  QStringLiteral( "/qgis/parallel_rendering" ),
49  QVariant::Bool,
50  QVariant( false ),
51  QVariant()
52  };
53  mSettings[ sParRend.envVar ] = sParRend;
54 
55  // max threads
58  QStringLiteral( "Number of threads to use when parallel rendering is activated" ),
59  QStringLiteral( "/qgis/max_threads" ),
60  QVariant::Int,
61  QVariant( -1 ),
62  QVariant()
63  };
64  mSettings[ sMaxThreads.envVar ] = sMaxThreads;
65 
66  // log level
69  QStringLiteral( "Log level" ),
70  QString(),
71  QVariant::Int,
72  QVariant( Qgis::None ),
73  QVariant()
74  };
75  mSettings[ sLogLevel.envVar ] = sLogLevel;
76 
77  // log file
80  QStringLiteral( "Log file" ),
81  QString(),
82  QVariant::String,
83  QVariant( "" ),
84  QVariant()
85  };
86  mSettings[ sLogFile.envVar ] = sLogFile;
87 
88  // log to stderr
91  QStringLiteral( "Activate/Deactivate logging to stderr" ),
92  QString(),
93  QVariant::Bool,
94  QVariant( false ),
95  QVariant()
96  };
97  mSettings[ sLogStderr.envVar ] = sLogStderr;
98 
99  // project file
102  QStringLiteral( "QGIS project file" ),
103  QString(),
104  QVariant::String,
105  QVariant( "" ),
106  QVariant()
107  };
108  mSettings[ sProject.envVar ] = sProject;
109 
110  // max cache layers
111  const Setting sMaxCacheLayers = { QgsServerSettingsEnv::MAX_CACHE_LAYERS,
113  QStringLiteral( "Specify the maximum number of cached layers" ),
114  QString(),
115  QVariant::Int,
116  QVariant( 100 ),
117  QVariant()
118  };
119  mSettings[ sMaxCacheLayers.envVar ] = sMaxCacheLayers;
120 
121  // cache directory
124  QStringLiteral( "Specify the cache directory" ),
125  QStringLiteral( "/cache/directory" ),
126  QVariant::String,
127  QVariant( QgsApplication::qgisSettingsDirPath() + "cache" ),
128  QVariant()
129  };
130  mSettings[ sCacheDir.envVar ] = sCacheDir;
131 
132  // cache size
135  QStringLiteral( "Specify the cache size" ),
136  QStringLiteral( "/cache/size" ),
137  QVariant::LongLong,
138  QVariant( 50 * 1024 * 1024 ),
139  QVariant()
140  };
141  mSettings[ sCacheSize.envVar ] = sCacheSize;
142 
143  // system locale override
146  QStringLiteral( "Override system locale" ),
147  QStringLiteral( "/locale/userLocale" ),
148  QVariant::String,
149  QVariant( "" ),
150  QVariant()
151  };
152  mSettings[ sOverrideSystemLocale.envVar ] = sOverrideSystemLocale;
153 
154  // show group separator
157  QStringLiteral( "Show group (thousands) separator" ),
158  QStringLiteral( "/locale/showGroupSeparator" ),
159  QVariant::String,
160  QVariant( false ),
161  QVariant()
162  };
163  mSettings[ sShowGroupSeparator.envVar ] = sShowGroupSeparator;
164 
165  // max height
168  QStringLiteral( "Maximum height for a WMS request. The lower one of this and the project configuration is used." ),
169  QStringLiteral( "/qgis/max_wms_height" ),
170  QVariant::LongLong,
171  QVariant( -1 ),
172  QVariant()
173  };
174  mSettings[ sMaxHeight.envVar ] = sMaxHeight;
175 
176  // max width
179  QStringLiteral( "Maximum width for a WMS request. The most conservative between this and the project one is used" ),
180  QStringLiteral( "/qgis/max_wms_width" ),
181  QVariant::LongLong,
182  QVariant( -1 ),
183  QVariant()
184  };
185  mSettings[ sMaxWidth.envVar ] = sMaxWidth;
186 }
187 
189 {
190  // init settings each time to take into account QgsApplication and
191  // QCoreApplication configuration for some default values
192  initSettings();
193 
194  // store environment variables
195  QMap<QgsServerSettingsEnv::EnvVar, QString> env = getEnv();
196 
197  // load QSettings if QGIS_OPTIONS_PATH is defined
198  loadQSettings( env[ QgsServerSettingsEnv::QGIS_OPTIONS_PATH ] );
199 
200  // prioritize values: 'env var' -> 'ini file' -> 'default value'
201  prioritize( env );
202 }
203 
204 bool QgsServerSettings::load( const QString &envVarName )
205 {
206  bool rc( false );
207  const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerSettingsEnv::EnvVar>() );
208  const int value = metaEnum.keyToValue( envVarName.toStdString().c_str() );
209 
210  if ( value >= 0 )
211  {
212  const QString envValue( getenv( envVarName.toStdString().c_str() ) );
213  prioritize( QMap<QgsServerSettingsEnv::EnvVar, QString> { {( QgsServerSettingsEnv::EnvVar ) value, envValue } } );
214  rc = true;
215  }
216 
217  return rc;
218 }
219 
220 QMap<QgsServerSettingsEnv::EnvVar, QString> QgsServerSettings::getEnv() const
221 {
222  QMap<QgsServerSettingsEnv::EnvVar, QString> env;
223 
224  const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerSettingsEnv::EnvVar>() );
225  for ( int i = 0; i < metaEnum.keyCount(); i++ )
226  {
227  env[( QgsServerSettingsEnv::EnvVar ) metaEnum.value( i )] = getenv( metaEnum.key( i ) );
228  }
229 
230  return env;
231 }
232 
233 QVariant QgsServerSettings::value( QgsServerSettingsEnv::EnvVar envVar ) const
234 {
235  if ( mSettings[ envVar ].src == QgsServerSettingsEnv::DEFAULT_VALUE )
236  {
237  return mSettings[ envVar ].defaultVal;
238  }
239  else
240  {
241  return mSettings[ envVar ].val;
242  }
243 }
244 
245 void QgsServerSettings::loadQSettings( const QString &envOptPath ) const
246 {
247  if ( ! envOptPath.isEmpty() )
248  {
249  QSettings::setDefaultFormat( QSettings::IniFormat );
250  QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, envOptPath );
251  }
252 }
253 
254 void QgsServerSettings::prioritize( const QMap<QgsServerSettingsEnv::EnvVar, QString> &env )
255 {
256  for ( QgsServerSettingsEnv::EnvVar e : env.keys() )
257  {
258  Setting s = mSettings[ e ];
259 
260  QVariant varValue;
261  if ( ! env.value( e ).isEmpty() )
262  {
263  varValue.setValue( env.value( e ) );
264  }
265 
266  if ( ! varValue.isNull() && varValue.canConvert( s.type ) )
267  {
268  s.val = varValue;
270  }
271  else if ( ! s.iniKey.isEmpty() && QSettings().contains( s.iniKey ) && QSettings().value( s.iniKey ).canConvert( s.type ) )
272  {
273  s.val = QSettings().value( s.iniKey );
275  }
276  else
277  {
278  s.val = QVariant();
280  }
281 
282  // an empty string can be returned from QSettings. In this case, we want
283  // to use the default value
284  if ( s.type == QVariant::String && s.val.toString().isEmpty() )
285  {
286  s.val = QVariant();
288  }
289 
290  mSettings[ e ] = s;
291  }
292 }
293 
295 {
296  const QMetaEnum metaEnumSrc( QMetaEnum::fromType<QgsServerSettingsEnv::Source>() );
297  const QMetaEnum metaEnumEnv( QMetaEnum::fromType<QgsServerSettingsEnv::EnvVar>() );
298 
299  QgsMessageLog::logMessage( "Qgis Server Settings: ", "Server", Qgis::Info );
300  for ( Setting s : mSettings )
301  {
302  const QString src = metaEnumSrc.valueToKey( s.src );
303  const QString var = metaEnumEnv.valueToKey( s.envVar );
304 
305  const QString msg = " - " + var + " / '" + s.iniKey + "' (" + s.descr + "): '" + value( s.envVar ).toString() + "' (read from " + src + ")";
306  QgsMessageLog::logMessage( msg, "Server", Qgis::Info );
307  }
308 
309  if ( ! iniFile().isEmpty() )
310  {
311  const QString msg = "Ini file used to initialize settings: " + iniFile();
312  QgsMessageLog::logMessage( msg, "Server", Qgis::Info );
313  }
314 }
315 
316 // getter
318 {
319  return QSettings().fileName();
320 }
321 
323 {
325 }
326 
328 {
329  return value( QgsServerSettingsEnv::QGIS_SERVER_MAX_THREADS ).toInt();
330 }
331 
333 {
334  return value( QgsServerSettingsEnv::QGIS_SERVER_LOG_FILE ).toString();
335 }
336 
338 {
339  return value( QgsServerSettingsEnv::QGIS_SERVER_LOG_STDERR ).toBool();
340 }
341 
343 {
344  return static_cast<Qgis::MessageLevel>( value( QgsServerSettingsEnv::QGIS_SERVER_LOG_LEVEL ).toInt() );
345 }
346 
348 {
349  return value( QgsServerSettingsEnv::MAX_CACHE_LAYERS ).toInt();
350 }
351 
353 {
354  return value( QgsServerSettingsEnv::QGIS_PROJECT_FILE ).toString();
355 }
356 
358 {
359  return value( QgsServerSettingsEnv::QGIS_SERVER_CACHE_SIZE ).toLongLong();
360 }
361 
363 {
364  return value( QgsServerSettingsEnv::QGIS_SERVER_CACHE_DIRECTORY ).toString();
365 }
366 
368 {
370 }
371 
373 {
375 }
376 
378 {
379  return value( QgsServerSettingsEnv::QGIS_SERVER_WMS_MAX_HEIGHT ).toInt();
380 }
381 
383 {
384  return value( QgsServerSettingsEnv::QGIS_SERVER_WMS_MAX_WIDTH ).toInt();
385 }
QString cacheDirectory() const
Returns the cache directory.
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user&#39;s home dir.
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:66
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.
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...
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.
QgsServerSettings()
Constructor.