vidaliasettings.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /** 
00023  * \file vidaliasettings.cpp
00024  * \version $Id: vidaliasettings.cpp 1238 2006-09-25 17:50:57Z edmanm $
00025  * \brief General Vidalia settings, such as language and interface style
00026  */
00027 
00028 #include <QDir>
00029 #include <QCoreApplication>
00030 #include <lang/languagesupport.h>
00031 #include <vidalia.h>
00032 
00033 #include "vidaliasettings.h"
00034 
00035 #if defined(Q_WS_WIN)
00036 #include <util/win32.h>
00037 #endif
00038 
00039 
00040 #define SETTING_LANGUAGE            "LanguageCode"
00041 #define SETTING_STYLE               "InterfaceStyle"
00042 #define SETTING_RUN_TOR_AT_START    "RunTorAtStart"
00043 #define SETTING_DATA_DIRECTORY      "DataDirectory"
00044 
00045 /* Default Vidalia Settings */
00046 #if defined(Q_WS_MAC)
00047 #define DEFAULT_STYLE               "macintosh (aqua)"
00048 #else
00049 #define DEFAULT_STYLE               "plastique"
00050 #endif
00051 
00052 #if defined(Q_OS_WIN32)
00053 #define STARTUP_REG_KEY        "Software\\Microsoft\\Windows\\CurrentVersion\\Run"
00054 #define VIDALIA_REG_KEY        "Vidalia" 
00055 #endif
00056 
00057 /** The location of Vidalia's settings and configuration file. */
00058 #define SETTINGS_FILE   (Vidalia::dataDirectory() + "/vidalia.conf")
00059 
00060 
00061 /** Default Constructor */
00062 VidaliaSettings::VidaliaSettings()
00063 : QSettings(SETTINGS_FILE, QSettings::IniFormat)
00064 {
00065   setDefault(SETTING_LANGUAGE, LanguageSupport::defaultLanguageCode());
00066   setDefault(SETTING_RUN_TOR_AT_START, true);
00067   setDefault(SETTING_STYLE, DEFAULT_STYLE);
00068 }
00069 
00070 /** Sets the default value of <b>key</b> to be <b>val</b>. */
00071 void
00072 VidaliaSettings::setDefault(QString key, QVariant val)
00073 {
00074   _defaults.insert(key, val);
00075 }
00076 
00077 /** Returns the default value for <b>key</b>. */
00078 QVariant
00079 VidaliaSettings::defaultValue(QString key)
00080 {
00081   if (_defaults.contains(key)) {
00082     return _defaults.value(key);
00083   }
00084   return QVariant();
00085 }
00086 
00087 /** Save <b>val</b> to the configuration file for the setting <b>key</b>, if
00088  * <b>val</b> is different than <b>key</b>'s current value. */
00089 void
00090 VidaliaSettings::setValue(QString key, QVariant val)
00091 {
00092   if (value(key) != val) {
00093     QSettings::setValue(key, val);
00094   }
00095 }
00096 
00097 /** Returns the value for <b>key</b>. If no value is currently saved, then the
00098  * default value for <b>key</b> will be returned. */
00099 QVariant
00100 VidaliaSettings::value(QString key)
00101 {
00102   return value(key, defaultValue(key));
00103 }
00104 
00105 /** Returns the value for <b>key</b>. If no value is currently saved, then
00106  * <b>defaultValue</b> will be returned. */
00107 QVariant
00108 VidaliaSettings::value(QString key, QVariant defaultValue)
00109 {
00110   return QSettings::value(key, defaultValue);
00111 }
00112 
00113 /** Resets all of Vidalia's settings. */
00114 void
00115 VidaliaSettings::reset()
00116 {
00117   QSettings settings(SETTINGS_FILE, QSettings::IniFormat);
00118   settings.clear();
00119 }
00120 
00121 /** Gets the currently preferred language code for Vidalia. */
00122 QString
00123 VidaliaSettings::getLanguageCode()
00124 {
00125   return value(SETTING_LANGUAGE).toString();
00126 }
00127 
00128 /** Sets the preferred language code. */
00129 void
00130 VidaliaSettings::setLanguageCode(QString languageCode)
00131 {
00132   setValue(SETTING_LANGUAGE, languageCode);
00133 }
00134 
00135 /** Gets the interface style key (e.g., "windows", "motif", etc.) */
00136 QString
00137 VidaliaSettings::getInterfaceStyle()
00138 {
00139   return value(SETTING_STYLE).toString();
00140 }
00141 
00142 /** Sets the interface style key. */
00143 void
00144 VidaliaSettings::setInterfaceStyle(QString styleKey)
00145 {
00146   setValue(SETTING_STYLE, styleKey);
00147 }
00148 
00149 /** Returns true if Tor is to be run when Vidalia starts. */
00150 bool
00151 VidaliaSettings::runTorAtStart()
00152 {
00153   return value(SETTING_RUN_TOR_AT_START).toBool();
00154 }
00155 
00156 /** If <b>run</b> is set to true, then Tor will be run when Vidalia starts. */
00157 void
00158 VidaliaSettings::setRunTorAtStart(bool run)
00159 {
00160   setValue(SETTING_RUN_TOR_AT_START, run);
00161 }
00162 
00163 /** Returns true if Vidalia is set to run on system boot. */
00164 bool
00165 VidaliaSettings::runVidaliaOnBoot()
00166 {
00167 #if defined(Q_WS_WIN)
00168   if (!win32_registry_get_key_value(STARTUP_REG_KEY, VIDALIA_REG_KEY).isEmpty()) {
00169     return true;
00170   } else {
00171     return false;
00172   }
00173 #else
00174   /* Platforms other than windows aren't supported yet */
00175   return false;
00176 #endif
00177 }
00178 
00179 /** If <b>run</b> is set to true, then Vidalia will run on system boot. */
00180 void
00181 VidaliaSettings::setRunVidaliaOnBoot(bool run)
00182 {
00183 #if defined(Q_WS_WIN)
00184   if (run) {
00185     win32_registry_set_key_value(STARTUP_REG_KEY, VIDALIA_REG_KEY,
00186         QString("\"" +
00187                 QDir::convertSeparators(QCoreApplication::applicationFilePath())) +
00188                 "\"");
00189   } else {
00190     win32_registry_remove_key(STARTUP_REG_KEY, VIDALIA_REG_KEY);
00191   }
00192 #else
00193   /* Platforms othe rthan windows aren't supported yet */
00194   Q_UNUSED(run);
00195   return;
00196 #endif
00197 }
00198 

Generated on Mon Oct 23 20:08:16 2006 for Vidalia by  doxygen 1.5.0