QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
107 QStringList QgsSettings::allKeys() const
108 {
109  QStringList keys = mUserSettings->allKeys();
110  if ( mGlobalSettings )
111  {
112  for ( auto &s : mGlobalSettings->allKeys() )
113  {
114  if ( ! keys.contains( s ) )
115  {
116  keys.append( s );
117  }
118  }
119  }
120  return keys;
121 }
122 
123 
124 QStringList QgsSettings::childKeys() const
125 {
126  QStringList keys = mUserSettings->childKeys();
127  if ( mGlobalSettings )
128  {
129  for ( auto &s : mGlobalSettings->childKeys() )
130  {
131  if ( ! keys.contains( s ) )
132  {
133  keys.append( s );
134  }
135  }
136  }
137  return keys;
138 }
139 
140 QStringList QgsSettings::childGroups() const
141 {
142  QStringList keys = mUserSettings->childGroups();
143  if ( mGlobalSettings )
144  {
145  for ( auto &s : mGlobalSettings->childGroups() )
146  {
147  if ( ! keys.contains( s ) )
148  {
149  keys.append( s );
150  }
151  }
152  }
153  return keys;
154 }
156 {
157  QStringList keys;
158  if ( mGlobalSettings )
159  {
160  keys = mGlobalSettings->childGroups();
161  }
162  return keys;
163 }
164 
165 QVariant QgsSettings::value( const QString &key, const QVariant &defaultValue, const QgsSettings::Section section ) const
166 {
167  QString pKey = prefixedKey( key, section );
168  if ( !mUserSettings->value( pKey ).isNull() )
169  {
170  return mUserSettings->value( pKey );
171  }
172  if ( mGlobalSettings )
173  {
174  return mGlobalSettings->value( pKey, defaultValue );
175  }
176  return defaultValue;
177 }
178 
179 bool QgsSettings::contains( const QString &key, const QgsSettings::Section section ) const
180 {
181  QString pKey = prefixedKey( key, section );
182  return mUserSettings->contains( pKey ) ||
183  ( mGlobalSettings && mGlobalSettings->contains( pKey ) );
184 }
185 
186 QString QgsSettings::fileName() const
187 {
188  return mUserSettings->fileName();
189 }
190 
192 {
193  mUserSettings->sync();
194 }
195 
196 void QgsSettings::remove( const QString &key, const QgsSettings::Section section )
197 {
198  QString pKey = prefixedKey( key, section );
199  mUserSettings->remove( pKey );
200 }
201 
202 QString QgsSettings::prefixedKey( const QString &key, const Section section ) const
203 {
204  QString prefix;
205  switch ( section )
206  {
207  case Section::Core :
208  prefix = QStringLiteral( "core" );
209  break;
210  case Section::Server :
211  prefix = QStringLiteral( "server" );
212  break;
213  case Section::Gui :
214  prefix = QStringLiteral( "gui" );
215  break;
216  case Section::Plugins :
217  prefix = QStringLiteral( "plugins" );
218  break;
219  case Section::Misc :
220  prefix = QStringLiteral( "misc" );
221  break;
222  case Section::Auth :
223  prefix = QStringLiteral( "auth" );
224  break;
225  case Section::App :
226  prefix = QStringLiteral( "app" );
227  break;
228  case Section::Providers :
229  prefix = QStringLiteral( "providers" );
230  break;
231  case Section::NoSection:
232  default:
233  return sanitizeKey( key );
234  }
235  return prefix + "/" + sanitizeKey( key );
236 }
237 
238 
239 int QgsSettings::beginReadArray( const QString &prefix )
240 {
241  int size = mUserSettings->beginReadArray( sanitizeKey( prefix ) );
242  if ( 0 == size && mGlobalSettings )
243  {
244  size = mGlobalSettings->beginReadArray( sanitizeKey( prefix ) );
245  mUsingGlobalArray = ( size > 0 );
246  }
247  return size;
248 }
249 
250 void QgsSettings::beginWriteArray( const QString &prefix, int size )
251 {
252  mUsingGlobalArray = false;
253  mUserSettings->beginWriteArray( prefix, size );
254 }
255 
257 {
258  mUserSettings->endArray();
259  if ( mGlobalSettings )
260  {
261  mGlobalSettings->endArray();
262  }
263  mUsingGlobalArray = false;
264 }
265 
267 {
268  if ( mGlobalSettings && mUsingGlobalArray )
269  {
270  mGlobalSettings->setArrayIndex( i );
271  }
272  else
273  {
274  mUserSettings->setArrayIndex( i );
275  }
276 }
277 
278 void QgsSettings::setValue( const QString &key, const QVariant &value, const QgsSettings::Section section )
279 {
280  // TODO: add valueChanged signal
281  mUserSettings->setValue( prefixedKey( key, section ), value );
282 }
283 
284 // To lower case and clean the path
285 QString QgsSettings::sanitizeKey( const QString &key ) const
286 {
287  return QDir::cleanPath( key );
288 }
289 
291 {
292  mUserSettings->clear();
293 }
void clear()
Removes all entries in the user settings.
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
QStringList allKeys() const
Returns a list of all keys, including subkeys, 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.
static bool setGlobalSettingsPath(const QString &path)
Sets the Global Settings QSettings storage file.
Definition: qgssettings.cpp:28
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
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
Section
Sections for namespaced settings.
Definition: qgssettings.h:64
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.
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
QStringList childGroups() const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
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
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().
QStringList childKeys() const
Returns a list of all top-level keys 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 beginWriteArray(const QString &prefix, int size=-1)
Adds prefix to the current group and starts writing an array of size size.
QStringList globalChildGroups() const
Returns a list of all key top-level groups (same as childGroups) but only for groups defined in globa...