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

[vidalia-svn] r1978: Add a new VSettings class (subclass of QSettings) that acts (in trunk: . src/config)



Author: edmanm
Date: 2007-10-09 23:37:12 -0400 (Tue, 09 Oct 2007)
New Revision: 1978

Added:
   trunk/src/config/vsettings.cpp
   trunk/src/config/vsettings.h
Modified:
   trunk/
   trunk/src/config/config.pri
Log:
 r2010@lysithea:  edmanm | 2007-10-09 23:07:53 -0400
 Add a new VSettings class (subclass of QSettings) that acts as a general
 wrapper around vidalia.conf. Any Vidalia class that wants to set or get
 setting values should go through VSettings or a subclass of it.



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

Modified: trunk/src/config/config.pri
===================================================================
--- trunk/src/config/config.pri	2007-10-10 03:37:04 UTC (rev 1977)
+++ trunk/src/config/config.pri	2007-10-10 03:37:12 UTC (rev 1978)
@@ -21,14 +21,18 @@
 #  02110-1301, USA.
 #################################################################
 
-HEADERS += $$PWD/vidaliasettings.h \
+INCLUDEPATH += $$PWD
+
+HEADERS += $$PWD/vsettings.h \
+           $$PWD/vidaliasettings.h \
            $$PWD/serversettings.h \
            $$PWD/torsettings.h \
            $$PWD/policy.h \
            $$PWD/exitpolicy.h \
            $$PWD/networksettings.h
            
-SOURCES += $$PWD/vidaliasettings.cpp \
+SOURCES += $$PWD/vsettings.cpp \
+           $$PWD/vidaliasettings.cpp \
            $$PWD/serversettings.cpp \
            $$PWD/torsettings.cpp \
            $$PWD/policy.cpp \

Added: trunk/src/config/vsettings.cpp
===================================================================
--- trunk/src/config/vsettings.cpp	                        (rev 0)
+++ trunk/src/config/vsettings.cpp	2007-10-10 03:37:12 UTC (rev 1978)
@@ -0,0 +1,101 @@
+/****************************************************************
+ *  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 vsettings.cpp
+ * \version $Id$
+ * \brief Stores and retrieves settings from Vidalia's configuration file. 
+ */
+
+#include <vidalia.h>
+
+#include "vsettings.h"
+
+/** The file in which all settings will read and written. */
+#define SETTINGS_FILE (Vidalia::dataDirectory() + "/vidalia.conf")
+
+
+/** Constructor */
+VSettings::VSettings(const QString settingsGroup)
+: QSettings(SETTINGS_FILE, QSettings::IniFormat)
+{
+  if (!settingsGroup.isEmpty())
+    beginGroup(settingsGroup);
+}
+
+/** Returns the saved value associated with <b>key</b>. If no value has been
+ * set, the default value is returned.
+ * \sa setDefault
+ */
+QVariant
+VSettings::value(const QString &key, const QVariant &defaultVal)
+{
+  return QSettings::value(key, defaultVal.isNull() ? defaultValue(key)
+                                                   : defaultVal);
+}
+
+/** Sets the value associated with <b>key</b> to <b>val</b>. */
+void
+VSettings::setValue(const QString &key, const QVariant &val)
+{
+  if (val == defaultValue(key))
+    QSettings::remove(key);
+  else if (val != value(key))
+    QSettings::setValue(key, val);
+}
+
+/** Sets the default setting for <b>key</b> to <b>val</b>. */
+void
+VSettings::setDefault(const QString &key, const QVariant &val)
+{
+  _defaults.insert(key, val);
+}
+
+/** Returns the default setting value associated with <b>key</b>. If
+ * <b>key</b> has no default value, then an empty QVariant is returned. */
+QVariant
+VSettings::defaultValue(const QString &key)
+{
+  if (_defaults.contains(key))
+    return _defaults.value(key);
+  return QVariant();
+}
+
+/** Resets all of Vidalia's settings. */
+void
+VSettings::reset()
+{
+  /* Static method, so we have to create a QSettings object. */
+  QSettings settings(SETTINGS_FILE, QSettings::IniFormat);
+  settings.clear();
+}
+
+/** Returns a map of all currently saved settings at the last appyl() point. */
+QMap<QString, QVariant>
+VSettings::allSettings()
+{
+  QMap<QString, QVariant> settings;
+  foreach (QString key, allKeys()) {
+    settings.insert(key, value(key));
+  }
+  return settings;
+}
+


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

Added: trunk/src/config/vsettings.h
===================================================================
--- trunk/src/config/vsettings.h	                        (rev 0)
+++ trunk/src/config/vsettings.h	2007-10-10 03:37:12 UTC (rev 1978)
@@ -0,0 +1,71 @@
+/****************************************************************
+ *  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 vsettings.h
+ * \version $Id$
+ * \brief Stores and retrieves settings from Vidalia's configuration file.
+ */
+
+#ifndef _VSETTINGS_H
+#define _VSETTINGS_H
+
+#include <QHash>
+#include <QSettings>
+
+
+class VSettings : public QSettings
+{
+public:
+  /** Default constructor. The optional parameter <b>group</b> can be used to
+   * set a prefix that will be prepended to keys specified to VSettings in
+   * value() and setValue(). */
+  VSettings(const QString group = QString());
+
+  /** Resets all of Vidalia's settings. */
+  static void reset();
+
+  /** Returns the saved value associated with <b>key</b>. If no value has been
+   * set, the default value is returned.
+   * \sa setDefault
+   */
+  virtual QVariant value(const QString &key,
+                         const QVariant &defaultVal = QVariant());
+  /** Sets the value associated with <b>key</b> to <b>val</b>. */
+  virtual void setValue(const QString &key, const QVariant &val);
+
+protected:
+  /** Sets the default setting for <b>key</b> to <b>val</b>. */
+  void setDefault(const QString &key, const QVariant &val);
+  /** Returns the default setting value associated with <b>key</b>. If
+   * <b>key</b> has no default value, then an empty QVariant is returned. */
+  QVariant defaultValue(const QString &key);
+  /** Returns a map of all currently saved settings at the last apply()
+   * point. */
+  QMap<QString, QVariant> allSettings();
+
+private:
+  /** Association of setting key names to default setting values. */
+  QHash<QString, QVariant> _defaults; 
+};
+
+#endif
+


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