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