preferences.h

00001 /***************************************************************************
00002   preferences.h
00003   -------------------
00004   A class to access persistant preferences for an application. Utilizes XML/DOM.
00005   Basic format is:
00006     <!DOCTYPE preferences>
00007     <preferences version="0.1" application="MyApp" >
00008         <group name="Default" >
00009             <option key="alpha" value="true" />
00010             <option key="beta" value="99" />
00011             <option key="gamma" value="test" />
00012         </group>
00013     </preferences>
00014   -------------------
00015   begin         Tue Sep 12 2000
00016   author        David Johnson, david@usermode.org
00017   -------------------
00018   Copyright 2000, David Johnson
00019   All rights reserved.
00020 
00021   Redistribution and use in source and binary forms, with or without
00022   modification, are permitted provided that the following conditions are met:
00023 
00024   1. Redistributions of source code must retain the above copyright notice, this
00025   list of conditions and the following disclaimer.
00026 
00027   2. Redistributions in binary form must reproduce the above copyright notice,
00028   this list of conditions and the following disclaimer in the documentation
00029   and/or other materials provided with the distribution.
00030 
00031   3. Neither name of the copyright holders nor the names of its contributors may
00032   be used to endorse or promote products derived from this software without
00033   specific prior written permission.
00034 
00035   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00036   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00037   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00038   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
00039   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00040   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00041   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00042   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00043   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00044   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00045  ***************************************************************************/
00046 
00047 // version 2
00048 
00049 #ifndef PREFERENCES_H
00050 #define PREFERENCES_H
00051 
00052 #include <qapplication.h>
00053 #include <qstring.h>
00054 #include <qmap.h>
00055 #include "teglobal.h"
00056 
00057 class QColor;
00058 class QDomElement;
00059 
00060 class LIB_EXPORT Preferences {
00061 public:
00062     // constructor
00063     Preferences(const QString& filename, const QString& format, const QString& version);
00064     // destructor
00065     virtual ~Preferences();
00066 
00067     // preference file information
00068     const QString& file();
00069     const QString& format();
00070     const QString& version();
00071     // did file open successfully?
00072     bool fileState();
00073     // is this a proper preferences file for format?
00074     bool formatState();
00075 
00076     // group settings
00077     const QString& getGroup();
00078     void setGroup(const QString& group);
00079 
00080     // boolean data storage
00081     bool getBool(const QString& key, bool def = false);
00082     void setBool(const QString& key, bool value);
00083     // integer data storage
00084     long getNumber(const QString& key, long def = 0);
00085     void setNumber(const QString& key, long value);
00086     // double data storage
00087     double getDouble(const QString& key, double def = 0.0);
00088     void setDouble(const QString& key, double value);
00089     // string data storage
00090     QString getString(const QString& key, const QString& def = "NULL");
00091     void setString(const QString& key, const QString& value);
00092 
00093     // remove a key/value from the preferences
00094     void removeKey(const QString& key);
00095     // remove the current group from the preferences
00096     void removeGroup();
00097 
00098     // flush the preferences out to file
00099     void flush();
00100 
00101 protected:
00102     // serialization
00103     void readData();
00104     void writeData();
00105     void processGroup(QDomElement group);
00106 
00107 private:
00108     bool dirty_;
00109     QString currentgroup_;
00110     QString file_;
00111     QString format_;
00112     QString version_;
00113     QString buffer_;
00114     bool filestate_;
00115     bool formatstate_;
00116 
00117 //    template class LIB_EXPORT QMap<QString, QString>;
00118     typedef QMap<QString, QString> PrefMap;
00119 
00120 #include "templexports.h"
00121 
00122     QMap<QString, PrefMap> groups_;
00123 };
00124 
00125 
00126 
00128 // Inline methods
00129 
00130 inline const QString& Preferences::file() { return file_; };
00131 
00132 inline const QString& Preferences::format() { return format_; }
00133 
00134 inline bool Preferences::fileState() { return filestate_; }
00135 
00136 inline bool Preferences::formatState() { return formatstate_; }
00137 
00138 inline void Preferences::setGroup(const QString& group) { currentgroup_ = group; }
00139 
00140 inline const QString& Preferences::getGroup() { return currentgroup_; }
00141 
00142 #endif // PREFERENCES

Generated on Sun Jul 2 23:37:24 2006 for Trade equipment common library. by  doxygen 1.4.7