Vidalia  0.2.17
VidaliaSettings.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.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 VidaliaSettings.cpp
00013 ** \brief General Vidalia settings, such as language and interface style
00014 */
00015 
00016 #include "VidaliaSettings.h"
00017 #include "LanguageSupport.h"
00018 #include "Vidalia.h"
00019 #if defined(Q_WS_WIN)
00020 #include "win32.h"
00021 #endif
00022 
00023 #include <QDir>
00024 #include <QCoreApplication>
00025 #include <QStyleFactory>
00026 
00027 #define SETTING_LANGUAGE            "LanguageCode"
00028 #define SETTING_STYLE               "InterfaceStyle"
00029 #define SETTING_RUN_TOR_AT_START    "RunTorAtStart"
00030 #define SETTING_DATA_DIRECTORY      "DataDirectory"
00031 #define SETTING_SHOW_MAINWINDOW_AT_START  "ShowMainWindowAtStart"
00032 #define SETTING_BROWSER_EXECUTABLE  "BrowserExecutable"
00033 #define SETTING_BROWSER_DIRECTORY   "BrowserDirectory"
00034 #define SETTING_IM_EXECUTABLE       "IMExecutable"
00035 #define SETTING_RUN_PROXY_AT_START  "RunProxyAtStart"
00036 #define SETTING_PROXY_EXECUTABLE    "ProxyExecutable"
00037 #define SETTING_PROXY_EXECUTABLE_ARGUMENTS  "ProxyExecutableArguments"
00038 #define SETTING_CHECK_FOR_UPDATES   "CheckForUpdates"
00039 #define SETTING_LAST_UPDATE_CHECK   "LastUpdateCheck"
00040 #define SETTING_USE_LOCAL_GEOIP_DATABASE  "UseLocalGeoIpDatabase"
00041 #define SETTING_LOCAL_GEOIP_DATABASE "LocalGeoIpDatabase"
00042 #define SETTING_SKIP_VERSION_CHECK  "SkipVersionCheck"
00043 
00044 #if defined(Q_OS_WIN32)
00045 #define STARTUP_REG_KEY        "Software\\Microsoft\\Windows\\CurrentVersion\\Run"
00046 #define VIDALIA_REG_KEY        "Vidalia" 
00047 #endif
00048 
00049 #define SETTING_ICON_PREF       "IconDisplayPreference"
00050 
00051 /** Default Constructor */
00052 VidaliaSettings::VidaliaSettings()
00053 {
00054 #if defined(Q_WS_MAC)
00055   setDefault(SETTING_STYLE, "macintosh (aqua)");
00056 #else
00057   static QStringList styles = QStyleFactory::keys();
00058 #if defined(Q_WS_WIN)
00059   if (styles.contains("windowsvista", Qt::CaseInsensitive))
00060     setDefault(SETTING_STYLE, "windowsvista");
00061   else
00062 #endif
00063   {
00064     if (styles.contains("cleanlooks", Qt::CaseInsensitive))
00065       setDefault(SETTING_STYLE, "cleanlooks");
00066     else
00067       setDefault(SETTING_STYLE, "plastique");
00068   }
00069 #endif
00070 
00071   setDefault(SETTING_LANGUAGE, LanguageSupport::defaultLanguageCode());
00072   setDefault(SETTING_RUN_TOR_AT_START, true);
00073   setDefault(SETTING_SHOW_MAINWINDOW_AT_START, true);
00074   setDefault(SETTING_BROWSER_EXECUTABLE, "");
00075   setDefault(SETTING_IM_EXECUTABLE, "");
00076   setDefault(SETTING_RUN_PROXY_AT_START, false);
00077   setDefault(SETTING_PROXY_EXECUTABLE, "");
00078   setDefault(SETTING_PROXY_EXECUTABLE_ARGUMENTS, QString());
00079 #if defined(Q_WS_WIN)
00080   setDefault(SETTING_CHECK_FOR_UPDATES, true);
00081 #else
00082   setDefault(SETTING_CHECK_FOR_UPDATES, false);
00083 #endif
00084   setDefault(SETTING_LAST_UPDATE_CHECK, QDateTime());
00085   setDefault(SETTING_USE_LOCAL_GEOIP_DATABASE, false);
00086   setDefault(SETTING_LOCAL_GEOIP_DATABASE, "");
00087   setDefault(SETTING_ICON_PREF, Both);
00088   setDefault(SETTING_SKIP_VERSION_CHECK, false);
00089 }
00090 
00091 /** Gets the currently preferred language code for Vidalia. */
00092 QString
00093 VidaliaSettings::getLanguageCode()
00094 {
00095   return value(SETTING_LANGUAGE).toString();
00096 }
00097 
00098 /** Sets the preferred language code. */
00099 void
00100 VidaliaSettings::setLanguageCode(QString languageCode)
00101 {
00102   setValue(SETTING_LANGUAGE, languageCode);
00103 }
00104 
00105 /** Gets the interface style key (e.g., "windows", "motif", etc.) */
00106 QString
00107 VidaliaSettings::getInterfaceStyle()
00108 {
00109   return value(SETTING_STYLE).toString();
00110 }
00111 
00112 /** Sets the interface style key. */
00113 void
00114 VidaliaSettings::setInterfaceStyle(QString styleKey)
00115 {
00116   setValue(SETTING_STYLE, styleKey);
00117 }
00118 
00119 /** Returns true if Tor is to be run when Vidalia starts. */
00120 bool
00121 VidaliaSettings::runTorAtStart()
00122 {
00123   return value(SETTING_RUN_TOR_AT_START).toBool();
00124 }
00125 
00126 /** If <b>run</b> is set to true, then Tor will be run when Vidalia starts. */
00127 void
00128 VidaliaSettings::setRunTorAtStart(bool run)
00129 {
00130   setValue(SETTING_RUN_TOR_AT_START, run);
00131 }
00132 
00133 /** Returns true if Vidalia's main window should be visible when the
00134  * application starts. */
00135 bool
00136 VidaliaSettings::showMainWindowAtStart()
00137 {
00138   return value(SETTING_SHOW_MAINWINDOW_AT_START).toBool();
00139 }
00140 
00141 /** Sets whether to show Vidalia's main window when the application starts. */
00142 void
00143 VidaliaSettings::setShowMainWindowAtStart(bool show)
00144 {
00145   setValue(SETTING_SHOW_MAINWINDOW_AT_START, show);
00146 }
00147 
00148 
00149 /** Returns true if Vidalia is set to run on system boot. */
00150 bool
00151 VidaliaSettings::runVidaliaOnBoot()
00152 {
00153 #if defined(Q_WS_WIN)
00154   if (!win32_registry_get_key_value(STARTUP_REG_KEY, VIDALIA_REG_KEY).isEmpty()) {
00155     return true;
00156   } else {
00157     return false;
00158   }
00159 #else
00160   /* Platforms other than windows aren't supported yet */
00161   return false;
00162 #endif
00163 }
00164 
00165 /** If <b>run</b> is set to true, then Vidalia will run on system boot. */
00166 void
00167 VidaliaSettings::setRunVidaliaOnBoot(bool run)
00168 {
00169 #if defined(Q_WS_WIN)
00170   if (run) {
00171     win32_registry_set_key_value(STARTUP_REG_KEY, VIDALIA_REG_KEY,
00172         QString("\"" +
00173                 QDir::convertSeparators(QCoreApplication::applicationFilePath())) +
00174                 "\"");
00175   } else {
00176     win32_registry_remove_key(STARTUP_REG_KEY, VIDALIA_REG_KEY);
00177   }
00178 #else
00179   /* Platforms othe rthan windows aren't supported yet */
00180   Q_UNUSED(run);
00181   return;
00182 #endif
00183 }
00184 
00185 /** If browserDirectory is empty, returns a fully-qualified path to
00186  * the web browser, including the executable name. If browserDirectory
00187  * is set, then returns the basename of the configured web browser */
00188 QString
00189 VidaliaSettings::getBrowserExecutable() const
00190 {
00191   return QDir::convertSeparators(value(SETTING_BROWSER_EXECUTABLE).toString());
00192 }
00193 
00194 /** Sets the location and name of the web browser executable to the given string.
00195  * If set to the empty string, the browser will not be started. */
00196 void
00197 VidaliaSettings::setBrowserExecutable(const QString &browserExecutable)
00198 {
00199   setValue(SETTING_BROWSER_EXECUTABLE, browserExecutable);
00200 }
00201 
00202 /** Returns a fully-qualified path to the web browser directory */
00203 QString
00204 VidaliaSettings::getBrowserDirectory() const
00205 {
00206   return QDir::convertSeparators(value(SETTING_BROWSER_DIRECTORY).toString());
00207 }
00208 
00209 /** Sets the location and name of the web browser directory to the given string.
00210  * If set to the empty string, the browser will not be started. */
00211 void
00212 VidaliaSettings::setBrowserDirectory(const QString &browserDirectory)
00213 {
00214   setValue(SETTING_BROWSER_DIRECTORY, browserDirectory);
00215 }
00216 
00217 /** Returns a fully-qualified path to the IM client, including the
00218  * executable name. */
00219 QString
00220 VidaliaSettings::getIMExecutable() const
00221 {
00222   return QDir::convertSeparators(value(SETTING_IM_EXECUTABLE).toString());
00223 }
00224 
00225 /** Sets the location and name of the IM client executable to the given string.
00226  * If set to the empty string, the client will not be started. */
00227 void
00228 VidaliaSettings::setIMExecutable(const QString &IMExecutable)
00229 {
00230   setValue(SETTING_IM_EXECUTABLE, IMExecutable);
00231 }
00232 
00233 /** Returns true if Vidalia should start a proxy application when it
00234  * starts. */
00235 bool
00236 VidaliaSettings::runProxyAtStart()
00237 {
00238   return value(SETTING_RUN_PROXY_AT_START).toBool();
00239 }
00240 
00241 /** Set whether to run a proxy application when Vidalia starts. */
00242 void
00243 VidaliaSettings::setRunProxyAtStart(bool run)
00244 {
00245   setValue(SETTING_RUN_PROXY_AT_START, run);
00246 }
00247 
00248 /** Returns a fully-qualified path to the proxy server, including the
00249  * executable name. */
00250 QString
00251 VidaliaSettings::getProxyExecutable() const
00252 {
00253   return QDir::convertSeparators(value(SETTING_PROXY_EXECUTABLE).toString());
00254 }
00255 
00256 /** Sets the location and name of the proxy server executable to the given
00257  * string. If set to the empty string, the proxy will not be started. */
00258 void
00259 VidaliaSettings::setProxyExecutable(const QString &proxyExecutable)
00260 {
00261   setValue(SETTING_PROXY_EXECUTABLE, proxyExecutable);
00262 }
00263 
00264 /** Returns a string containing additional command line arguments to be passed
00265  * to ProxyExecutable */
00266 QString
00267 VidaliaSettings::getProxyExecutableArguments() const
00268 {
00269   return value(SETTING_PROXY_EXECUTABLE_ARGUMENTS).toString();
00270 }
00271 
00272 /** Sets the additional arguments to be passed to Proxy Executable */
00273 void
00274 VidaliaSettings::setProxyExecutableArguments(const QString
00275                                              &proxyExecutableArguments)
00276 {
00277   setValue(SETTING_PROXY_EXECUTABLE_ARGUMENTS, proxyExecutableArguments);
00278 }
00279 
00280 bool
00281 VidaliaSettings::isAutoUpdateEnabled() const
00282 {
00283   return value(SETTING_CHECK_FOR_UPDATES).toBool();
00284 }
00285 
00286 void
00287 VidaliaSettings::setAutoUpdateEnabled(bool enabled)
00288 {
00289   setValue(SETTING_CHECK_FOR_UPDATES, enabled);
00290 }
00291 
00292 QDateTime
00293 VidaliaSettings::lastCheckedForUpdates() const
00294 {
00295   return value(SETTING_LAST_UPDATE_CHECK).toDateTime();
00296 }
00297 
00298 void
00299 VidaliaSettings::setLastCheckedForUpdates(const QDateTime &checkedAt)
00300 {
00301   setValue(SETTING_LAST_UPDATE_CHECK, checkedAt);
00302 }
00303 
00304 bool
00305 VidaliaSettings::useLocalGeoIpDatabase() const
00306 {
00307   return value(SETTING_USE_LOCAL_GEOIP_DATABASE).toBool();
00308 }
00309 
00310 void
00311 VidaliaSettings::setUseLocalGeoIpDatabase(bool enabled)
00312 {
00313   setValue(SETTING_USE_LOCAL_GEOIP_DATABASE, enabled);
00314 }
00315 
00316 QString
00317 VidaliaSettings::localGeoIpDatabase() const
00318 {
00319   return QDir::convertSeparators(value(SETTING_LOCAL_GEOIP_DATABASE).toString());
00320 }
00321 
00322 void
00323 VidaliaSettings::setLocalGeoIpDatabase(const QString &databaseFile)
00324 {
00325   setValue(SETTING_LOCAL_GEOIP_DATABASE, databaseFile);
00326 }
00327 
00328 /** Get the icon preference */
00329 VidaliaSettings::IconPosition
00330 VidaliaSettings::getIconPref()
00331 {
00332   return fromString(value(SETTING_ICON_PREF).toString());
00333 }
00334 
00335 /** Set the icon preference */
00336 void
00337 VidaliaSettings::setIconPref(const IconPosition iconPref)
00338 {
00339   setValue(SETTING_ICON_PREF, toString(iconPref));
00340 }
00341 
00342 QString
00343 VidaliaSettings::toString(const IconPosition iconPref)
00344 {
00345   switch(iconPref) {
00346   case Dock: return "Dock";
00347   case Tray: return "Tray";
00348   default: return "Both";
00349   }
00350 }
00351 
00352 VidaliaSettings::IconPosition
00353 VidaliaSettings::fromString(QString iconPref)
00354 {
00355   if(iconPref == "Dock") return Dock;
00356   if(iconPref == "Tray") return Tray;
00357 
00358   return Both;
00359 }
00360 
00361 bool
00362 VidaliaSettings::skipVersionCheck() const
00363 {
00364   return value(SETTING_SKIP_VERSION_CHECK).toBool();
00365 }