~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/corelib/io/qsettings_p.h

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 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
QT_BEGIN_NAMESPACE
 
68
 
 
69
#ifndef Q_OS_WIN
 
70
#define QT_QSETTINGS_ALWAYS_CASE_SENSITIVE_AND_FORGET_ORIGINAL_KEY_ORDER
 
71
#endif
 
72
 
 
73
// used in testing framework
 
74
#define QSETTINGS_P_H_VERSION 3
 
75
 
 
76
#ifdef QT_QSETTINGS_ALWAYS_CASE_SENSITIVE_AND_FORGET_ORIGINAL_KEY_ORDER
 
77
static const Qt::CaseSensitivity IniCaseSensitivity = Qt::CaseSensitive;
 
78
 
 
79
class QSettingsKey : public QString
 
80
{
 
81
public:
 
82
    inline QSettingsKey(const QString &key, Qt::CaseSensitivity cs, int /* position */ = -1)
 
83
        : QString(key) { Q_ASSERT(cs == Qt::CaseSensitive); Q_UNUSED(cs); }
 
84
 
 
85
    inline QString originalCaseKey() const { return *this; }
 
86
    inline int originalKeyPosition() const { return -1; }
 
87
};
 
88
#else
 
89
static const Qt::CaseSensitivity IniCaseSensitivity = Qt::CaseInsensitive;
 
90
 
 
91
class QSettingsKey : public QString
 
92
{
 
93
public:
 
94
    inline QSettingsKey(const QString &key, Qt::CaseSensitivity cs, int position = -1)
 
95
         : QString(key), theOriginalKey(key), theOriginalKeyPosition(position)
 
96
    {
 
97
        if (cs == Qt::CaseInsensitive)
 
98
            QString::operator=(toLower());
 
99
    }
 
100
 
 
101
    inline QString originalCaseKey() const { return theOriginalKey; }
 
102
    inline int originalKeyPosition() const { return theOriginalKeyPosition; }
 
103
 
 
104
private:
 
105
    QString theOriginalKey;
 
106
    int theOriginalKeyPosition;
 
107
};
 
108
#endif
 
109
 
 
110
typedef QMap<QSettingsKey, QByteArray> UnparsedSettingsMap;
 
111
typedef QMap<QSettingsKey, QVariant> ParsedSettingsMap;
 
112
 
 
113
class QSettingsGroup
 
114
{
 
115
public:
 
116
    inline QSettingsGroup()
 
117
        : num(-1), maxNum(-1) {}
 
118
    inline QSettingsGroup(const QString &s)
 
119
        : str(s), num(-1), maxNum(-1) {}
 
120
    inline QSettingsGroup(const QString &s, bool guessArraySize)
 
121
        : str(s), num(0), maxNum(guessArraySize ? 0 : -1) {}
 
122
 
 
123
    inline QString name() const { return str; }
 
124
    inline QString toString() const;
 
125
    inline bool isArray() const { return num != -1; }
 
126
    inline int arraySizeGuess() const { return maxNum; }
 
127
    inline void setArrayIndex(int i)
 
128
    { num = i + 1; if (maxNum != -1 && num > maxNum) maxNum = num; }
 
129
 
 
130
    QString str;
 
131
    int num;
 
132
    int maxNum;
 
133
};
 
134
 
 
135
inline QString QSettingsGroup::toString() const
 
136
{
 
137
    QString result;
 
138
    result = str;
 
139
    if (num > 0) {
 
140
        result += QLatin1Char('/');
 
141
        result += QString::number(num);
 
142
    }
 
143
    return result;
 
144
}
 
145
 
 
146
class Q_AUTOTEST_EXPORT QConfFile
 
147
{
 
148
public:
 
149
    ~QConfFile();
 
150
 
 
151
    ParsedSettingsMap mergedKeyMap() const;
 
152
    bool isWritable() const;
 
153
 
 
154
    static QConfFile *fromName(const QString &name, bool _userPerms);
 
155
    static void clearCache();
 
156
 
 
157
    QString name;
 
158
    QDateTime timeStamp;
 
159
    qint64 size;
 
160
    UnparsedSettingsMap unparsedIniSections;
 
161
    ParsedSettingsMap originalKeys;
 
162
    ParsedSettingsMap addedKeys;
 
163
    ParsedSettingsMap removedKeys;
 
164
    QAtomicInt ref;
 
165
    QMutex mutex;
 
166
    bool userPerms;
 
167
 
 
168
private:
 
169
#ifdef Q_DISABLE_COPY
 
170
    QConfFile(const QConfFile &);
 
171
    QConfFile &operator=(const QConfFile &);
 
172
#endif
 
173
    QConfFile(const QString &name, bool _userPerms);
 
174
 
 
175
    friend class QConfFile_createsItself; // silences compiler warning
 
176
};
 
177
 
 
178
class Q_AUTOTEST_EXPORT QSettingsPrivate
 
179
#ifndef QT_NO_QOBJECT
 
180
    : public QObjectPrivate
 
181
#endif
 
182
{
 
183
#ifdef QT_NO_QOBJECT
 
184
    QSettings *q_ptr;
 
185
#endif
 
186
    Q_DECLARE_PUBLIC(QSettings)
 
187
 
 
188
public:
 
189
    QSettingsPrivate(QSettings::Format format);
 
190
    QSettingsPrivate(QSettings::Format format, QSettings::Scope scope,
 
191
                     const QString &organization, const QString &application);
 
192
    virtual ~QSettingsPrivate();
 
193
 
 
194
    virtual void remove(const QString &key) = 0;
 
195
    virtual void set(const QString &key, const QVariant &value) = 0;
 
196
    virtual bool get(const QString &key, QVariant *value) const = 0;
 
197
 
 
198
    enum ChildSpec { AllKeys, ChildKeys, ChildGroups };
 
199
    virtual QStringList children(const QString &prefix, ChildSpec spec) const = 0;
 
200
 
 
201
    virtual void clear() = 0;
 
202
    virtual void sync() = 0;
 
203
    virtual void flush() = 0;
 
204
    virtual bool isWritable() const = 0;
 
205
    virtual QString fileName() const = 0;
 
206
 
 
207
    QString actualKey(const QString &key) const;
 
208
    void beginGroupOrArray(const QSettingsGroup &group);
 
209
    void setStatus(QSettings::Status status) const;
 
210
    void requestUpdate();
 
211
    void update();
 
212
 
 
213
    static QString normalizedKey(const QString &key);
 
214
    static QSettingsPrivate *create(QSettings::Format format, QSettings::Scope scope,
 
215
                                        const QString &organization, const QString &application);
 
216
    static QSettingsPrivate *create(const QString &fileName, QSettings::Format format);
 
217
 
 
218
    static void processChild(QString key, ChildSpec spec, QMap<QString, QString> &result);
 
219
 
 
220
    // Variant streaming functions
 
221
    static QStringList variantListToStringList(const QVariantList &l);
 
222
    static QVariant stringListToVariantList(const QStringList &l);
 
223
 
 
224
    // parser functions
 
225
    static QString variantToString(const QVariant &v);
 
226
    static QVariant stringToVariant(const QString &s);
 
227
    static void iniEscapedKey(const QString &key, QByteArray &result);
 
228
    static bool iniUnescapedKey(const QByteArray &key, int from, int to, QString &result);
 
229
    static void iniEscapedString(const QString &str, QByteArray &result, QTextCodec *codec);
 
230
    static void iniEscapedStringList(const QStringList &strs, QByteArray &result, QTextCodec *codec);
 
231
    static bool iniUnescapedStringList(const QByteArray &str, int from, int to,
 
232
                                       QString &stringResult, QStringList &stringListResult,
 
233
                                       QTextCodec *codec);
 
234
    static QStringList splitArgs(const QString &s, int idx);
 
235
 
 
236
    /*
 
237
    The numeric values of these enums define their search order. For example,
 
238
    F_User | F_Organization is searched before F_System | F_Application,
 
239
    because their values are respectively 1 and 2.
 
240
    */
 
241
    enum {
 
242
#if !defined(Q_OS_BLACKBERRY)
 
243
        F_Application = 0x0,
 
244
        F_Organization = 0x1,
 
245
        F_User = 0x0,
 
246
        F_System = 0x2,
 
247
        NumConfFiles = 4
 
248
#else
 
249
        SandboxConfFile = 0,
 
250
        NumConfFiles = 1
 
251
#endif
 
252
    };
 
253
 
 
254
    QSettings::Format format;
 
255
    QSettings::Scope scope;
 
256
    QString organizationName;
 
257
    QString applicationName;
 
258
    QTextCodec *iniCodec;
 
259
 
 
260
protected:
 
261
    QStack<QSettingsGroup> groupStack;
 
262
    QString groupPrefix;
 
263
    int spec;
 
264
    bool fallbacks;
 
265
    bool pendingChanges;
 
266
    mutable QSettings::Status status;
 
267
};
 
268
 
 
269
class QConfFileSettingsPrivate : public QSettingsPrivate
 
270
{
 
271
public:
 
272
    QConfFileSettingsPrivate(QSettings::Format format, QSettings::Scope scope,
 
273
                             const QString &organization, const QString &application);
 
274
    QConfFileSettingsPrivate(const QString &fileName, QSettings::Format format);
 
275
    ~QConfFileSettingsPrivate();
 
276
 
 
277
    void remove(const QString &key);
 
278
    void set(const QString &key, const QVariant &value);
 
279
    bool get(const QString &key, QVariant *value) const;
 
280
 
 
281
    QStringList children(const QString &prefix, ChildSpec spec) const;
 
282
 
 
283
    void clear();
 
284
    void sync();
 
285
    void flush();
 
286
    bool isWritable() const;
 
287
    QString fileName() const;
 
288
 
 
289
    static bool readIniFile(const QByteArray &data, UnparsedSettingsMap *unparsedIniSections);
 
290
    static bool readIniSection(const QSettingsKey &section, const QByteArray &data,
 
291
                               ParsedSettingsMap *settingsMap, QTextCodec *codec);
 
292
    static bool readIniLine(const QByteArray &data, int &dataPos, int &lineStart, int &lineLen,
 
293
                            int &equalsPos);
 
294
 
 
295
private:
 
296
    void initFormat();
 
297
    void initAccess();
 
298
    void syncConfFile(int confFileNo);
 
299
    bool writeIniFile(QIODevice &device, const ParsedSettingsMap &map);
 
300
#ifdef Q_OS_MAC
 
301
    bool readPlistFile(const QString &fileName, ParsedSettingsMap *map) const;
 
302
    bool writePlistFile(const QString &fileName, const ParsedSettingsMap &map) const;
 
303
#endif
 
304
    void ensureAllSectionsParsed(QConfFile *confFile) const;
 
305
    void ensureSectionParsed(QConfFile *confFile, const QSettingsKey &key) const;
 
306
 
 
307
    QScopedSharedPointer<QConfFile> confFiles[NumConfFiles];
 
308
    QSettings::ReadFunc readFunc;
 
309
    QSettings::WriteFunc writeFunc;
 
310
    QString extension;
 
311
    Qt::CaseSensitivity caseSensitivity;
 
312
    int nextPosition;
 
313
};
 
314
 
 
315
QT_END_NAMESPACE
 
316
 
 
317
#endif // QSETTINGS_P_H