QGIS API Documentation  3.6.0-Noosa (5873452)
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  "Override the default path for user configuration",
37  "",
38  QVariant::String,
39  QVariant( "" ),
40  QVariant()
41  };
42  mSettings[ sOptPath.envVar ] = sOptPath;
43 
44  // parallel rendering
47  "Activate/Deactivate parallel rendering for WMS getMap request",
48  "/qgis/parallel_rendering",
49  QVariant::Bool,
50  QVariant( false ),
51  QVariant()
52  };
53  mSettings[ sParRend.envVar ] = sParRend;
54 
55  // max threads
58  "Number of threads to use when parallel rendering is activated",
59  "/qgis/max_threads",
60  QVariant::Int,
61  QVariant( -1 ),
62  QVariant()
63  };
64  mSettings[ sMaxThreads.envVar ] = sMaxThreads;
65 
66  // log level
69  "Log level",
70  "",
71  QVariant::Int,
72  QVariant( Qgis::None ),
73  QVariant()
74  };
75  mSettings[ sLogLevel.envVar ] = sLogLevel;
76 
77  // log file
80  "Log file",
81  "",
82  QVariant::String,
83  QVariant( "" ),
84  QVariant()
85  };
86  mSettings[ sLogFile.envVar ] = sLogFile;
87 
88  // log to stderr
91  "Activate/Deactivate logging to stderr",
92  "",
93  QVariant::Bool,
94  QVariant( false ),
95  QVariant()
96  };
97  mSettings[ sLogStderr.envVar ] = sLogStderr;
98 
99  // project file
102  "QGIS project file",
103  "",
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  "Specify the maximum number of cached layers",
114  "",
115  QVariant::Int,
116  QVariant( 100 ),
117  QVariant()
118  };
119  mSettings[ sMaxCacheLayers.envVar ] = sMaxCacheLayers;
120 
121  // cache directory
124  "Specify the cache directory",
125  "/cache/directory",
126  QVariant::String,
127  QVariant( QgsApplication::qgisSettingsDirPath() + "cache" ),
128  QVariant()
129  };
130  mSettings[ sCacheDir.envVar ] = sCacheDir;
131 
132  // cache size
135  "Specify the cache size",
136  "/cache/size",
137  QVariant::LongLong,
138  QVariant( 50 * 1024 * 1024 ),
139  QVariant()
140  };
141  mSettings[ sCacheSize.envVar ] = sCacheSize;
142 }
143 
145 {
146  // init settings each time to take into account QgsApplication and
147  // QCoreApplication configuration for some default values
148  initSettings();
149 
150  // store environment variables
151  QMap<QgsServerSettingsEnv::EnvVar, QString> env = getEnv();
152 
153  // load QSettings if QGIS_OPTIONS_PATH is defined
154  loadQSettings( env[ QgsServerSettingsEnv::QGIS_OPTIONS_PATH ] );
155 
156  // prioritize values: 'env var' -> 'ini file' -> 'default value'
157  prioritize( env );
158 }
159 
160 bool QgsServerSettings::load( const QString &envVarName )
161 {
162  bool rc( false );
163  const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerSettingsEnv::EnvVar>() );
164  const int value = metaEnum.keyToValue( envVarName.toStdString().c_str() );
165 
166  if ( value >= 0 )
167  {
168  const QString envValue( getenv( envVarName.toStdString().c_str() ) );
169  prioritize( QMap<QgsServerSettingsEnv::EnvVar, QString> { {( QgsServerSettingsEnv::EnvVar ) value, envValue } } );
170  rc = true;
171  }
172 
173  return rc;
174 }
175 
176 QMap<QgsServerSettingsEnv::EnvVar, QString> QgsServerSettings::getEnv() const
177 {
178  QMap<QgsServerSettingsEnv::EnvVar, QString> env;
179 
180  const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerSettingsEnv::EnvVar>() );
181  for ( int i = 0; i < metaEnum.keyCount(); i++ )
182  {
183  env[( QgsServerSettingsEnv::EnvVar ) metaEnum.value( i )] = getenv( metaEnum.key( i ) );
184  }
185 
186  return env;
187 }
188 
189 QVariant QgsServerSettings::value( QgsServerSettingsEnv::EnvVar envVar ) const
190 {
191  if ( mSettings[ envVar ].src == QgsServerSettingsEnv::DEFAULT_VALUE )
192  {
193  return mSettings[ envVar ].defaultVal;
194  }
195  else
196  {
197  return mSettings[ envVar ].val;
198  }
199 }
200 
201 void QgsServerSettings::loadQSettings( const QString &envOptPath ) const
202 {
203  if ( ! envOptPath.isEmpty() )
204  {
205  QSettings::setDefaultFormat( QSettings::IniFormat );
206  QSettings::setPath( QSettings::IniFormat, QSettings::UserScope, envOptPath );
207  }
208 }
209 
210 void QgsServerSettings::prioritize( const QMap<QgsServerSettingsEnv::EnvVar, QString> &env )
211 {
212  for ( QgsServerSettingsEnv::EnvVar e : env.keys() )
213  {
214  Setting s = mSettings[ e ];
215 
216  QVariant varValue;
217  if ( ! env.value( e ).isEmpty() )
218  {
219  varValue.setValue( env.value( e ) );
220  }
221 
222  if ( ! varValue.isNull() && varValue.canConvert( s.type ) )
223  {
224  s.val = varValue;
226  }
227  else if ( ! s.iniKey.isEmpty() && QSettings().contains( s.iniKey ) && QSettings().value( s.iniKey ).canConvert( s.type ) )
228  {
229  s.val = QSettings().value( s.iniKey );
231  }
232  else
233  {
234  s.val = QVariant();
236  }
237 
238  // an empty string can be returned from QSettings. In this case, we want
239  // to use the default value
240  if ( s.type == QVariant::String && s.val.toString().isEmpty() )
241  {
242  s.val = QVariant();
244  }
245 
246  mSettings[ e ] = s;
247  }
248 }
249 
251 {
252  const QMetaEnum metaEnumSrc( QMetaEnum::fromType<QgsServerSettingsEnv::Source>() );
253  const QMetaEnum metaEnumEnv( QMetaEnum::fromType<QgsServerSettingsEnv::EnvVar>() );
254 
255  QgsMessageLog::logMessage( "Qgis Server Settings: ", "Server", Qgis::Info );
256  for ( Setting s : mSettings )
257  {
258  const QString src = metaEnumSrc.valueToKey( s.src );
259  const QString var = metaEnumEnv.valueToKey( s.envVar );
260 
261  const QString msg = " - " + var + " / '" + s.iniKey + "' (" + s.descr + "): '" + value( s.envVar ).toString() + "' (read from " + src + ")";
262  QgsMessageLog::logMessage( msg, "Server", Qgis::Info );
263  }
264 
265  if ( ! iniFile().isEmpty() )
266  {
267  const QString msg = "Ini file used to initialize settings: " + iniFile();
268  QgsMessageLog::logMessage( msg, "Server", Qgis::Info );
269  }
270 }
271 
272 // getter
274 {
275  return QSettings().fileName();
276 }
277 
279 {
281 }
282 
284 {
285  return value( QgsServerSettingsEnv::QGIS_SERVER_MAX_THREADS ).toInt();
286 }
287 
289 {
290  return value( QgsServerSettingsEnv::QGIS_SERVER_LOG_FILE ).toString();
291 }
292 
294 {
295  return value( QgsServerSettingsEnv::QGIS_SERVER_LOG_STDERR ).toBool();
296 }
297 
299 {
300  return static_cast<Qgis::MessageLevel>( value( QgsServerSettingsEnv::QGIS_SERVER_LOG_LEVEL ).toInt() );
301 }
302 
304 {
305  return value( QgsServerSettingsEnv::MAX_CACHE_LAYERS ).toInt();
306 }
307 
309 {
310  return value( QgsServerSettingsEnv::QGIS_PROJECT_FILE ).toString();
311 }
312 
314 {
315  return value( QgsServerSettingsEnv::QGIS_SERVER_CACHE_SIZE ).toLongLong();
316 }
317 
319 {
320  return value( QgsServerSettingsEnv::QGIS_SERVER_CACHE_DIRECTORY ).toString();
321 }
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.
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
int maxCacheLayers() const
Returns the maximum number of cached layers.
qint64 cacheSize() const
Returns the cache size.
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.
bool parallelRendering() const
Returns parallel rendering setting.
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.