QtCore/qsettings.h Source File

qsettings.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QSETTINGS_H
43 #define QSETTINGS_H
44 
45 #include <QtCore/qobject.h>
46 #include <QtCore/qvariant.h>
47 #include <QtCore/qstring.h>
48 
50 
52 QT_MODULE(Core)
54 
55 #ifndef QT_NO_SETTINGS
56 
57 #ifdef QT3_SUPPORT
58 #include <QtCore/qstringlist.h>
59 #endif
60 
61 #include <ctype.h>
62 
64 
65 #ifdef Status // we seem to pick up a macro Status --> int somewhere
66 #undef Status
67 #endif
68 
69 class QIODevice;
70 class QSettingsPrivate;
71 
72 #ifndef QT_NO_QOBJECT
73 class Q_CORE_EXPORT QSettings : public QObject
74 #else
75 class Q_CORE_EXPORT QSettings
76 #endif
77 {
78 #ifndef QT_NO_QOBJECT
79  Q_OBJECT
80 #else
82 #endif
83  Q_DECLARE_PRIVATE(QSettings)
84 
85 public:
86  enum Status {
87  NoError = 0,
89  FormatError
90  };
91 
92  enum Format {
95 
96  InvalidFormat = 16,
112  CustomFormat16
113  };
114 
115  enum Scope {
117  SystemScope
118 #ifdef QT3_SUPPORT
119  ,
120  User = UserScope,
121  Global = SystemScope
122 #endif
123  };
124 
125 #ifndef QT_NO_QOBJECT
126  explicit QSettings(const QString &organization,
127  const QString &application = QString(), QObject *parent = 0);
128  QSettings(Scope scope, const QString &organization,
129  const QString &application = QString(), QObject *parent = 0);
130  QSettings(Format format, Scope scope, const QString &organization,
131  const QString &application = QString(), QObject *parent = 0);
132  QSettings(const QString &fileName, Format format, QObject *parent = 0);
133  explicit QSettings(QObject *parent = 0);
134 #else
135  explicit QSettings(const QString &organization,
136  const QString &application = QString());
137  QSettings(Scope scope, const QString &organization,
138  const QString &application = QString());
139  QSettings(Format format, Scope scope, const QString &organization,
140  const QString &application = QString());
141  QSettings(const QString &fileName, Format format);
142 #endif
143  ~QSettings();
144 
145  void clear();
146  void sync();
147  Status status() const;
148 
149  void beginGroup(const QString &prefix);
150  void endGroup();
151  QString group() const;
152 
153  int beginReadArray(const QString &prefix);
154  void beginWriteArray(const QString &prefix, int size = -1);
155  void endArray();
156  void setArrayIndex(int i);
157 
158  QStringList allKeys() const;
159  QStringList childKeys() const;
160  QStringList childGroups() const;
161  bool isWritable() const;
162 
163  void setValue(const QString &key, const QVariant &value);
164  QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const;
165 
166  void remove(const QString &key);
167  bool contains(const QString &key) const;
168 
169  void setFallbacksEnabled(bool b);
170  bool fallbacksEnabled() const;
171 
172  QString fileName() const;
173  Format format() const;
174  Scope scope() const;
175  QString organizationName() const;
176  QString applicationName() const;
177 
178 #ifndef QT_NO_TEXTCODEC
179  void setIniCodec(QTextCodec *codec);
180  void setIniCodec(const char *codecName);
181  QTextCodec *iniCodec() const;
182 #endif
183 
184  static void setDefaultFormat(Format format);
185  static Format defaultFormat();
186  static void setSystemIniPath(const QString &dir); // ### remove in 5.0 (use setPath() instead)
187  static void setUserIniPath(const QString &dir); // ### remove in 5.0 (use setPath() instead)
188  static void setPath(Format format, Scope scope, const QString &path);
189 
191  typedef bool (*ReadFunc)(QIODevice &device, SettingsMap &map);
192  typedef bool (*WriteFunc)(QIODevice &device, const SettingsMap &map);
193 
194  static Format registerFormat(const QString &extension, ReadFunc readFunc, WriteFunc writeFunc,
195  Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive);
196 
197 #ifdef QT3_SUPPORT
198  inline QT3_SUPPORT bool writeEntry(const QString &key, bool value)
199  { setValue(key, value); return isWritable(); }
200  inline QT3_SUPPORT bool writeEntry(const QString &key, double value)
201  { setValue(key, value); return isWritable(); }
202  inline QT3_SUPPORT bool writeEntry(const QString &key, int value)
203  { setValue(key, value); return isWritable(); }
204  inline QT3_SUPPORT bool writeEntry(const QString &key, const char *value)
205  { setValue(key, QString::fromAscii(value)); return isWritable(); }
206  inline QT3_SUPPORT bool writeEntry(const QString &key, const QString &value)
207  { setValue(key, value); return isWritable(); }
208  inline QT3_SUPPORT bool writeEntry(const QString &key, const QStringList &value)
209  { setValue(key, value); return isWritable(); }
210  inline QT3_SUPPORT bool writeEntry(const QString &key, const QStringList &value, QChar separator)
211  { setValue(key, value.join(QString(separator))); return isWritable(); }
212  inline QT3_SUPPORT QStringList readListEntry(const QString &key, bool *ok = 0)
213  {
214  if (ok)
215  *ok = contains(key);
216  return value(key).toStringList();
217  }
218  inline QT3_SUPPORT QStringList readListEntry(const QString &key, QChar separator, bool *ok = 0)
219  {
220  if (ok)
221  *ok = contains(key);
222  QString str = value(key).toString();
223  if (str.isEmpty())
224  return QStringList();
225  return str.split(separator);
226  }
227  inline QT3_SUPPORT QString readEntry(const QString &key, const QString &defaultValue = QString(),
228  bool *ok = 0)
229  {
230  if (ok)
231  *ok = contains(key);
232  return value(key, defaultValue).toString();
233  }
234  inline QT3_SUPPORT int readNumEntry(const QString &key, int defaultValue = 0, bool *ok = 0)
235  {
236  if (ok)
237  *ok = contains(key);
238  return value(key, defaultValue).toInt();
239  }
240  inline QT3_SUPPORT double readDoubleEntry(const QString &key, double defaultValue = 0,
241  bool *ok = 0)
242  {
243  if (ok)
244  *ok = contains(key);
245  return value(key, defaultValue).toDouble();
246  }
247  inline QT3_SUPPORT bool readBoolEntry(const QString &key, bool defaultValue = false,
248  bool *ok = 0)
249  {
250  if (ok)
251  *ok = contains(key);
252  return value(key, defaultValue).toBool();
253  }
254  inline QT3_SUPPORT bool removeEntry(const QString &key)
255  { remove(key); return true; }
256 
257  enum System { Unix, Windows, Mac };
258  inline QT3_SUPPORT void insertSearchPath(System, const QString &) {}
259  inline QT3_SUPPORT void removeSearchPath(System, const QString &) {}
260 
261  inline QT3_SUPPORT void setPath(const QString &organization, const QString &application,
262  Scope scope = Global)
263  {
264  setPath_helper(scope == Global ? QSettings::SystemScope : QSettings::UserScope,
265  organization, application);
266  }
267  inline QT3_SUPPORT void resetGroup()
268  {
269  while (!group().isEmpty())
270  endGroup();
271  }
272  inline QT3_SUPPORT QStringList entryList(const QString &key) const
273  {
274  QSettings *that = const_cast<QSettings *>(this);
275  QStringList result;
276 
277  that->beginGroup(key);
278  result = that->childKeys();
279  that->endGroup();
280  return result;
281  }
282  inline QT3_SUPPORT QStringList subkeyList(const QString &key) const
283  {
284  QSettings *that = const_cast<QSettings *>(this);
285  QStringList result;
286 
287  that->beginGroup(key);
288  result = that->childGroups();
289  that->endGroup();
290  return result;
291  }
292 #endif
293 
294 protected:
295 #ifndef QT_NO_QOBJECT
296  bool event(QEvent *event);
297 #endif
298 
299 private:
300 #ifdef QT3_SUPPORT
301  void setPath_helper(Scope scope, const QString &organization, const QString &application);
302 #endif
303 
304  Q_DISABLE_COPY(QSettings)
305 };
306 
308 
309 #endif // QT_NO_SETTINGS
310 
312 
313 #endif // QSETTINGS_H
#define QT_END_NAMESPACE
Definition: qglobal.h:128
#define QT_BEGIN_HEADER
Definition: qglobal.h:141
CaseSensitivity
Definition: qnamespace.h:1451
QFuture< void > map(Sequence &sequence, MapFunctor map)
QStringList childGroups() const
void endArray()
static QString fromAscii(const char *, int size=-1)
bool fallbacksEnabled() const
QSettings(const QString &organization, const QString &application=QString(), QObject *parent=0)
Status status() const
typedef Status(APIENTRYP PFNGLXGETTRANSPARENTINDEXSUNPROC)(Display *dpy
void beginGroup(const QString &prefix)
static void setPath(Format format, Scope scope, const QString &path)
QStringList allKeys() const
void sync()
QString fileName() const
QScopedPointer< QObjectData > d_ptr
Definition: qobject.h:320
bool isWritable() const
#define QT_BEGIN_NAMESPACE
Definition: qglobal.h:127
QString organizationName() const
GLenum GLsizei GLenum format
Definition: GLee.h:873
void setFallbacksEnabled(bool b)
static Format defaultFormat()
QMap< QString, QVariant > SettingsMap
Definition: qsettings.h:190
void setArrayIndex(int i)
void setIniCodec(QTextCodec *codec)
static Format registerFormat(const QString &extension, ReadFunc readFunc, WriteFunc writeFunc, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
#define Q_OBJECT
Definition: qobjectdefs.h:157
QStringList childKeys() const
GLubyte GLubyte b
Definition: GLee.h:5404
GLsizei const GLfloat * value
Definition: GLee.h:1742
virtual bool event(QEvent *)
QString join(const QString &sep) const
Definition: qstringlist.h:162
QString group() const
Scope scope() const
bool contains(const QString &key) const
Definition: qchar.h:72
static void setSystemIniPath(const QString &dir)
QTextCodec * iniCodec() const
static void setUserIniPath(const QString &dir)
void clear()
#define QT_END_HEADER
Definition: qglobal.h:142
GLsizeiptr size
Definition: GLee.h:1561
int beginReadArray(const QString &prefix)
static void setDefaultFormat(Format format)
bool isEmpty() const
Definition: qstring.h:704
void endGroup()
void setValue(const QString &key, const QVariant &value)
QString applicationName() const
void beginWriteArray(const QString &prefix, int size=-1)