Vidalia  0.3.1
AbstractTorSettings.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 AbstractTorSettings.cpp
13 ** \brief Manages settings that need to be SETCONF'ed to Tor
14 */
15 
16 #include "AbstractTorSettings.h"
17 
18 /** Setting that gets set to <i>true</i> if any settings in the current
19  * settings group have been changed since the last time apply() was called. */
20 #define SETTING_CHANGED "Changed"
21 
22 
23 /** Constructor. All settings will be under the heading <b>group</b> and
24  * <b>torControl</b> will be used to <i>getconf</i> values from Tor. */
26  TorControl *torControl)
27 : VSettings(group)
28 {
31 
33 }
34 
35 /** Reverts all settings to their values at the last time apply() was
36  * called. */
37 void
39 {
40  remove("");
41  foreach (QString key, _backupSettings.keys()) {
42  setValue(key, _backupSettings.value(key));
43  }
44 }
45 
46 /** Returns true if any settings have changed since the last time apply()
47  * was called. */
48 bool
50 {
51  return localValue(SETTING_CHANGED).toBool();
52 }
53 
54 /** Sets a value indicating that the server settings have changed since
55  * apply() was last called. */
56 void
58 {
60  if (!changed)
62 }
63 
64 /** Returns true if the given QVariant contains an empty value, depending on
65  * the data type. For example, 0 is considered an empty integer and "" is
66  * an empty string. */
67 bool
69 {
70  switch (value.type()) {
71  case QVariant::String:
72  return (value.toString().isEmpty());
73  case QVariant::StringList:
74  return (value.toStringList().isEmpty());
75  case QVariant::UInt:
76  case QVariant::Int:
77  return (value.toUInt() == 0);
78  case QVariant::Invalid:
79  return true;
80  default: break;
81  }
82  return false;
83 }
84 
85 /** Returns the value associated with <b>key</b> saved in the local
86  * configuration file. */
88 AbstractTorSettings::localValue(const QString &key) const
89 {
90  return VSettings::value(key);
91 }
92 
93 /** Returns the value associated with <b>key</b> by querying TOr via
94  * <i>getconf key</i>. */
96 AbstractTorSettings::torValue(const QString &key) const
97 {
98  QVariant defaultVal;
99  QVariant confValue;
100 
101  defaultVal = defaultValue(key);
102  if (_torControl) {
103  confValue = _torControl->getConf(key);
104  confValue.convert(defaultVal.type());
105  }
106  return (isEmptyValue(confValue) ? localValue(key) : confValue);
107 }
108 
109 /** If Vidalia is connected to Tor, this returns the value associated with
110  * <b>key</b> by calling torValue(). Otherwise, this calls localValue() to get
111  * the locally saved value associated with <b>key</b>. */
112 QVariant
113 AbstractTorSettings::value(const QString &key) const
114 {
116  return torValue(key);
117  return localValue(key);
118 }
119 
120 /** Saves the value <b>val</b> for the setting <b>key</b> to the local
121  * settings file. */
122 void
123 AbstractTorSettings::setValue(const QString &key, const QVariant &value)
124 {
125  if (value != localValue(key)) {
126  setChanged(true);
127  VSettings::setValue(key, value);
128  }
129 }
130