advancedpage.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.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to the
00008 **  terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file advancedpage.cpp
00013 ** \version $Id: advancedpage.cpp 3291 2008-11-10 01:18:11Z edmanm $
00014 ** \brief Advanced Tor and Vidalia configuration options
00015 */
00016 
00017 #include <QFile>
00018 #include <QFileInfo>
00019 #include <QHostAddress>
00020 #include <vmessagebox.h>
00021 #include <file.h>
00022 #include <vidalia.h>
00023 
00024 #include "ipvalidator.h"
00025 #include "advancedpage.h"
00026 
00027 #if defined(Q_WS_WIN)
00028 #include <torservice.h>
00029 #endif
00030 
00031 
00032 /** Constructor */
00033 AdvancedPage::AdvancedPage(QWidget *parent)
00034 : ConfigPage(parent, tr("Advanced"))
00035 {
00036   /* Invoke the Qt Designer generated object setup routine */
00037   ui.setupUi(this);
00038 
00039   /* Create TorSettings object */
00040   _settings = new TorSettings(Vidalia::torControl());
00041   
00042   /* Set validators for the control port and IP address fields */
00043   ui.lineControlAddress->setValidator(new IPValidator(this));
00044   ui.lineControlPort->setValidator(new QIntValidator(1, 65535, this));
00045   
00046   /* Bind event to actions */
00047   connect(ui.btnBrowseTorConfig, SIGNAL(clicked()), this, SLOT(browseTorConfig()));
00048   connect(ui.btnBrowseTorDataDirectory, SIGNAL(clicked()),
00049           this, SLOT(browseTorDataDirectory()));
00050   connect(ui.cmbAuthMethod, SIGNAL(currentIndexChanged(int)),
00051           this, SLOT(authMethodChanged(int)));
00052   connect(ui.chkRandomPassword, SIGNAL(toggled(bool)),
00053           ui.linePassword, SLOT(setDisabled(bool)));
00054 
00055   /* Hide platform specific features */
00056 #if defined(Q_WS_WIN)
00057 #if 0
00058   ui.grpService->setVisible(TorService::isSupported());
00059 #endif
00060 #endif
00061 }
00062 
00063 /** Destructor */
00064 AdvancedPage::~AdvancedPage()
00065 {
00066   delete _settings;
00067 }
00068 
00069 /** Applies the network configuration settings to Tor. Returns true if the
00070  * settings were applied successfully. Otherwise, <b>errmsg</b> is set
00071  * and false is returned. */
00072 bool
00073 AdvancedPage::apply(QString &errmsg)
00074 {
00075   return _settings->apply(&errmsg);
00076 }
00077 
00078 /** Reverts the Tor configuration settings to their values at the last
00079  * time they were successfully applied to Tor. */
00080 bool
00081 AdvancedPage::changedSinceLastApply()
00082 {
00083   return _settings->changedSinceLastApply();
00084 }
00085 
00086 /** Returns true if the user has changed their advanced Tor settings since
00087  * the last time they were applied to Tor. */
00088 void
00089 AdvancedPage::revert()
00090 {
00091   return _settings->revert();
00092 }
00093 
00094 /** Saves all settings for this page. */
00095 bool
00096 AdvancedPage::save(QString &errmsg)
00097 {
00098   /* Validate the control listener address */
00099   QHostAddress controlAddress(ui.lineControlAddress->text());
00100   if (controlAddress.isNull()) {
00101     errmsg = tr("'%1' is not a valid IP address.")
00102                .arg(ui.lineControlAddress->text());
00103     return false; 
00104   }
00105   
00106   /* Validate the selected authentication options */
00107   TorSettings::AuthenticationMethod authMethod = 
00108     indexToAuthMethod(ui.cmbAuthMethod->currentIndex());
00109   if (authMethod == TorSettings::PasswordAuth
00110         && ui.linePassword->text().isEmpty()
00111         && !ui.chkRandomPassword->isChecked()) {
00112     errmsg = tr("You selected 'Password' authentication, but did not "
00113                 "specify a password.");
00114     return false;
00115   }
00116  
00117   /* Only remember the torrc and datadir values if Vidalia started Tor, or
00118    * if the user changed the displayed values. */
00119   if (!Vidalia::torControl()->isVidaliaRunningTor()) {
00120     QString torrc = ui.lineTorConfig->text();
00121     if (torrc != _settings->getTorrc())
00122       _settings->setTorrc(torrc);
00123 
00124     QString dataDir = ui.lineTorDataDirectory->text();
00125     if (dataDir != _settings->getDataDirectory())
00126       _settings->setDataDirectory(dataDir);
00127   } else {
00128     _settings->setTorrc(ui.lineTorConfig->text());
00129     _settings->setDataDirectory(ui.lineTorDataDirectory->text());
00130   }
00131 
00132   _settings->setControlAddress(controlAddress);
00133   _settings->setControlPort(ui.lineControlPort->text().toUShort());
00134 
00135   _settings->setAuthenticationMethod(authMethod);
00136   _settings->setUseRandomPassword(ui.chkRandomPassword->isChecked());
00137   if (authMethod == TorSettings::PasswordAuth
00138         && !ui.chkRandomPassword->isChecked())
00139     _settings->setControlPassword(ui.linePassword->text());
00140 
00141 #if 0
00142 #if defined(Q_WS_WIN)
00143   /* Install or uninstall the Tor service as necessary */
00144   setupService(ui.chkUseService->isChecked());
00145 #endif
00146 #endif
00147 
00148   return true;
00149 }
00150 
00151 /** Loads previously saved settings. */
00152 void
00153 AdvancedPage::load()
00154 {
00155   ui.lineControlAddress->setText(_settings->getControlAddress().toString());
00156   ui.lineControlPort->setText(QString::number(_settings->getControlPort()));
00157   ui.lineTorConfig->setText(_settings->getTorrc());
00158   ui.lineTorDataDirectory->setText(_settings->getDataDirectory());
00159 
00160   ui.cmbAuthMethod->setCurrentIndex(
00161     authMethodToIndex(_settings->getAuthenticationMethod()));
00162   ui.chkRandomPassword->setChecked(_settings->useRandomPassword());
00163   if (!ui.chkRandomPassword->isChecked())
00164     ui.linePassword->setText(_settings->getControlPassword());
00165 
00166 #if 0
00167 #if defined(Q_WS_WIN)
00168   TorService s;
00169   ui.chkUseService->setChecked(s.isInstalled());
00170 #endif
00171 #endif
00172 }
00173 
00174 /** Called when the user selects a different authentication method from the
00175  * combo box. */
00176 void
00177 AdvancedPage::authMethodChanged(int index)
00178 {
00179   bool usePassword = (indexToAuthMethod(index) == TorSettings::PasswordAuth);
00180   ui.linePassword->setEnabled(usePassword && !ui.chkRandomPassword->isChecked());
00181   ui.chkRandomPassword->setEnabled(usePassword);
00182 }
00183 
00184 /** Returns the authentication method for the given <b>index</b>. */
00185 TorSettings::AuthenticationMethod
00186 AdvancedPage::indexToAuthMethod(int index)
00187 {
00188   switch (index) {
00189     case 0: return TorSettings::NullAuth;
00190     case 1: return TorSettings::CookieAuth;
00191     case 2: return TorSettings::PasswordAuth;
00192     default: break;
00193   }
00194   return TorSettings::UnknownAuth;
00195 }
00196 
00197 /** Returns the index in the authentication methods combo box for the given
00198  * authentication <b>method</b>. */
00199 int
00200 AdvancedPage::authMethodToIndex(TorSettings::AuthenticationMethod method)
00201 {
00202   switch (method) {
00203     case TorSettings::NullAuth: return 0;
00204     case TorSettings::CookieAuth: return 1;
00205     default: break;
00206   }
00207   return 2;
00208 }
00209 
00210 /** Open a QFileDialog to browse for Tor config file. */
00211 void
00212 AdvancedPage::browseTorConfig()
00213 {
00214   /* Prompt the user to select a file or create a new one */
00215   QString filename = QFileDialog::getOpenFileName(this, 
00216                        tr("Select Tor Configuration File"),
00217                        QFileInfo(ui.lineTorConfig->text()).fileName());
00218  
00219   /* Make sure a filename was selected */
00220   if (filename.isEmpty()) {
00221     return;
00222   }
00223 
00224   /* Check if the file exists */
00225   QFile torrcFile(filename);
00226   if (!QFileInfo(filename).exists()) {
00227     /* The given file does not exist. Should we create it? */
00228     int response = VMessageBox::question(this,
00229                      tr("File Not Found"),
00230                      tr("%1 does not exist. Would you like to create it?")
00231                                                             .arg(filename),
00232                      VMessageBox::Yes, VMessageBox::No);
00233     
00234     if (response == VMessageBox::No) {
00235       /* Don't create it. Just bail. */
00236       return;
00237     }
00238     /* Attempt to create the specified file */
00239     QString errmsg;
00240     if (!touch_file(filename, false, &errmsg)) {
00241       VMessageBox::warning(this,
00242         tr("Failed to Create File"),
00243         tr("Unable to create %1 [%2]").arg(filename)
00244                                       .arg(errmsg),
00245         VMessageBox::Ok);
00246       return;
00247     }
00248   }
00249   ui.lineTorConfig->setText(filename);
00250 }
00251 
00252 /** Opens a QFileDialog for the user to browse to or create a directory for
00253  * Tor's DataDirectory. */
00254 void
00255 AdvancedPage::browseTorDataDirectory()
00256 {
00257   QString dataDir = QFileDialog::getExistingDirectory(this,
00258                       tr("Select a Directory to Use for Tor Data"),
00259                       ui.lineTorDataDirectory->text());
00260   
00261   if (!dataDir.isEmpty()) 
00262     ui.lineTorDataDirectory->setText(dataDir);
00263 }
00264 
00265 #if 0
00266 #if defined(Q_WS_WIN)
00267 /** Installs or removes the Tor service as necessary. */
00268 void
00269 AdvancedPage::setupService(bool useService)
00270 {
00271   TorService service;
00272   bool isInstalled = service.isInstalled();
00273 
00274   if (!useService && isInstalled) {
00275     /* Uninstall if we don't want to use it anymore */
00276     Vidalia::torControl()->stop();
00277     
00278     if (!service.remove()) {
00279       VMessageBox::critical(this,
00280                             tr("Unable to remove Tor Service"),
00281                             tr("Vidalia was unable to remove the Tor service.\n\n"
00282                                "You may need to remove it manually."), 
00283                             VMessageBox::Ok, VMessageBox::Cancel);
00284     }
00285   } else if (useService && !isInstalled) {
00286     /* Install if we want to start using a service */
00287     if (!service.install(_settings->getExecutable(),
00288                          _settings->getTorrc(),
00289                          _settings->getControlPort())) {
00290       VMessageBox::critical(this,
00291                             tr("Unable to install Tor Service"),
00292                             tr("Vidalia was unable to install the Tor service."),
00293                             VMessageBox::Ok, VMessageBox::Cancel);
00294     }
00295   }
00296 }
00297 #endif
00298 #endif
00299 

Generated on Tue Jul 7 16:58:26 2009 for Vidalia by  doxygen 1.4.7