1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/as_pref.h Fri Nov 28 11:21:08 2008 +0100 1.3 @@ -0,0 +1,134 @@ 1.4 +/*************************************************************************** 1.5 + preferences.h 1.6 + ------------------- 1.7 + A class to access persistant preferences for an application. Utilizes XML/DOM. 1.8 + Basic format is: 1.9 + <!DOCTYPE preferences> 1.10 + <preferences version="0.1" application="MyApp" > 1.11 + <group name="Default" > 1.12 + <option key="alpha" value="true" /> 1.13 + <option key="beta" value="99" /> 1.14 + <option key="gamma" value="test" /> 1.15 + </group> 1.16 + </preferences> 1.17 + ------------------- 1.18 + begin Tue Sep 12 2000 1.19 + author David Johnson, david@usermode.org 1.20 + ------------------- 1.21 + Copyright 2000, David Johnson 1.22 + All rights reserved. 1.23 + 1.24 + Redistribution and use in source and binary forms, with or without 1.25 + modification, are permitted provided that the following conditions are met: 1.26 + 1.27 + 1. Redistributions of source code must retain the above copyright notice, this 1.28 + list of conditions and the following disclaimer. 1.29 + 1.30 + 2. Redistributions in binary form must reproduce the above copyright notice, 1.31 + this list of conditions and the following disclaimer in the documentation 1.32 + and/or other materials provided with the distribution. 1.33 + 1.34 + 3. Neither name of the copyright holders nor the names of its contributors may 1.35 + be used to endorse or promote products derived from this software without 1.36 + specific prior written permission. 1.37 + 1.38 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' 1.39 + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.40 + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 1.41 + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE 1.42 + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1.43 + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 1.44 + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 1.45 + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 1.46 + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.47 + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.48 + ***************************************************************************/ 1.49 + 1.50 +// version 2 1.51 + 1.52 +#ifndef PREFERENCES_H 1.53 +#define PREFERENCES_H 1.54 + 1.55 +#include <qapplication.h> 1.56 +#include <qstring.h> 1.57 +#include <qmap.h> 1.58 + 1.59 +class QDomElement; 1.60 + 1.61 +class Preferences { 1.62 +public: 1.63 + // constructor 1.64 + Preferences(const QString& filename, const QString& format, const QString& version); 1.65 + // destructor 1.66 + virtual ~Preferences(void); 1.67 + 1.68 + // preference file information 1.69 + const QString& file(); 1.70 + const QString& format(); 1.71 + const QString& version(); 1.72 + // did file open successfully? 1.73 + bool fileState(); 1.74 + // is this a proper preferences file for format? 1.75 + bool formatState(); 1.76 + 1.77 + // group settings 1.78 + const QString& getGroup(); 1.79 + void setGroup(const QString& group); 1.80 + 1.81 + // boolean data storage 1.82 + bool getBool(const QString& key, bool def = false); 1.83 + void setBool(const QString& key, bool value); 1.84 + // integer data storage 1.85 + long getNumber(const QString& key, long def = 0); 1.86 + void setNumber(const QString& key, long value); 1.87 + // double data storage 1.88 + double getDouble(const QString& key, double def = 0.0); 1.89 + void setDouble(const QString& key, double value); 1.90 + // string data storage 1.91 + QString getString(const QString& key, const QString& def = "NULL"); 1.92 + void setString(const QString& key, const QString& value); 1.93 + 1.94 + // remove a key/value from the preferences 1.95 + void removeKey(const QString& key); 1.96 + // remove the current group from the preferences 1.97 + void removeGroup(); 1.98 + 1.99 + // flush the preferences out to file 1.100 + void flush(); 1.101 + 1.102 +protected: 1.103 + // serialization 1.104 + void readData(); 1.105 + void writeData(); 1.106 + void processGroup(QDomElement group); 1.107 + 1.108 +private: 1.109 + bool dirty_; 1.110 + QString currentgroup_; 1.111 + QString file_; 1.112 + QString format_; 1.113 + QString version_; 1.114 + QString buffer_; 1.115 + bool filestate_; 1.116 + bool formatstate_; 1.117 + 1.118 + typedef QMap<QString, QString> PrefMap; 1.119 + QMap<QString, PrefMap> groups_; 1.120 +}; 1.121 + 1.122 +////////////////////////////////////////////////////////////////////////////// 1.123 +// Inline methods 1.124 + 1.125 +inline const QString& Preferences::file() { return file_; }; 1.126 + 1.127 +inline const QString& Preferences::format() { return format_; } 1.128 + 1.129 +inline bool Preferences::fileState() { return filestate_; } 1.130 + 1.131 +inline bool Preferences::formatState() { return formatstate_; } 1.132 + 1.133 +inline void Preferences::setGroup(const QString& group) { currentgroup_ = group; } 1.134 + 1.135 +inline const QString& Preferences::getGroup() { return currentgroup_; } 1.136 + 1.137 +#endif // PREFERENCES