Thu, 06 Aug 2009 13:21:30 +0200
Remove seemingly declarations unnecessary according to Qt 4.5.2 headers.
michael@1 | 1 | /*************************************************************************** |
michael@1 | 2 | preferences.cc |
michael@1 | 3 | ------------------- |
michael@1 | 4 | A class to access persistant preferences for an application. Utilizes XML/DOM. |
michael@1 | 5 | Basic format is: |
michael@1 | 6 | <!DOCTYPE preferences> |
michael@1 | 7 | <preferences version="0.1" application="MyApp" > |
michael@1 | 8 | <group name="Default" > |
michael@1 | 9 | <option key="alpha" value="true" /> |
michael@1 | 10 | <option key="beta" value="99" /> |
michael@1 | 11 | <option key="gamma" value="test" /> |
michael@1 | 12 | </group> |
michael@1 | 13 | </preferences> |
michael@1 | 14 | ------------------- |
michael@1 | 15 | begin Tue Sep 12 2000 |
michael@1 | 16 | author David Johnson, david@usermode.org |
michael@1 | 17 | ------------------- |
michael@1 | 18 | Copyright 2000, David Johnson |
michael@1 | 19 | Please see the header file for copyright and license information |
michael@1 | 20 | ***************************************************************************/ |
michael@1 | 21 | |
michael@1 | 22 | // version 2 |
michael@1 | 23 | |
michael@1 | 24 | // TODO: fix up to account for worst case scenarios: |
michael@1 | 25 | // keys without values in file, and |
michael@1 | 26 | // checking for a key that doesn't exist puts it into the map |
michael@1 | 27 | // then it gets written out if dirty, possibly corrupting the file |
michael@1 | 28 | |
michael@1 | 29 | // TODO: Fix up error reporting |
michael@1 | 30 | |
michael@3 | 31 | #include <Qt/qdom.h> |
michael@1 | 32 | #include <qfile.h> |
michael@1 | 33 | #include <qtextstream.h> |
michael@1 | 34 | |
michael@1 | 35 | #include "as_pref.h" |
michael@1 | 36 | |
michael@1 | 37 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 38 | // Construction // |
michael@1 | 39 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 40 | |
michael@1 | 41 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 42 | // Preferences() |
michael@1 | 43 | // ------------- |
michael@1 | 44 | // Constructor. Takes the preferences file name as an argument. |
michael@1 | 45 | |
michael@1 | 46 | Preferences::Preferences(const QString& filename, |
michael@1 | 47 | const QString& format, |
michael@1 | 48 | const QString& version) |
michael@1 | 49 | : dirty_(false), |
michael@1 | 50 | currentgroup_(), |
michael@1 | 51 | file_(filename), |
michael@1 | 52 | format_(format), |
michael@1 | 53 | version_(version), |
michael@1 | 54 | filestate_(false), |
michael@1 | 55 | formatstate_(false), |
michael@1 | 56 | groups_() |
michael@1 | 57 | { |
michael@1 | 58 | readData(); |
michael@1 | 59 | dirty_ = false; |
michael@1 | 60 | currentgroup_ = "Default"; |
michael@1 | 61 | } |
michael@1 | 62 | |
michael@1 | 63 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 64 | // ~Preferences() |
michael@1 | 65 | // --------------- |
michael@1 | 66 | // Destructor |
michael@1 | 67 | |
michael@1 | 68 | Preferences::~Preferences() |
michael@1 | 69 | { |
michael@1 | 70 | if (dirty_) writeData(); |
michael@1 | 71 | } |
michael@1 | 72 | |
michael@1 | 73 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 74 | // Settings |
michael@1 | 75 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 76 | |
michael@1 | 77 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 78 | // getBoolean() |
michael@1 | 79 | // ------------ |
michael@1 | 80 | // Get a boolean value |
michael@1 | 81 | |
michael@1 | 82 | bool Preferences::getBool(const QString& key, bool def) |
michael@1 | 83 | { |
michael@1 | 84 | buffer_ = getString(key, def ? "true" : "false"); |
michael@1 | 85 | if (buffer_.isEmpty()) return def; |
michael@1 | 86 | if (buffer_.contains("true")) |
michael@1 | 87 | return true; |
michael@1 | 88 | else |
michael@1 | 89 | return false; |
michael@1 | 90 | } |
michael@1 | 91 | |
michael@1 | 92 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 93 | // setBoolean() |
michael@1 | 94 | // ------------ |
michael@1 | 95 | // Set a boolean value |
michael@1 | 96 | |
michael@1 | 97 | void Preferences::setBool(const QString& key, bool value) |
michael@1 | 98 | { |
michael@1 | 99 | groups_[currentgroup_][key] = value ? "true" : "false"; |
michael@1 | 100 | dirty_ = true; |
michael@1 | 101 | } |
michael@1 | 102 | |
michael@1 | 103 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 104 | // getNumber() |
michael@1 | 105 | // ----------- |
michael@1 | 106 | // Get a number value |
michael@1 | 107 | |
michael@1 | 108 | // TODO: this might be a place for templates |
michael@1 | 109 | |
michael@1 | 110 | long Preferences::getNumber(const QString& key, long def) |
michael@1 | 111 | { |
michael@1 | 112 | buffer_ = getString(key, QString::number(def)); |
michael@1 | 113 | if (buffer_.isEmpty()) return def; |
michael@1 | 114 | |
michael@1 | 115 | bool ok; |
michael@1 | 116 | long num = buffer_.toLong(&ok); |
michael@1 | 117 | if (ok) return num; |
michael@1 | 118 | else return def; |
michael@1 | 119 | } |
michael@1 | 120 | |
michael@1 | 121 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 122 | // setNumber() |
michael@1 | 123 | // ----------- |
michael@1 | 124 | // Set a number value |
michael@1 | 125 | |
michael@1 | 126 | void Preferences::setNumber(const QString& key, long value) |
michael@1 | 127 | { |
michael@1 | 128 | buffer_.setNum(value); |
michael@1 | 129 | |
michael@1 | 130 | groups_[currentgroup_][key] = buffer_; |
michael@1 | 131 | dirty_ = true; |
michael@1 | 132 | } |
michael@1 | 133 | |
michael@1 | 134 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 135 | // getDouble() |
michael@1 | 136 | // ----------- |
michael@1 | 137 | // Get a double value |
michael@1 | 138 | |
michael@1 | 139 | double Preferences::getDouble(const QString& key, double def) |
michael@1 | 140 | { |
michael@1 | 141 | buffer_ = getString(key, QString::number(def)); |
michael@1 | 142 | if (buffer_.isEmpty()) return def; |
michael@1 | 143 | |
michael@1 | 144 | bool ok; |
michael@1 | 145 | double num = buffer_.toDouble(&ok); |
michael@1 | 146 | if (ok) return num; |
michael@1 | 147 | else return def; |
michael@1 | 148 | } |
michael@1 | 149 | |
michael@1 | 150 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 151 | // setDouble() |
michael@1 | 152 | // ----------- |
michael@1 | 153 | // Set a double value |
michael@1 | 154 | |
michael@1 | 155 | void Preferences::setDouble(const QString& key, double value) |
michael@1 | 156 | { |
michael@1 | 157 | buffer_.setNum(value); |
michael@1 | 158 | |
michael@1 | 159 | groups_[currentgroup_][key] = buffer_; |
michael@1 | 160 | dirty_ = true; |
michael@1 | 161 | } |
michael@1 | 162 | |
michael@1 | 163 | |
michael@1 | 164 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 165 | // getString() |
michael@1 | 166 | // ----------- |
michael@1 | 167 | // Get a string value |
michael@1 | 168 | |
michael@1 | 169 | QString Preferences::getString(const QString& key, const QString& def) |
michael@1 | 170 | { |
michael@1 | 171 | buffer_ = ""; |
michael@1 | 172 | if (groups_.contains(currentgroup_)) { |
michael@1 | 173 | if (groups_[currentgroup_].contains(key)) { |
michael@1 | 174 | buffer_ = groups_[currentgroup_][key]; |
michael@1 | 175 | } |
michael@1 | 176 | } |
michael@1 | 177 | if (buffer_.isEmpty()) return def; |
michael@1 | 178 | return buffer_; |
michael@1 | 179 | } |
michael@1 | 180 | |
michael@1 | 181 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 182 | // setString() |
michael@1 | 183 | // ----------- |
michael@1 | 184 | // Set a string value |
michael@1 | 185 | |
michael@1 | 186 | void Preferences::setString(const QString& key, const QString& value) |
michael@1 | 187 | { |
michael@1 | 188 | groups_[currentgroup_][key] = value; |
michael@1 | 189 | dirty_ = true; |
michael@1 | 190 | } |
michael@1 | 191 | |
michael@1 | 192 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 193 | // removeValue() |
michael@1 | 194 | // ------------- |
michael@1 | 195 | // Remove a value from the preferences |
michael@1 | 196 | |
michael@1 | 197 | void Preferences::removeKey(const QString& key) |
michael@1 | 198 | { |
michael@1 | 199 | groups_[currentgroup_].remove(key); |
michael@1 | 200 | } |
michael@1 | 201 | |
michael@1 | 202 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 203 | // removeGroup() |
michael@1 | 204 | // ------------- |
michael@1 | 205 | // Remove a group from the preferences, and all its options |
michael@1 | 206 | |
michael@1 | 207 | void Preferences::removeGroup() |
michael@1 | 208 | { |
michael@1 | 209 | groups_.remove(currentgroup_); |
michael@1 | 210 | } |
michael@1 | 211 | |
michael@1 | 212 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 213 | // flush() |
michael@1 | 214 | // ------- |
michael@1 | 215 | // Flush the preferences to file |
michael@1 | 216 | |
michael@1 | 217 | void Preferences::flush() |
michael@1 | 218 | { |
michael@1 | 219 | if (dirty_) { |
michael@1 | 220 | writeData(); |
michael@1 | 221 | dirty_ = false; |
michael@1 | 222 | } |
michael@1 | 223 | } |
michael@1 | 224 | |
michael@1 | 225 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 226 | // Serialization // |
michael@1 | 227 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 228 | |
michael@1 | 229 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 230 | // readData() |
michael@1 | 231 | // ---------- |
michael@1 | 232 | // Read data from the file |
michael@1 | 233 | |
michael@1 | 234 | void Preferences::readData() |
michael@1 | 235 | { |
michael@1 | 236 | // open file |
michael@1 | 237 | QFile* datafile = new QFile(file_); |
michael@3 | 238 | if (!datafile->open(QIODevice::ReadOnly)) { |
michael@1 | 239 | // error opening file |
michael@1 | 240 | qWarning("Error: cannot open preferences file " + file_); |
michael@1 | 241 | datafile->close(); |
michael@1 | 242 | delete (datafile); |
michael@1 | 243 | filestate_ = false; |
michael@1 | 244 | return; |
michael@1 | 245 | } |
michael@1 | 246 | filestate_ = true; |
michael@1 | 247 | |
michael@1 | 248 | // open dom document |
michael@1 | 249 | QDomDocument doc("preferences"); |
michael@1 | 250 | if (!doc.setContent(datafile)) { |
michael@1 | 251 | qWarning("Error: " + file_ + " is not a proper preferences file"); |
michael@1 | 252 | datafile->close(); |
michael@1 | 253 | delete (datafile); |
michael@1 | 254 | formatstate_ = false; |
michael@1 | 255 | return; |
michael@1 | 256 | } |
michael@1 | 257 | datafile->close(); |
michael@1 | 258 | delete (datafile); |
michael@1 | 259 | |
michael@1 | 260 | // check the doc type and stuff |
michael@1 | 261 | if (doc.doctype().name() != "preferences") { |
michael@1 | 262 | // wrong file type |
michael@1 | 263 | qWarning("Error: " +file_ + " is not a valid preferences file"); |
michael@1 | 264 | formatstate_ = false; |
michael@1 | 265 | return; |
michael@1 | 266 | } |
michael@1 | 267 | QDomElement root = doc.documentElement(); |
michael@1 | 268 | if (root.attribute("application") != format_) { |
michael@1 | 269 | // right file type, wrong application |
michael@1 | 270 | qWarning("Error: " + file_ + " is not a preferences file for " + format_); |
michael@1 | 271 | formatstate_ = false; |
michael@1 | 272 | return; |
michael@1 | 273 | } |
michael@1 | 274 | // We don't care about application version... |
michael@1 | 275 | |
michael@1 | 276 | // get list of groups |
michael@1 | 277 | QDomNodeList nodes = root.elementsByTagName("group"); |
michael@1 | 278 | |
michael@1 | 279 | // iterate through the groups |
michael@1 | 280 | QDomNodeList options; |
michael@1 | 281 | for (unsigned n=0; n<nodes.count(); ++n) { |
michael@1 | 282 | if (nodes.item(n).isElement()) { |
michael@1 | 283 | processGroup(nodes.item(n).toElement()); |
michael@1 | 284 | } |
michael@1 | 285 | } |
michael@1 | 286 | formatstate_ = true; |
michael@1 | 287 | } |
michael@1 | 288 | |
michael@1 | 289 | void Preferences::processGroup(QDomElement group) |
michael@1 | 290 | { |
michael@1 | 291 | QDomElement elem; |
michael@1 | 292 | QDomNodeList options; |
michael@1 | 293 | currentgroup_ = group.attribute("name", "Default"); |
michael@1 | 294 | options = group.elementsByTagName("option"); |
michael@1 | 295 | for (unsigned n=0; n<options.count(); ++n) { |
michael@1 | 296 | if (options.item(n).isElement()) { |
michael@1 | 297 | elem = options.item(n).toElement(); |
michael@1 | 298 | setString(elem.attribute("key"), elem.attribute("value")); |
michael@1 | 299 | } |
michael@1 | 300 | } |
michael@1 | 301 | } |
michael@1 | 302 | |
michael@1 | 303 | ////////////////////////////////////////////////////////////////////////////// |
michael@1 | 304 | // writeData() |
michael@1 | 305 | // ----------- |
michael@1 | 306 | // Write data out to the file |
michael@1 | 307 | |
michael@1 | 308 | void Preferences::writeData() |
michael@1 | 309 | { |
michael@1 | 310 | QDomDocument doc("preferences"); |
michael@1 | 311 | |
michael@1 | 312 | // create the root element |
michael@1 | 313 | QDomElement root = doc.createElement(doc.doctype().name()); |
michael@1 | 314 | root.setAttribute("version", version_); |
michael@1 | 315 | root.setAttribute("application", format_); |
michael@1 | 316 | |
michael@1 | 317 | // now do our options group by group |
michael@1 | 318 | QMap<QString, PrefMap>::Iterator git; |
michael@1 | 319 | PrefMap::Iterator pit; |
michael@1 | 320 | QDomElement group, option; |
michael@1 | 321 | for (git = groups_.begin(); git != groups_.end(); ++git) { |
michael@1 | 322 | // create a group element |
michael@1 | 323 | group = doc.createElement("group"); |
michael@1 | 324 | group.setAttribute("name", git.key()); |
michael@1 | 325 | // add in options |
michael@1 | 326 | for (pit = (*git).begin(); pit != (*git).end(); ++pit) { |
michael@1 | 327 | option = doc.createElement("option"); |
michael@1 | 328 | option.setAttribute("key", pit.key()); |
michael@1 | 329 | option.setAttribute("value", pit.data()); |
michael@1 | 330 | group.appendChild(option); |
michael@1 | 331 | } |
michael@1 | 332 | root.appendChild(group); |
michael@1 | 333 | } |
michael@1 | 334 | doc.appendChild(root); |
michael@1 | 335 | |
michael@1 | 336 | // open file |
michael@1 | 337 | QFile* datafile = new QFile(file_); |
michael@3 | 338 | if (!datafile->open(QIODevice::WriteOnly)) { |
michael@1 | 339 | // error opening file |
michael@1 | 340 | qWarning("Error: Cannot open preferences file " + file_); |
michael@1 | 341 | datafile->close(); |
michael@1 | 342 | delete (datafile); |
michael@1 | 343 | filestate_ = false; |
michael@1 | 344 | return; |
michael@1 | 345 | } |
michael@1 | 346 | filestate_ = true; |
michael@1 | 347 | |
michael@1 | 348 | // write it out |
michael@1 | 349 | QTextStream textstream(datafile); |
michael@1 | 350 | doc.save(textstream, 0); |
michael@1 | 351 | datafile->close(); |
michael@1 | 352 | delete (datafile); |
michael@1 | 353 | formatstate_ = true; |
michael@1 | 354 | } |