QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
24 #include <iostream>
25 
27 {
28  load();
29 }
30 
31 void QgsServerSettings::initSettings()
32 {
33  mSettings.clear();
34 
35  // options path
38  "Override the default path for user configuration",
39  "",
40  QVariant::String,
41  QVariant( "" ),
42  QVariant()
43  };
44  mSettings[ sOptPath.envVar ] = sOptPath;
45 
46  // parallel rendering
49  "Activate/Deactivate parallel rendering for WMS getMap request",
50  "/qgis/parallel_rendering",
51  QVariant::Bool,
52  QVariant( false ),
53  QVariant()
54  };
55  mSettings[ sParRend.envVar ] = sParRend;
56 
57  // max threads
60  "Number of threads to use when parallel rendering is activated",
61  "/qgis/max_threads",
62  QVariant::Int,
63  QVariant( -1 ),
64  QVariant()
65  };
66  mSettings[ sMaxThreads.envVar ] = sMaxThreads;
67 
68  // log level
71  "Log level",
72  "",
73  QVariant::Int,
74  QVariant( Qgis::None ),
75  QVariant()
76  };
77  mSettings[ sLogLevel.envVar ] = sLogLevel;
78 
79  // log file
82  "Log file",
83  "",
84  QVariant::String,
85  QVariant( "" ),
86  QVariant()
87  };
88  mSettings[ sLogFile.envVar ] = sLogFile;
89 
90  // log to stderr
93  "Activate/Deactivate logging to stderr",
94  "",
95  QVariant::Bool,
96  QVariant( false ),
97  QVariant()
98  };
99  mSettings[ sLogStderr.envVar ] = sLogStderr;
100 
101  // project file
104  "QGIS project file",
105  "",
106  QVariant::String,
107  QVariant( "" ),
108  QVariant()
109  };
110  mSettings[ sProject.envVar ] = sProject;
111 
112  // max cache layers
113  const Setting sMaxCacheLayers = { QgsServerSettingsEnv::MAX_CACHE_LAYERS,
115  "Specify the maximum number of cached layers",
116  "",
117  QVariant::Int,
118  QVariant( 100 ),
119  QVariant()
120  };
121  mSettings[ sMaxCacheLayers.envVar ] = sMaxCacheLayers;
122 
123  // cache directory
126  "Specify the cache directory",
127  "/cache/directory",
128  QVariant::String,
129  QVariant( QgsApplication::qgisSettingsDirPath() + "cache" ),
130  QVariant()
131  };
132  mSettings[ sCacheDir.envVar ] = sCacheDir;
133 
134  // cache size
137  "Specify the cache size",
138  "/cache/size",
139  QVariant::LongLong,
140  QVariant( 50 * 1024 * 1024 ),
141  QVariant()
142  };
143  mSettings[ sCacheSize.envVar ] = sCacheSize;
144 
145  // system locale override
148  QStringLiteral( "Override system locale" ),
149  QStringLiteral( "/locale/userLocale" ),
150  QVariant::String,
151  QVariant( "" ),
152  QVariant()
153  };
154  mSettings[ sOverrideSystemLocale.envVar ] = sOverrideSystemLocale;
155 
156  // show group separator
159  QStringLiteral( "Show group (thousands) separator" ),
160  QStringLiteral( "/locale/showGroupSeparator" ),
161  QVariant::String,
162  QVariant( false ),
163  QVariant()
164  };
165  mSettings[ sShowGroupSeparator.envVar ] = sShowGroupSeparator;
166 
167 }
168 
170 {
171  // init settings each time to take into account QgsApplication and
172  // QCoreApplication configuration for some default values
173  initSettings();
174 
175  // store environment variables
176  QMap<QgsServerSettingsEnv::EnvVar, QString> env = getEnv();
177 
178  // load QSettings if QGIS_OPTIONS_PATH is defined
179  loadQSettings( env[ QgsServerSettingsEnv::QGIS_OPTIONS_PATH ] );
180 
181  // prioritize values: 'env var' -> 'ini file' -> 'default value'
182  prioritize( env );
183 }
184 
185 bool QgsServerSettings::load( const QString &envVarName )
186 {
187  bool rc( false );
188  const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerSettingsEnv::EnvVar>() );
189  const int value = metaEnum.keyToValue( envVarName.toStdString().c_str() );
190 
191  if ( value >= 0 )
192  {
193  const QString envValue( getenv( envVarName.toStdString().c_str() ) );
194  prioritize( QMap<QgsServerSettingsEnv::EnvVar, QString> { {( QgsServerSettingsEnv::EnvVar ) value, envValue } } );
195  rc = true;
196  }
197 
198  return rc;
199 }
200 
201 QMap<QgsServerSettingsEnv::EnvVar, QString> QgsServerSettings::getEnv() const
202 {
203  QMap<QgsServerSettingsEnv::EnvVar, QString> env;
204 
205  const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerSettingsEnv::EnvVar>() );
206  for ( int i = 0; i < metaEnum.keyCount(); i++ )
207  {
208  env[( QgsServerSettingsEnv::EnvVar ) metaEnum.value( i )] = getenv( metaEnum.key( i ) );
209  }
210 
211  return env;
212 }
213 
214 QVariant QgsServerSettings::value( QgsServerSettingsEnv::EnvVar envVar ) const
215 {
216  if ( mSettings[ envVar ].src == QgsServerSettingsEnv::DEFAULT_VALUE )
217  {
218  return mSettings[ envVar ].defaultVal;
219  }
220  else
221  {
222  return mSettings[ envVar ].val;
223  }
224 }
225 
226 void QgsServerSettings::loadQSettings( const QString &envOptPath ) const
227 {
228  if ( ! envOptPath.isEmpty() )
229  {
230  QSettings::setDefaultFormat( QSettings::IniFormat );
231  QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, envOptPath );
232  }
233 }
234 
235 void QgsServerSettings::prioritize( const QMap<QgsServerSettingsEnv::EnvVar, QString> &env )
236 {
237  for ( QgsServerSettingsEnv::EnvVar e : env.keys() )
238  {
239  Setting s = mSettings[ e ];
240 
241  QVariant varValue;
242  if ( ! env.value( e ).isEmpty() )
243  {
244  varValue.setValue( env.value( e ) );
245  }
246 
247  if ( ! varValue.isNull() && varValue.canConvert( s.type ) )
248  {
249  s.val = varValue;
251  }
252  else if ( ! s.iniKey.isEmpty() && QSettings().contains( s.iniKey ) && QSettings().value( s.iniKey ).canConvert( s.type ) )
253  {
254  s.val = QSettings().value( s.iniKey );
256  }
257  else
258  {
259  s.val = QVariant();
261  }
262 
263  // an empty string can be returned from QSettings. In this case, we want
264  // to use the default value
265  if ( s.type == QVariant::String && s.val.toString().isEmpty() )
266  {
267  s.val = QVariant();
269  }
270 
271  mSettings[ e ] = s;
272  }
273 }
274 
276 {
277  const QMetaEnum metaEnumSrc( QMetaEnum::fromType<QgsServerSettingsEnv::Source>() );
278  const QMetaEnum metaEnumEnv( QMetaEnum::fromType<QgsServerSettingsEnv::EnvVar>() );
279 
280  QgsMessageLog::logMessage( "Qgis Server Settings: ", "Server", Qgis::Info );
281  for ( Setting s : mSettings )
282  {
283  const QString src = metaEnumSrc.valueToKey( s.src );
284  const QString var = metaEnumEnv.valueToKey( s.envVar );
285 
286  const QString msg = " - " + var + " / '" + s.iniKey + "' (" + s.descr + "): '" + value( s.envVar ).toString() + "' (read from " + src + ")";
287  QgsMessageLog::logMessage( msg, "Server", Qgis::Info );
288  }
289 
290  if ( ! iniFile().isEmpty() )
291  {
292  const QString msg = "Ini file used to initialize settings: " + iniFile();
293  QgsMessageLog::logMessage( msg, "Server", Qgis::Info );
294  }
295 }
296 
297 // getter
299 {
300  return QSettings().fileName();
301 }
302 
304 {
306 }
307 
309 {
310  return value( QgsServerSettingsEnv::QGIS_SERVER_MAX_THREADS ).toInt();
311 }
312 
314 {
315  return value( QgsServerSettingsEnv::QGIS_SERVER_LOG_FILE ).toString();
316 }
317 
319 {
320  return value( QgsServerSettingsEnv::QGIS_SERVER_LOG_STDERR ).toBool();
321 }
322 
324 {
325  return static_cast<Qgis::MessageLevel>( value( QgsServerSettingsEnv::QGIS_SERVER_LOG_LEVEL ).toInt() );
326 }
327 
329 {
330  return value( QgsServerSettingsEnv::MAX_CACHE_LAYERS ).toInt();
331 }
332 
334 {
335  return value( QgsServerSettingsEnv::QGIS_PROJECT_FILE ).toString();
336 }
337 
339 {
340  return value( QgsServerSettingsEnv::QGIS_SERVER_CACHE_SIZE ).toLongLong();
341 }
342 
344 {
345  return value( QgsServerSettingsEnv::QGIS_SERVER_CACHE_DIRECTORY ).toString();
346 }
347 
349 {
351 }
352 
354 {
356 }
357 
QString logFile() const
Returns the log file.
static QString qgisSettingsDirPath()
Returns the path to the settings directory in user&#39;s home dir.
QString cacheDirectory() const
Returns the cache directory.
int maxCacheLayers() const
Returns the maximum number of cached layers.
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:79
Show group (thousands) separator when formatting numeric values, defaults to FALSE (since QGIS 3...
bool showGroupSeparator() const
Show group (thousand) separator.
QString projectFile() const
Returns the QGS project file to use.
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).
qint64 cacheSize() const
Returns the cache size.
void logSummary() const
Log a summary of settings currently loaded.
bool logStderr() const
Returns whether logging to stderr is activated.
void load()
Load settings according to current environment variables.
QgsServerSettingsEnv::EnvVar envVar
QString iniFile() const
Returns the ini file loaded by QSetting.
QgsServerSettingsEnv::Source src
QString overrideSystemLocale() const
Overrides system locale.
EnvVar
Environment variables to configure the server.
bool parallelRendering() const
Returns parallel rendering setting.
Qgis::MessageLevel logLevel() const
Returns the log level.
QgsServerSettings()
Constructor.
int maxThreads() const
Returns the maximum number of threads to use.