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

Generated on Sat Aug 16 17:38:35 2008 for Vidalia by  doxygen 1.5.6