advancedpage.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 advancedpage.cpp
00024  * \version $Id: advancedpage.cpp 1238 2006-09-25 17:50:57Z edmanm $
00025  * \brief Advanced Tor and Vidalia configuration options
00026  */
00027 
00028 #include <QFile>
00029 #include <QFileInfo>
00030 #include <gui/common/vmessagebox.h>
00031 #include <util/file.h>
00032 #include <vidalia.h>
00033 #include "advancedpage.h"
00034 
00035 #if defined(Q_WS_WIN)
00036 #include <control/torservice.h>
00037 #endif
00038 
00039 
00040 /** Constructor */
00041 AdvancedPage::AdvancedPage(QWidget *parent)
00042 : ConfigPage(parent)
00043 {
00044   /* Invoke the Qt Designer generated object setup routine */
00045   ui.setupUi(this);
00046 
00047   /* Create TorSettings object */
00048   _settings = new TorSettings();
00049 
00050   /* Bind event to actions */
00051   connect(ui.btnBrowseTorConfig, SIGNAL(clicked()), this, SLOT(browseTorConfig()));
00052 
00053   /* Hide platform specific features */
00054 #if defined(Q_WS_WIN)
00055   ui.grpPermissions->setVisible(false);
00056   ui.grpService->setVisible(TorService::isSupported());
00057 #endif
00058 }
00059 
00060 /** Destructor */
00061 AdvancedPage::~AdvancedPage()
00062 {
00063   delete _settings;
00064 }
00065 
00066 /** Saves all settings for this page. */
00067 bool
00068 AdvancedPage::save(QString &errmsg)
00069 {
00070   Q_UNUSED(errmsg);
00071   _settings->setControlPort(ui.lineControlPort->text().toUShort());
00072   _settings->setTorrc(ui.lineTorConfig->text());
00073   _settings->setUser(ui.lineUser->text());
00074   _settings->setGroup(ui.lineGroup->text());
00075   
00076 #if defined(Q_WS_WIN)
00077   /* Install or uninstall the Tor service as necessary */
00078   setupService();
00079 #endif
00080 
00081   return true;
00082 }
00083 
00084 /** Loads previously saved settings. */
00085 void
00086 AdvancedPage::load()
00087 {
00088   ui.lineControlPort->setText(QString::number(_settings->getControlPort()));
00089   ui.lineTorConfig->setText(_settings->getTorrc());
00090   ui.lineUser->setText(_settings->getUser());
00091   ui.lineGroup->setText(_settings->getGroup());
00092 
00093 #if defined(Q_WS_WIN)
00094   ui.chkUseService->setChecked(useService());
00095 #endif
00096 }
00097 
00098 
00099 
00100 /** Open a QFileDialog to browse for Tor config file. */
00101 void
00102 AdvancedPage::browseTorConfig()
00103 {
00104   /* Prompt the user to select a file or create a new one */
00105   QString filename = QFileDialog::getOpenFileName(this, 
00106                        tr("Select Tor Configuration File"),
00107                        QFileInfo(ui.lineTorConfig->text()).fileName());
00108  
00109   /* Make sure a filename was selected */
00110   if (filename.isEmpty()) {
00111     return;
00112   }
00113 
00114   /* Check if the file exists */
00115   QFile torrcFile(filename);
00116   if (!QFileInfo(filename).exists()) {
00117     /* The given file does not exist. Should we create it? */
00118     int response = VMessageBox::question(this,
00119                      tr("File Not Found"),
00120                      tr("%1 does not exist. Would you like to create it?")
00121                                                             .arg(filename),
00122                      VMessageBox::Yes, VMessageBox::No);
00123     
00124     if (response == VMessageBox::No) {
00125       /* Don't create it. Just bail. */
00126       return;
00127     }
00128     /* Attempt to create the specified file */
00129     QString errmsg;
00130     if (!touch_file(filename, false, &errmsg)) {
00131       VMessageBox::warning(this,
00132         tr("Failed to Create File"),
00133         tr("Unable to create %1 [%2]").arg(filename)
00134                                       .arg(errmsg),
00135         VMessageBox::Ok);
00136       return;
00137     }
00138   }
00139   ui.lineTorConfig->setText(filename);
00140 }
00141 
00142 #if defined(Q_WS_WIN)
00143 /** Returns true if service support is enabled and functional */
00144 bool
00145 AdvancedPage::useService()
00146 {
00147   bool use = false;
00148 
00149   /* If we think we're supposed to be using a service we'd better make
00150      sure that the service still actually exists since the last time we checked.
00151   */
00152   
00153   if (_settings->getUseService()) {
00154     TorService* s = new TorService(_settings->getExecutable(),
00155                                    _settings->getTorrc());
00156     use = s->isInstalled();
00157     delete s;
00158 
00159     /* No point in trying to use a broken or non-existent service */
00160     if (!use) _settings->setUseService(false);
00161   }
00162   return use;
00163 }
00164 
00165 /** Installs or removes the Tor service as necessary. */
00166 void
00167 AdvancedPage::setupService()
00168 {
00169   bool checked = ui.chkUseService->isChecked();
00170   TorService* s = new TorService(_settings->getExecutable(),
00171                                  _settings->getTorrc());
00172 
00173   if (!checked && s->isInstalled()) {
00174     /* Uninstall if we don't want to use it anymore */
00175     Vidalia::torControl()->stop();
00176     
00177     if (!s->remove()) {
00178       VMessageBox::critical(this,
00179                             tr("Unable to remove Tor Service"),
00180                             tr("Vidalia was unable to remove the Tor service.\n\n"
00181                                "You may need to remove it manually."), 
00182                             VMessageBox::Ok, VMessageBox::Cancel);
00183     }
00184     _settings->setUseService(false);
00185 
00186   } else if (checked && !s->isInstalled()) {
00187     /* Install if we want to start using a service */
00188     if (!s->install()) {
00189       VMessageBox::critical(this,
00190                             tr("Unable to install Tor Service"),
00191                             tr("Vidalia was unable to install the Tor service."),
00192                             VMessageBox::Ok, VMessageBox::Cancel);
00193     }
00194     _settings->setUseService(s->isInstalled());
00195   } else {
00196     _settings->setUseService(checked);
00197   }
00198 
00199   delete s;
00200 }
00201 #endif
00202 

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