Bitcoin ABC  0.26.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 <qt/bitcoinunits.h>
15 #include <qt/guiconstants.h>
16 #include <qt/guiutil.h>
17 #include <qt/optionsmodel.h>
18 #include <txdb.h> // for -dbcache defaults
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 
30 OptionsDialog::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->setMinimum(MIN_DB_CACHE_MB);
37  ui->databaseCache->setMaximum(MAX_DB_CACHE_MB);
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 #ifndef USE_UPNP
49  ui->mapPortUpnp->setEnabled(false);
50 #endif
51 #ifndef USE_NATPMP
52  ui->mapPortNatpmp->setEnabled(false);
53 #endif
54  connect(this, &QDialog::accepted, [this]() {
55  QSettings settings;
56  model->node().mapPort(settings.value("fUseUPnP").toBool(),
57  settings.value("fUseNatpmp").toBool());
58  });
59 
60  ui->proxyIp->setEnabled(false);
61  ui->proxyPort->setEnabled(false);
62  ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
63 
64  ui->proxyIpTor->setEnabled(false);
65  ui->proxyPortTor->setEnabled(false);
66  ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));
67 
68  connect(ui->connectSocks, &QPushButton::toggled, ui->proxyIp,
69  &QWidget::setEnabled);
70  connect(ui->connectSocks, &QPushButton::toggled, ui->proxyPort,
71  &QWidget::setEnabled);
72  connect(ui->connectSocks, &QPushButton::toggled, this,
74 
75  connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyIpTor,
76  &QWidget::setEnabled);
77  connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyPortTor,
78  &QWidget::setEnabled);
79  connect(ui->connectSocksTor, &QPushButton::toggled, this,
81 
82  /* Window elements init */
83 #ifdef Q_OS_MAC
84  /* remove Window tab on Mac */
85  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
86  /* hide launch at startup option on macOS */
87  ui->bitcoinAtStartup->setVisible(false);
88  ui->verticalLayout_Main->removeWidget(ui->bitcoinAtStartup);
89  ui->verticalLayout_Main->removeItem(ui->horizontalSpacer_0_Main);
90 #endif
91 
92  /* remove Wallet tab and 3rd party-URL textbox in case of -disablewallet */
93  if (!enableWallet) {
94  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet));
95  ui->thirdPartyTxUrlsLabel->setVisible(false);
96  ui->thirdPartyTxUrls->setVisible(false);
97  }
98 
99  /* Display elements init */
100  QDir translations(":translations");
101 
102  ui->bitcoinAtStartup->setToolTip(
103  ui->bitcoinAtStartup->toolTip().arg(PACKAGE_NAME));
104  ui->bitcoinAtStartup->setText(
105  ui->bitcoinAtStartup->text().arg(PACKAGE_NAME));
106 
107  ui->openBitcoinConfButton->setToolTip(
108  ui->openBitcoinConfButton->toolTip().arg(PACKAGE_NAME));
109 
110  ui->lang->setToolTip(ui->lang->toolTip().arg(PACKAGE_NAME));
111  ui->lang->addItem(QString("(") + tr("default") + QString(")"),
112  QVariant(""));
113  for (const QString &langStr : translations.entryList()) {
114  QLocale locale(langStr);
115 
117  if (langStr.contains("_")) {
120  ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") +
121  locale.nativeCountryName() + QString(" (") +
122  langStr + QString(")"),
123  QVariant(langStr));
124  } else {
127  ui->lang->addItem(locale.nativeLanguageName() + QString(" (") +
128  langStr + QString(")"),
129  QVariant(langStr));
130  }
131  }
132  ui->unit->setModel(new BitcoinUnits(this));
133 
134  /* Widget-to-option mapper */
135  mapper = new QDataWidgetMapper(this);
136  mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
137  mapper->setOrientation(Qt::Vertical);
138 
140  connect(delegate, &GUIUtil::ItemDelegate::keyEscapePressed, this,
141  &OptionsDialog::reject);
142  mapper->setItemDelegate(delegate);
143 
144  /* setup/change UI elements when proxy IPs are invalid/valid */
145  ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
146  ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
147  connect(ui->proxyIp, &QValidatedLineEdit::validationDidChange, this,
149  connect(ui->proxyIpTor, &QValidatedLineEdit::validationDidChange, this,
151  connect(ui->proxyPort, &QLineEdit::textChanged, this,
153  connect(ui->proxyPortTor, &QLineEdit::textChanged, this,
155 
156  /* setup/change UI elements when third party tx URLs are invalid/valid */
157  ui->thirdPartyTxUrls->setCheckValidator(
158  new ThirdPartyTxUrlsValidator(parent));
159  connect(ui->thirdPartyTxUrls, &QValidatedLineEdit::validationDidChange,
161 
162  if (!QSystemTrayIcon::isSystemTrayAvailable()) {
163  ui->hideTrayIcon->setChecked(true);
164  ui->hideTrayIcon->setEnabled(false);
165  ui->minimizeToTray->setChecked(false);
166  ui->minimizeToTray->setEnabled(false);
167  }
168 
170 }
171 
173  delete ui;
174 }
175 
177  this->model = _model;
178 
179  if (_model) {
180  /* check if client restart is needed and show persistent message */
181  if (_model->isRestartRequired()) {
182  showRestartWarning(true);
183  }
184 
185  // Prune values are in GB to be consistent with intro.cpp
186  static constexpr uint64_t nMinDiskSpace =
189  ? 1
190  : 0;
191  ui->pruneSize->setRange(nMinDiskSpace, std::numeric_limits<int>::max());
192 
193  QString strLabel = _model->getOverriddenByCommandLine();
194  if (strLabel.isEmpty()) {
195  strLabel = tr("none");
196  }
197  ui->overriddenByCommandLineLabel->setText(strLabel);
198 
199  mapper->setModel(_model);
200  setMapper();
201  mapper->toFirst();
202 
204  }
205 
206  /* warn when one of the following settings changes by user action (placed
207  * here so init via mapper doesn't trigger them) */
208 
209  /* Main */
210  connect(ui->prune, &QCheckBox::clicked, this,
212  connect(ui->prune, &QCheckBox::clicked, this,
214  connect(ui->pruneSize,
215  static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this,
217  connect(ui->databaseCache,
218  static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this,
220  connect(ui->threadsScriptVerif,
221  static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this,
223  /* Wallet */
224  connect(ui->spendZeroConfChange, &QCheckBox::clicked, this,
226  /* Network */
227  connect(ui->allowIncoming, &QCheckBox::clicked, this,
229  connect(ui->connectSocks, &QCheckBox::clicked, this,
231  connect(ui->connectSocksTor, &QCheckBox::clicked, this,
233  /* Display */
234  connect(
235  ui->lang,
236  static_cast<void (QValueComboBox::*)()>(&QValueComboBox::valueChanged),
237  [this] { showRestartWarning(); });
238  connect(ui->thirdPartyTxUrls, &QLineEdit::textChanged,
239  [this] { showRestartWarning(); });
240 }
241 
243  QWidget *tab_widget = nullptr;
244  if (tab == OptionsDialog::Tab::TAB_NETWORK) {
245  tab_widget = ui->tabNetwork;
246  }
247  if (tab == OptionsDialog::Tab::TAB_MAIN) {
248  tab_widget = ui->tabMain;
249  }
250  if (tab_widget && ui->tabWidget->currentWidget() != tab_widget) {
251  ui->tabWidget->setCurrentWidget(tab_widget);
252  }
253 }
254 
256  /* Main */
257  mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
258  mapper->addMapping(ui->threadsScriptVerif,
260  mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
261  mapper->addMapping(ui->prune, OptionsModel::Prune);
262  mapper->addMapping(ui->pruneSize, OptionsModel::PruneSize);
263 
264  /* Wallet */
265  mapper->addMapping(ui->spendZeroConfChange,
267  mapper->addMapping(ui->coinControlFeatures,
269 
270  /* Network */
271  mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
272  mapper->addMapping(ui->mapPortNatpmp, OptionsModel::MapPortNatpmp);
273  mapper->addMapping(ui->allowIncoming, OptionsModel::Listen);
274 
275  mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
276  mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
277  mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
278 
279  mapper->addMapping(ui->connectSocksTor, OptionsModel::ProxyUseTor);
280  mapper->addMapping(ui->proxyIpTor, OptionsModel::ProxyIPTor);
281  mapper->addMapping(ui->proxyPortTor, OptionsModel::ProxyPortTor);
282 
283 /* Window */
284 #ifndef Q_OS_MAC
285  if (QSystemTrayIcon::isSystemTrayAvailable()) {
286  mapper->addMapping(ui->hideTrayIcon, OptionsModel::HideTrayIcon);
287  mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
288  }
289  mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
290 #endif
291 
292  /* Display */
293  mapper->addMapping(ui->lang, OptionsModel::Language);
294  mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
295  mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
296 }
297 
299  ui->okButton->setEnabled(fState);
300 }
301 
303  if (model) {
304  // confirmation dialog
305  QMessageBox::StandardButton btnRetVal = QMessageBox::question(
306  this, tr("Confirm options reset"),
307  tr("Client restart required to activate changes.") + "<br><br>" +
308  tr("Client will be shut down. Do you want to proceed?"),
309  QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
310 
311  if (btnRetVal == QMessageBox::Cancel) {
312  return;
313  }
314 
315  /* reset all options and close GUI */
316  model->Reset();
317  QApplication::quit();
318  }
319 }
320 
322  /* explain the purpose of the config file */
323  QMessageBox::information(
324  this, tr("Configuration options"),
325  tr("The configuration file is used to specify advanced user options "
326  "which override GUI settings. Additionally, any command-line "
327  "options will override this configuration file."));
328 
329  /* show an error if there was some problem opening the file */
330  if (!GUIUtil::openBitcoinConf()) {
331  QMessageBox::critical(
332  this, tr("Error"),
333  tr("The configuration file could not be opened."));
334  }
335 }
336 
338  mapper->submit();
339  accept();
341 }
342 
344  reject();
345 }
346 
348  if (fState) {
349  ui->minimizeToTray->setChecked(false);
350  ui->minimizeToTray->setEnabled(false);
351  } else {
352  ui->minimizeToTray->setEnabled(true);
353  }
354 }
355 
357  ui->pruneWarning->setVisible(!ui->pruneWarning->isVisible());
358 }
359 
360 void OptionsDialog::showRestartWarning(bool fPersistent) {
361  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
362 
363  if (fPersistent) {
364  ui->statusLabel->setText(
365  tr("Client restart required to activate changes."));
366  } else {
367  ui->statusLabel->setText(
368  tr("This change would require a client restart."));
369  // clear non-persistent status label after 10 seconds
370  // TODO: should perhaps be a class attribute, if we extend the use of
371  // statusLabel
372  QTimer::singleShot(10000, this, &OptionsDialog::clearStatusLabel);
373  }
374 }
375 
377  ui->statusLabel->clear();
378  if (model && model->isRestartRequired()) {
379  showRestartWarning(true);
380  }
381 }
382 
384  QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
385  QValidatedLineEdit *otherProxyWidget =
386  (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
387  if (pUiProxyIp->isValid() &&
388  (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) &&
389  (!ui->proxyPortTor->isEnabled() ||
390  ui->proxyPortTor->text().toInt() > 0)) {
391  // Only enable ok button if both proxys are valid
392  setOkButtonState(otherProxyWidget->isValid());
394  } else {
395  setOkButtonState(false);
396  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
397  ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
398  }
399 }
400 
402  proxyType proxy;
403  std::string strProxy;
404  QString strDefaultProxyGUI;
405 
406  model->node().getProxy(NET_IPV4, proxy);
407  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
408  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
409  (strProxy == strDefaultProxyGUI.toStdString())
410  ? ui->proxyReachIPv4->setChecked(true)
411  : ui->proxyReachIPv4->setChecked(false);
412 
413  model->node().getProxy(NET_IPV6, proxy);
414  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
415  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
416  (strProxy == strDefaultProxyGUI.toStdString())
417  ? ui->proxyReachIPv6->setChecked(true)
418  : ui->proxyReachIPv6->setChecked(false);
419 
420  model->node().getProxy(NET_ONION, proxy);
421  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
422  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
423  (strProxy == strDefaultProxyGUI.toStdString())
424  ? ui->proxyReachTor->setChecked(true)
425  : ui->proxyReachTor->setChecked(false);
426 }
427 
429  : QValidator(parent) {}
430 
431 QValidator::State ProxyAddressValidator::validate(QString &input,
432  int &pos) const {
433  Q_UNUSED(pos);
434  // Validate the proxy
435  CService serv(LookupNumeric(input.toStdString(), DEFAULT_GUI_PROXY_PORT));
436  proxyType addrProxy = proxyType(serv, true);
437  if (addrProxy.IsValid()) {
438  return QValidator::Acceptable;
439  }
440 
441  return QValidator::Invalid;
442 }
443 
445  QValidatedLineEdit *thirdPartyTxUrls = ui->thirdPartyTxUrls;
446  if (thirdPartyTxUrls->isValid()) {
447  // Only enable OK button if the third party tx URLS pattern is valid
448  setOkButtonState(true);
450  } else {
451  setOkButtonState(false);
452  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
453  ui->statusLabel->setText(
454  tr("The third party transaction URLs should start with https://."));
455  }
456 }
457 
459  : QValidator(parent) {}
460 
461 QValidator::State ThirdPartyTxUrlsValidator::validate(QString &input,
462  int &pos) const {
463  Q_UNUSED(pos);
464  // Check the URL starts with https. All other schemes are rejected for
465  // security reasons.
466  if (input.isEmpty() || input.startsWith("https://")) {
467  return QValidator::Acceptable;
468  }
469 
470  return QValidator::Invalid;
471 }
Bitcoin unit definitions.
Definition: bitcoinunits.h:32
std::string ToStringIP() const
Definition: netaddress.cpp:578
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:545
std::string ToStringPort() const
Preferences dialog.
Definition: optionsdialog.h:43
void setModel(OptionsModel *model)
OptionsModel * model
Definition: optionsdialog.h:82
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:81
OptionsDialog(QWidget *parent, bool enableWallet)
QDataWidgetMapper * mapper
Definition: optionsdialog.h:83
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
const QString & getOverriddenByCommandLine()
Definition: optionsmodel.h:101
interfaces::Node & node() const
Definition: optionsmodel.h:113
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
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, proxyType &proxy_info)=0
Get proxy.
bool IsValid() const
Definition: netbase.h:38
CService proxy
Definition: netbase.h:40
static constexpr uint64_t GB_BYTES
Definition: guiconstants.h:50
bool openBitcoinConf()
Definition: guiutil.cpp:414
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:399
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:57
@ NET_IPV6
IPv6.
Definition: netaddress.h:54
@ NET_IPV4
IPv4.
Definition: netaddress.h:51
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:260
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:1371
static constexpr int64_t MAX_DB_CACHE_MB
max. -dbcache (MiB)
Definition: txdb.h:32
static constexpr int64_t MIN_DB_CACHE_MB
min. -dbcache (MiB)
Definition: txdb.h:30
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:117
static const int MAX_SCRIPTCHECK_THREADS
Maximum number of dedicated script-checking threads allowed.
Definition: validation.h:81