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