Trade equipment common library.  1.0.0
preferences.h
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 #include "teglobal.h"
56 
57 class QColor;
58 class QDomElement;
59 
60 class LIB_EXPORT Preferences {
61 public:
62  // constructor
63  Preferences(const QString& filename, const QString& format, const QString& version);
64  // destructor
65  virtual ~Preferences();
66 
67  // preference file information
68  const QString& file();
69  const QString& format();
70  const QString& version();
71  // did file open successfully?
72  bool fileState();
73  // is this a proper preferences file for format?
74  bool formatState();
75 
76  // group settings
77  const QString& getGroup();
78  void setGroup(const QString& group);
79 
80  // boolean data storage
81  bool getBool(const QString& key, bool def = false);
82  void setBool(const QString& key, bool value);
83  // integer data storage
84  long getNumber(const QString& key, long def = 0);
85  void setNumber(const QString& key, long value);
86  // double data storage
87  double getDouble(const QString& key, double def = 0.0);
88  void setDouble(const QString& key, double value);
89  // string data storage
90  QString getString(const QString& key, const QString& def = "NULL");
91  void setString(const QString& key, const QString& value);
92 
93  // remove a key/value from the preferences
94  void removeKey(const QString& key);
95  // remove the current group from the preferences
96  void removeGroup();
97 
98  // flush the preferences out to file
99  void flush();
100 
101 protected:
102  // serialization
103  void readData();
104  void writeData();
105  void processGroup(QDomElement group);
106 
107 private:
108  bool dirty_;
109  QString currentgroup_;
110  QString file_;
111  QString format_;
112  QString version_;
113  QString buffer_;
114  bool filestate_;
115  bool formatstate_;
116 
117 // template class LIB_EXPORT QMap<QString, QString>;
118  typedef QMap<QString, QString> PrefMap;
119 
120 #include "templexports.h"
121 
122  QMap<QString, PrefMap> groups_;
123 };
124 
125 
126 
128 // Inline methods
129 
130 inline const QString& Preferences::file() { return file_; };
131 
132 inline const QString& Preferences::format() { return format_; }
133 
134 inline bool Preferences::fileState() { return filestate_; }
135 
136 inline bool Preferences::formatState() { return formatstate_; }
137 
138 inline void Preferences::setGroup(const QString& group) { currentgroup_ = group; }
139 
140 inline const QString& Preferences::getGroup() { return currentgroup_; }
141 
142 #endif // PREFERENCES
Definition: preferences.h:60