Vidalia  0.3.1
VSettings.cpp
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file VSettings.cpp
13 ** \brief Stores and retrieves settings from Vidalia's configuration file.
14 */
15 
16 #include "VSettings.h"
17 #include "Vidalia.h"
18 
19 #include <QFileInfo>
20 
21 /** The file in which all settings will read and written. */
22 #define SETTINGS_FILE (Vidalia::dataDirectory() + "/vidalia.conf")
23 
24 
25 /** Constructor */
26 VSettings::VSettings(const QString settingsGroup)
27 : QSettings(SETTINGS_FILE, QSettings::IniFormat)
28 {
29  if (!settingsGroup.isEmpty())
30  beginGroup(settingsGroup);
31 }
32 
33 /** Returns the location of Vidalia's configuration settings file. */
34 QString
36 {
37  return SETTINGS_FILE;
38 }
39 
40 /** Returns true if Vidalia's configuration settings file already exists. */
41 bool
43 {
44  QFileInfo fi(settingsFile());
45  return fi.exists();
46 }
47 
48 /** Returns the saved value associated with <b>key</b>. If no value has been
49  * set, the default value is returned.
50  * \sa setDefault
51  */
53 VSettings::value(const QString &key, const QVariant &defaultVal) const
54 {
55  return QSettings::value(key, defaultVal.isNull() ? defaultValue(key)
56  : defaultVal);
57 }
58 
59 /** Sets the value associated with <b>key</b> to <b>val</b>. */
60 void
61 VSettings::setValue(const QString &key, const QVariant &val)
62 {
63  if (val == defaultValue(key))
64  QSettings::remove(key);
65  else if (val != value(key))
66  QSettings::setValue(key, val);
67 }
68 
69 /** Sets the default setting for <b>key</b> to <b>val</b>. */
70 void
71 VSettings::setDefault(const QString &key, const QVariant &val)
72 {
73  _defaults.insert(key, val);
74 }
75 
76 /** Returns the default setting value associated with <b>key</b>. If
77  * <b>key</b> has no default value, then an empty QVariant is returned. */
79 VSettings::defaultValue(const QString &key) const
80 {
81  if (_defaults.contains(key))
82  return _defaults.value(key);
83  return QVariant();
84 }
85 
86 /** Resets all of Vidalia's settings. */
87 void
89 {
90  /* Static method, so we have to create a QSettings object. */
91  QSettings settings(SETTINGS_FILE, QSettings::IniFormat);
92  settings.clear();
93 }
94 
95 /** Returns a map of all currently saved settings at the last appyl() point. */
96 QMap<QString, QVariant>
98 {
99  QMap<QString, QVariant> settings;
100  foreach (QString key, allKeys()) {
101  settings.insert(key, value(key));
102  }
103  return settings;
104 }
105 
static QString settingsFile()
Definition: VSettings.cpp:35
stop errmsg QVariant
QHash< QString, QVariant > _defaults
Definition: VSettings.h:62
virtual void setValue(const QString &key, const QVariant &val)
Definition: VSettings.cpp:61
QMap< QString, QVariant > allSettings() const
Definition: VSettings.cpp:97
#define SETTINGS_FILE
Definition: VSettings.cpp:22
QVariant defaultValue(const QString &key) const
Definition: VSettings.cpp:79
VSettings(const QString group=QString())
Definition: VSettings.cpp:26
static bool settingsFileExists()
Definition: VSettings.cpp:42
virtual QVariant value(const QString &key, const QVariant &defaultVal=QVariant()) const
Definition: VSettings.cpp:53
void setDefault(const QString &key, const QVariant &val)
Definition: VSettings.cpp:71
static void reset()
Definition: VSettings.cpp:88