QGIS API Documentation  3.6.0-Noosa (5873452)
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 #include <QFileInfo>
19 #include <QSettings>
20 #include <QDir>
21 
22 #include "qgssettings.h"
23 #include "qgslogger.h"
24 
25 QString QgsSettings::sGlobalSettingsPath = QString();
26 
27 bool QgsSettings::setGlobalSettingsPath( const QString &path )
28 {
29  if ( QFileInfo::exists( path ) )
30  {
31  sGlobalSettingsPath = path;
32  return true;
33  }
34  return false;
35 }
36 
37 void QgsSettings::init()
38 {
39  if ( ! sGlobalSettingsPath.isEmpty() )
40  {
41  mGlobalSettings = new QSettings( sGlobalSettingsPath, QSettings::IniFormat );
42  mGlobalSettings->setIniCodec( "UTF-8" );
43  }
44 }
45 
46 
47 QgsSettings::QgsSettings( const QString &organization, const QString &application, QObject *parent )
48 {
49  mUserSettings = new QSettings( organization, application, parent );
50  init();
51 }
52 
53 QgsSettings::QgsSettings( QSettings::Scope scope, const QString &organization,
54  const QString &application, QObject *parent )
55 {
56  mUserSettings = new QSettings( scope, organization, application, parent );
57  init();
58 }
59 
60 QgsSettings::QgsSettings( QSettings::Format format, QSettings::Scope scope,
61  const QString &organization, const QString &application, QObject *parent )
62 {
63  mUserSettings = new QSettings( format, scope, organization, application, parent );
64  init();
65 }
66 
67 QgsSettings::QgsSettings( const QString &fileName, QSettings::Format format, QObject *parent )
68 {
69  mUserSettings = new QSettings( fileName, format, parent );
70  init();
71 }
72 
73 QgsSettings::QgsSettings( QObject *parent )
74 {
75  mUserSettings = new QSettings( parent );
76  init();
77 }
78 
80 {
81  delete mUserSettings;
82  delete mGlobalSettings;
83 }
84 
85 
86 void QgsSettings::beginGroup( const QString &prefix, const QgsSettings::Section section )
87 {
88  QString pKey = prefixedKey( prefix, section );
89  mUserSettings->beginGroup( pKey );
90  if ( mGlobalSettings )
91  {
92  mGlobalSettings->beginGroup( pKey );
93  }
94 }
95 
97 {
98  mUserSettings->endGroup();
99  if ( mGlobalSettings )
100  {
101  mGlobalSettings->endGroup();
102  }
103 }
104 
105 QString QgsSettings::group() const
106 {
107  return mUserSettings->group();
108 }
109 
110 QStringList QgsSettings::allKeys() const
111 {
112  QStringList keys = mUserSettings->allKeys();
113  if ( mGlobalSettings )
114  {
115  for ( auto &s : mGlobalSettings->allKeys() )
116  {
117  if ( ! keys.contains( s ) )
118  {
119  keys.append( s );
120  }
121  }
122  }
123  return keys;
124 }
125 
126 
127 QStringList QgsSettings::childKeys() const
128 {
129  QStringList keys = mUserSettings->childKeys();
130  if ( mGlobalSettings )
131  {
132  for ( auto &s : mGlobalSettings->childKeys() )
133  {
134  if ( ! keys.contains( s ) )
135  {
136  keys.append( s );
137  }
138  }
139  }
140  return keys;
141 }
142 
143 QStringList QgsSettings::childGroups() const
144 {
145  QStringList keys = mUserSettings->childGroups();
146  if ( mGlobalSettings )
147  {
148  for ( auto &s : mGlobalSettings->childGroups() )
149  {
150  if ( ! keys.contains( s ) )
151  {
152  keys.append( s );
153  }
154  }
155  }
156  return keys;
157 }
159 {
160  QStringList keys;
161  if ( mGlobalSettings )
162  {
163  keys = mGlobalSettings->childGroups();
164  }
165  return keys;
166 }
167 
168 QVariant QgsSettings::value( const QString &key, const QVariant &defaultValue, const QgsSettings::Section section ) const
169 {
170  QString pKey = prefixedKey( key, section );
171  if ( !mUserSettings->value( pKey ).isNull() )
172  {
173  return mUserSettings->value( pKey );
174  }
175  if ( mGlobalSettings )
176  {
177  return mGlobalSettings->value( pKey, defaultValue );
178  }
179  return defaultValue;
180 }
181 
182 bool QgsSettings::contains( const QString &key, const QgsSettings::Section section ) const
183 {
184  QString pKey = prefixedKey( key, section );
185  return mUserSettings->contains( pKey ) ||
186  ( mGlobalSettings && mGlobalSettings->contains( pKey ) );
187 }
188 
189 QString QgsSettings::fileName() const
190 {
191  return mUserSettings->fileName();
192 }
193 
195 {
196  mUserSettings->sync();
197 }
198 
199 void QgsSettings::remove( const QString &key, const QgsSettings::Section section )
200 {
201  QString pKey = prefixedKey( key, section );
202  mUserSettings->remove( pKey );
203 }
204 
205 QString QgsSettings::prefixedKey( const QString &key, const Section section ) const
206 {
207  QString prefix;
208  switch ( section )
209  {
210  case Section::Core :
211  prefix = QStringLiteral( "core" );
212  break;
213  case Section::Server :
214  prefix = QStringLiteral( "server" );
215  break;
216  case Section::Gui :
217  prefix = QStringLiteral( "gui" );
218  break;
219  case Section::Plugins :
220  prefix = QStringLiteral( "plugins" );
221  break;
222  case Section::Misc :
223  prefix = QStringLiteral( "misc" );
224  break;
225  case Section::Auth :
226  prefix = QStringLiteral( "auth" );
227  break;
228  case Section::App :
229  prefix = QStringLiteral( "app" );
230  break;
231  case Section::Providers :
232  prefix = QStringLiteral( "providers" );
233  break;
234  case Section::NoSection:
235  default:
236  return sanitizeKey( key );
237  }
238  return prefix + "/" + sanitizeKey( key );
239 }
240 
241 
242 int QgsSettings::beginReadArray( const QString &prefix )
243 {
244  int size = mUserSettings->beginReadArray( sanitizeKey( prefix ) );
245  if ( 0 == size && mGlobalSettings )
246  {
247  size = mGlobalSettings->beginReadArray( sanitizeKey( prefix ) );
248  mUsingGlobalArray = ( size > 0 );
249  }
250  return size;
251 }
252 
253 void QgsSettings::beginWriteArray( const QString &prefix, int size )
254 {
255  mUsingGlobalArray = false;
256  mUserSettings->beginWriteArray( prefix, size );
257 }
258 
260 {
261  mUserSettings->endArray();
262  if ( mGlobalSettings )
263  {
264  mGlobalSettings->endArray();
265  }
266  mUsingGlobalArray = false;
267 }
268 
270 {
271  if ( mGlobalSettings && mUsingGlobalArray )
272  {
273  mGlobalSettings->setArrayIndex( i );
274  }
275  else
276  {
277  mUserSettings->setArrayIndex( i );
278  }
279 }
280 
281 void QgsSettings::setValue( const QString &key, const QVariant &value, const QgsSettings::Section section )
282 {
283  // TODO: add valueChanged signal
284  mUserSettings->setValue( prefixedKey( key, section ), value );
285 }
286 
287 // To lower case and clean the path
288 QString QgsSettings::sanitizeKey( const QString &key ) const
289 {
290  return QDir::cleanPath( key );
291 }
292 
294 {
295  mUserSettings->clear();
296 }
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:96
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:27
void sync()
Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in t...
~QgsSettings() override
Definition: qgssettings.cpp:79
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:86
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:47
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.