src/corelib/io/qsettings.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtCore module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    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 
       
    49 QT_BEGIN_HEADER
       
    50 
       
    51 QT_BEGIN_NAMESPACE
       
    52 QT_MODULE(Core)
       
    53 QT_END_NAMESPACE
       
    54 
       
    55 #ifndef QT_NO_SETTINGS
       
    56 
       
    57 #ifdef QT3_SUPPORT
       
    58 #include <QtCore/qstringlist.h>
       
    59 #endif
       
    60 
       
    61 #include <ctype.h>
       
    62 
       
    63 QT_BEGIN_NAMESPACE
       
    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
       
    81     QScopedPointer<QSettingsPrivate> d_ptr;
       
    82 #endif
       
    83     Q_DECLARE_PRIVATE(QSettings)
       
    84 
       
    85 public:
       
    86     enum Status {
       
    87         NoError = 0,
       
    88         AccessError,
       
    89         FormatError
       
    90     };
       
    91 
       
    92     enum Format {
       
    93         NativeFormat,
       
    94         IniFormat,
       
    95 
       
    96         InvalidFormat = 16,
       
    97         CustomFormat1,
       
    98         CustomFormat2,
       
    99         CustomFormat3,
       
   100         CustomFormat4,
       
   101         CustomFormat5,
       
   102         CustomFormat6,
       
   103         CustomFormat7,
       
   104         CustomFormat8,
       
   105         CustomFormat9,
       
   106         CustomFormat10,
       
   107         CustomFormat11,
       
   108         CustomFormat12,
       
   109         CustomFormat13,
       
   110         CustomFormat14,
       
   111         CustomFormat15,
       
   112         CustomFormat16
       
   113     };
       
   114 
       
   115     enum Scope {
       
   116         UserScope,
       
   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 
       
   190     typedef QMap<QString, QVariant> SettingsMap;
       
   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 
       
   307 QT_END_NAMESPACE
       
   308 
       
   309 #endif // QT_NO_SETTINGS
       
   310 
       
   311 QT_END_HEADER
       
   312 
       
   313 #endif // QSETTINGS_H