Bitcoin Core  26.1.0
P2P Digital Currency
optionsdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2022 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>
11 
12 #include <qt/bitcoinunits.h>
13 #include <qt/clientmodel.h>
14 #include <qt/guiconstants.h>
15 #include <qt/guiutil.h>
16 #include <qt/optionsmodel.h>
17 
18 #include <common/system.h>
19 #include <interfaces/node.h>
20 #include <netbase.h>
21 #include <txdb.h>
22 #include <validation.h>
23 
24 #include <chrono>
25 
26 #include <QDataWidgetMapper>
27 #include <QDir>
28 #include <QIntValidator>
29 #include <QLocale>
30 #include <QMessageBox>
31 #include <QSystemTrayIcon>
32 #include <QTimer>
33 
34 OptionsDialog::OptionsDialog(QWidget* parent, bool enableWallet)
35  : QDialog(parent, GUIUtil::dialog_flags),
36  ui(new Ui::OptionsDialog)
37 {
38  ui->setupUi(this);
39 
40  /* Main elements init */
41  ui->databaseCache->setMinimum(nMinDbCache);
42  ui->databaseCache->setMaximum(nMaxDbCache);
43  ui->threadsScriptVerif->setMinimum(-GetNumCores());
45  ui->pruneWarning->setVisible(false);
46  ui->pruneWarning->setStyleSheet("QLabel { color: red; }");
47 
48  ui->pruneSize->setEnabled(false);
49  connect(ui->prune, &QPushButton::toggled, ui->pruneSize, &QWidget::setEnabled);
50 
51  /* Network elements init */
52 #ifndef USE_UPNP
53  ui->mapPortUpnp->setEnabled(false);
54 #endif
55 #ifndef USE_NATPMP
56  ui->mapPortNatpmp->setEnabled(false);
57 #endif
58 
59  ui->proxyIp->setEnabled(false);
60  ui->proxyPort->setEnabled(false);
61  ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
62 
63  ui->proxyIpTor->setEnabled(false);
64  ui->proxyPortTor->setEnabled(false);
65  ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));
66 
67  connect(ui->connectSocks, &QPushButton::toggled, ui->proxyIp, &QWidget::setEnabled);
68  connect(ui->connectSocks, &QPushButton::toggled, ui->proxyPort, &QWidget::setEnabled);
69  connect(ui->connectSocks, &QPushButton::toggled, this, &OptionsDialog::updateProxyValidationState);
70 
71  connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyIpTor, &QWidget::setEnabled);
72  connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyPortTor, &QWidget::setEnabled);
73  connect(ui->connectSocksTor, &QPushButton::toggled, this, &OptionsDialog::updateProxyValidationState);
74 
75  /* Window elements init */
76 #ifdef Q_OS_MACOS
77  /* remove Window tab on Mac */
78  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
79  /* hide launch at startup option on macOS */
80  ui->bitcoinAtStartup->setVisible(false);
81  ui->verticalLayout_Main->removeWidget(ui->bitcoinAtStartup);
83 #endif
84 
85  /* remove Wallet tab and 3rd party-URL textbox in case of -disablewallet */
86  if (!enableWallet) {
87  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet));
88  ui->thirdPartyTxUrlsLabel->setVisible(false);
89  ui->thirdPartyTxUrls->setVisible(false);
90  }
91 
92 #ifdef ENABLE_EXTERNAL_SIGNER
93  ui->externalSignerPath->setToolTip(ui->externalSignerPath->toolTip().arg(PACKAGE_NAME));
94 #else
95  //: "External signing" means using devices such as hardware wallets.
96  ui->externalSignerPath->setToolTip(tr("Compiled without external signing support (required for external signing)"));
97  ui->externalSignerPath->setEnabled(false);
98 #endif
99  /* Display elements init */
100  QDir translations(":translations");
101 
102  ui->bitcoinAtStartup->setToolTip(ui->bitcoinAtStartup->toolTip().arg(PACKAGE_NAME));
103  ui->bitcoinAtStartup->setText(ui->bitcoinAtStartup->text().arg(PACKAGE_NAME));
104 
105  ui->openBitcoinConfButton->setToolTip(ui->openBitcoinConfButton->toolTip().arg(PACKAGE_NAME));
106 
107  ui->lang->setToolTip(ui->lang->toolTip().arg(PACKAGE_NAME));
108  ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
109  for (const QString &langStr : translations.entryList())
110  {
111  QLocale locale(langStr);
112 
114  if(langStr.contains("_"))
115  {
117  ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
118  }
119  else
120  {
122  ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
123  }
124  }
125  ui->unit->setModel(new BitcoinUnits(this));
126 
127  /* Widget-to-option mapper */
128  mapper = new QDataWidgetMapper(this);
129  mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
130  mapper->setOrientation(Qt::Vertical);
131 
133  connect(delegate, &GUIUtil::ItemDelegate::keyEscapePressed, this, &OptionsDialog::reject);
134  mapper->setItemDelegate(delegate);
135 
136  /* setup/change UI elements when proxy IPs are invalid/valid */
141  connect(ui->proxyPort, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
142  connect(ui->proxyPortTor, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
143 
144  if (!QSystemTrayIcon::isSystemTrayAvailable()) {
145  ui->showTrayIcon->setChecked(false);
146  ui->showTrayIcon->setEnabled(false);
147  ui->minimizeToTray->setChecked(false);
148  ui->minimizeToTray->setEnabled(false);
149  }
150 
151  QFont embedded_font{GUIUtil::fixedPitchFont(true)};
152  ui->embeddedFont_radioButton->setText(ui->embeddedFont_radioButton->text().arg(QFontInfo(embedded_font).family()));
153  embedded_font.setWeight(QFont::Bold);
154  ui->embeddedFont_label_1->setFont(embedded_font);
155  ui->embeddedFont_label_9->setFont(embedded_font);
156 
157  QFont system_font{GUIUtil::fixedPitchFont(false)};
158  ui->systemFont_radioButton->setText(ui->systemFont_radioButton->text().arg(QFontInfo(system_font).family()));
159  system_font.setWeight(QFont::Bold);
160  ui->systemFont_label_1->setFont(system_font);
161  ui->systemFont_label_9->setFont(system_font);
162  // Checking the embeddedFont_radioButton automatically unchecks the systemFont_radioButton.
163  ui->systemFont_radioButton->setChecked(true);
164 
166 }
167 
169 {
170  delete ui;
171 }
172 
174 {
175  m_client_model = client_model;
176 }
177 
179 {
180  this->model = _model;
181 
182  if(_model)
183  {
184  /* check if client restart is needed and show persistent message */
185  if (_model->isRestartRequired())
186  showRestartWarning(true);
187 
188  // Prune values are in GB to be consistent with intro.cpp
189  static constexpr uint64_t nMinDiskSpace = (MIN_DISK_SPACE_FOR_BLOCK_FILES / GB_BYTES) + (MIN_DISK_SPACE_FOR_BLOCK_FILES % GB_BYTES) ? 1 : 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  ui->overriddenByCommandLineLabel->setText(strLabel);
196 
197  mapper->setModel(_model);
198  setMapper();
199  mapper->toFirst();
200 
202  }
203 
204  /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
205 
206  /* Main */
207  connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
208  connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::togglePruneWarning);
209  connect(ui->pruneSize, qOverload<int>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
210  connect(ui->databaseCache, qOverload<int>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
211  connect(ui->externalSignerPath, &QLineEdit::textChanged, [this]{ showRestartWarning(); });
212  connect(ui->threadsScriptVerif, qOverload<int>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
213  /* Wallet */
214  connect(ui->spendZeroConfChange, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
215  /* Network */
216  connect(ui->allowIncoming, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
217  connect(ui->enableServer, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
218  connect(ui->connectSocks, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
219  connect(ui->connectSocksTor, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
220  /* Display */
221  connect(ui->lang, qOverload<>(&QValueComboBox::valueChanged), [this]{ showRestartWarning(); });
222  connect(ui->thirdPartyTxUrls, &QLineEdit::textChanged, [this]{ showRestartWarning(); });
223 }
224 
226 {
227  QWidget *tab_widget = nullptr;
228  if (tab == OptionsDialog::Tab::TAB_NETWORK) tab_widget = ui->tabNetwork;
229  if (tab == OptionsDialog::Tab::TAB_MAIN) tab_widget = ui->tabMain;
230  if (tab_widget && ui->tabWidget->currentWidget() != tab_widget) {
231  ui->tabWidget->setCurrentWidget(tab_widget);
232  }
233 }
234 
236 {
237  /* Main */
241  mapper->addMapping(ui->prune, OptionsModel::Prune);
243 
244  /* Wallet */
250 
251  /* Network */
256 
258  mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
260 
264 
265  /* Window */
266 #ifndef Q_OS_MACOS
267  if (QSystemTrayIcon::isSystemTrayAvailable()) {
270  }
272 #endif
273 
274  /* Display */
275  mapper->addMapping(ui->lang, OptionsModel::Language);
276  mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
279 }
280 
282 {
283  ui->okButton->setEnabled(fState);
284 }
285 
287 {
288  if (model) {
289  // confirmation dialog
290  /*: Text explaining that the settings changed will not come into effect
291  until the client is restarted. */
292  QString reset_dialog_text = tr("Client restart required to activate changes.") + "<br><br>";
293  /*: Text explaining to the user that the client's current settings
294  will be backed up at a specific location. %1 is a stand-in
295  argument for the backup location's path. */
296  reset_dialog_text.append(tr("Current settings will be backed up at \"%1\".").arg(m_client_model->dataDir()) + "<br><br>");
297  /*: Text asking the user to confirm if they would like to proceed
298  with a client shutdown. */
299  reset_dialog_text.append(tr("Client will be shut down. Do you want to proceed?"));
300  //: Window title text of pop-up window shown when the user has chosen to reset options.
301  QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
302  reset_dialog_text, QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
303 
304  if (btnRetVal == QMessageBox::Cancel)
305  return;
306 
307  /* reset all options and close GUI */
308  model->Reset();
309  close();
310  Q_EMIT quitOnReset();
311  }
312 }
313 
315 {
316  QMessageBox config_msgbox(this);
317  config_msgbox.setIcon(QMessageBox::Information);
318  //: Window title text of pop-up box that allows opening up of configuration file.
319  config_msgbox.setWindowTitle(tr("Configuration options"));
320  /*: Explanatory text about the priority order of instructions considered by client.
321  The order from high to low being: command-line, configuration file, GUI settings. */
322  config_msgbox.setText(tr("The configuration file is used to specify advanced user options which override GUI settings. "
323  "Additionally, any command-line options will override this configuration file."));
324 
325  QPushButton* open_button = config_msgbox.addButton(tr("Continue"), QMessageBox::ActionRole);
326  config_msgbox.addButton(tr("Cancel"), QMessageBox::RejectRole);
327  open_button->setDefault(true);
328 
329  config_msgbox.exec();
330 
331  if (config_msgbox.clickedButton() != open_button) return;
332 
333  /* show an error if there was some problem opening the file */
335  QMessageBox::critical(this, tr("Error"), tr("The configuration file could not be opened."));
336 }
337 
339 {
340  mapper->submit();
341  accept();
343 }
344 
346 {
347  reject();
348 }
349 
351 {
352  if (state == Qt::Checked) {
353  ui->minimizeToTray->setEnabled(true);
354  } else {
355  ui->minimizeToTray->setChecked(false);
356  ui->minimizeToTray->setEnabled(false);
357  }
358 }
359 
361 {
362  ui->pruneWarning->setVisible(!ui->pruneWarning->isVisible());
363 }
364 
365 void OptionsDialog::showRestartWarning(bool fPersistent)
366 {
367  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
368 
369  if(fPersistent)
370  {
371  ui->statusLabel->setText(tr("Client restart required to activate changes."));
372  }
373  else
374  {
375  ui->statusLabel->setText(tr("This change would require a client restart."));
376  // clear non-persistent status label after 10 seconds
377  // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
378  QTimer::singleShot(10s, this, &OptionsDialog::clearStatusLabel);
379  }
380 }
381 
383 {
384  ui->statusLabel->clear();
385  if (model && model->isRestartRequired()) {
386  showRestartWarning(true);
387  }
388 }
389 
391 {
392  QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
393  QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
394  if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
395  {
396  setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
398  }
399  else
400  {
401  setOkButtonState(false);
402  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
403  ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
404  }
405 }
406 
408 {
409  const std::optional<CNetAddr> ui_proxy_netaddr{LookupHost(ui->proxyIp->text().toStdString(), /*fAllowLookup=*/false)};
410  const CService ui_proxy{ui_proxy_netaddr.value_or(CNetAddr{}), ui->proxyPort->text().toUShort()};
411 
412  Proxy proxy;
413  bool has_proxy;
414 
415  has_proxy = model->node().getProxy(NET_IPV4, proxy);
416  ui->proxyReachIPv4->setChecked(has_proxy && proxy.proxy == ui_proxy);
417 
418  has_proxy = model->node().getProxy(NET_IPV6, proxy);
419  ui->proxyReachIPv6->setChecked(has_proxy && proxy.proxy == ui_proxy);
420 
421  has_proxy = model->node().getProxy(NET_ONION, proxy);
422  ui->proxyReachTor->setChecked(has_proxy && proxy.proxy == ui_proxy);
423 }
424 
426 QValidator(parent)
427 {
428 }
429 
430 QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
431 {
432  Q_UNUSED(pos);
433  // Validate the proxy
434  CService serv(LookupNumeric(input.toStdString(), DEFAULT_GUI_PROXY_PORT));
435  Proxy addrProxy = Proxy(serv, true);
436  if (addrProxy.IsValid())
437  return QValidator::Acceptable;
438 
439  return QValidator::Invalid;
440 }
QLabel * systemFont_label_1
QCheckBox * spendZeroConfChange
QTabWidget * tabWidget
Ui::OptionsDialog * ui
Definition: optionsdialog.h:76
QCheckBox * proxyReachIPv6
QLabel * embeddedFont_label_9
OptionsDialog(QWidget *parent, bool enableWallet)
Bitcoin unit definitions.
Definition: bitcoinunits.h:32
Proxy address widget validator, checks for a valid proxy address.
Definition: optionsdialog.h:25
QSpinBox * threadsScriptVerif
QPushButton * openBitcoinConfButton
Utility functions used by the Bitcoin Qt UI.
Definition: bitcoingui.h:60
void setupUi(QDialog *OptionsDialog)
void setClientModel(ClientModel *client_model)
State validate(QString &input, int &pos) const override
IPv4.
Definition: netaddress.h:41
QLineEdit * proxyPortTor
void setOkButtonState(bool fState)
QCheckBox * m_enable_psbt_controls
const QString & getOverriddenByCommandLine()
Definition: optionsmodel.h:100
#define PACKAGE_NAME
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:31
QFont fixedPitchFont(bool use_embedded_font)
Definition: guiutil.cpp:100
QCheckBox * minimizeToTray
static constexpr uint64_t GB_BYTES
Definition: guiconstants.h:57
QValueComboBox * unit
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:201
Line edit that can be marked as "invalid" to show input validation feedback.
QCheckBox * showTrayIcon
State
The various states a (txhash,peer) pair can be in.
Definition: txrequest.cpp:42
constexpr auto dialog_flags
Definition: guiutil.h:60
void togglePruneWarning(bool enabled)
bool isRestartRequired() const
interfaces::Node & node() const
Definition: optionsmodel.h:112
OptionsModel * model
Definition: optionsdialog.h:78
QCheckBox * enableServer
CService proxy
Definition: netbase.h:57
void on_openBitcoinConfButton_clicked()
QCheckBox * connectSocks
QLabel * embeddedFont_label_1
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:158
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:419
QSpinBox * pruneSize
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:534
QCheckBox * allowIncoming
void validationDidChange(QValidatedLineEdit *validatedLineEdit)
void setModel(OptionsModel *model)
virtual bool getProxy(Network net, Proxy &proxy_info)=0
Get proxy.
QDataWidgetMapper * mapper
Definition: optionsdialog.h:79
ClientModel * m_client_model
Definition: optionsdialog.h:77
void on_okButton_clicked()
void updateDefaultProxyNets()
QCheckBox * proxyReachIPv4
void setCurrentTab(OptionsDialog::Tab tab)
bool IsValid() const
Definition: netbase.h:55
QCheckBox * mapPortUpnp
Model for Bitcoin network client.
Definition: clientmodel.h:53
QValueComboBox * lang
QCheckBox * connectSocksTor
static constexpr uint16_t DEFAULT_GUI_PROXY_PORT
Definition: optionsmodel.h:22
Definition: netbase.h:49
QRadioButton * embeddedFont_radioButton
if(!SetupNetworking())
QLabel * overriddenByCommandLineLabel
QLabel * systemFont_label_9
Network address.
Definition: netaddress.h:115
QCheckBox * subFeeFromAmount
QValidatedLineEdit * proxyIpTor
QPushButton * okButton
bool openBitcoinConf()
Definition: guiutil.cpp:433
QCheckBox * minimizeOnClose
QLineEdit * thirdPartyTxUrls
QRadioButton * systemFont_radioButton
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:40
void on_showTrayIcon_stateChanged(int state)
QSpacerItem * horizontalSpacer_0_Main
QLineEdit * proxyPort
QLabel * thirdPartyTxUrlsLabel
QCheckBox * bitcoinAtStartup
QCheckBox * proxyReachTor
static const int64_t nMaxDbCache
max. -dbcache (MiB)
Definition: txdb.h:29
QSpinBox * databaseCache
IPv6.
Definition: netaddress.h:44
QVBoxLayout * verticalLayout_Main
TOR (v2 or v3)
Definition: netaddress.h:47
void showRestartWarning(bool fPersistent=false)
void setCheckValidator(const QValidator *v)
void setEnabled(bool enabled)
void on_cancelButton_clicked()
QValidatedLineEdit * proxyIp
QCheckBox * mapPortNatpmp
Preferences dialog.
Definition: optionsdialog.h:36
QString dataDir() const
void clearStatusLabel()
void updateProxyValidationState()
static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES
Definition: validation.h:84
QCheckBox * coinControlFeatures
QLineEdit * externalSignerPath
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:98
ProxyAddressValidator(QObject *parent)
static const int MAX_SCRIPTCHECK_THREADS
Maximum number of dedicated script-checking threads allowed.
Definition: validation.h:69