00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00033 AdvancedPage::AdvancedPage(QWidget *parent)
00034 : ConfigPage(parent, tr("Advanced"))
00035 {
00036
00037 ui.setupUi(this);
00038
00039
00040 _settings = new TorSettings(Vidalia::torControl());
00041
00042
00043 ui.lineControlAddress->setValidator(new IPValidator(this));
00044 ui.lineControlPort->setValidator(new QIntValidator(1, 65535, this));
00045
00046
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
00056 #if defined(Q_WS_WIN)
00057 #if 0
00058 ui.grpService->setVisible(TorService::isSupported());
00059 #endif
00060 #endif
00061 }
00062
00063
00064 AdvancedPage::~AdvancedPage()
00065 {
00066 delete _settings;
00067 }
00068
00069
00070
00071
00072 bool
00073 AdvancedPage::apply(QString &errmsg)
00074 {
00075 return _settings->apply(&errmsg);
00076 }
00077
00078
00079
00080 bool
00081 AdvancedPage::changedSinceLastApply()
00082 {
00083 return _settings->changedSinceLastApply();
00084 }
00085
00086
00087
00088 void
00089 AdvancedPage::revert()
00090 {
00091 return _settings->revert();
00092 }
00093
00094
00095 bool
00096 AdvancedPage::save(QString &errmsg)
00097 {
00098
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
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
00118
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
00144 setupService(ui.chkUseService->isChecked());
00145 #endif
00146 #endif
00147
00148 return true;
00149 }
00150
00151
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
00175
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
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
00198
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
00211 void
00212 AdvancedPage::browseTorConfig()
00213 {
00214
00215 QString filename = QFileDialog::getOpenFileName(this,
00216 tr("Select Tor Configuration File"),
00217 QFileInfo(ui.lineTorConfig->text()).fileName());
00218
00219
00220 if (filename.isEmpty()) {
00221 return;
00222 }
00223
00224
00225 QFile torrcFile(filename);
00226 if (!QFileInfo(filename).exists()) {
00227
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
00236 return;
00237 }
00238
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
00253
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
00268 void
00269 AdvancedPage::setupService(bool useService)
00270 {
00271 TorService service;
00272 bool isInstalled = service.isInstalled();
00273
00274 if (!useService && isInstalled) {
00275
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
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