|
1 /*************************************************************************** |
|
2 preferences.h |
|
3 ------------------- |
|
4 A class to access persistant preferences for an application. Utilizes XML/DOM. |
|
5 Basic format is: |
|
6 <!DOCTYPE preferences> |
|
7 <preferences version="0.1" application="MyApp" > |
|
8 <group name="Default" > |
|
9 <option key="alpha" value="true" /> |
|
10 <option key="beta" value="99" /> |
|
11 <option key="gamma" value="test" /> |
|
12 </group> |
|
13 </preferences> |
|
14 ------------------- |
|
15 begin Tue Sep 12 2000 |
|
16 author David Johnson, david@usermode.org |
|
17 ------------------- |
|
18 Copyright 2000, David Johnson |
|
19 All rights reserved. |
|
20 |
|
21 Redistribution and use in source and binary forms, with or without |
|
22 modification, are permitted provided that the following conditions are met: |
|
23 |
|
24 1. Redistributions of source code must retain the above copyright notice, this |
|
25 list of conditions and the following disclaimer. |
|
26 |
|
27 2. Redistributions in binary form must reproduce the above copyright notice, |
|
28 this list of conditions and the following disclaimer in the documentation |
|
29 and/or other materials provided with the distribution. |
|
30 |
|
31 3. Neither name of the copyright holders nor the names of its contributors may |
|
32 be used to endorse or promote products derived from this software without |
|
33 specific prior written permission. |
|
34 |
|
35 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' |
|
36 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
37 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
38 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE |
|
39 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
40 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
41 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
42 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
43 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
44 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
45 ***************************************************************************/ |
|
46 |
|
47 // version 2 |
|
48 |
|
49 #ifndef PREFERENCES_H |
|
50 #define PREFERENCES_H |
|
51 |
|
52 #include <qapplication.h> |
|
53 #include <qstring.h> |
|
54 #include <qmap.h> |
|
55 |
|
56 class QDomElement; |
|
57 |
|
58 class Preferences { |
|
59 public: |
|
60 // constructor |
|
61 Preferences(const QString& filename, const QString& format, const QString& version); |
|
62 // destructor |
|
63 virtual ~Preferences(void); |
|
64 |
|
65 // preference file information |
|
66 const QString& file(); |
|
67 const QString& format(); |
|
68 const QString& version(); |
|
69 // did file open successfully? |
|
70 bool fileState(); |
|
71 // is this a proper preferences file for format? |
|
72 bool formatState(); |
|
73 |
|
74 // group settings |
|
75 const QString& getGroup(); |
|
76 void setGroup(const QString& group); |
|
77 |
|
78 // boolean data storage |
|
79 bool getBool(const QString& key, bool def = false); |
|
80 void setBool(const QString& key, bool value); |
|
81 // integer data storage |
|
82 long getNumber(const QString& key, long def = 0); |
|
83 void setNumber(const QString& key, long value); |
|
84 // double data storage |
|
85 double getDouble(const QString& key, double def = 0.0); |
|
86 void setDouble(const QString& key, double value); |
|
87 // string data storage |
|
88 QString getString(const QString& key, const QString& def = "NULL"); |
|
89 void setString(const QString& key, const QString& value); |
|
90 |
|
91 // remove a key/value from the preferences |
|
92 void removeKey(const QString& key); |
|
93 // remove the current group from the preferences |
|
94 void removeGroup(); |
|
95 |
|
96 // flush the preferences out to file |
|
97 void flush(); |
|
98 |
|
99 protected: |
|
100 // serialization |
|
101 void readData(); |
|
102 void writeData(); |
|
103 void processGroup(QDomElement group); |
|
104 |
|
105 private: |
|
106 bool dirty_; |
|
107 QString currentgroup_; |
|
108 QString file_; |
|
109 QString format_; |
|
110 QString version_; |
|
111 QString buffer_; |
|
112 bool filestate_; |
|
113 bool formatstate_; |
|
114 |
|
115 typedef QMap<QString, QString> PrefMap; |
|
116 QMap<QString, PrefMap> groups_; |
|
117 }; |
|
118 |
|
119 ////////////////////////////////////////////////////////////////////////////// |
|
120 // Inline methods |
|
121 |
|
122 inline const QString& Preferences::file() { return file_; }; |
|
123 |
|
124 inline const QString& Preferences::format() { return format_; } |
|
125 |
|
126 inline bool Preferences::fileState() { return filestate_; } |
|
127 |
|
128 inline bool Preferences::formatState() { return formatstate_; } |
|
129 |
|
130 inline void Preferences::setGroup(const QString& group) { currentgroup_ = group; } |
|
131 |
|
132 inline const QString& Preferences::getGroup() { return currentgroup_; } |
|
133 |
|
134 #endif // PREFERENCES |