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

[vidalia-svn] r1981: Make ServerSettings inherit from AbstractTorSettings, remove (in trunk: . src/config)



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

Modified:
   trunk/
   trunk/src/config/serversettings.cpp
   trunk/src/config/serversettings.h
Log:
 r2013@lysithea:  edmanm | 2007-10-09 23:14:21 -0400
 Make ServerSettings inherit from AbstractTorSettings, remove some duplicated
 code, and shorten/clarify some #defines for setting keys.



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

Modified: trunk/src/config/serversettings.cpp
===================================================================
--- trunk/src/config/serversettings.cpp	2007-10-10 03:38:07 UTC (rev 1980)
+++ trunk/src/config/serversettings.cpp	2007-10-10 03:38:15 UTC (rev 1981)
@@ -38,28 +38,18 @@
 /** Define the maximum length of a server's nickname. */
 #define MAX_NICKNAME_LEN   19
 
-/* Server-related torrc configuration parameters */
-#define SERVER_NICKNAME         "Nickname"
-#define SERVER_ORPORT           "ORPort"
-#define SERVER_DIRPORT          "DirPort"
-#define SERVER_CONTACTINFO      "ContactInfo"
-#define SERVER_EXITPOLICY       "ExitPolicy"
-#define SERVER_BANDWIDTH_RATE   "BandwidthRate"
-#define SERVER_BANDWIDTH_BURST  "BandwidthBurst"
-#define SERVER_RELAY_BANDWIDTH_RATE   "RelayBandwidthRate"
-#define SERVER_RELAY_BANDWIDTH_BURST  "RelayBandwidthBurst"
-
 /* Server configuration settings */
-#define SETTING_SERVER_ENABLED    "Server/Enabled"
-#define SETTING_SERVER_CHANGED    "Server/Changed"
-#define SETTING_SERVER_DIRMIRROR  "Server/DirectoryMirror"
-#define SETTING_SERVER_NICKNAME   "Server/"SERVER_NICKNAME
-#define SETTING_SERVER_ORPORT     "Server/"SERVER_ORPORT
-#define SETTING_SERVER_DIRPORT    "Server/"SERVER_DIRPORT
-#define SETTING_SERVER_CONTACT    "Server/"SERVER_CONTACTINFO
-#define SETTING_SERVER_EXITPOLICY "Server/"SERVER_EXITPOLICY
-#define SETTING_SERVER_BWRATE     "Server/"SERVER_BANDWIDTH_RATE
-#define SETTING_SERVER_BWBURST    "Server/"SERVER_BANDWIDTH_BURST
+#define SETTING_ENABLED         "Enabled"
+#define SETTING_DIRMIRROR       "DirectoryMirror"
+#define SETTING_NICKNAME        "Nickname"
+#define SETTING_ORPORT          "ORPort"
+#define SETTING_DIRPORT         "DirPort"
+#define SETTING_CONTACT         "ContactInfo"
+#define SETTING_EXITPOLICY      "ExitPolicy"
+#define SETTING_BANDWIDTH_RATE  "BandwidthRate"
+#define SETTING_BANDWIDTH_BURST "BandwidthBurst"
+#define SETTING_RELAY_BANDWIDTH_RATE   "RelayBandwidthRate"
+#define SETTING_RELAY_BANDWIDTH_BURST  "RelayBandwidthBurst"
 
 
 /** Constructor.
@@ -67,138 +57,20 @@
  * configuration settings.
  */
 ServerSettings::ServerSettings(TorControl *torControl)
+: AbstractTorSettings("Server", torControl)
 {
-  _torControl = torControl;
-  _backupSettings = allSettings();
-
-  setDefault(SETTING_SERVER_ENABLED,    false);
-  setDefault(SETTING_SERVER_CHANGED,    false);
-  setDefault(SETTING_SERVER_DIRMIRROR,  true);
-  setDefault(SETTING_SERVER_ORPORT,     9001);
-  setDefault(SETTING_SERVER_DIRPORT,    9030);
-  setDefault(SETTING_SERVER_CONTACT,    "<your@xxxxxxxxx>");
-  setDefault(SETTING_SERVER_BWRATE,     3145728);
-  setDefault(SETTING_SERVER_BWBURST,    6291456);
-  setDefault(SETTING_SERVER_NICKNAME,   "Unnamed");
-  setDefault(SETTING_SERVER_EXITPOLICY,
+  setDefault(SETTING_ENABLED,    false);
+  setDefault(SETTING_DIRMIRROR,  true);
+  setDefault(SETTING_ORPORT,     9001);
+  setDefault(SETTING_DIRPORT,    9030);
+  setDefault(SETTING_CONTACT,    "<your@xxxxxxxxx>");
+  setDefault(SETTING_BANDWIDTH_RATE,  3145728);
+  setDefault(SETTING_BANDWIDTH_BURST, 6291456);
+  setDefault(SETTING_NICKNAME,        "Unnamed");
+  setDefault(SETTING_EXITPOLICY,
     ExitPolicy(ExitPolicy::Default).toString());
 }
 
-/** Stores a boolean value indicating if the server's configuration has
- * changed since it was last applied.
- * \param changed Boolean value indicating whether settings have been changed.
- */
-void
-ServerSettings::setChanged(bool changed)
-{
-  VidaliaSettings::setValue(SETTING_SERVER_CHANGED, changed);
-}
-
-/** Returns a boolean value indicating if the server's configuration has
- * changed since it was last applied.
- */
-bool
-ServerSettings::changedSinceLastApply()
-{
-  return VidaliaSettings::value(SETTING_SERVER_CHANGED).toBool();
-}
-
-/** Restores the server configuration back to its state after the last call to
- * apply().
- * \sa apply()
- */
-void
-ServerSettings::revert()
-{
-  beginGroup("Server");
-  remove(""); /* Removes all settings in the Server group */
-  foreach(QString key, _backupSettings.keys()) {
-    setValue(key, _backupSettings.value(key));
-  }
-  endGroup();
-}
-
-/** Returns a map of all server configuration settings currently stored in
- * Vidalia's settings file. */
-QMap<QString, QVariant>
-ServerSettings::allSettings()
-{
-  QMap<QString, QVariant> settings;
-  beginGroup("Server");
-  foreach(QString key, allKeys()) {
-    settings.insert(key, QSettings::value(key));
-  }
-  endGroup();
-  return settings;
-}
-
-/** 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
-ServerSettings::isEmptyValue(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 stored value for the given key. If no stored value exists for
- * the given key, the specified default value is used. If Vidalia is currently
- * connected to Tor, we will ask Tor what its value is. Otherwise, we will
- * retrieve it from Vidalia's stored settings, allowing the configuration
- * information to be edited even if Tor isn't running.
- * \param key Configuration key
- * \param defaultValue Value to use if no configuration value exists for the
- * specified key.
- */
-QVariant
-ServerSettings::value(QString key)
-{
-  QVariant value;
-  QString confKey, confValue;
-  confKey = key.mid(key.indexOf("/")+1);
-  if (_torControl->isConnected() && !changedSinceLastApply()) {
-    quint32 torVersion = _torControl->getTorVersion();
-    if (torVersion >= 0x020001) {
-      if (confKey == SERVER_BANDWIDTH_RATE)
-        confKey = SERVER_RELAY_BANDWIDTH_RATE;
-      else if (confKey == SERVER_BANDWIDTH_BURST)
-        confKey = SERVER_RELAY_BANDWIDTH_BURST;
-    }
-    if (_torControl->getConf(confKey, confValue)) {
-      /* Get the value from Tor */
-      value.setValue(confValue);
-      value.convert(defaultValue(key).type());
-    }
-  } else {
-    /* Read our saved value from vidalia.conf */
-    value = VidaliaSettings::value(key);
-  }
-  return (isEmptyValue(value) ? defaultValue(key) : value);
-}
-
-/** Saves the given configuration key-value to the application settings file.
- * \param key Configuration key
- * \param value Value to assign to <b>key</b>
- */
-void
-ServerSettings::setValue(QString key, QVariant value)
-{
-  if (value != VidaliaSettings::value(key)) {
-    setChanged(true);
-    VidaliaSettings::setValue(key, value);
-  }
-}
-
 /** Returns a QHash of Tor-recognizable configuratin keys to their current
  * values. */
 QHash<QString, QString>
@@ -208,48 +80,45 @@
   quint32 torVersion = _torControl->getTorVersion();
 
   /* Server Nickname */
-  conf.insert(SERVER_NICKNAME,
-    (isServerEnabled() ? VidaliaSettings::value(SETTING_SERVER_NICKNAME).toString()
+  conf.insert(SETTING_NICKNAME,
+    (isServerEnabled() ? localValue(SETTING_NICKNAME).toString()
                        : ""));
   /* Server ORPort */
-  conf.insert(SERVER_ORPORT,
-    (isServerEnabled() ? VidaliaSettings::value(SETTING_SERVER_ORPORT).toString()
+  conf.insert(SETTING_ORPORT,
+    (isServerEnabled() ? localValue(SETTING_ORPORT).toString()
                        : "0"));
   /* Server DirPort */
-  conf.insert(SERVER_DIRPORT, 
-    (isDirectoryMirror() ? VidaliaSettings::value(SETTING_SERVER_DIRPORT).toString() 
+  conf.insert(SETTING_DIRPORT, 
+    (isDirectoryMirror() ? localValue(SETTING_DIRPORT).toString() 
                          : "0"));
   /* Server Exit Policy */
-  conf.insert(SERVER_EXITPOLICY, 
-    VidaliaSettings::value(SETTING_SERVER_EXITPOLICY).toString());
+  conf.insert(SETTING_EXITPOLICY, 
+    localValue(SETTING_EXITPOLICY).toString());
   
   /* Server bandwidth settings */
-  conf.insert((torVersion >= 0x020001 ? SERVER_RELAY_BANDWIDTH_RATE 
-                                      : SERVER_BANDWIDTH_RATE),
-    QString::number(VidaliaSettings::value(SETTING_SERVER_BWRATE).toUInt()) + " bytes");
-  conf.insert((torVersion >= 0x020001 ? SERVER_RELAY_BANDWIDTH_BURST
-                                      : SERVER_BANDWIDTH_BURST),
-    QString::number(VidaliaSettings::value(SETTING_SERVER_BWBURST).toUInt()) + " bytes");
+  conf.insert((torVersion >= 0x020001 ? SETTING_RELAY_BANDWIDTH_RATE 
+                                      : SETTING_BANDWIDTH_RATE),
+    QString::number(localValue(SETTING_BANDWIDTH_RATE).toUInt()) + " bytes");
+  conf.insert((torVersion >= 0x020001 ? SETTING_RELAY_BANDWIDTH_BURST
+                                      : SETTING_BANDWIDTH_BURST),
+    QString::number(localValue(SETTING_BANDWIDTH_BURST).toUInt()) + " bytes");
     
   /* Server Contact Information */
   QString contact = 
-    VidaliaSettings::value(SETTING_SERVER_CONTACT).toString().trimmed();
-  QString defaultContact =
-    VidaliaSettings::defaultValue(SETTING_SERVER_CONTACT).toString();
+    localValue(SETTING_CONTACT).toString().trimmed();
+  QString defaultContact = defaultValue(SETTING_CONTACT).toString();
   if ((contact == defaultContact) ||
       (contact == scrub_email_addr(defaultContact))) {
     /* Only set the contact info if they put something non-default there */
     contact = "";
   }
-  conf.insert(SERVER_CONTACTINFO, scrub_email_addr(contact));
+  conf.insert(SETTING_CONTACT, scrub_email_addr(contact));
   return conf;
 }
 
-/** Applies the current server configuration settings to Tor and tells Tor to
- * write the new configuration to disk. 
- * \param errmsg If specified, will store an error message should something go
- * wrong. 
- */
+/** Applies the current server configuration settings to Tor. If <b>errmsg</b>
+ * is specified and an error occurs while applying the settings, it will be 
+ * set to a string describing the error. */
 bool
 ServerSettings::apply(QString *errmsg)
 {
@@ -260,25 +129,20 @@
   } else { 
     QStringList resetKeys;
     quint32 torVersion = _torControl->getTorVersion();
-    resetKeys << SERVER_ORPORT 
-              << SERVER_NICKNAME 
-              << SERVER_DIRPORT
-              << SERVER_CONTACTINFO
-              << SERVER_EXITPOLICY;
+    resetKeys << SETTING_ORPORT 
+              << SETTING_NICKNAME 
+              << SETTING_DIRPORT
+              << SETTING_CONTACT
+              << SETTING_EXITPOLICY;
     if (torVersion >= 0x020001) {
-      resetKeys << SERVER_RELAY_BANDWIDTH_RATE
-                << SERVER_RELAY_BANDWIDTH_BURST;
+      resetKeys << SETTING_RELAY_BANDWIDTH_RATE
+                << SETTING_RELAY_BANDWIDTH_BURST;
     } else {
-      resetKeys << SERVER_BANDWIDTH_RATE
-                << SERVER_BANDWIDTH_BURST;
+      resetKeys << SETTING_BANDWIDTH_RATE
+                << SETTING_BANDWIDTH_BURST;
     }
     rc = _torControl->resetConf(resetKeys, errmsg);
   }
-  if (rc) {
-    if (_torControl->saveConf(errmsg))
-      setChanged(false);
-    _backupSettings = allSettings();
-  }
   return rc;
 }
 
@@ -288,7 +152,7 @@
 void
 ServerSettings::setServerEnabled(bool enable)
 {
-  setValue(SETTING_SERVER_ENABLED, enable);
+  setValue(SETTING_ENABLED, enable);
 }
 
 /** Returns true if Tor is currently configured to run as a Tor server. If Tor
@@ -299,56 +163,56 @@
 {
   QHash<QString,QString> confValues;
   if (_torControl->isConnected() && !changedSinceLastApply()) {
-    confValues.insert(SERVER_ORPORT, "");
-    confValues.insert(SERVER_NICKNAME, "");
+    confValues.insert(SETTING_ORPORT, "");
+    confValues.insert(SETTING_NICKNAME, "");
     if (_torControl->getConf(confValues)) {
-      return (confValues.value(SERVER_ORPORT).toUInt() != 0 &&
-              !confValues.value(SERVER_NICKNAME).isEmpty());
+      return (confValues.value(SETTING_ORPORT).toUInt() != 0 &&
+              !confValues.value(SETTING_NICKNAME).isEmpty());
     }
   }
-  return VidaliaSettings::value(SETTING_SERVER_ENABLED).toBool();
+  return localValue(SETTING_ENABLED).toBool();
 }
 
 /** Sets the server's ORPort. */
 void
 ServerSettings::setORPort(quint16 orPort)
 {
-  setValue(SETTING_SERVER_ORPORT, orPort);
+  setValue(SETTING_ORPORT, orPort);
 }
 
 /** Gets the server's current ORPort setting. */
 quint16
 ServerSettings::getORPort()
 {
-  return (quint16)value(SETTING_SERVER_ORPORT).toUInt();
+  return (quint16)value(SETTING_ORPORT).toUInt();
 }
 
 /** Sets the server's current DirPort. */
 void
 ServerSettings::setDirPort(quint16 dirPort)
 {
-  setValue(SETTING_SERVER_DIRPORT, dirPort);
+  setValue(SETTING_DIRPORT, dirPort);
 }
 
 /** Gets the server's current DirPort. */
 quint16
 ServerSettings::getDirPort()
 {
-  return (quint16)value(SETTING_SERVER_DIRPORT).toUInt();
+  return (quint16)value(SETTING_DIRPORT).toUInt();
 }
 
 /** Sets the server's nickname. */
 void
 ServerSettings::setNickname(QString nickname)
 {
-  setValue(SETTING_SERVER_NICKNAME, nickname);
+  setValue(SETTING_NICKNAME, nickname);
 }
 
 /** Gets the server's nickname. */
 QString
 ServerSettings::getNickname()
 {
-  QString nickname = value(SETTING_SERVER_NICKNAME).toString();
+  QString nickname = value(SETTING_NICKNAME).toString();
   /* Ensure the nickname contains only valid characters and is not too long. */
   return ensure_valid_chars(nickname, 
                             VALID_NICKNAME_CHARS).left(MAX_NICKNAME_LEN);
@@ -358,69 +222,69 @@
 void
 ServerSettings::setContactInfo(QString contact)
 {
-  setValue(SETTING_SERVER_CONTACT, contact);
+  setValue(SETTING_CONTACT, contact);
 }
 
 /** Gets the server's contact information. */
 QString
 ServerSettings::getContactInfo()
 {
-  return value(SETTING_SERVER_CONTACT).toString();
+  return value(SETTING_CONTACT).toString();
 }
 
 /** Returns whether this server will act as a directory mirror or not. */
 bool
 ServerSettings::isDirectoryMirror()
 {
-  return VidaliaSettings::value(SETTING_SERVER_DIRMIRROR).toBool();
+  return localValue(SETTING_DIRMIRROR).toBool();
 }
 
 /** Sets whether this server will act as a directory mirror. */
 void
 ServerSettings::setDirectoryMirror(bool mirror)
 {
-  setValue(SETTING_SERVER_DIRMIRROR, mirror);
+  setValue(SETTING_DIRMIRROR, mirror);
 }
 
 /** Returns the exit policy for this server. */
 ExitPolicy
 ServerSettings::getExitPolicy()
 {
-  return ExitPolicy(value(SETTING_SERVER_EXITPOLICY).toString());
+  return ExitPolicy(value(SETTING_EXITPOLICY).toString());
 }
 
 /** Sets the exit policy for this server. */
 void
 ServerSettings::setExitPolicy(ExitPolicy &exitPolicy)
 {
-  setValue(SETTING_SERVER_EXITPOLICY, exitPolicy.toString());
+  setValue(SETTING_EXITPOLICY, exitPolicy.toString());
 }
 
 /** Returns the long-term average bandwidth rate (in KB/s) for this server. */
 quint32
 ServerSettings::getBandwidthAvgRate()
 {
-  return value(SETTING_SERVER_BWRATE).toUInt();
+  return value(SETTING_BANDWIDTH_RATE).toUInt();
 }
 
 /** Sets the long-term average bandwidth rate (in KB/s) for this server. */
 void
 ServerSettings::setBandwidthAvgRate(quint32 rate)
 {
-  setValue(SETTING_SERVER_BWRATE, rate);
+  setValue(SETTING_BANDWIDTH_RATE, rate);
 }
 
 /** Returns the maximum bandwidth burst rate (in KB/s) for this server. */
 quint32
 ServerSettings::getBandwidthBurstRate()
 {
-  return value(SETTING_SERVER_BWBURST).toUInt();
+  return value(SETTING_BANDWIDTH_BURST).toUInt();
 }
 
 /** Sets the maximum bandwidth burst rate (in KB/s) for this server. */
 void
 ServerSettings::setBandwidthBurstRate(quint32 rate)
 {
-  setValue(SETTING_SERVER_BWBURST, rate);
+  setValue(SETTING_BANDWIDTH_BURST, rate);
 }
 

Modified: trunk/src/config/serversettings.h
===================================================================
--- trunk/src/config/serversettings.h	2007-10-10 03:38:07 UTC (rev 1980)
+++ trunk/src/config/serversettings.h	2007-10-10 03:38:15 UTC (rev 1981)
@@ -28,23 +28,17 @@
 #ifndef _SERVERSETTINGS_H
 #define _SERVERSETTINGS_H
 
-#include <control/torcontrol.h>
-
-#include "vidaliasettings.h"
+#include "abstracttorsettings.h"
 #include "exitpolicy.h"
 
 
-class ServerSettings : private VidaliaSettings
+class ServerSettings : public AbstractTorSettings
 {
 
 public:
   /** Constructor */
   ServerSettings(TorControl *torControl);
 
-  /** Reverts all settings changes since the last apply. */
-  void revert();
-  /** Returns true if the settings have changed since the last apply. */
-  bool changedSinceLastApply();
   /** Applies changese to Tor. */
   bool apply(QString *errmsg = 0);
 
@@ -93,27 +87,8 @@
   quint32 getBandwidthBurstRate();
 
 private:
-  /** Sets a value indicating that the server settings have changed since
-   * apply() was last called. */
-  void setChanged(bool changed);
-  
   /** Returns Tor-recognizable configuration keys and current values. */
   QHash<QString,QString> confValues();
-
-  /** Returns all currently stored server settings. */
-  QMap<QString, QVariant> allSettings();
-  
-  /** Returns true if the specified QVariant contains an empty value. */
-  bool isEmptyValue(QVariant value);
-  /** Retrieves a configuration value. If one isn't found, use a default. */
-  QVariant value(QString key);
-  /** Stores a configuration key-value. */
-  void setValue(QString key, QVariant value);
-
-  /** A TorControl object used to talk to Tor. */
-  TorControl* _torControl;
-  /** Values of all stored settings at the last apply() point. */
-  QMap<QString, QVariant> _backupSettings;
 };
 
 #endif