Vidalia
0.2.15
|
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.torproject.org/projects/vidalia.html. No part of Vidalia, 00007 ** including this file, may be copied, modified, propagated, or distributed 00008 ** except according to the terms described in the LICENSE file. 00009 */ 00010 00011 /* 00012 ** \file VSettings.cpp 00013 ** \brief Stores and retrieves settings from Vidalia's configuration file. 00014 */ 00015 00016 #include "VSettings.h" 00017 #include "Vidalia.h" 00018 00019 #include <QFileInfo> 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 location of Vidalia's configuration settings file. */ 00034 QString 00035 VSettings::settingsFile() 00036 { 00037 return SETTINGS_FILE; 00038 } 00039 00040 /** Returns true if Vidalia's configuration settings file already exists. */ 00041 bool 00042 VSettings::settingsFileExists() 00043 { 00044 QFileInfo fi(settingsFile()); 00045 return fi.exists(); 00046 } 00047 00048 /** Returns the saved value associated with <b>key</b>. If no value has been 00049 * set, the default value is returned. 00050 * \sa setDefault 00051 */ 00052 QVariant 00053 VSettings::value(const QString &key, const QVariant &defaultVal) const 00054 { 00055 return QSettings::value(key, defaultVal.isNull() ? defaultValue(key) 00056 : defaultVal); 00057 } 00058 00059 /** Sets the value associated with <b>key</b> to <b>val</b>. */ 00060 void 00061 VSettings::setValue(const QString &key, const QVariant &val) 00062 { 00063 if (val == defaultValue(key)) 00064 QSettings::remove(key); 00065 else if (val != value(key)) 00066 QSettings::setValue(key, val); 00067 } 00068 00069 /** Sets the default setting for <b>key</b> to <b>val</b>. */ 00070 void 00071 VSettings::setDefault(const QString &key, const QVariant &val) 00072 { 00073 _defaults.insert(key, val); 00074 } 00075 00076 /** Returns the default setting value associated with <b>key</b>. If 00077 * <b>key</b> has no default value, then an empty QVariant is returned. */ 00078 QVariant 00079 VSettings::defaultValue(const QString &key) const 00080 { 00081 if (_defaults.contains(key)) 00082 return _defaults.value(key); 00083 return QVariant(); 00084 } 00085 00086 /** Resets all of Vidalia's settings. */ 00087 void 00088 VSettings::reset() 00089 { 00090 /* Static method, so we have to create a QSettings object. */ 00091 QSettings settings(SETTINGS_FILE, QSettings::IniFormat); 00092 settings.clear(); 00093 } 00094 00095 /** Returns a map of all currently saved settings at the last appyl() point. */ 00096 QMap<QString, QVariant> 00097 VSettings::allSettings() const 00098 { 00099 QMap<QString, QVariant> settings; 00100 foreach (QString key, allKeys()) { 00101 settings.insert(key, value(key)); 00102 } 00103 return settings; 00104 } 00105