Bitcoin Core  24.99.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>
10 #include <qt/forms/ui_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 {
38  ui->setupUi(this);
39 
40  /* Main elements init */
41  ui->databaseCache->setMinimum(nMinDbCache);
42  ui->databaseCache->setMaximum(nMaxDbCache);
43  ui->threadsScriptVerif->setMinimum(-GetNumCores());
44  ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
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);
82  ui->verticalLayout_Main->removeItem(ui->horizontalSpacer_0_Main);
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 */
137  ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
138  ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
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 */
238  mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
239  mapper->addMapping(ui->threadsScriptVerif, OptionsModel::ThreadsScriptVerif);
240  mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
241  mapper->addMapping(ui->prune, OptionsModel::Prune);
242  mapper->addMapping(ui->pruneSize, OptionsModel::PruneSize);
243 
244  /* Wallet */
245  mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
246  mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
247  mapper->addMapping(ui->subFeeFromAmount, OptionsModel::SubFeeFromAmount);
248  mapper->addMapping(ui->externalSignerPath, OptionsModel::ExternalSignerPath);
249  mapper->addMapping(ui->m_enable_psbt_controls, OptionsModel::EnablePSBTControls);
250 
251  /* Network */
252  mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
253  mapper->addMapping(ui->mapPortNatpmp, OptionsModel::MapPortNatpmp);
254  mapper->addMapping(ui->allowIncoming, OptionsModel::Listen);
255  mapper->addMapping(ui->enableServer, OptionsModel::Server);
256 
257  mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
258  mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
259  mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
260 
261  mapper->addMapping(ui->connectSocksTor, OptionsModel::ProxyUseTor);
262  mapper->addMapping(ui->proxyIpTor, OptionsModel::ProxyIPTor);
263  mapper->addMapping(ui->proxyPortTor, OptionsModel::ProxyPortTor);
264 
265  /* Window */
266 #ifndef Q_OS_MACOS
267  if (QSystemTrayIcon::isSystemTrayAvailable()) {
268  mapper->addMapping(ui->showTrayIcon, OptionsModel::ShowTrayIcon);
269  mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
270  }
271  mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
272 #endif
273 
274  /* Display */
275  mapper->addMapping(ui->lang, OptionsModel::Language);
276  mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
277  mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
278  mapper->addMapping(ui->embeddedFont_radioButton, OptionsModel::UseEmbeddedMonospacedFont);
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 }
if(!SetupNetworking())
#define PACKAGE_NAME
Bitcoin unit definitions.
Definition: bitcoinunits.h:33
std::string ToStringIP() const
Definition: netaddress.cpp:602
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:523
std::string ToStringPort() const
Definition: netaddress.cpp:919
Model for Bitcoin network client.
Definition: clientmodel.h:55
QString dataDir() const
Preferences dialog.
Definition: optionsdialog.h:37
void setModel(OptionsModel *model)
OptionsModel * model
Definition: optionsdialog.h:78
void setCurrentTab(OptionsDialog::Tab tab)
void on_showTrayIcon_stateChanged(int state)
void on_okButton_clicked()
void on_openBitcoinConfButton_clicked()
void updateDefaultProxyNets()
void updateProxyValidationState()
void togglePruneWarning(bool enabled)
void showRestartWarning(bool fPersistent=false)
ClientModel * m_client_model
Definition: optionsdialog.h:77
void on_resetButton_clicked()
Ui::OptionsDialog * ui
Definition: optionsdialog.h:76
void quitOnReset()
OptionsDialog(QWidget *parent, bool enableWallet)
QDataWidgetMapper * mapper
Definition: optionsdialog.h:79
void clearStatusLabel()
void setClientModel(ClientModel *client_model)
void on_cancelButton_clicked()
void setOkButtonState(bool fState)
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:41
bool isRestartRequired() const
@ UseEmbeddedMonospacedFont
Definition: optionsmodel.h:63
const QString & getOverriddenByCommandLine()
Definition: optionsmodel.h:99
interfaces::Node & node() const
Definition: optionsmodel.h:108
Proxy address widget validator, checks for a valid proxy address.
Definition: optionsdialog.h:26
ProxyAddressValidator(QObject *parent)
State validate(QString &input, int &pos) const override
Definition: netbase.h:49
bool IsValid() const
Definition: netbase.h:54
CService proxy
Definition: netbase.h:56
Line edit that can be marked as "invalid" to show input validation feedback.
void validationDidChange(QValidatedLineEdit *validatedLineEdit)
void valueChanged()
virtual bool getProxy(Network net, Proxy &proxy_info)=0
Get proxy.
static constexpr uint64_t GB_BYTES
Definition: guiconstants.h:57
Utility functions used by the Bitcoin Qt UI.
Definition: bitcoingui.h:60
bool openBitcoinConf()
Definition: guiutil.cpp:429
QFont fixedPitchFont(bool use_embedded_font)
Definition: guiutil.cpp:96
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:415
constexpr auto dialog_flags
Definition: guiutil.h:60
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:55
@ NET_IPV6
IPv6.
Definition: netaddress.h:52
@ NET_IPV4
IPv4.
Definition: netaddress.h:49
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
static constexpr uint16_t DEFAULT_GUI_PROXY_PORT
Definition: optionsmodel.h:22
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:36
static const int64_t nMaxDbCache
max. -dbcache (MiB)
Definition: txdb.h:34
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:1377
static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES
Definition: validation.h:81
static const int MAX_SCRIPTCHECK_THREADS
Maximum number of dedicated script-checking threads allowed.
Definition: validation.h:64