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