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 1680 2007-03-19 02:42:30Z 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 #define SETTING_SHOW_MAINWINDOW_AT_START  "ShowMainWindowAtStart"
00045 
00046 /* Default Vidalia Settings */
00047 #if defined(Q_WS_MAC)
00048 #define DEFAULT_STYLE               "macintosh (aqua)"
00049 #else
00050 #define DEFAULT_STYLE               "plastique"
00051 #endif
00052 
00053 #if defined(Q_OS_WIN32)
00054 #define STARTUP_REG_KEY        "Software\\Microsoft\\Windows\\CurrentVersion\\Run"
00055 #define VIDALIA_REG_KEY        "Vidalia" 
00056 #endif
00057 
00058 /** The location of Vidalia's settings and configuration file. */
00059 #define SETTINGS_FILE   (Vidalia::dataDirectory() + "/vidalia.conf")
00060 
00061 
00062 /** Default Constructor */
00063 VidaliaSettings::VidaliaSettings()
00064 : QSettings(SETTINGS_FILE, QSettings::IniFormat)
00065 {
00066   setDefault(SETTING_LANGUAGE, LanguageSupport::defaultLanguageCode());
00067   setDefault(SETTING_RUN_TOR_AT_START, true);
00068   setDefault(SETTING_STYLE, DEFAULT_STYLE);
00069   setDefault(SETTING_SHOW_MAINWINDOW_AT_START, true);
00070 }
00071 
00072 /** Sets the default value of <b>key</b> to be <b>val</b>. */
00073 void
00074 VidaliaSettings::setDefault(QString key, QVariant val)
00075 {
00076   _defaults.insert(key, val);
00077 }
00078 
00079 /** Returns the default value for <b>key</b>. */
00080 QVariant
00081 VidaliaSettings::defaultValue(QString key)
00082 {
00083   if (_defaults.contains(key)) {
00084     return _defaults.value(key);
00085   }
00086   return QVariant();
00087 }
00088 
00089 /** Save <b>val</b> to the configuration file for the setting <b>key</b>, if
00090  * <b>val</b> is different than <b>key</b>'s current value. */
00091 void
00092 VidaliaSettings::setValue(QString key, QVariant val)
00093 {
00094   if (value(key) != val) {
00095     QSettings::setValue(key, val);
00096   }
00097 }
00098 
00099 /** Returns the value for <b>key</b>. If no value is currently saved, then the
00100  * default value for <b>key</b> will be returned. */
00101 QVariant
00102 VidaliaSettings::value(QString key)
00103 {
00104   return value(key, defaultValue(key));
00105 }
00106 
00107 /** Returns the value for <b>key</b>. If no value is currently saved, then
00108  * <b>defaultValue</b> will be returned. */
00109 QVariant
00110 VidaliaSettings::value(QString key, QVariant defaultValue)
00111 {
00112   return QSettings::value(key, defaultValue);
00113 }
00114 
00115 /** Resets all of Vidalia's settings. */
00116 void
00117 VidaliaSettings::reset()
00118 {
00119   QSettings settings(SETTINGS_FILE, QSettings::IniFormat);
00120   settings.clear();
00121 }
00122 
00123 /** Gets the currently preferred language code for Vidalia. */
00124 QString
00125 VidaliaSettings::getLanguageCode()
00126 {
00127   return value(SETTING_LANGUAGE).toString();
00128 }
00129 
00130 /** Sets the preferred language code. */
00131 void
00132 VidaliaSettings::setLanguageCode(QString languageCode)
00133 {
00134   setValue(SETTING_LANGUAGE, languageCode);
00135 }
00136 
00137 /** Gets the interface style key (e.g., "windows", "motif", etc.) */
00138 QString
00139 VidaliaSettings::getInterfaceStyle()
00140 {
00141   return value(SETTING_STYLE).toString();
00142 }
00143 
00144 /** Sets the interface style key. */
00145 void
00146 VidaliaSettings::setInterfaceStyle(QString styleKey)
00147 {
00148   setValue(SETTING_STYLE, styleKey);
00149 }
00150 
00151 /** Returns true if Tor is to be run when Vidalia starts. */
00152 bool
00153 VidaliaSettings::runTorAtStart()
00154 {
00155   return value(SETTING_RUN_TOR_AT_START).toBool();
00156 }
00157 
00158 /** If <b>run</b> is set to true, then Tor will be run when Vidalia starts. */
00159 void
00160 VidaliaSettings::setRunTorAtStart(bool run)
00161 {
00162   setValue(SETTING_RUN_TOR_AT_START, run);
00163 }
00164 
00165 /** Returns true if Vidalia's main window should be visible when the
00166  * application starts. */
00167 bool
00168 VidaliaSettings::showMainWindowAtStart()
00169 {
00170   return value(SETTING_SHOW_MAINWINDOW_AT_START).toBool();
00171 }
00172 
00173 /** Sets whether to show Vidalia's main window when the application starts. */
00174 void
00175 VidaliaSettings::setShowMainWindowAtStart(bool show)
00176 {
00177   setValue(SETTING_SHOW_MAINWINDOW_AT_START, show);
00178 }
00179 
00180 
00181 /** Returns true if Vidalia is set to run on system boot. */
00182 bool
00183 VidaliaSettings::runVidaliaOnBoot()
00184 {
00185 #if defined(Q_WS_WIN)
00186   if (!win32_registry_get_key_value(STARTUP_REG_KEY, VIDALIA_REG_KEY).isEmpty()) {
00187     return true;
00188   } else {
00189     return false;
00190   }
00191 #else
00192   /* Platforms other than windows aren't supported yet */
00193   return false;
00194 #endif
00195 }
00196 
00197 /** If <b>run</b> is set to true, then Vidalia will run on system boot. */
00198 void
00199 VidaliaSettings::setRunVidaliaOnBoot(bool run)
00200 {
00201 #if defined(Q_WS_WIN)
00202   if (run) {
00203     win32_registry_set_key_value(STARTUP_REG_KEY, VIDALIA_REG_KEY,
00204         QString("\"" +
00205                 QDir::convertSeparators(QCoreApplication::applicationFilePath())) +
00206                 "\"");
00207   } else {
00208     win32_registry_remove_key(STARTUP_REG_KEY, VIDALIA_REG_KEY);
00209   }
00210 #else
00211   /* Platforms othe rthan windows aren't supported yet */
00212   Q_UNUSED(run);
00213   return;
00214 #endif
00215 }
00216 

Generated on Wed Sep 5 15:49:28 2007 for Vidalia by  doxygen 1.5.3