1 // Copyright (c) 2011-2018 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #if defined(HAVE_CONFIG_H)
6 #include <config/bitcoin-config.h>
7 #endif
8
9 #include <qt/optionsdialog.h>
10 #include <qt/forms/ui_optionsdialog.h>
11
12 #include <qt/bitcoinunits.h>
13 #include <qt/guiconstants.h>
14 #include <qt/guiutil.h>
15 #include <qt/optionsmodel.h>
16
17 #include <interfaces/node.h>
18 #include <validation.h> // for DEFAULT_SCRIPTCHECK_THREADS and MAX_SCRIPTCHECK_THREADS
19 #include <netbase.h>
20 #include <txdb.h> // for -dbcache defaults
21
22 #include <QDataWidgetMapper>
23 #include <QDir>
24 #include <QIntValidator>
25 #include <QLocale>
26 #include <QMessageBox>
27 #include <QSystemTrayIcon>
28 #include <QTimer>
29
OptionsDialog(QWidget * parent,bool enableWallet)30 OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
31 QDialog(parent),
32 ui(new Ui::OptionsDialog),
33 model(nullptr),
34 mapper(nullptr)
35 {
36 ui->setupUi(this);
37
38 /* Main elements init */
39 ui->databaseCache->setMinimum(nMinDbCache);
40 ui->databaseCache->setMaximum(nMaxDbCache);
41 ui->threadsScriptVerif->setMinimum(-GetNumCores());
42 ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
43 ui->pruneWarning->setVisible(false);
44 ui->pruneWarning->setStyleSheet("QLabel { color: red; }");
45
46 ui->pruneSize->setEnabled(false);
47 connect(ui->prune, &QPushButton::toggled, ui->pruneSize, &QWidget::setEnabled);
48
49 /* Network elements init */
50 #ifndef USE_UPNP
51 ui->mapPortUpnp->setEnabled(false);
52 #endif
53
54 ui->proxyIp->setEnabled(false);
55 ui->proxyPort->setEnabled(false);
56 ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
57
58 ui->proxyIpTor->setEnabled(false);
59 ui->proxyPortTor->setEnabled(false);
60 ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));
61
62 connect(ui->connectSocks, &QPushButton::toggled, ui->proxyIp, &QWidget::setEnabled);
63 connect(ui->connectSocks, &QPushButton::toggled, ui->proxyPort, &QWidget::setEnabled);
64 connect(ui->connectSocks, &QPushButton::toggled, this, &OptionsDialog::updateProxyValidationState);
65
66 connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyIpTor, &QWidget::setEnabled);
67 connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyPortTor, &QWidget::setEnabled);
68 connect(ui->connectSocksTor, &QPushButton::toggled, this, &OptionsDialog::updateProxyValidationState);
69
70 /* Window elements init */
71 #ifdef Q_OS_MAC
72 /* remove Window tab on Mac */
73 ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
74 #if defined(MAC_OS_X_VERSION_MIN_REQUIRED) && MAC_OS_X_VERSION_MIN_REQUIRED > 101100
75 /* hide launch at startup option if compiled against macOS > 10.11 (removed API) */
76 ui->bitcoinAtStartup->setVisible(false);
77 ui->verticalLayout_Main->removeWidget(ui->bitcoinAtStartup);
78 ui->verticalLayout_Main->removeItem(ui->horizontalSpacer_0_Main);
79 #endif
80 #endif
81
82 /* remove Wallet tab in case of -disablewallet */
83 if (!enableWallet) {
84 ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet));
85 }
86
87 /* Display elements init */
88 QDir translations(":translations");
89
90 ui->bitcoinAtStartup->setToolTip(ui->bitcoinAtStartup->toolTip().arg(PACKAGE_NAME));
91 ui->bitcoinAtStartup->setText(ui->bitcoinAtStartup->text().arg(PACKAGE_NAME));
92
93 ui->openBitcoinConfButton->setToolTip(ui->openBitcoinConfButton->toolTip().arg(PACKAGE_NAME));
94
95 ui->lang->setToolTip(ui->lang->toolTip().arg(PACKAGE_NAME));
96 ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
97 for (const QString &langStr : translations.entryList())
98 {
99 QLocale locale(langStr);
100
101 /** check if the locale name consists of 2 parts (language_country) */
102 if(langStr.contains("_"))
103 {
104 /** display language strings as "native language - native country (locale name)", e.g. "Deutsch - Deutschland (de)" */
105 ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
106 }
107 else
108 {
109 /** display language strings as "native language (locale name)", e.g. "Deutsch (de)" */
110 ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
111 }
112 }
113 ui->thirdPartyTxUrls->setPlaceholderText("https://example.com/tx/%s");
114
115 ui->unit->setModel(new BitcoinUnits(this));
116
117 /* Widget-to-option mapper */
118 mapper = new QDataWidgetMapper(this);
119 mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
120 mapper->setOrientation(Qt::Vertical);
121
122 GUIUtil::ItemDelegate* delegate = new GUIUtil::ItemDelegate(mapper);
123 connect(delegate, &GUIUtil::ItemDelegate::keyEscapePressed, this, &OptionsDialog::reject);
124 mapper->setItemDelegate(delegate);
125
126 /* setup/change UI elements when proxy IPs are invalid/valid */
127 ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
128 ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
129 connect(ui->proxyIp, &QValidatedLineEdit::validationDidChange, this, &OptionsDialog::updateProxyValidationState);
130 connect(ui->proxyIpTor, &QValidatedLineEdit::validationDidChange, this, &OptionsDialog::updateProxyValidationState);
131 connect(ui->proxyPort, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
132 connect(ui->proxyPortTor, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
133
134 if (!QSystemTrayIcon::isSystemTrayAvailable()) {
135 ui->hideTrayIcon->setChecked(true);
136 ui->hideTrayIcon->setEnabled(false);
137 ui->minimizeToTray->setChecked(false);
138 ui->minimizeToTray->setEnabled(false);
139 }
140 }
141
~OptionsDialog()142 OptionsDialog::~OptionsDialog()
143 {
144 delete ui;
145 }
146
setModel(OptionsModel * _model)147 void OptionsDialog::setModel(OptionsModel *_model)
148 {
149 this->model = _model;
150
151 if(_model)
152 {
153 /* check if client restart is needed and show persistent message */
154 if (_model->isRestartRequired())
155 showRestartWarning(true);
156
157 // Prune values are in GB to be consistent with intro.cpp
158 static constexpr uint64_t nMinDiskSpace = (MIN_DISK_SPACE_FOR_BLOCK_FILES / GB_BYTES) + (MIN_DISK_SPACE_FOR_BLOCK_FILES % GB_BYTES) ? 1 : 0;
159 ui->pruneSize->setRange(nMinDiskSpace, std::numeric_limits<int>::max());
160
161 QString strLabel = _model->getOverriddenByCommandLine();
162 if (strLabel.isEmpty())
163 strLabel = tr("none");
164 ui->overriddenByCommandLineLabel->setText(strLabel);
165
166 mapper->setModel(_model);
167 setMapper();
168 mapper->toFirst();
169
170 updateDefaultProxyNets();
171 }
172
173 /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
174
175 /* Main */
176 connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
177 connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::togglePruneWarning);
178 connect(ui->pruneSize, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
179 connect(ui->databaseCache, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
180 connect(ui->threadsScriptVerif, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
181 /* Wallet */
182 connect(ui->spendZeroConfChange, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
183 /* Network */
184 connect(ui->allowIncoming, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
185 connect(ui->connectSocks, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
186 connect(ui->connectSocksTor, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
187 /* Display */
188 connect(ui->lang, static_cast<void (QValueComboBox::*)()>(&QValueComboBox::valueChanged), [this]{ showRestartWarning(); });
189 connect(ui->thirdPartyTxUrls, &QLineEdit::textChanged, [this]{ showRestartWarning(); });
190 }
191
setCurrentTab(OptionsDialog::Tab tab)192 void OptionsDialog::setCurrentTab(OptionsDialog::Tab tab)
193 {
194 QWidget *tab_widget = nullptr;
195 if (tab == OptionsDialog::Tab::TAB_NETWORK) tab_widget = ui->tabNetwork;
196 if (tab == OptionsDialog::Tab::TAB_MAIN) tab_widget = ui->tabMain;
197 if (tab_widget && ui->tabWidget->currentWidget() != tab_widget) {
198 ui->tabWidget->setCurrentWidget(tab_widget);
199 }
200 }
201
setMapper()202 void OptionsDialog::setMapper()
203 {
204 /* Main */
205 mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
206 mapper->addMapping(ui->threadsScriptVerif, OptionsModel::ThreadsScriptVerif);
207 mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
208 mapper->addMapping(ui->prune, OptionsModel::Prune);
209 mapper->addMapping(ui->pruneSize, OptionsModel::PruneSize);
210
211 /* Wallet */
212 mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
213 mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
214
215 /* Network */
216 mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
217 mapper->addMapping(ui->allowIncoming, OptionsModel::Listen);
218
219 mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
220 mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
221 mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
222
223 mapper->addMapping(ui->connectSocksTor, OptionsModel::ProxyUseTor);
224 mapper->addMapping(ui->proxyIpTor, OptionsModel::ProxyIPTor);
225 mapper->addMapping(ui->proxyPortTor, OptionsModel::ProxyPortTor);
226
227 /* Window */
228 #ifndef Q_OS_MAC
229 if (QSystemTrayIcon::isSystemTrayAvailable()) {
230 mapper->addMapping(ui->hideTrayIcon, OptionsModel::HideTrayIcon);
231 mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
232 }
233 mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
234 #endif
235
236 /* Display */
237 mapper->addMapping(ui->lang, OptionsModel::Language);
238 mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
239 mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
240 }
241
setOkButtonState(bool fState)242 void OptionsDialog::setOkButtonState(bool fState)
243 {
244 ui->okButton->setEnabled(fState);
245 }
246
on_resetButton_clicked()247 void OptionsDialog::on_resetButton_clicked()
248 {
249 if(model)
250 {
251 // confirmation dialog
252 QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
253 tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shut down. Do you want to proceed?"),
254 QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
255
256 if(btnRetVal == QMessageBox::Cancel)
257 return;
258
259 /* reset all options and close GUI */
260 model->Reset();
261 QApplication::quit();
262 }
263 }
264
on_openBitcoinConfButton_clicked()265 void OptionsDialog::on_openBitcoinConfButton_clicked()
266 {
267 /* explain the purpose of the config file */
268 QMessageBox::information(this, tr("Configuration options"),
269 tr("The configuration file is used to specify advanced user options which override GUI settings. "
270 "Additionally, any command-line options will override this configuration file."));
271
272 /* show an error if there was some problem opening the file */
273 if (!GUIUtil::openBitcoinConf())
274 QMessageBox::critical(this, tr("Error"), tr("The configuration file could not be opened."));
275 }
276
on_okButton_clicked()277 void OptionsDialog::on_okButton_clicked()
278 {
279 mapper->submit();
280 accept();
281 updateDefaultProxyNets();
282 }
283
on_cancelButton_clicked()284 void OptionsDialog::on_cancelButton_clicked()
285 {
286 reject();
287 }
288
on_hideTrayIcon_stateChanged(int fState)289 void OptionsDialog::on_hideTrayIcon_stateChanged(int fState)
290 {
291 if(fState)
292 {
293 ui->minimizeToTray->setChecked(false);
294 ui->minimizeToTray->setEnabled(false);
295 }
296 else
297 {
298 ui->minimizeToTray->setEnabled(true);
299 }
300 }
301
togglePruneWarning(bool enabled)302 void OptionsDialog::togglePruneWarning(bool enabled)
303 {
304 ui->pruneWarning->setVisible(!ui->pruneWarning->isVisible());
305 }
306
showRestartWarning(bool fPersistent)307 void OptionsDialog::showRestartWarning(bool fPersistent)
308 {
309 ui->statusLabel->setStyleSheet("QLabel { color: red; }");
310
311 if(fPersistent)
312 {
313 ui->statusLabel->setText(tr("Client restart required to activate changes."));
314 }
315 else
316 {
317 ui->statusLabel->setText(tr("This change would require a client restart."));
318 // clear non-persistent status label after 10 seconds
319 // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
320 QTimer::singleShot(10000, this, &OptionsDialog::clearStatusLabel);
321 }
322 }
323
clearStatusLabel()324 void OptionsDialog::clearStatusLabel()
325 {
326 ui->statusLabel->clear();
327 if (model && model->isRestartRequired()) {
328 showRestartWarning(true);
329 }
330 }
331
updateProxyValidationState()332 void OptionsDialog::updateProxyValidationState()
333 {
334 QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
335 QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
336 if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
337 {
338 setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
339 clearStatusLabel();
340 }
341 else
342 {
343 setOkButtonState(false);
344 ui->statusLabel->setStyleSheet("QLabel { color: red; }");
345 ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
346 }
347 }
348
updateDefaultProxyNets()349 void OptionsDialog::updateDefaultProxyNets()
350 {
351 proxyType proxy;
352 std::string strProxy;
353 QString strDefaultProxyGUI;
354
355 model->node().getProxy(NET_IPV4, proxy);
356 strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
357 strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
358 (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);
359
360 model->node().getProxy(NET_IPV6, proxy);
361 strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
362 strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
363 (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);
364
365 model->node().getProxy(NET_ONION, proxy);
366 strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
367 strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
368 (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
369 }
370
ProxyAddressValidator(QObject * parent)371 ProxyAddressValidator::ProxyAddressValidator(QObject *parent) :
372 QValidator(parent)
373 {
374 }
375
validate(QString & input,int & pos) const376 QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
377 {
378 Q_UNUSED(pos);
379 // Validate the proxy
380 CService serv(LookupNumeric(input.toStdString().c_str(), DEFAULT_GUI_PROXY_PORT));
381 proxyType addrProxy = proxyType(serv, true);
382 if (addrProxy.IsValid())
383 return QValidator::Acceptable;
384
385 return QValidator::Invalid;
386 }
387