Improve Tor support
[novacoin.git] / src / qt / optionsdialog.cpp
1 #include "optionsdialog.h"
2 #include "ui_optionsdialog.h"
3
4 #include "bitcoinunits.h"
5 #include "monitoreddatamapper.h"
6 #include "netbase.h"
7 #include "optionsmodel.h"
8 #include "dialogwindowflags.h"
9
10 #include <QDir>
11 #include <QIntValidator>
12 #include <QLocale>
13 #include <QMessageBox>
14 #include <QRegExp>
15 #include <QRegExpValidator>
16
17 OptionsDialog::OptionsDialog(QWidget *parent) :
18     QDialog(parent, DIALOGWINDOWHINTS),
19     ui(new Ui::OptionsDialog),
20     model(0),
21     mapper(0),
22     fRestartWarningDisplayed_Proxy(false),
23     fRestartWarningDisplayed_Tor(false),
24     fRestartWarningDisplayed_Lang(false),
25     fRestartWarningDisplayed_URL(false),
26     fProxyIpValid(true),
27     fTorIpValid(true)
28 {
29     ui->setupUi(this);
30
31     /* Network elements init */
32 #ifndef USE_UPNP
33     ui->mapPortUpnp->setEnabled(false);
34 #endif
35
36     ui->proxyIp->setEnabled(false);
37     ui->proxyPort->setEnabled(false);
38     ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
39
40     ui->torIp->setEnabled(false);
41     ui->torPort->setEnabled(false);
42     ui->torPort->setValidator(new QIntValidator(1, 65535, this));
43     ui->TorOnly->setEnabled(false);
44     ui->torName->setEnabled(false);
45
46     ui->socksVersion->setEnabled(false);
47     ui->socksVersion->addItem("5", 5);
48     ui->socksVersion->addItem("4", 4);
49     ui->socksVersion->setCurrentIndex(0);
50
51     connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyIp, SLOT(setEnabled(bool)));
52     connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyPort, SLOT(setEnabled(bool)));
53     connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->socksVersion, SLOT(setEnabled(bool)));
54     connect(ui->connectSocks, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning_Proxy()));
55
56     connect(ui->connectTor, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning_Tor()));
57     connect(ui->connectTor, SIGNAL(toggled(bool)), ui->torIp, SLOT(setEnabled(bool)));
58     connect(ui->connectTor, SIGNAL(toggled(bool)), ui->torPort, SLOT(setEnabled(bool)));
59     connect(ui->connectTor, SIGNAL(toggled(bool)), ui->TorOnly, SLOT(setEnabled(bool)));
60     connect(ui->connectTor, SIGNAL(toggled(bool)), ui->torName, SLOT(setEnabled(bool)));
61     connect(ui->TorOnly, SIGNAL(toggled(bool)), ui->connectSocks, SLOT(setDisabled(bool)));
62
63     ui->proxyIp->installEventFilter(this);
64     ui->torIp->installEventFilter(this);
65
66     /* Window elements init */
67 #ifdef Q_OS_MAC
68     ui->tabWindow->setVisible(false);
69 #endif
70
71     /* Display elements init */
72     QDir translations(":translations");
73     ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
74     foreach(const QString &langStr, translations.entryList())
75     {
76         QLocale locale(langStr);
77
78         /** check if the locale name consists of 2 parts (language_country) */
79         if(langStr.contains("_"))
80         {
81 #if QT_VERSION >= 0x040800
82             /** display language strings as "native language - native country (locale name)", e.g. "Deutsch - Deutschland (de)" */
83             ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
84 #else
85             /** display language strings as "language - country (locale name)", e.g. "German - Germany (de)" */
86             ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" - ") + QLocale::countryToString(locale.country()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
87 #endif
88         }
89         else
90         {
91 #if QT_VERSION >= 0x040800
92             /** display language strings as "native language (locale name)", e.g. "Deutsch (de)" */
93             ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
94 #else
95             /** display language strings as "language (locale name)", e.g. "German (de)" */
96             ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
97 #endif
98         }
99     }
100
101 #if QT_VERSION >= 0x040700
102     ui->thirdPartyTxUrls->setPlaceholderText("https://example.com/tx/%s");
103 #endif
104
105
106     ui->unit->setModel(new BitcoinUnits(this));
107
108     /* Widget-to-option mapper */
109     mapper = new MonitoredDataMapper(this);
110     mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
111     mapper->setOrientation(Qt::Vertical);
112
113     /* enable apply button when data modified */
114     connect(mapper, SIGNAL(viewModified()), this, SLOT(enableApplyButton()));
115     /* disable apply button when new data loaded */
116     connect(mapper, SIGNAL(currentIndexChanged(int)), this, SLOT(disableApplyButton()));
117     /* setup/change UI elements when proxy IP is invalid/valid */
118     connect(this, SIGNAL(proxyIpValid(QValidatedLineEdit *, bool)), this, SLOT(handleProxyIpValid(QValidatedLineEdit *, bool)));
119     /* setup/change UI elements when Tor IP is invalid/valid */
120     connect(this, SIGNAL(torIpValid(QValidatedLineEdit *, bool)), this, SLOT(handleTorIpValid(QValidatedLineEdit *, bool)));
121 }
122
123 OptionsDialog::~OptionsDialog()
124 {
125     delete ui;
126 }
127
128 void OptionsDialog::setModel(OptionsModel *model)
129 {
130     this->model = model;
131
132     if(model)
133     {
134         connect(model, SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
135
136         mapper->setModel(model);
137         setMapper();
138         mapper->toFirst();
139     }
140
141     /* update the display unit, to not use the default ("BTC") */
142     updateDisplayUnit();
143
144     /* warn only when language selection changes by user action (placed here so init via mapper doesn't trigger this) */
145     connect(ui->lang, SIGNAL(valueChanged()), this, SLOT(showRestartWarning_Lang()));
146     connect(ui->thirdPartyTxUrls, SIGNAL(textChanged(const QString &)), this, SLOT(showRestartWarning_URL()));
147
148     /* disable apply button after settings are loaded as there is nothing to save */
149     disableApplyButton();
150 }
151
152 void OptionsDialog::setMapper()
153 {
154     /* Main */
155     mapper->addMapping(ui->transactionFee, OptionsModel::Fee);
156     mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
157     mapper->addMapping(ui->detachDatabases, OptionsModel::DetachDatabases);
158
159     /* Network */
160     mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
161
162     mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
163     mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
164     mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
165     mapper->addMapping(ui->socksVersion, OptionsModel::ProxySocksVersion);
166
167     mapper->addMapping(ui->connectTor, OptionsModel::TorUse);
168     mapper->addMapping(ui->torIp, OptionsModel::TorIP);
169     mapper->addMapping(ui->torPort, OptionsModel::TorPort);
170     mapper->addMapping(ui->TorOnly, OptionsModel::TorOnly);
171     mapper->addMapping(ui->torName, OptionsModel::TorName);
172
173
174     /* Window */
175 #ifndef Q_OS_MAC
176     mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
177     mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
178 #endif
179
180     /* Display */
181     mapper->addMapping(ui->lang, OptionsModel::Language);
182     mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
183     mapper->addMapping(ui->displayAddresses, OptionsModel::DisplayAddresses);
184     mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
185     mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
186 }
187
188 void OptionsDialog::enableApplyButton()
189 {
190     ui->applyButton->setEnabled(true);
191 }
192
193 void OptionsDialog::disableApplyButton()
194 {
195     ui->applyButton->setEnabled(false);
196 }
197
198 void OptionsDialog::enableSaveButtons()
199 {
200     /* prevent enabling of the save buttons when data modified, if there is an invalid proxy address present */
201     if(fProxyIpValid && fTorIpValid)
202         setSaveButtonState(true);
203 }
204
205 void OptionsDialog::disableSaveButtons()
206 {
207     setSaveButtonState(false);
208 }
209
210 void OptionsDialog::setSaveButtonState(bool fState)
211 {
212     ui->applyButton->setEnabled(fState);
213     ui->okButton->setEnabled(fState);
214 }
215
216 void OptionsDialog::on_okButton_clicked()
217 {
218     mapper->submit();
219     accept();
220 }
221
222 void OptionsDialog::on_cancelButton_clicked()
223 {
224     reject();
225 }
226
227 void OptionsDialog::on_applyButton_clicked()
228 {
229     mapper->submit();
230     disableApplyButton();
231 }
232
233 void OptionsDialog::showRestartWarning_Proxy()
234 {
235     if(!fRestartWarningDisplayed_Proxy)
236     {
237         QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting NovaCoin."), QMessageBox::Ok);
238         fRestartWarningDisplayed_Proxy = true;
239     }
240 }
241
242 void OptionsDialog::showRestartWarning_Tor()
243 {
244     if(!fRestartWarningDisplayed_Proxy)
245     {
246         QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting NovaCoin."), QMessageBox::Ok);
247         fRestartWarningDisplayed_Tor = true;
248     }
249 }
250
251 void OptionsDialog::showRestartWarning_Lang()
252 {
253     if(!fRestartWarningDisplayed_Lang)
254     {
255         QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting NovaCoin."), QMessageBox::Ok);
256         fRestartWarningDisplayed_Lang = true;
257     }
258 }
259
260 void OptionsDialog::showRestartWarning_URL()
261 {
262     if(!fRestartWarningDisplayed_URL)
263     {
264         QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting NovaCoin."), QMessageBox::Ok);
265         fRestartWarningDisplayed_URL = true;
266     }
267 }
268
269
270 void OptionsDialog::updateDisplayUnit()
271 {
272     if(model)
273     {
274         /* Update transactionFee with the current unit */
275         ui->transactionFee->setDisplayUnit(model->getDisplayUnit());
276     }
277 }
278
279 void OptionsDialog::handleProxyIpValid(QValidatedLineEdit *object, bool fState)
280 {
281     // this is used in a check before re-enabling the save buttons
282     fProxyIpValid = fState;
283
284     if(fProxyIpValid)
285     {
286         enableSaveButtons();
287         ui->statusLabel->clear();
288     }
289     else
290     {
291         disableSaveButtons();
292         object->setValid(fProxyIpValid);
293         ui->statusLabel->setStyleSheet("QLabel { color: red; }");
294         ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
295     }
296 }
297
298 void OptionsDialog::handleTorIpValid(QValidatedLineEdit *object, bool fState)
299 {
300     // this is used in a check before re-enabling the save buttons
301     fTorIpValid = fState;
302
303     if(fTorIpValid)
304     {
305         enableSaveButtons();
306         ui->statusLabel->clear();
307     }
308     else
309     {
310         disableSaveButtons();
311         object->setValid(fTorIpValid);
312         ui->statusLabel->setStyleSheet("QLabel { color: red; }");
313         ui->statusLabel->setText(tr("The supplied tor address is invalid."));
314     }
315 }
316
317 bool OptionsDialog::eventFilter(QObject *object, QEvent *event)
318 {
319     if(event->type() == QEvent::FocusOut)
320     {
321         if(object == ui->proxyIp)
322         {
323             CService addr;
324             /* Check proxyIp for a valid IPv4/IPv6 address and emit the proxyIpValid signal */
325             emit proxyIpValid(ui->proxyIp, LookupNumeric(ui->proxyIp->text().toStdString().c_str(), addr));
326         }
327
328         if(object == ui->torIp)
329         {
330             CService addr;
331             /* Check proxyIp for a valid IPv4/IPv6 address and emit the torIpValid signal */
332             emit torIpValid(ui->torIp, LookupNumeric(ui->torIp->text().toStdString().c_str(), addr));
333         }
334     }
335     return QDialog::eventFilter(object, event);
336 }