Bitcoin Core  22.0.0
P2P Digital Currency
optionsdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2020 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)
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 <QSettings>
28 #include <QSystemTrayIcon>
29 #include <QTimer>
30 
31 OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
32  QDialog(parent, GUIUtil::dialog_flags),
33  ui(new Ui::OptionsDialog),
34  model(nullptr),
35  mapper(nullptr)
36 {
37  ui->setupUi(this);
38 
39  /* Main elements init */
40  ui->databaseCache->setMinimum(nMinDbCache);
41  ui->databaseCache->setMaximum(nMaxDbCache);
42  ui->threadsScriptVerif->setMinimum(-GetNumCores());
43  ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
44  ui->pruneWarning->setVisible(false);
45  ui->pruneWarning->setStyleSheet("QLabel { color: red; }");
46 
47  ui->pruneSize->setEnabled(false);
48  connect(ui->prune, &QPushButton::toggled, ui->pruneSize, &QWidget::setEnabled);
49 
50  /* Network elements init */
51 #ifndef USE_UPNP
52  ui->mapPortUpnp->setEnabled(false);
53 #endif
54 #ifndef USE_NATPMP
55  ui->mapPortNatpmp->setEnabled(false);
56 #endif
57  connect(this, &QDialog::accepted, [this](){
58  QSettings settings;
59  model->node().mapPort(settings.value("fUseUPnP").toBool(), settings.value("fUseNatpmp").toBool());
60  });
61 
62  ui->proxyIp->setEnabled(false);
63  ui->proxyPort->setEnabled(false);
64  ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
65 
66  ui->proxyIpTor->setEnabled(false);
67  ui->proxyPortTor->setEnabled(false);
68  ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));
69 
70  connect(ui->connectSocks, &QPushButton::toggled, ui->proxyIp, &QWidget::setEnabled);
71  connect(ui->connectSocks, &QPushButton::toggled, ui->proxyPort, &QWidget::setEnabled);
72  connect(ui->connectSocks, &QPushButton::toggled, this, &OptionsDialog::updateProxyValidationState);
73 
74  connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyIpTor, &QWidget::setEnabled);
75  connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyPortTor, &QWidget::setEnabled);
76  connect(ui->connectSocksTor, &QPushButton::toggled, this, &OptionsDialog::updateProxyValidationState);
77 
78  /* Window elements init */
79 #ifdef Q_OS_MAC
80  /* remove Window tab on Mac */
81  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
82  /* hide launch at startup option on macOS */
83  ui->bitcoinAtStartup->setVisible(false);
84  ui->verticalLayout_Main->removeWidget(ui->bitcoinAtStartup);
85  ui->verticalLayout_Main->removeItem(ui->horizontalSpacer_0_Main);
86 #endif
87 
88  /* remove Wallet tab and 3rd party-URL textbox in case of -disablewallet */
89  if (!enableWallet) {
90  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet));
91  ui->thirdPartyTxUrlsLabel->setVisible(false);
92  ui->thirdPartyTxUrls->setVisible(false);
93  }
94 
95 #ifndef ENABLE_EXTERNAL_SIGNER
96  //: "External signing" means using devices such as hardware wallets.
97  ui->externalSignerPath->setToolTip(tr("Compiled without external signing support (required for external signing)"));
98  ui->externalSignerPath->setEnabled(false);
99 #endif
100  /* Display elements init */
101  QDir translations(":translations");
102 
103  ui->bitcoinAtStartup->setToolTip(ui->bitcoinAtStartup->toolTip().arg(PACKAGE_NAME));
104  ui->bitcoinAtStartup->setText(ui->bitcoinAtStartup->text().arg(PACKAGE_NAME));
105 
106  ui->openBitcoinConfButton->setToolTip(ui->openBitcoinConfButton->toolTip().arg(PACKAGE_NAME));
107 
108  ui->lang->setToolTip(ui->lang->toolTip().arg(PACKAGE_NAME));
109  ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
110  for (const QString &langStr : translations.entryList())
111  {
112  QLocale locale(langStr);
113 
115  if(langStr.contains("_"))
116  {
118  ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
119  }
120  else
121  {
123  ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
124  }
125  }
126  ui->unit->setModel(new BitcoinUnits(this));
127 
128  /* Widget-to-option mapper */
129  mapper = new QDataWidgetMapper(this);
130  mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
131  mapper->setOrientation(Qt::Vertical);
132 
134  connect(delegate, &GUIUtil::ItemDelegate::keyEscapePressed, this, &OptionsDialog::reject);
135  mapper->setItemDelegate(delegate);
136 
137  /* setup/change UI elements when proxy IPs are invalid/valid */
138  ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
139  ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
142  connect(ui->proxyPort, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
143  connect(ui->proxyPortTor, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
144 
145  if (!QSystemTrayIcon::isSystemTrayAvailable()) {
146  ui->showTrayIcon->setChecked(false);
147  ui->showTrayIcon->setEnabled(false);
148  ui->minimizeToTray->setChecked(false);
149  ui->minimizeToTray->setEnabled(false);
150  }
151 
152  QFont embedded_font{GUIUtil::fixedPitchFont(true)};
153  ui->embeddedFont_radioButton->setText(ui->embeddedFont_radioButton->text().arg(QFontInfo(embedded_font).family()));
154  embedded_font.setWeight(QFont::Bold);
155  ui->embeddedFont_label_1->setFont(embedded_font);
156  ui->embeddedFont_label_9->setFont(embedded_font);
157 
158  QFont system_font{GUIUtil::fixedPitchFont(false)};
159  ui->systemFont_radioButton->setText(ui->systemFont_radioButton->text().arg(QFontInfo(system_font).family()));
160  system_font.setWeight(QFont::Bold);
161  ui->systemFont_label_1->setFont(system_font);
162  ui->systemFont_label_9->setFont(system_font);
163  // Checking the embeddedFont_radioButton automatically unchecks the systemFont_radioButton.
164  ui->systemFont_radioButton->setChecked(true);
165 
167 }
168 
170 {
171  delete ui;
172 }
173 
175 {
176  this->model = _model;
177 
178  if(_model)
179  {
180  /* check if client restart is needed and show persistent message */
181  if (_model->isRestartRequired())
182  showRestartWarning(true);
183 
184  // Prune values are in GB to be consistent with intro.cpp
185  static constexpr uint64_t nMinDiskSpace = (MIN_DISK_SPACE_FOR_BLOCK_FILES / GB_BYTES) + (MIN_DISK_SPACE_FOR_BLOCK_FILES % GB_BYTES) ? 1 : 0;
186  ui->pruneSize->setRange(nMinDiskSpace, std::numeric_limits<int>::max());
187 
188  QString strLabel = _model->getOverriddenByCommandLine();
189  if (strLabel.isEmpty())
190  strLabel = tr("none");
191  ui->overriddenByCommandLineLabel->setText(strLabel);
192 
193  mapper->setModel(_model);
194  setMapper();
195  mapper->toFirst();
196 
198  }
199 
200  /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
201 
202  /* Main */
203  connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
204  connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::togglePruneWarning);
205  connect(ui->pruneSize, qOverload<int>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
206  connect(ui->databaseCache, qOverload<int>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
207  connect(ui->externalSignerPath, &QLineEdit::textChanged, [this]{ showRestartWarning(); });
208  connect(ui->threadsScriptVerif, qOverload<int>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
209  /* Wallet */
210  connect(ui->spendZeroConfChange, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
211  /* Network */
212  connect(ui->allowIncoming, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
213  connect(ui->connectSocks, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
214  connect(ui->connectSocksTor, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
215  /* Display */
216  connect(ui->lang, qOverload<>(&QValueComboBox::valueChanged), [this]{ showRestartWarning(); });
217  connect(ui->thirdPartyTxUrls, &QLineEdit::textChanged, [this]{ showRestartWarning(); });
218 }
219 
221 {
222  QWidget *tab_widget = nullptr;
223  if (tab == OptionsDialog::Tab::TAB_NETWORK) tab_widget = ui->tabNetwork;
224  if (tab == OptionsDialog::Tab::TAB_MAIN) tab_widget = ui->tabMain;
225  if (tab_widget && ui->tabWidget->currentWidget() != tab_widget) {
226  ui->tabWidget->setCurrentWidget(tab_widget);
227  }
228 }
229 
231 {
232  /* Main */
233  mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
234  mapper->addMapping(ui->threadsScriptVerif, OptionsModel::ThreadsScriptVerif);
235  mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
236  mapper->addMapping(ui->prune, OptionsModel::Prune);
237  mapper->addMapping(ui->pruneSize, OptionsModel::PruneSize);
238 
239  /* Wallet */
240  mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
241  mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
242  mapper->addMapping(ui->externalSignerPath, OptionsModel::ExternalSignerPath);
243 
244  /* Network */
245  mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
246  mapper->addMapping(ui->mapPortNatpmp, OptionsModel::MapPortNatpmp);
247  mapper->addMapping(ui->allowIncoming, OptionsModel::Listen);
248 
249  mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
250  mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
251  mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
252 
253  mapper->addMapping(ui->connectSocksTor, OptionsModel::ProxyUseTor);
254  mapper->addMapping(ui->proxyIpTor, OptionsModel::ProxyIPTor);
255  mapper->addMapping(ui->proxyPortTor, OptionsModel::ProxyPortTor);
256 
257  /* Window */
258 #ifndef Q_OS_MAC
259  if (QSystemTrayIcon::isSystemTrayAvailable()) {
260  mapper->addMapping(ui->showTrayIcon, OptionsModel::ShowTrayIcon);
261  mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
262  }
263  mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
264 #endif
265 
266  /* Display */
267  mapper->addMapping(ui->lang, OptionsModel::Language);
268  mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
269  mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
270  mapper->addMapping(ui->embeddedFont_radioButton, OptionsModel::UseEmbeddedMonospacedFont);
271 }
272 
274 {
275  ui->okButton->setEnabled(fState);
276 }
277 
279 {
280  if(model)
281  {
282  // confirmation dialog
283  QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
284  tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shut down. Do you want to proceed?"),
285  QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
286 
287  if(btnRetVal == QMessageBox::Cancel)
288  return;
289 
290  /* reset all options and close GUI */
291  model->Reset();
292  QApplication::quit();
293  }
294 }
295 
297 {
298  /* explain the purpose of the config file */
299  QMessageBox::information(this, tr("Configuration options"),
300  tr("The configuration file is used to specify advanced user options which override GUI settings. "
301  "Additionally, any command-line options will override this configuration file."));
302 
303  /* show an error if there was some problem opening the file */
305  QMessageBox::critical(this, tr("Error"), tr("The configuration file could not be opened."));
306 }
307 
309 {
310  mapper->submit();
311  accept();
313 }
314 
316 {
317  reject();
318 }
319 
321 {
322  if (state == Qt::Checked) {
323  ui->minimizeToTray->setEnabled(true);
324  } else {
325  ui->minimizeToTray->setChecked(false);
326  ui->minimizeToTray->setEnabled(false);
327  }
328 }
329 
331 {
332  ui->pruneWarning->setVisible(!ui->pruneWarning->isVisible());
333 }
334 
335 void OptionsDialog::showRestartWarning(bool fPersistent)
336 {
337  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
338 
339  if(fPersistent)
340  {
341  ui->statusLabel->setText(tr("Client restart required to activate changes."));
342  }
343  else
344  {
345  ui->statusLabel->setText(tr("This change would require a client restart."));
346  // clear non-persistent status label after 10 seconds
347  // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
348  QTimer::singleShot(10000, this, &OptionsDialog::clearStatusLabel);
349  }
350 }
351 
353 {
354  ui->statusLabel->clear();
355  if (model && model->isRestartRequired()) {
356  showRestartWarning(true);
357  }
358 }
359 
361 {
362  QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
363  QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
364  if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
365  {
366  setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
368  }
369  else
370  {
371  setOkButtonState(false);
372  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
373  ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
374  }
375 }
376 
378 {
379  proxyType proxy;
380  std::string strProxy;
381  QString strDefaultProxyGUI;
382 
383  model->node().getProxy(NET_IPV4, proxy);
384  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
385  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
386  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);
387 
388  model->node().getProxy(NET_IPV6, proxy);
389  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
390  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
391  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);
392 
393  model->node().getProxy(NET_ONION, proxy);
394  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
395  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
396  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
397 }
398 
400 QValidator(parent)
401 {
402 }
403 
404 QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
405 {
406  Q_UNUSED(pos);
407  // Validate the proxy
408  CService serv(LookupNumeric(input.toStdString(), DEFAULT_GUI_PROXY_PORT));
409  proxyType addrProxy = proxyType(serv, true);
410  if (addrProxy.IsValid())
411  return QValidator::Acceptable;
412 
413  return QValidator::Invalid;
414 }
Ui::OptionsDialog * ui
Definition: optionsdialog.h:73
std::string ToStringPort() const
if(expired !=0)
Definition: validation.cpp:310
OptionsDialog(QWidget *parent, bool enableWallet)
Bitcoin unit definitions.
Definition: bitcoinunits.h:31
Proxy address widget validator, checks for a valid proxy address.
Definition: optionsdialog.h:24
Utility functions used by the Bitcoin Qt UI.
Definition: bitcoingui.h:59
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:1339
State validate(QString &input, int &pos) const override
virtual bool getProxy(Network net, proxyType &proxy_info)=0
Get proxy.
IPv4.
Definition: netaddress.h:51
void valueChanged()
void setOkButtonState(bool fState)
const QString & getOverriddenByCommandLine()
Definition: optionsmodel.h:91
#define PACKAGE_NAME
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:30
QFont fixedPitchFont(bool use_embedded_font)
Definition: guiutil.cpp:87
static constexpr uint64_t GB_BYTES
Definition: guiconstants.h:53
void on_resetButton_clicked()
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:229
Line edit that can be marked as "invalid" to show input validation feedback.
State
The various states a (txhash,peer) pair can be in.
Definition: txrequest.cpp:38
constexpr auto dialog_flags
Definition: guiutil.h:59
void togglePruneWarning(bool enabled)
bool isRestartRequired() const
interfaces::Node & node() const
Definition: optionsmodel.h:101
OptionsModel * model
Definition: optionsdialog.h:74
void on_openBitcoinConfButton_clicked()
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:402
std::string ToStringIP() const
Definition: netaddress.cpp:608
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:560
void validationDidChange(QValidatedLineEdit *validatedLineEdit)
void setModel(OptionsModel *model)
QDataWidgetMapper * mapper
Definition: optionsdialog.h:75
void on_okButton_clicked()
void updateDefaultProxyNets()
void setCurrentTab(OptionsDialog::Tab tab)
static constexpr uint16_t DEFAULT_GUI_PROXY_PORT
Definition: optionsmodel.h:21
bool openBitcoinConf()
Definition: guiutil.cpp:416
CService proxy
Definition: netbase.h:56
bool IsValid() const
Definition: netbase.h:54
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:39
void on_showTrayIcon_stateChanged(int state)
static const int64_t nMaxDbCache
max. -dbcache (MiB)
Definition: txdb.h:28
IPv6.
Definition: netaddress.h:54
TOR (v2 or v3)
Definition: netaddress.h:57
void showRestartWarning(bool fPersistent=false)
void on_cancelButton_clicked()
Preferences dialog.
Definition: optionsdialog.h:35
void clearStatusLabel()
void updateProxyValidationState()
static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES
Definition: validation.h:99
virtual void mapPort(bool use_upnp, bool use_natpmp)=0
Map port.
ProxyAddressValidator(QObject *parent)
static const int MAX_SCRIPTCHECK_THREADS
Maximum number of dedicated script-checking threads allowed.
Definition: validation.h:75