QGIS API Documentation  3.9.0-Master (224899f119)
qgssettings.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssettings.cpp
3  --------------------------------------
4  Date : January 2017
5  Copyright : (C) 2017 by Alessandro Pasotti
6  Email : apasotti at boundlessgeo dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 
17 #include <cstdlib>
18 
19 #include <QFileInfo>
20 #include <QSettings>
21 #include <QDir>
22 
23 #include "qgssettings.h"
24 #include "qgslogger.h"
25 
26 QString QgsSettings::sGlobalSettingsPath = QString();
27 
28 bool QgsSettings::setGlobalSettingsPath( const QString &path )
29 {
30  if ( QFileInfo::exists( path ) )
31  {
32  sGlobalSettingsPath = path;
33  return true;
34  }
35  return false;
36 }
37 
38 void QgsSettings::init()
39 {
40  if ( ! sGlobalSettingsPath.isEmpty() )
41  {
42  mGlobalSettings = new QSettings( sGlobalSettingsPath, QSettings::IniFormat );
43  mGlobalSettings->setIniCodec( "UTF-8" );
44  }
45 }
46 
47 
48 QgsSettings::QgsSettings( const QString &organization, const QString &application, QObject *parent )
49 {
50  mUserSettings = new QSettings( organization, application, parent );
51  init();
52 }
53 
54 QgsSettings::QgsSettings( QSettings::Scope scope, const QString &organization,
55  const QString &application, QObject *parent )
56 {
57  mUserSettings = new QSettings( scope, organization, application, parent );
58  init();
59 }
60 
61 QgsSettings::QgsSettings( QSettings::Format format, QSettings::Scope scope,
62  const QString &organization, const QString &application, QObject *parent )
63 {
64  mUserSettings = new QSettings( format, scope, organization, application, parent );
65  init();
66 }
67 
68 QgsSettings::QgsSettings( const QString &fileName, QSettings::Format format, QObject *parent )
69 {
70  mUserSettings = new QSettings( fileName, format, parent );
71  init();
72 }
73 
74 QgsSettings::QgsSettings( QObject *parent )
75 {
76  mUserSettings = new QSettings( parent );
77  init();
78 }
79 
81 {
82  delete mUserSettings;
83  delete mGlobalSettings;
84 }
85 
86 
87 void QgsSettings::beginGroup( const QString &prefix, const QgsSettings::Section section )
88 {
89  QString pKey = prefixedKey( prefix, section );
90  mUserSettings->beginGroup( pKey );
91  if ( mGlobalSettings )
92  {
93  mGlobalSettings->beginGroup( pKey );
94  }
95 }
96 
98 {
99  mUserSettings->endGroup();
100  if ( mGlobalSettings )
101  {
102  mGlobalSettings->endGroup();
103  }
104 }
105 
106 QString QgsSettings::group() const
107 {
108  return mUserSettings->group();
109 }
110 
111 QStringList QgsSettings::allKeys() const
112 {
113  QStringList keys = mUserSettings->allKeys();
114  if ( mGlobalSettings )
115  {
116  for ( auto &s : mGlobalSettings->allKeys() )
117  {
118  if ( ! keys.contains( s ) )
119  {
120  keys.append( s );
121  }
122  }
123  }
124  return keys;
125 }
126 
127 
128 QStringList QgsSettings::childKeys() const
129 {
130  QStringList keys = mUserSettings->childKeys();
131  if ( mGlobalSettings )
132  {
133  for ( auto &s : mGlobalSettings->childKeys() )
134  {
135  if ( ! keys.contains( s ) )
136  {
137  keys.append( s );
138  }
139  }
140  }
141  return keys;
142 }
143 
144 QStringList QgsSettings::childGroups() const
145 {
146  QStringList keys = mUserSettings->childGroups();
147  if ( mGlobalSettings )
148  {
149  for ( auto &s : mGlobalSettings->childGroups() )
150  {
151  if ( ! keys.contains( s ) )
152  {
153  keys.append( s );
154  }
155  }
156  }
157  return keys;
158 }
160 {
161  QStringList keys;
162  if ( mGlobalSettings )
163  {
164  keys = mGlobalSettings->childGroups();
165  }
166  return keys;
167 }
168 
169 QVariant QgsSettings::value( const QString &key, const QVariant &defaultValue, const QgsSettings::Section section ) const
170 {
171  QString pKey = prefixedKey( key, section );
172  if ( !mUserSettings->value( pKey ).isNull() )
173  {
174  return mUserSettings->value( pKey );
175  }
176  if ( mGlobalSettings )
177  {
178  return mGlobalSettings->value( pKey, defaultValue );
179  }
180  return defaultValue;
181 }
182 
183 bool QgsSettings::contains( const QString &key, const QgsSettings::Section section ) const
184 {
185  QString pKey = prefixedKey( key, section );
186  return mUserSettings->contains( pKey ) ||
187  ( mGlobalSettings && mGlobalSettings->contains( pKey ) );
188 }
189 
190 QString QgsSettings::fileName() const
191 {
192  return mUserSettings->fileName();
193 }
194 
196 {
197  mUserSettings->sync();
198 }
199 
200 void QgsSettings::remove( const QString &key, const QgsSettings::Section section )
201 {
202  QString pKey = prefixedKey( key, section );
203  mUserSettings->remove( pKey );
204 }
205 
206 QString QgsSettings::prefixedKey( const QString &key, const Section section ) const
207 {
208  QString prefix;
209  switch ( section )
210  {
211  case Section::Core :
212  prefix = QStringLiteral( "core" );
213  break;
214  case Section::Server :
215  prefix = QStringLiteral( "server" );
216  break;
217  case Section::Gui :
218  prefix = QStringLiteral( "gui" );
219  break;
220  case Section::Plugins :
221  prefix = QStringLiteral( "plugins" );
222  break;
223  case Section::Misc :
224  prefix = QStringLiteral( "misc" );
225  break;
226  case Section::Auth :
227  prefix = QStringLiteral( "auth" );
228  break;
229  case Section::App :
230  prefix = QStringLiteral( "app" );
231  break;
232  case Section::Providers :
233  prefix = QStringLiteral( "providers" );
234  break;
235  case Section::NoSection:
236  default:
237  return sanitizeKey( key );
238  }
239  return prefix + "/" + sanitizeKey( key );
240 }
241 
242 
243 int QgsSettings::beginReadArray( const QString &prefix )
244 {
245  int size = mUserSettings->beginReadArray( sanitizeKey( prefix ) );
246  if ( 0 == size && mGlobalSettings )
247  {
248  size = mGlobalSettings->beginReadArray( sanitizeKey( prefix ) );
249  mUsingGlobalArray = ( size > 0 );
250  }
251  return size;
252 }
253 
254 void QgsSettings::beginWriteArray( const QString &prefix, int size )
255 {
256  mUsingGlobalArray = false;
257  mUserSettings->beginWriteArray( prefix, size );
258 }
259 
261 {
262  mUserSettings->endArray();
263  if ( mGlobalSettings )
264  {
265  mGlobalSettings->endArray();
266  }
267  mUsingGlobalArray = false;
268 }
269 
271 {
272  if ( mGlobalSettings && mUsingGlobalArray )
273  {
274  mGlobalSettings->setArrayIndex( i );
275  }
276  else
277  {
278  mUserSettings->setArrayIndex( i );
279  }
280 }
281 
282 void QgsSettings::setValue( const QString &key, const QVariant &value, const QgsSettings::Section section )
283 {
284  // TODO: add valueChanged signal
285  // Do not store if it hasn't changed from default value
286  // First check if the values are different and if at least one of them is valid.
287  // The valid check is required because different invalid QVariant types
288  // like QVariant(QVariant::String) and QVariant(QVariant::Int))
289  // may be considered different and we don't want to store the value in that case.
290  QVariant currentValue { QgsSettings::value( prefixedKey( key, section ) ) };
291  if ( ( currentValue.isValid() || value.isValid() ) && ( currentValue != value ) )
292  {
293  mUserSettings->setValue( prefixedKey( key, section ), value );
294  }
295  // Deliberately an "else if" because we want to remove a value from the user settings
296  // only if the value is different than the one stored in the global settings (because
297  // it would be the default anyway). The first check is necessary because the global settings
298  // might be a nullptr (for example in case of standalone scripts or apps).
299  else if ( mGlobalSettings && mGlobalSettings->value( prefixedKey( key, section ) ) == currentValue )
300  {
301  mUserSettings->remove( prefixedKey( key, section ) );
302  }
303 }
304 
305 // To lower case and clean the path
306 QString QgsSettings::sanitizeKey( const QString &key ) const
307 {
308  return QDir::cleanPath( key );
309 }
310 
312 {
313  mUserSettings->clear();
314 }
void clear()
Removes all entries in the user settings.
QStringList childGroups() const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
void setArrayIndex(int i)
Sets the current array index to i.
void endGroup()
Resets the group to what it was before the corresponding beginGroup() call.
Definition: qgssettings.cpp:97
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QStringList childKeys() const
Returns a list of all top-level keys that can be read using the QSettings object. ...
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
QStringList allKeys() const
Returns a list of all keys, including subkeys, that can be read using the QSettings object...
static bool setGlobalSettingsPath(const QString &path)
Sets the Global Settings QSettings storage file.
Definition: qgssettings.cpp:28
void sync()
Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in t...
~QgsSettings() override
Definition: qgssettings.cpp:80
QStringList globalChildGroups() const
Returns a list of all key top-level groups (same as childGroups) but only for groups defined in globa...
Section
Sections for namespaced settings.
Definition: qgssettings.h:64
QString prefixedKey(const QString &key, QgsSettings::Section section) const
Returns the sanitized and prefixed key.
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
Definition: qgssettings.cpp:87
QString fileName() const
Returns the path where settings written using this QSettings object are stored.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QString group() const
Returns the current group.
QgsSettings(const QString &organization, const QString &application=QString(), QObject *parent=nullptr)
Construct a QgsSettings object for accessing settings of the application called application from the ...
Definition: qgssettings.cpp:48
void endArray()
Closes the array that was started using beginReadArray() or beginWriteArray().
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
int beginReadArray(const QString &prefix)
Adds prefix to the current group and starts reading from an array. Returns the size of the array...
void beginWriteArray(const QString &prefix, int size=-1)
Adds prefix to the current group and starts writing an array of size size.