Bitcoin Core  24.1.0
P2P Digital Currency
optionsdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2021 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 <interfaces/node.h>
19 #include <validation.h> // for DEFAULT_SCRIPTCHECK_THREADS and MAX_SCRIPTCHECK_THREADS
20 #include <netbase.h>
21 #include <txdb.h> // for -dbcache defaults
22 #include <util/system.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  model(nullptr),
38  mapper(nullptr)
39 {
40  ui->setupUi(this);
41 
42  /* Main elements init */
43  ui->databaseCache->setMinimum(nMinDbCache);
44  ui->databaseCache->setMaximum(nMaxDbCache);
45  ui->threadsScriptVerif->setMinimum(-GetNumCores());
47  ui->pruneWarning->setVisible(false);
48  ui->pruneWarning->setStyleSheet("QLabel { color: red; }");
49 
50  ui->pruneSize->setEnabled(false);
51  connect(ui->prune, &QPushButton::toggled, ui->pruneSize, &QWidget::setEnabled);
52 
53  /* Network elements init */
54 #ifndef USE_UPNP
55  ui->mapPortUpnp->setEnabled(false);
56 #endif
57 #ifndef USE_NATPMP
58  ui->mapPortNatpmp->setEnabled(false);
59 #endif
60 
61  ui->proxyIp->setEnabled(false);
62  ui->proxyPort->setEnabled(false);
63  ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
64 
65  ui->proxyIpTor->setEnabled(false);
66  ui->proxyPortTor->setEnabled(false);
67  ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));
68 
69  connect(ui->connectSocks, &QPushButton::toggled, ui->proxyIp, &QWidget::setEnabled);
70  connect(ui->connectSocks, &QPushButton::toggled, ui->proxyPort, &QWidget::setEnabled);
71  connect(ui->connectSocks, &QPushButton::toggled, this, &OptionsDialog::updateProxyValidationState);
72 
73  connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyIpTor, &QWidget::setEnabled);
74  connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyPortTor, &QWidget::setEnabled);
75  connect(ui->connectSocksTor, &QPushButton::toggled, this, &OptionsDialog::updateProxyValidationState);
76 
77  /* Window elements init */
78 #ifdef Q_OS_MACOS
79  /* remove Window tab on Mac */
80  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
81  /* hide launch at startup option on macOS */
82  ui->bitcoinAtStartup->setVisible(false);
83  ui->verticalLayout_Main->removeWidget(ui->bitcoinAtStartup);
85 #endif
86 
87  /* remove Wallet tab and 3rd party-URL textbox in case of -disablewallet */
88  if (!enableWallet) {
89  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet));
90  ui->thirdPartyTxUrlsLabel->setVisible(false);
91  ui->thirdPartyTxUrls->setVisible(false);
92  }
93 
94 #ifndef ENABLE_EXTERNAL_SIGNER
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  Proxy proxy;
410  std::string strProxy;
411  QString strDefaultProxyGUI;
412 
413  model->node().getProxy(NET_IPV4, proxy);
414  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
415  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
416  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);
417 
418  model->node().getProxy(NET_IPV6, proxy);
419  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
420  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
421  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);
422 
423  model->node().getProxy(NET_ONION, proxy);
424  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
425  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
426  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
427 }
428 
430 QValidator(parent)
431 {
432 }
433 
434 QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
435 {
436  Q_UNUSED(pos);
437  // Validate the proxy
438  CService serv(LookupNumeric(input.toStdString(), DEFAULT_GUI_PROXY_PORT));
439  Proxy addrProxy = Proxy(serv, true);
440  if (addrProxy.IsValid())
441  return QValidator::Acceptable;
442 
443  return QValidator::Invalid;
444 }
QLabel * systemFont_label_1
QCheckBox * spendZeroConfChange
QTabWidget * tabWidget
Ui::OptionsDialog * ui
Definition: optionsdialog.h:76
std::string ToStringPort() const
Definition: netaddress.cpp:919
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:49
QLineEdit * proxyPortTor
void setOkButtonState(bool fState)
QCheckBox * m_enable_psbt_controls
const QString & getOverriddenByCommandLine()
Definition: optionsmodel.h:99
#define PACKAGE_NAME
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:34
QFont fixedPitchFont(bool use_embedded_font)
Definition: guiutil.cpp:96
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:230
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:38
constexpr auto dialog_flags
Definition: guiutil.h:60
void togglePruneWarning(bool enabled)
bool isRestartRequired() const
interfaces::Node & node() const
Definition: optionsmodel.h:108
OptionsModel * model
Definition: optionsdialog.h:78
QCheckBox * enableServer
CService proxy
Definition: netbase.h:56
void on_openBitcoinConfButton_clicked()
QCheckBox * connectSocks
QLabel * embeddedFont_label_1
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:415
std::string ToStringIP() const
Definition: netaddress.cpp:602
QSpinBox * pruneSize
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:520
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:1413
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:54
QCheckBox * mapPortUpnp
Model for Bitcoin network client.
Definition: clientmodel.h:54
QValueComboBox * lang
QCheckBox * connectSocksTor
static constexpr uint16_t DEFAULT_GUI_PROXY_PORT
Definition: optionsmodel.h:22
Definition: netbase.h:48
QRadioButton * embeddedFont_radioButton
if(!SetupNetworking())
QLabel * overriddenByCommandLineLabel
QLabel * systemFont_label_9
QCheckBox * subFeeFromAmount
QValidatedLineEdit * proxyIpTor
QPushButton * okButton
bool openBitcoinConf()
Definition: guiutil.cpp:429
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:32
QSpinBox * databaseCache
IPv6.
Definition: netaddress.h:52
QVBoxLayout * verticalLayout_Main
TOR (v2 or v3)
Definition: netaddress.h:55
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:85
QCheckBox * coinControlFeatures
QLineEdit * externalSignerPath
ProxyAddressValidator(QObject *parent)
static const int MAX_SCRIPTCHECK_THREADS
Maximum number of dedicated script-checking threads allowed.
Definition: validation.h:63