[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[vidalia-svn] r1980: Add an AbstractTorSettings class that provides general metho (in trunk: . src/config)



Author: edmanm
Date: 2007-10-09 23:38:07 -0400 (Tue, 09 Oct 2007)
New Revision: 1980

Added:
   trunk/src/config/abstracttorsettings.cpp
   trunk/src/config/abstracttorsettings.h
Modified:
   trunk/
   trunk/src/config/config.pri
Log:
 r2012@lysithea:  edmanm | 2007-10-09 23:11:27 -0400
 Add an AbstractTorSettings class that provides general methods for setting or
 getting values that may need to get SETCONF'ed to or GETCONF'ed from Tor, but
 which Vidalia will also need to remember (for example, if we can't SAVECONF or
 the user wants to muck with their settings while Tor isn't running).



Property changes on: trunk
___________________________________________________________________
 svk:merge ticket from /local/vidalia/trunk [r2012] on dc66be73-d13e-47ba-a267-8dc7cda68c65

Added: trunk/src/config/abstracttorsettings.cpp
===================================================================
--- trunk/src/config/abstracttorsettings.cpp	                        (rev 0)
+++ trunk/src/config/abstracttorsettings.cpp	2007-10-10 03:38:07 UTC (rev 1980)
@@ -0,0 +1,141 @@
+/****************************************************************
+ *  Vidalia is distributed under the following license:
+ *
+ *  Copyright (C) 2006-2007,  Matt Edman, Justin Hipple
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; either version 2
+ *  of the License, or (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
+ *  Boston, MA  02110-1301, USA.
+ ****************************************************************/
+
+/** 
+ * \file abstracttorsettings.cpp
+ * \version $Id$
+ * \brief Manages settings that need to be SETCONF'ed to Tor
+ */
+
+#include "abstracttorsettings.h"
+
+/** Setting that gets set to <i>true</i> if any settings in the current
+ * settings group have been changed since the last time apply() was called. */
+#define SETTING_CHANGED     "Changed"
+
+
+/** Constructor. All settings will be under the heading <b>group</b> and
+ * <b>torControl</b> will be used to <i>getconf</i> values from Tor. */
+AbstractTorSettings::AbstractTorSettings(const QString &group,
+                                         TorControl *torControl)
+: VSettings(group)
+{
+  _torControl = torControl;
+  _backupSettings = allSettings();
+
+  setDefault(SETTING_CHANGED, false);
+}
+
+/** Reverts all settings to their values at the last time apply() was
+ * called. */
+void
+AbstractTorSettings::revert()
+{
+  remove("");
+  foreach (QString key, _backupSettings.keys()) {
+    setValue(key, _backupSettings.value(key));
+  }
+}
+
+/** Returns true if any settings have changed since the last time apply()
+ * was called. */
+bool
+AbstractTorSettings::changedSinceLastApply()
+{
+  return localValue(SETTING_CHANGED).toBool();
+}
+
+/** Sets a value indicating that the server settings have changed since
+ * apply() was last called. */
+void
+AbstractTorSettings::setChanged(bool changed)
+{
+  VSettings::setValue(SETTING_CHANGED, changed);
+  if (!changed)
+    _backupSettings = allSettings();
+}
+
+/** Returns true if the given QVariant contains an empty value, depending on
+ * the data type. For example, 0 is considered an empty integer and "" is
+ * an empty string. */
+bool
+AbstractTorSettings::isEmptyValue(const QVariant &value)
+{
+  switch (value.type()) {
+    case QVariant::String: 
+      return (value.toString().isEmpty());
+    case QVariant::UInt:
+    case QVariant::Int:
+      return (value.toUInt() == 0); 
+    case QVariant::Invalid:
+      return true;
+    default:  break;
+  }
+  return false;
+}
+
+/** Returns the value associated with <b>key</b> saved in the local
+ * configuration file. */
+QVariant
+AbstractTorSettings::localValue(const QString &key)
+{
+  return VSettings::value(key);
+}
+
+/** Returns the value associated with <b>key</b> by querying TOr via 
+ * <i>getconf key</i>. */
+QVariant
+AbstractTorSettings::torValue(const QString &key)
+{
+  QVariant value;
+  QVariant defaultVal;
+  QString confValue;
+
+  defaultVal = defaultValue(key);
+  if (_torControl->getConf(key, confValue)) {
+    value.setValue(confValue);
+    value.convert(defaultVal.type());
+  }
+  return (isEmptyValue(value) ? defaultVal : value);
+}
+
+/** If Vidalia is connected to Tor, this returns the value associated with
+ * <b>key</b> by calling torValue(). Otherwise, this calls localValue() to get
+ * the locally saved value associated with <b>key</b>. */
+QVariant
+AbstractTorSettings::value(const QString &key)
+{
+  if (_torControl->isConnected() && !changedSinceLastApply())
+    return torValue(key);
+  return localValue(key);
+}
+
+/** Saves the value <b>val</b> for the setting <b>key</b> to the local
+ * settings file. */
+void
+AbstractTorSettings::setValue(const QString &key, const QVariant &value)
+{
+  if (value != localValue(key)) {
+    setChanged(true);
+    VSettings::setValue(key, value);
+  }
+}
+


Property changes on: trunk/src/config/abstracttorsettings.cpp
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/src/config/abstracttorsettings.h
===================================================================
--- trunk/src/config/abstracttorsettings.h	                        (rev 0)
+++ trunk/src/config/abstracttorsettings.h	2007-10-10 03:38:07 UTC (rev 1980)
@@ -0,0 +1,82 @@
+/****************************************************************
+ *  Vidalia is distributed under the following license:
+ *
+ *  Copyright (C) 2006-2007,  Matt Edman, Justin Hipple
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; either version 2
+ *  of the License, or (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
+ *  Boston, MA  02110-1301, USA.
+ ****************************************************************/
+
+/** 
+ * \file abstracttorsettings.h
+ * \version $Id$
+ * \brief Manages settings that need to be SETCONF'ed to Tor
+ */
+
+#ifndef _ABSTRACTTORSETTINGS_H
+#define _ABSTRACTTORSETTINGS_H
+
+#include <torcontrol.h>
+#include "vsettings.h"
+
+
+class AbstractTorSettings : public VSettings
+{
+public:
+  /** Constructor. All settings will be under the heading <b>group</b> and
+   * <b>torControl</b> will be used to <i>getconf</i> values from Tor. */
+  AbstractTorSettings(const QString &group, TorControl *torControl);
+  
+  /** Sets a value indicating that the server settings have changed since
+   * apply() was last called. */
+  void setChanged(bool changed);
+  /** Returns true if any settings have changed since the last time apply()
+   * was called. */
+  virtual bool changedSinceLastApply();
+  /** Reverts all settings to their values at the last time apply() was
+   * called. */
+  virtual void revert();
+  /** Subclasses must implement this to <i>setconf</i> values to apply them
+   * to a running Tor instance. */
+  virtual bool apply(QString *errmsg) = 0;
+
+protected:
+  /** If Vidalia is connected to Tor, this returns the value associated with
+   * <b>key</b> by calling torValue(). Otherwise, this calls localValue()
+   * to get the locally saved value associated with <b>key</b>. */
+  virtual QVariant value(const QString &key);
+  /** Returns the value associated with <b>key</b> saved in the local
+   * configuration file. */
+  virtual QVariant localValue(const QString &key);
+  /** Returns the value associated with <b>key</b> by querying TOr via 
+   * <i>getconf key</i>. */
+  virtual QVariant torValue(const QString &key);
+  /** Saves the value <b>val</b> for the setting <b>key</b> to the local
+   * settings file. */
+  virtual void setValue(const QString &key, const QVariant &value);
+  
+  /** Returns true if the given QVariant contains an empty value, depending on
+   * the data type. For example, 0 is considered an empty integer and "" is
+   * an empty string. */
+  bool isEmptyValue(const QVariant &value);
+
+  TorControl* _torControl;
+
+private:
+  QMap<QString, QVariant> _backupSettings;
+};
+
+#endif
+


Property changes on: trunk/src/config/abstracttorsettings.h
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: trunk/src/config/config.pri
===================================================================
--- trunk/src/config/config.pri	2007-10-10 03:37:44 UTC (rev 1979)
+++ trunk/src/config/config.pri	2007-10-10 03:38:07 UTC (rev 1980)
@@ -29,7 +29,8 @@
            $$PWD/torsettings.h \
            $$PWD/policy.h \
            $$PWD/exitpolicy.h \
-           $$PWD/networksettings.h
+           $$PWD/networksettings.h \
+           $$PWD/abstracttorsettings.h
            
 SOURCES += $$PWD/vsettings.cpp \
            $$PWD/vidaliasettings.cpp \
@@ -37,5 +38,6 @@
            $$PWD/torsettings.cpp \
            $$PWD/policy.cpp \
            $$PWD/exitpolicy.cpp \
-           $$PWD/networksettings.cpp
+           $$PWD/networksettings.cpp \
+           $$PWD/abstracttorsettings.cpp