vsettings.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 vsettings.cpp
00013 ** \version $Id: vsettings.cpp 2362 2008-02-29 04:30:11Z edmanm $
00014 ** \brief Stores and retrieves settings from Vidalia's configuration file. 
00015 */
00016 
00017 #include <vidalia.h>
00018 
00019 #include "vsettings.h"
00020 
00021 /** The file in which all settings will read and written. */
00022 #define SETTINGS_FILE (Vidalia::dataDirectory() + "/vidalia.conf")
00023 
00024 
00025 /** Constructor */
00026 VSettings::VSettings(const QString settingsGroup)
00027 : QSettings(SETTINGS_FILE, QSettings::IniFormat)
00028 {
00029   if (!settingsGroup.isEmpty())
00030     beginGroup(settingsGroup);
00031 }
00032 
00033 /** Returns the saved value associated with <b>key</b>. If no value has been
00034  * set, the default value is returned.
00035  * \sa setDefault
00036  */
00037 QVariant
00038 VSettings::value(const QString &key, const QVariant &defaultVal) const
00039 {
00040   return QSettings::value(key, defaultVal.isNull() ? defaultValue(key)
00041                                                    : defaultVal);
00042 }
00043 
00044 /** Sets the value associated with <b>key</b> to <b>val</b>. */
00045 void
00046 VSettings::setValue(const QString &key, const QVariant &val)
00047 {
00048   if (val == defaultValue(key))
00049     QSettings::remove(key);
00050   else if (val != value(key))
00051     QSettings::setValue(key, val);
00052 }
00053 
00054 /** Sets the default setting for <b>key</b> to <b>val</b>. */
00055 void
00056 VSettings::setDefault(const QString &key, const QVariant &val)
00057 {
00058   _defaults.insert(key, val);
00059 }
00060 
00061 /** Returns the default setting value associated with <b>key</b>. If
00062  * <b>key</b> has no default value, then an empty QVariant is returned. */
00063 QVariant
00064 VSettings::defaultValue(const QString &key) const
00065 {
00066   if (_defaults.contains(key))
00067     return _defaults.value(key);
00068   return QVariant();
00069 }
00070 
00071 /** Resets all of Vidalia's settings. */
00072 void
00073 VSettings::reset()
00074 {
00075   /* Static method, so we have to create a QSettings object. */
00076   QSettings settings(SETTINGS_FILE, QSettings::IniFormat);
00077   settings.clear();
00078 }
00079 
00080 /** Returns a map of all currently saved settings at the last appyl() point. */
00081 QMap<QString, QVariant>
00082 VSettings::allSettings() const
00083 {
00084   QMap<QString, QVariant> settings;
00085   foreach (QString key, allKeys()) {
00086     settings.insert(key, value(key));
00087   }
00088   return settings;
00089 }
00090 

Generated on 22 Feb 2010 for Vidalia by  doxygen 1.6.1