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

Generated on Mon Aug 30 19:10:00 2010 for Vidalia by  doxygen 1.5.9