Bitcoin ABC 0.33.3
P2P Digital Currency
optionsdialog.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2016 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/forms/ui_optionsdialog.h>
10#include <qt/optionsdialog.h>
11
12#include <interfaces/node.h>
13#include <netbase.h>
14#include <node/caches.h>
15#include <qt/bitcoinunits.h>
16#include <qt/guiconstants.h>
17#include <qt/guiutil.h>
18#include <qt/optionsmodel.h>
19#include <validation.h> // for DEFAULT_SCRIPTCHECK_THREADS and MAX_SCRIPTCHECK_THREADS
20
21#include <QDataWidgetMapper>
22#include <QDir>
23#include <QIntValidator>
24#include <QLocale>
25#include <QMessageBox>
26#include <QSettings>
27#include <QSystemTrayIcon>
28#include <QTimer>
29
30OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet)
31 : QDialog(parent), ui(new Ui::OptionsDialog), model(nullptr),
32 mapper(nullptr) {
33 ui->setupUi(this);
34
35 /* Main elements init */
36 ui->databaseCache->setRange(MIN_DB_CACHE >> 20,
37 std::numeric_limits<int>::max());
38 ui->threadsScriptVerif->setMinimum(-GetNumCores());
39 ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
40 ui->pruneWarning->setVisible(false);
41 ui->pruneWarning->setStyleSheet("QLabel { color: red; }");
42
43 ui->pruneSize->setEnabled(false);
44 connect(ui->prune, &QPushButton::toggled, ui->pruneSize,
45 &QWidget::setEnabled);
46
47 /* Network elements init */
48 connect(this, &QDialog::accepted, [this]() {
49 QSettings settings;
50 model->node().mapPort(settings.value("fUseNatpmp").toBool());
51 });
52 ui->proxyIp->setEnabled(false);
53 ui->proxyPort->setEnabled(false);
54 ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
55
56 ui->proxyIpTor->setEnabled(false);
57 ui->proxyPortTor->setEnabled(false);
58 ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));
59
60 connect(ui->connectSocks, &QPushButton::toggled, ui->proxyIp,
61 &QWidget::setEnabled);
62 connect(ui->connectSocks, &QPushButton::toggled, ui->proxyPort,
63 &QWidget::setEnabled);
64 connect(ui->connectSocks, &QPushButton::toggled, this,
66
67 connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyIpTor,
68 &QWidget::setEnabled);
69 connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyPortTor,
70 &QWidget::setEnabled);
71 connect(ui->connectSocksTor, &QPushButton::toggled, this,
73
74 /* Window elements init */
75#ifdef Q_OS_MAC
76 /* remove Window tab on Mac */
77 ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
78 /* hide launch at startup option on macOS */
79 ui->bitcoinAtStartup->setVisible(false);
80 ui->verticalLayout_Main->removeWidget(ui->bitcoinAtStartup);
81 ui->verticalLayout_Main->removeItem(ui->horizontalSpacer_0_Main);
82#endif
83
84 /* remove Wallet tab and 3rd party-URL textbox in case of -disablewallet */
85 if (!enableWallet) {
86 ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet));
87 ui->thirdPartyTxUrlsLabel->setVisible(false);
88 ui->thirdPartyTxUrls->setVisible(false);
89 }
90
91 /* Display elements init */
92 QDir translations(":translations");
93
94 ui->bitcoinAtStartup->setToolTip(
95 ui->bitcoinAtStartup->toolTip().arg(PACKAGE_NAME));
96 ui->bitcoinAtStartup->setText(
97 ui->bitcoinAtStartup->text().arg(PACKAGE_NAME));
98
99 ui->openBitcoinConfButton->setToolTip(
100 ui->openBitcoinConfButton->toolTip().arg(PACKAGE_NAME));
101
102 ui->lang->setToolTip(ui->lang->toolTip().arg(PACKAGE_NAME));
103 ui->lang->addItem(QString("(") + tr("default") + QString(")"),
104 QVariant(""));
105 for (const QString &langStr : translations.entryList()) {
106 QLocale locale(langStr);
107
109 if (langStr.contains("_")) {
115 ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") +
116#if (QT_VERSION >= QT_VERSION_CHECK(6, 2, 0))
117 locale.nativeTerritoryName() +
118#else
119 locale.nativeCountryName() +
120#endif
121 QString(" (") + langStr + QString(")"),
122 QVariant(langStr));
123 } else {
126 ui->lang->addItem(locale.nativeLanguageName() + QString(" (") +
127 langStr + QString(")"),
128 QVariant(langStr));
129 }
130 }
131 ui->unit->setModel(new BitcoinUnits(this));
132
133 /* Widget-to-option mapper */
134 mapper = new QDataWidgetMapper(this);
135 mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
136 mapper->setOrientation(Qt::Vertical);
137
139 connect(delegate, &GUIUtil::ItemDelegate::keyEscapePressed, this,
140 &OptionsDialog::reject);
141 mapper->setItemDelegate(delegate);
142
143 /* setup/change UI elements when proxy IPs are invalid/valid */
144 ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
145 ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
146 connect(ui->proxyIp, &QValidatedLineEdit::validationDidChange, this,
148 connect(ui->proxyIpTor, &QValidatedLineEdit::validationDidChange, this,
150 connect(ui->proxyPort, &QLineEdit::textChanged, this,
152 connect(ui->proxyPortTor, &QLineEdit::textChanged, this,
154
155 /* setup/change UI elements when third party tx URLs are invalid/valid */
156 ui->thirdPartyTxUrls->setCheckValidator(
157 new ThirdPartyTxUrlsValidator(parent));
158 connect(ui->thirdPartyTxUrls, &QValidatedLineEdit::validationDidChange,
160
161 if (!QSystemTrayIcon::isSystemTrayAvailable()) {
162 ui->hideTrayIcon->setChecked(true);
163 ui->hideTrayIcon->setEnabled(false);
164 ui->minimizeToTray->setChecked(false);
165 ui->minimizeToTray->setEnabled(false);
166 }
167
169}
170
172 delete ui;
173}
174
176 this->model = _model;
177
178 if (_model) {
179 /* check if client restart is needed and show persistent message */
180 if (_model->isRestartRequired()) {
181 showRestartWarning(true);
182 }
183
184 // Prune values are in GB to be consistent with intro.cpp
185 static constexpr uint64_t nMinDiskSpace =
188 ? 1
189 : 0;
190 ui->pruneSize->setRange(nMinDiskSpace, std::numeric_limits<int>::max());
191
192 QString strLabel = _model->getOverriddenByCommandLine();
193 if (strLabel.isEmpty()) {
194 strLabel = tr("none");
195 }
196 ui->overriddenByCommandLineLabel->setText(strLabel);
197
198 mapper->setModel(_model);
199 setMapper();
200 mapper->toFirst();
201
203 }
204
205 /* warn when one of the following settings changes by user action (placed
206 * here so init via mapper doesn't trigger them) */
207
208 /* Main */
209 connect(ui->prune, &QCheckBox::clicked, this,
211 connect(ui->prune, &QCheckBox::clicked, this,
213 connect(ui->pruneSize,
214 static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this,
216 connect(ui->databaseCache,
217 static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this,
219 connect(ui->threadsScriptVerif,
220 static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this,
222 /* Wallet */
223 connect(ui->spendZeroConfChange, &QCheckBox::clicked, this,
225 /* Network */
226 connect(ui->allowIncoming, &QCheckBox::clicked, this,
228 connect(ui->connectSocks, &QCheckBox::clicked, this,
230 connect(ui->connectSocksTor, &QCheckBox::clicked, this,
232 /* Display */
233 connect(
234 ui->lang,
235 static_cast<void (QValueComboBox::*)()>(&QValueComboBox::valueChanged),
236 [this] { showRestartWarning(); });
237 connect(ui->thirdPartyTxUrls, &QLineEdit::textChanged,
238 [this] { showRestartWarning(); });
239}
240
242 QWidget *tab_widget = nullptr;
243 if (tab == OptionsDialog::Tab::TAB_NETWORK) {
244 tab_widget = ui->tabNetwork;
245 }
246 if (tab == OptionsDialog::Tab::TAB_MAIN) {
247 tab_widget = ui->tabMain;
248 }
249 if (tab_widget && ui->tabWidget->currentWidget() != tab_widget) {
250 ui->tabWidget->setCurrentWidget(tab_widget);
251 }
252}
253
255 /* Main */
256 mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
257 mapper->addMapping(ui->threadsScriptVerif,
259 mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
260 mapper->addMapping(ui->prune, OptionsModel::Prune);
261 mapper->addMapping(ui->pruneSize, OptionsModel::PruneSize);
262
263 /* Wallet */
264 mapper->addMapping(ui->spendZeroConfChange,
266 mapper->addMapping(ui->coinControlFeatures,
268
269 /* Network */
270 mapper->addMapping(ui->mapPortNatpmp, OptionsModel::MapPortNatpmp);
271 mapper->addMapping(ui->allowIncoming, OptionsModel::Listen);
272
273 mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
274 mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
275 mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
276
277 mapper->addMapping(ui->connectSocksTor, OptionsModel::ProxyUseTor);
278 mapper->addMapping(ui->proxyIpTor, OptionsModel::ProxyIPTor);
279 mapper->addMapping(ui->proxyPortTor, OptionsModel::ProxyPortTor);
280
281/* Window */
282#ifndef Q_OS_MAC
283 if (QSystemTrayIcon::isSystemTrayAvailable()) {
284 mapper->addMapping(ui->hideTrayIcon, OptionsModel::HideTrayIcon);
285 mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
286 }
287 mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
288#endif
289
290 /* Display */
291 mapper->addMapping(ui->lang, OptionsModel::Language);
292 mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
293 mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
294}
295
297 ui->okButton->setEnabled(fState);
298}
299
301 if (model) {
302 // confirmation dialog
303 QMessageBox::StandardButton btnRetVal = QMessageBox::question(
304 this, tr("Confirm options reset"),
305 tr("Client restart required to activate changes.") + "<br><br>" +
306 tr("Client will be shut down. Do you want to proceed?"),
307 QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
308
309 if (btnRetVal == QMessageBox::Cancel) {
310 return;
311 }
312
313 /* reset all options and close GUI */
314 model->Reset();
315 close();
316 Q_EMIT quitOnReset();
317 }
318}
319
321 /* explain the purpose of the config file */
322 QMessageBox::information(
323 this, tr("Configuration options"),
324 tr("The configuration file is used to specify advanced user options "
325 "which override GUI settings. Additionally, any command-line "
326 "options will override this configuration file."));
327
328 /* show an error if there was some problem opening the file */
330 QMessageBox::critical(
331 this, tr("Error"),
332 tr("The configuration file could not be opened."));
333 }
334}
335
337 mapper->submit();
338 accept();
340}
341
343 reject();
344}
345
347 if (fState) {
348 ui->minimizeToTray->setChecked(false);
349 ui->minimizeToTray->setEnabled(false);
350 } else {
351 ui->minimizeToTray->setEnabled(true);
352 }
353}
354
356 ui->pruneWarning->setVisible(!ui->pruneWarning->isVisible());
357}
358
359void OptionsDialog::showRestartWarning(bool fPersistent) {
360 ui->statusLabel->setStyleSheet("QLabel { color: red; }");
361
362 if (fPersistent) {
363 ui->statusLabel->setText(
364 tr("Client restart required to activate changes."));
365 } else {
366 ui->statusLabel->setText(
367 tr("This change would require a client restart."));
368 // clear non-persistent status label after 10 seconds
369 // TODO: should perhaps be a class attribute, if we extend the use of
370 // statusLabel
371 QTimer::singleShot(10000, this, &OptionsDialog::clearStatusLabel);
372 }
373}
374
376 ui->statusLabel->clear();
377 if (model && model->isRestartRequired()) {
378 showRestartWarning(true);
379 }
380}
381
383 QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
384 QValidatedLineEdit *otherProxyWidget =
385 (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
386 if (pUiProxyIp->isValid() &&
387 (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) &&
388 (!ui->proxyPortTor->isEnabled() ||
389 ui->proxyPortTor->text().toInt() > 0)) {
390 // Only enable ok button if both proxys are valid
391 setOkButtonState(otherProxyWidget->isValid());
393 } else {
394 setOkButtonState(false);
395 ui->statusLabel->setStyleSheet("QLabel { color: red; }");
396 ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
397 }
398}
399
401 std::string proxyIpText{ui->proxyIp->text().toStdString()};
402 if (!IsUnixSocketPath(proxyIpText)) {
403 const std::optional<CNetAddr> ui_proxy_netaddr{
404 LookupHost(proxyIpText, /*fAllowLookup=*/false)};
405 const CService ui_proxy{ui_proxy_netaddr.value_or(CNetAddr{}),
406 ui->proxyPort->text().toUShort()};
407 proxyIpText = ui_proxy.ToStringAddrPort();
408 }
409
410 Proxy proxy;
411 bool has_proxy;
412
413 has_proxy = model->node().getProxy(NET_IPV4, proxy);
414 ui->proxyReachIPv4->setChecked(has_proxy &&
415 proxy.ToString() == proxyIpText);
416
417 has_proxy = model->node().getProxy(NET_IPV6, proxy);
418 ui->proxyReachIPv6->setChecked(has_proxy &&
419 proxy.ToString() == proxyIpText);
420
421 has_proxy = model->node().getProxy(NET_ONION, proxy);
422 ui->proxyReachTor->setChecked(has_proxy && proxy.ToString() == proxyIpText);
423}
424
426 : QValidator(parent) {}
427
428QValidator::State ProxyAddressValidator::validate(QString &input,
429 int &pos) const {
430 Q_UNUSED(pos);
431 // Validate the proxy
432 CService serv(LookupNumeric(input.toStdString(), DEFAULT_GUI_PROXY_PORT));
433 Proxy addrProxy = Proxy(serv, true);
434 if (addrProxy.IsValid()) {
435 return QValidator::Acceptable;
436 }
437
438 return QValidator::Invalid;
439}
440
442 QValidatedLineEdit *thirdPartyTxUrls = ui->thirdPartyTxUrls;
443 if (thirdPartyTxUrls->isValid()) {
444 // Only enable OK button if the third party tx URLS pattern is valid
445 setOkButtonState(true);
447 } else {
448 setOkButtonState(false);
449 ui->statusLabel->setStyleSheet("QLabel { color: red; }");
450 ui->statusLabel->setText(
451 tr("The third party transaction URLs should start with https://."));
452 }
453}
454
456 : QValidator(parent) {}
457
458QValidator::State ThirdPartyTxUrlsValidator::validate(QString &input,
459 int &pos) const {
460 Q_UNUSED(pos);
461 // Check the URL starts with https. All other schemes are rejected for
462 // security reasons.
463 if (input.isEmpty() || input.startsWith("https://")) {
464 return QValidator::Acceptable;
465 }
466
467 return QValidator::Invalid;
468}
Bitcoin unit definitions.
Definition: bitcoinunits.h:32
Network address.
Definition: netaddress.h:114
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:573
Preferences dialog.
Definition: optionsdialog.h:43
void setModel(OptionsModel *model)
OptionsModel * model
Definition: optionsdialog.h:83
void setCurrentTab(OptionsDialog::Tab tab)
void on_okButton_clicked()
void on_openBitcoinConfButton_clicked()
void updateDefaultProxyNets()
void updateProxyValidationState()
void togglePruneWarning(bool enabled)
void on_hideTrayIcon_stateChanged(int fState)
void showRestartWarning(bool fPersistent=false)
void updateThirdPartyTxUrlsState()
void on_resetButton_clicked()
Ui::OptionsDialog * ui
Definition: optionsdialog.h:82
void quitOnReset()
OptionsDialog(QWidget *parent, bool enableWallet)
QDataWidgetMapper * mapper
Definition: optionsdialog.h:84
void clearStatusLabel()
void on_cancelButton_clicked()
void setOkButtonState(bool fState)
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:48
bool isRestartRequired() const
interfaces::Node & node() const
Definition: optionsmodel.h:112
const QString & getOverriddenByCommandLine()
Definition: optionsmodel.h:100
Proxy address widget validator, checks for a valid proxy address.
Definition: optionsdialog.h:23
ProxyAddressValidator(QObject *parent)
State validate(QString &input, int &pos) const override
Definition: netbase.h:67
std::string ToString() const
Definition: netbase.h:96
bool IsValid() const
Definition: netbase.h:82
Line edit that can be marked as "invalid" to show input validation feedback.
void validationDidChange(QValidatedLineEdit *validatedLineEdit)
void valueChanged()
Third party tx URL validator, checks for an https link.
Definition: optionsdialog.h:33
ThirdPartyTxUrlsValidator(QObject *parent)
State validate(QString &input, int &pos) const override
virtual bool getProxy(Network net, Proxy &proxy_info)=0
Get proxy.
static constexpr uint64_t GB_BYTES
Definition: guiconstants.h:53
bool openBitcoinConf()
Definition: guiutil.cpp:425
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:410
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:49
@ NET_IPV6
IPv6.
Definition: netaddress.h:46
@ NET_IPV4
IPv4.
Definition: netaddress.h:43
std::vector< CNetAddr > LookupHost(const std::string &name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
Resolve a host string to its corresponding network addresses.
Definition: netbase.cpp:198
bool IsUnixSocketPath(const std::string &name)
Check if a string is a valid UNIX domain socket path.
Definition: netbase.cpp:269
CService LookupNumeric(const std::string &name, uint16_t portDefault, DNSLookupFn dns_lookup_function)
Resolve a service string with a numeric IP to its first corresponding service.
Definition: netbase.cpp:257
static constexpr size_t MIN_DB_CACHE
min. -dbcache (bytes)
Definition: caches.h:16
static constexpr uint16_t DEFAULT_GUI_PROXY_PORT
Definition: optionsmodel.h:24
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:114
static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES
Require that user allocate at least 550 MiB for block & undo files (blk???.dat and rev?...
Definition: validation.h:115
static const int MAX_SCRIPTCHECK_THREADS
Maximum number of dedicated script-checking threads allowed.
Definition: validation.h:89