qsettings.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtCore module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QSETTINGS_H
00043 #define QSETTINGS_H
00044 
00045 #include <QtCore/qobject.h>
00046 #include <QtCore/qvariant.h>
00047 #include <QtCore/qstring.h>
00048 
00049 QT_BEGIN_HEADER
00050 
00051 QT_BEGIN_NAMESPACE
00052 QT_MODULE(Core)
00053 QT_END_NAMESPACE
00054 
00055 #ifndef QT_NO_SETTINGS
00056 
00057 #ifdef QT3_SUPPORT
00058 #include <QtCore/qstringlist.h>
00059 #endif
00060 
00061 #include <ctype.h>
00062 
00063 QT_BEGIN_NAMESPACE
00064 
00065 #ifdef Status // we seem to pick up a macro Status --> int somewhere
00066 #undef Status
00067 #endif
00068 
00069 class QIODevice;
00070 class QSettingsPrivate;
00071 
00072 #ifndef QT_NO_QOBJECT
00073 class Q_CORE_EXPORT QSettings : public QObject
00074 #else
00075 class Q_CORE_EXPORT QSettings
00076 #endif
00077 {
00078 #ifndef QT_NO_QOBJECT
00079     Q_OBJECT
00080 #else
00081     QScopedPointer<QSettingsPrivate> d_ptr;
00082 #endif
00083     Q_DECLARE_PRIVATE(QSettings)
00084 
00085 public:
00086     enum Status {
00087         NoError = 0,
00088         AccessError,
00089         FormatError
00090     };
00091 
00092     enum Format {
00093         NativeFormat,
00094         IniFormat,
00095 
00096         InvalidFormat = 16,
00097         CustomFormat1,
00098         CustomFormat2,
00099         CustomFormat3,
00100         CustomFormat4,
00101         CustomFormat5,
00102         CustomFormat6,
00103         CustomFormat7,
00104         CustomFormat8,
00105         CustomFormat9,
00106         CustomFormat10,
00107         CustomFormat11,
00108         CustomFormat12,
00109         CustomFormat13,
00110         CustomFormat14,
00111         CustomFormat15,
00112         CustomFormat16
00113     };
00114 
00115     enum Scope {
00116         UserScope,
00117         SystemScope
00118 #ifdef QT3_SUPPORT
00119         ,
00120         User = UserScope,
00121         Global = SystemScope
00122 #endif
00123     };
00124 
00125 #ifndef QT_NO_QOBJECT
00126     explicit QSettings(const QString &organization,
00127                        const QString &application = QString(), QObject *parent = 0);
00128     QSettings(Scope scope, const QString &organization,
00129               const QString &application = QString(), QObject *parent = 0);
00130     QSettings(Format format, Scope scope, const QString &organization,
00131           const QString &application = QString(), QObject *parent = 0);
00132     QSettings(const QString &fileName, Format format, QObject *parent = 0);
00133     explicit QSettings(QObject *parent = 0);
00134 #else
00135     explicit QSettings(const QString &organization,
00136                        const QString &application = QString());
00137     QSettings(Scope scope, const QString &organization,
00138               const QString &application = QString());
00139     QSettings(Format format, Scope scope, const QString &organization,
00140               const QString &application = QString());
00141     QSettings(const QString &fileName, Format format);
00142 #endif
00143     ~QSettings();
00144 
00145     void clear();
00146     void sync();
00147     Status status() const;
00148 
00149     void beginGroup(const QString &prefix);
00150     void endGroup();
00151     QString group() const;
00152 
00153     int beginReadArray(const QString &prefix);
00154     void beginWriteArray(const QString &prefix, int size = -1);
00155     void endArray();
00156     void setArrayIndex(int i);
00157 
00158     QStringList allKeys() const;
00159     QStringList childKeys() const;
00160     QStringList childGroups() const;
00161     bool isWritable() const;
00162 
00163     void setValue(const QString &key, const QVariant &value);
00164     QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const;
00165 
00166     void remove(const QString &key);
00167     bool contains(const QString &key) const;
00168 
00169     void setFallbacksEnabled(bool b);
00170     bool fallbacksEnabled() const;
00171 
00172     QString fileName() const;
00173     Format format() const;
00174     Scope scope() const;
00175     QString organizationName() const;
00176     QString applicationName() const;
00177 
00178 #ifndef QT_NO_TEXTCODEC
00179     void setIniCodec(QTextCodec *codec);
00180     void setIniCodec(const char *codecName);
00181     QTextCodec *iniCodec() const;
00182 #endif
00183 
00184     static void setDefaultFormat(Format format);
00185     static Format defaultFormat();
00186     static void setSystemIniPath(const QString &dir); // ### remove in 5.0 (use setPath() instead)
00187     static void setUserIniPath(const QString &dir);   // ### remove in 5.0 (use setPath() instead)
00188     static void setPath(Format format, Scope scope, const QString &path);
00189 
00190     typedef QMap<QString, QVariant> SettingsMap;
00191     typedef bool (*ReadFunc)(QIODevice &device, SettingsMap &map);
00192     typedef bool (*WriteFunc)(QIODevice &device, const SettingsMap &map);
00193 
00194     static Format registerFormat(const QString &extension, ReadFunc readFunc, WriteFunc writeFunc,
00195                                  Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive);
00196 
00197 #ifdef QT3_SUPPORT
00198     inline QT3_SUPPORT bool writeEntry(const QString &key, bool value)
00199     { setValue(key, value); return isWritable(); }
00200     inline QT3_SUPPORT bool writeEntry(const QString &key, double value)
00201     { setValue(key, value); return isWritable(); }
00202     inline QT3_SUPPORT bool writeEntry(const QString &key, int value)
00203     { setValue(key, value); return isWritable(); }
00204     inline QT3_SUPPORT bool writeEntry(const QString &key, const char *value)
00205     { setValue(key, QString::fromAscii(value)); return isWritable(); }
00206     inline QT3_SUPPORT bool writeEntry(const QString &key, const QString &value)
00207     { setValue(key, value); return isWritable(); }
00208     inline QT3_SUPPORT bool writeEntry(const QString &key, const QStringList &value)
00209     { setValue(key, value); return isWritable(); }
00210     inline QT3_SUPPORT bool writeEntry(const QString &key, const QStringList &value, QChar separator)
00211     { setValue(key, value.join(QString(separator))); return isWritable(); }
00212     inline QT3_SUPPORT QStringList readListEntry(const QString &key, bool *ok = 0)
00213     {
00214         if (ok)
00215             *ok = contains(key);
00216         return value(key).toStringList();
00217     }
00218     inline QT3_SUPPORT QStringList readListEntry(const QString &key, QChar separator, bool *ok = 0)
00219     {
00220         if (ok)
00221             *ok = contains(key);
00222         QString str = value(key).toString();
00223         if (str.isEmpty())
00224             return QStringList();
00225         return str.split(separator);
00226     }
00227     inline QT3_SUPPORT QString readEntry(const QString &key, const QString &defaultValue = QString(),
00228                                          bool *ok = 0)
00229     {
00230         if (ok)
00231             *ok = contains(key);
00232         return value(key, defaultValue).toString();
00233     }
00234     inline QT3_SUPPORT int readNumEntry(const QString &key, int defaultValue = 0, bool *ok = 0)
00235     {
00236         if (ok)
00237             *ok = contains(key);
00238         return value(key, defaultValue).toInt();
00239     }
00240     inline QT3_SUPPORT double readDoubleEntry(const QString &key, double defaultValue = 0,
00241                                               bool *ok = 0)
00242     {
00243         if (ok)
00244             *ok = contains(key);
00245         return value(key, defaultValue).toDouble();
00246     }
00247     inline QT3_SUPPORT bool readBoolEntry(const QString &key, bool defaultValue = false,
00248                                           bool *ok = 0)
00249     {
00250         if (ok)
00251             *ok = contains(key);
00252         return value(key, defaultValue).toBool();
00253     }
00254     inline QT3_SUPPORT bool removeEntry(const QString &key)
00255     { remove(key); return true; }
00256 
00257     enum System { Unix, Windows, Mac };
00258     inline QT3_SUPPORT void insertSearchPath(System, const QString &) {}
00259     inline QT3_SUPPORT void removeSearchPath(System, const QString &) {}
00260 
00261     inline QT3_SUPPORT void setPath(const QString &organization, const QString &application,
00262                                     Scope scope = Global)
00263     {
00264         setPath_helper(scope == Global ? QSettings::SystemScope : QSettings::UserScope,
00265                        organization, application);
00266     }
00267     inline QT3_SUPPORT void resetGroup()
00268     {
00269         while (!group().isEmpty())
00270             endGroup();
00271     }
00272     inline QT3_SUPPORT QStringList entryList(const QString &key) const
00273     {
00274         QSettings *that = const_cast<QSettings *>(this);
00275         QStringList result;
00276 
00277         that->beginGroup(key);
00278         result = that->childKeys();
00279         that->endGroup();
00280         return result;
00281     }
00282     inline QT3_SUPPORT QStringList subkeyList(const QString &key) const
00283     {
00284         QSettings *that = const_cast<QSettings *>(this);
00285         QStringList result;
00286 
00287         that->beginGroup(key);
00288         result = that->childGroups();
00289         that->endGroup();
00290         return result;
00291     }
00292 #endif
00293 
00294 protected:
00295 #ifndef QT_NO_QOBJECT
00296     bool event(QEvent *event);
00297 #endif
00298 
00299 private:
00300 #ifdef QT3_SUPPORT
00301     void setPath_helper(Scope scope, const QString &organization, const QString &application);
00302 #endif
00303 
00304     Q_DISABLE_COPY(QSettings)
00305 };
00306 
00307 QT_END_NAMESPACE
00308 
00309 #endif // QT_NO_SETTINGS
00310 
00311 QT_END_HEADER
00312 
00313 #endif // QSETTINGS_H