michael@1: /*************************************************************************** michael@1: preferences.h michael@1: ------------------- michael@1: A class to access persistant preferences for an application. Utilizes XML/DOM. michael@1: Basic format is: michael@1: michael@1: michael@1: michael@1: michael@1: michael@1: ------------------- michael@1: begin Tue Sep 12 2000 michael@1: author David Johnson, david@usermode.org michael@1: ------------------- michael@1: Copyright 2000, David Johnson michael@1: All rights reserved. michael@1: michael@1: Redistribution and use in source and binary forms, with or without michael@1: modification, are permitted provided that the following conditions are met: michael@1: michael@1: 1. Redistributions of source code must retain the above copyright notice, this michael@1: list of conditions and the following disclaimer. michael@1: michael@1: 2. Redistributions in binary form must reproduce the above copyright notice, michael@1: this list of conditions and the following disclaimer in the documentation michael@1: and/or other materials provided with the distribution. michael@1: michael@1: 3. Neither name of the copyright holders nor the names of its contributors may michael@1: be used to endorse or promote products derived from this software without michael@1: specific prior written permission. michael@1: michael@1: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' michael@1: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@1: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE michael@1: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE michael@1: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL michael@1: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR michael@1: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER michael@1: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, michael@1: OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@1: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@1: ***************************************************************************/ michael@1: michael@1: // version 2 michael@1: michael@1: #ifndef PREFERENCES_H michael@1: #define PREFERENCES_H michael@1: michael@1: #include michael@1: #include michael@1: #include michael@1: michael@1: class QDomElement; michael@1: michael@1: class Preferences { michael@1: public: michael@1: // constructor michael@1: Preferences(const QString& filename, const QString& format, const QString& version); michael@1: // destructor michael@1: virtual ~Preferences(void); michael@1: michael@1: // preference file information michael@1: const QString& file(); michael@1: const QString& format(); michael@1: const QString& version(); michael@1: // did file open successfully? michael@1: bool fileState(); michael@1: // is this a proper preferences file for format? michael@1: bool formatState(); michael@1: michael@1: // group settings michael@1: const QString& getGroup(); michael@1: void setGroup(const QString& group); michael@1: michael@1: // boolean data storage michael@1: bool getBool(const QString& key, bool def = false); michael@1: void setBool(const QString& key, bool value); michael@1: // integer data storage michael@1: long getNumber(const QString& key, long def = 0); michael@1: void setNumber(const QString& key, long value); michael@1: // double data storage michael@1: double getDouble(const QString& key, double def = 0.0); michael@1: void setDouble(const QString& key, double value); michael@1: // string data storage michael@1: QString getString(const QString& key, const QString& def = "NULL"); michael@1: void setString(const QString& key, const QString& value); michael@1: michael@1: // remove a key/value from the preferences michael@1: void removeKey(const QString& key); michael@1: // remove the current group from the preferences michael@1: void removeGroup(); michael@1: michael@1: // flush the preferences out to file michael@1: void flush(); michael@1: michael@1: protected: michael@1: // serialization michael@1: void readData(); michael@1: void writeData(); michael@1: void processGroup(QDomElement group); michael@1: michael@1: private: michael@1: bool dirty_; michael@1: QString currentgroup_; michael@1: QString file_; michael@1: QString format_; michael@1: QString version_; michael@1: QString buffer_; michael@1: bool filestate_; michael@1: bool formatstate_; michael@1: michael@1: typedef QMap PrefMap; michael@1: QMap groups_; michael@1: }; michael@1: michael@1: ////////////////////////////////////////////////////////////////////////////// michael@1: // Inline methods michael@1: michael@1: inline const QString& Preferences::file() { return file_; }; michael@1: michael@1: inline const QString& Preferences::format() { return format_; } michael@1: michael@1: inline bool Preferences::fileState() { return filestate_; } michael@1: michael@1: inline bool Preferences::formatState() { return formatstate_; } michael@1: michael@1: inline void Preferences::setGroup(const QString& group) { currentgroup_ = group; } michael@1: michael@1: inline const QString& Preferences::getGroup() { return currentgroup_; } michael@1: michael@1: #endif // PREFERENCES