Vidalia  0.2.15
AbstractTorSettings.cpp
Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.torproject.org/projects/vidalia.html. No part of Vidalia, 
00007 **  including this file, may be copied, modified, propagated, or distributed 
00008 **  except according to the terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file AbstractTorSettings.cpp
00013 ** \brief Manages settings that need to be SETCONF'ed to Tor
00014 */
00015 
00016 #include "AbstractTorSettings.h"
00017 
00018 /** Setting that gets set to <i>true</i> if any settings in the current
00019  * settings group have been changed since the last time apply() was called. */
00020 #define SETTING_CHANGED     "Changed"
00021 
00022 
00023 /** Constructor. All settings will be under the heading <b>group</b> and
00024  * <b>torControl</b> will be used to <i>getconf</i> values from Tor. */
00025 AbstractTorSettings::AbstractTorSettings(const QString &group,
00026                                          TorControl *torControl)
00027 : VSettings(group)
00028 {
00029   _torControl = torControl;
00030   _backupSettings = allSettings();
00031 
00032   setDefault(SETTING_CHANGED, false);
00033 }
00034 
00035 /** Reverts all settings to their values at the last time apply() was
00036  * called. */
00037 void
00038 AbstractTorSettings::revert()
00039 {
00040   remove("");
00041   foreach (QString key, _backupSettings.keys()) {
00042     setValue(key, _backupSettings.value(key));
00043   }
00044 }
00045 
00046 /** Returns true if any settings have changed since the last time apply()
00047  * was called. */
00048 bool
00049 AbstractTorSettings::changedSinceLastApply() const
00050 {
00051   return localValue(SETTING_CHANGED).toBool();
00052 }
00053 
00054 /** Sets a value indicating that the server settings have changed since
00055  * apply() was last called. */
00056 void
00057 AbstractTorSettings::setChanged(bool changed)
00058 {
00059   VSettings::setValue(SETTING_CHANGED, changed);
00060   if (!changed)
00061     _backupSettings = allSettings();
00062 }
00063 
00064 /** Returns true if the given QVariant contains an empty value, depending on
00065  * the data type. For example, 0 is considered an empty integer and "" is
00066  * an empty string. */
00067 bool
00068 AbstractTorSettings::isEmptyValue(const QVariant &value) const
00069 {
00070   switch (value.type()) {
00071     case QVariant::String: 
00072       return (value.toString().isEmpty());
00073     case QVariant::StringList:
00074       return (value.toStringList().isEmpty());
00075     case QVariant::UInt:
00076     case QVariant::Int:
00077       return (value.toUInt() == 0); 
00078     case QVariant::Invalid:
00079       return true;
00080     default:  break;
00081   }
00082   return false;
00083 }
00084 
00085 /** Returns the value associated with <b>key</b> saved in the local
00086  * configuration file. */
00087 QVariant
00088 AbstractTorSettings::localValue(const QString &key) const
00089 {
00090   return VSettings::value(key);
00091 }
00092 
00093 /** Returns the value associated with <b>key</b> by querying TOr via 
00094  * <i>getconf key</i>. */
00095 QVariant
00096 AbstractTorSettings::torValue(const QString &key) const
00097 {
00098   QVariant defaultVal;
00099   QVariant confValue;
00100 
00101   defaultVal = defaultValue(key);
00102   if (_torControl) {
00103     confValue = _torControl->getConf(key);
00104     confValue.convert(defaultVal.type());
00105   }
00106   return (isEmptyValue(confValue) ? localValue(key) : confValue);
00107 }
00108 
00109 /** If Vidalia is connected to Tor, this returns the value associated with
00110  * <b>key</b> by calling torValue(). Otherwise, this calls localValue() to get
00111  * the locally saved value associated with <b>key</b>. */
00112 QVariant
00113 AbstractTorSettings::value(const QString &key) const
00114 {
00115   if (_torControl && _torControl->isConnected() && !changedSinceLastApply())
00116     return torValue(key);
00117   return localValue(key);
00118 }
00119 
00120 /** Saves the value <b>val</b> for the setting <b>key</b> to the local
00121  * settings file. */
00122 void
00123 AbstractTorSettings::setValue(const QString &key, const QVariant &value)
00124 {
00125   if (value != localValue(key)) {
00126     setChanged(true);
00127     VSettings::setValue(key, value);
00128   }
00129 }
00130