Qt 4.8
qsettings_p.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_P_H
43 #define QSETTINGS_P_H
44 
45 //
46 // W A R N I N G
47 // -------------
48 //
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55 
56 #include "QtCore/qdatetime.h"
57 #include "QtCore/qmap.h"
58 #include "QtCore/qmutex.h"
59 #include "QtCore/qiodevice.h"
60 #include "QtCore/qstack.h"
61 #include "QtCore/qstringlist.h"
62 #ifndef QT_NO_QOBJECT
63 #include "private/qobject_p.h"
64 #endif
65 #include "private/qscopedpointer_p.h"
66 
67 #ifdef Q_OS_WIN
68 #include "QtCore/qt_windows.h"
69 #endif
70 
72 
73 #if defined(Q_WS_QWS) || defined(Q_WS_QPA)
74 #define QT_QSETTINGS_ALWAYS_CASE_SENSITIVE_AND_FORGET_ORIGINAL_KEY_ORDER
75 #endif
76 
77 // used in testing framework
78 #define QSETTINGS_P_H_VERSION 3
79 
80 #ifdef QT_QSETTINGS_ALWAYS_CASE_SENSITIVE_AND_FORGET_ORIGINAL_KEY_ORDER
82 
83 class QSettingsKey : public QString
84 {
85 public:
86  inline QSettingsKey(const QString &key, Qt::CaseSensitivity cs, int /* position */ = -1)
87  : QString(key) { Q_ASSERT(cs == Qt::CaseSensitive); Q_UNUSED(cs); }
88 
89  inline QString originalCaseKey() const { return *this; }
90  inline int originalKeyPosition() const { return -1; }
91 };
92 #else
94 
95 class QSettingsKey : public QString
96 {
97 public:
98  inline QSettingsKey(const QString &key, Qt::CaseSensitivity cs, int position = -1)
99  : QString(key), theOriginalKey(key), theOriginalKeyPosition(position)
100  {
101  if (cs == Qt::CaseInsensitive)
103  }
104 
105  inline QString originalCaseKey() const { return theOriginalKey; }
106  inline int originalKeyPosition() const { return theOriginalKeyPosition; }
107 
108 private:
109  QString theOriginalKey;
110  int theOriginalKeyPosition;
111 };
112 #endif
113 
116 
118 {
119 public:
120  inline QSettingsGroup()
121  : num(-1), maxNum(-1) {}
122  inline QSettingsGroup(const QString &s)
123  : str(s), num(-1), maxNum(-1) {}
124  inline QSettingsGroup(const QString &s, bool guessArraySize)
125  : str(s), num(0), maxNum(guessArraySize ? 0 : -1) {}
126 
127  inline QString name() const { return str; }
128  inline QString toString() const;
129  inline bool isArray() const { return num != -1; }
130  inline int arraySizeGuess() const { return maxNum; }
131  inline void setArrayIndex(int i)
132  { num = i + 1; if (maxNum != -1 && num > maxNum) maxNum = num; }
133 
135  int num;
136  int maxNum;
137 };
138 
140 {
141  QString result;
142  result = str;
143  if (num > 0) {
144  result += QLatin1Char('/');
145  result += QString::number(num);
146  }
147  return result;
148 }
149 
151 {
152 public:
153  ~QConfFile();
154 
155  ParsedSettingsMap mergedKeyMap() const;
156  bool isWritable() const;
157 
158  static QConfFile *fromName(const QString &name, bool _userPerms);
159  static void clearCache();
160 
170  bool userPerms;
171 
172 private:
173 #ifdef Q_DISABLE_COPY
174  QConfFile(const QConfFile &);
175  QConfFile &operator=(const QConfFile &);
176 #endif
177  QConfFile(const QString &name, bool _userPerms);
178 
179  friend class QConfFile_createsItself; // silences compiler warning
180 };
181 
183 #ifndef QT_NO_QOBJECT
184  : public QObjectPrivate
185 #endif
186 {
187 #ifdef QT_NO_QOBJECT
188  QSettings *q_ptr;
189 #endif
191 
192 public:
195  const QString &organization, const QString &application);
196  virtual ~QSettingsPrivate();
197 
198  virtual void remove(const QString &key) = 0;
199  virtual void set(const QString &key, const QVariant &value) = 0;
200  virtual bool get(const QString &key, QVariant *value) const = 0;
201 
202  enum ChildSpec { AllKeys, ChildKeys, ChildGroups };
203  virtual QStringList children(const QString &prefix, ChildSpec spec) const = 0;
204 
205  virtual void clear() = 0;
206  virtual void sync() = 0;
207  virtual void flush() = 0;
208  virtual bool isWritable() const = 0;
209  virtual QString fileName() const = 0;
210 
211  QString actualKey(const QString &key) const;
212  void beginGroupOrArray(const QSettingsGroup &group);
213  void setStatus(QSettings::Status status) const;
214  void requestUpdate();
215  void update();
216 
217  static QString normalizedKey(const QString &key);
219  const QString &organization, const QString &application);
220  static QSettingsPrivate *create(const QString &fileName, QSettings::Format format);
221 
222  static void processChild(QString key, ChildSpec spec, QMap<QString, QString> &result);
223 
224  // Variant streaming functions
225  static QStringList variantListToStringList(const QVariantList &l);
226  static QVariant stringListToVariantList(const QStringList &l);
227 
228  // parser functions
229  static QString variantToString(const QVariant &v);
230  static QVariant stringToVariant(const QString &s);
231  static void iniEscapedKey(const QString &key, QByteArray &result);
232  static bool iniUnescapedKey(const QByteArray &key, int from, int to, QString &result);
233  static void iniEscapedString(const QString &str, QByteArray &result, QTextCodec *codec);
234  static void iniEscapedStringList(const QStringList &strs, QByteArray &result, QTextCodec *codec);
235  static bool iniUnescapedStringList(const QByteArray &str, int from, int to,
236  QString &stringResult, QStringList &stringListResult,
237  QTextCodec *codec);
238  static QStringList splitArgs(const QString &s, int idx);
239 
240  /*
241  The numeric values of these enums define their search order. For example,
242  F_User | F_Organization is searched before F_System | F_Application,
243  because their values are respectively 1 and 2.
244  */
245  enum {
246 #if !defined(Q_OS_BLACKBERRY)
247  F_Application = 0x0,
248  F_Organization = 0x1,
249  F_User = 0x0,
250  F_System = 0x2,
251  NumConfFiles = 4
252 #else
253  SandboxConfFile = 0,
254  NumConfFiles = 1
255 #endif
256  };
257 
263 
264 protected:
267  int spec;
268  bool fallbacks;
271 };
272 
274 {
275 public:
277  const QString &organization, const QString &application);
280 
281  void remove(const QString &key);
282  void set(const QString &key, const QVariant &value);
283  bool get(const QString &key, QVariant *value) const;
284 
285  QStringList children(const QString &prefix, ChildSpec spec) const;
286 
287  void clear();
288  void sync();
289  void flush();
290  bool isWritable() const;
291  QString fileName() const;
292 
293  static bool readIniFile(const QByteArray &data, UnparsedSettingsMap *unparsedIniSections);
294  static bool readIniSection(const QSettingsKey &section, const QByteArray &data,
295  ParsedSettingsMap *settingsMap, QTextCodec *codec);
296  static bool readIniLine(const QByteArray &data, int &dataPos, int &lineStart, int &lineLen,
297  int &equalsPos);
298 
299 private:
300  void initFormat();
301  void initAccess();
302  void syncConfFile(int confFileNo);
303  bool writeIniFile(QIODevice &device, const ParsedSettingsMap &map);
304 #ifdef Q_OS_MAC
305  bool readPlistFile(const QString &fileName, ParsedSettingsMap *map) const;
306  bool writePlistFile(const QString &fileName, const ParsedSettingsMap &map) const;
307 #endif
308  void ensureAllSectionsParsed(QConfFile *confFile) const;
309  void ensureSectionParsed(QConfFile *confFile, const QSettingsKey &key) const;
310 
311  QScopedSharedPointer<QConfFile> confFiles[NumConfFiles];
317 };
318 
320 
321 #endif // QSETTINGS_P_H
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
Scope
) in file paths:
Definition: qsettings.h:115
QString section(QChar sep, int start, int end=-1, SectionFlags flags=SectionDefault) const
This function returns a section of the string.
Definition: qstring.h:781
QString originalCaseKey() const
Definition: qsettings_p.h:89
Status
The following status values are possible:
Definition: qsettings.h:86
Qt::CaseSensitivity caseSensitivity
Definition: qsettings_p.h:315
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QMutex class provides access serialization between threads.
Definition: qmutex.h:60
QString & operator=(QChar c)
Definition: qstring.cpp:1582
The QAtomicInt class provides platform-independent atomic operations on integers. ...
Definition: qatomic.h:55
The QSettings class provides persistent platform-independent application settings.
Definition: qsettings.h:73
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static Expression::Ptr create(Expression *const expr, const YYLTYPE &sourceLocator, const ParserContext *const parseInfo)
QSettings::WriteFunc writeFunc
Definition: qsettings_p.h:313
Format
This enum type specifies the storage format used by QSettings.
Definition: qsettings.h:92
bool(* WriteFunc)(QIODevice &device, const SettingsMap &map)
Typedef for a pointer to a function with the following signature:
Definition: qsettings.h:192
static qreal position(QGraphicsObject *item, QDeclarativeAnchorLine::AnchorLine anchorLine)
QSettings::Scope scope
Definition: qsettings_p.h:259
static LibLoadStatus status
Definition: qlocale_icu.cpp:69
QString applicationName
Definition: qsettings_p.h:261
QDateTime timeStamp
Definition: qsettings_p.h:162
QString name
Definition: qsettings_p.h:161
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
int arraySizeGuess() const
Definition: qsettings_p.h:130
QChar * data()
Returns a pointer to the data stored in the QString.
Definition: qstring.h:710
QString toString() const
Definition: qsettings_p.h:139
static QString toString(Register *reg, int type, bool *ok=0)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QSettings::ReadFunc readFunc
Definition: qsettings_p.h:312
QSettingsGroup(const QString &s)
Definition: qsettings_p.h:122
const char * name
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
ParsedSettingsMap addedKeys
Definition: qsettings_p.h:166
QMutex mutex
Definition: qsettings_p.h:169
static bool variantToString(const QVariant &arg, QString &out)
Definition: qdbusutil.cpp:81
static QTextCodec * codec(MYSQL *mysql)
Definition: qsql_mysql.cpp:220
QString name() const
Definition: qsettings_p.h:127
bool(* ReadFunc)(QIODevice &device, SettingsMap &map)
Typedef for a pointer to a function with the following signature:
Definition: qsettings.h:191
__int64 qint64
Definition: qglobal.h:942
static const Qt::CaseSensitivity IniCaseSensitivity
Definition: qsettings_p.h:81
int originalKeyPosition() const
Definition: qsettings_p.h:90
QSettingsGroup(const QString &s, bool guessArraySize)
Definition: qsettings_p.h:124
QTextCodec * iniCodec
Definition: qsettings_p.h:262
CaseSensitivity
Definition: qnamespace.h:1451
ParsedSettingsMap originalKeys
Definition: qsettings_p.h:165
void setArrayIndex(int i)
Definition: qsettings_p.h:131
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
QSettingsKey(const QString &key, Qt::CaseSensitivity cs, int=-1)
Definition: qsettings_p.h:86
bool userPerms
Definition: qsettings_p.h:170
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
ParsedSettingsMap removedKeys
Definition: qsettings_p.h:167
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
QSettings::Format format
Definition: qsettings_p.h:258
int key
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
QString groupPrefix
Definition: qsettings_p.h:266
QMap< QSettingsKey, QByteArray > UnparsedSettingsMap
Definition: qsettings_p.h:114
UnparsedSettingsMap unparsedIniSections
Definition: qsettings_p.h:164
#define Q_AUTOTEST_EXPORT
Definition: qglobal.h:1510
QFactoryLoader * l
Q_CORE_EXPORT QTextStream & flush(QTextStream &s)
QMap< QSettingsKey, QVariant > ParsedSettingsMap
Definition: qsettings_p.h:115
QStack< QSettingsGroup > groupStack
Definition: qsettings_p.h:265
QAtomicInt ref
Definition: qsettings_p.h:168
The QTextCodec class provides conversions between text encodings.
Definition: qtextcodec.h:62
QSettings::Status status
Definition: qsettings_p.h:270
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
qint64 size
Definition: qsettings_p.h:163
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
static QString fileName(const QString &fileUrl)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
bool isArray() const
Definition: qsettings_p.h:129
QString organizationName
Definition: qsettings_p.h:260
QString()
Constructs a null string.
Definition: qstring.h:879