Bitcoin Core  21.99.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),
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  /* Display elements init */
96  QDir translations(":translations");
97 
98  ui->bitcoinAtStartup->setToolTip(ui->bitcoinAtStartup->toolTip().arg(PACKAGE_NAME));
99  ui->bitcoinAtStartup->setText(ui->bitcoinAtStartup->text().arg(PACKAGE_NAME));
100 
101  ui->openBitcoinConfButton->setToolTip(ui->openBitcoinConfButton->toolTip().arg(PACKAGE_NAME));
102 
103  ui->lang->setToolTip(ui->lang->toolTip().arg(PACKAGE_NAME));
104  ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
105  for (const QString &langStr : translations.entryList())
106  {
107  QLocale locale(langStr);
108 
110  if(langStr.contains("_"))
111  {
113  ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
114  }
115  else
116  {
118  ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
119  }
120  }
121  ui->unit->setModel(new BitcoinUnits(this));
122 
123  /* Widget-to-option mapper */
124  mapper = new QDataWidgetMapper(this);
125  mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
126  mapper->setOrientation(Qt::Vertical);
127 
129  connect(delegate, &GUIUtil::ItemDelegate::keyEscapePressed, this, &OptionsDialog::reject);
130  mapper->setItemDelegate(delegate);
131 
132  /* setup/change UI elements when proxy IPs are invalid/valid */
133  ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
134  ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
137  connect(ui->proxyPort, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
138  connect(ui->proxyPortTor, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
139 
140  if (!QSystemTrayIcon::isSystemTrayAvailable()) {
141  ui->showTrayIcon->setChecked(false);
142  ui->showTrayIcon->setEnabled(false);
143  ui->minimizeToTray->setChecked(false);
144  ui->minimizeToTray->setEnabled(false);
145  }
146 
148 }
149 
151 {
152  delete ui;
153 }
154 
156 {
157  this->model = _model;
158 
159  if(_model)
160  {
161  /* check if client restart is needed and show persistent message */
162  if (_model->isRestartRequired())
163  showRestartWarning(true);
164 
165  // Prune values are in GB to be consistent with intro.cpp
166  static constexpr uint64_t nMinDiskSpace = (MIN_DISK_SPACE_FOR_BLOCK_FILES / GB_BYTES) + (MIN_DISK_SPACE_FOR_BLOCK_FILES % GB_BYTES) ? 1 : 0;
167  ui->pruneSize->setRange(nMinDiskSpace, std::numeric_limits<int>::max());
168 
169  QString strLabel = _model->getOverriddenByCommandLine();
170  if (strLabel.isEmpty())
171  strLabel = tr("none");
172  ui->overriddenByCommandLineLabel->setText(strLabel);
173 
174  mapper->setModel(_model);
175  setMapper();
176  mapper->toFirst();
177 
179  }
180 
181  /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
182 
183  /* Main */
184  connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
185  connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::togglePruneWarning);
186  connect(ui->pruneSize, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
187  connect(ui->databaseCache, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
188  connect(ui->threadsScriptVerif, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
189  /* Wallet */
190  connect(ui->spendZeroConfChange, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
191  /* Network */
192  connect(ui->allowIncoming, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
193  connect(ui->connectSocks, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
194  connect(ui->connectSocksTor, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
195  /* Display */
196  connect(ui->lang, static_cast<void (QValueComboBox::*)()>(&QValueComboBox::valueChanged), [this]{ showRestartWarning(); });
197  connect(ui->thirdPartyTxUrls, &QLineEdit::textChanged, [this]{ showRestartWarning(); });
198 }
199 
201 {
202  QWidget *tab_widget = nullptr;
203  if (tab == OptionsDialog::Tab::TAB_NETWORK) tab_widget = ui->tabNetwork;
204  if (tab == OptionsDialog::Tab::TAB_MAIN) tab_widget = ui->tabMain;
205  if (tab_widget && ui->tabWidget->currentWidget() != tab_widget) {
206  ui->tabWidget->setCurrentWidget(tab_widget);
207  }
208 }
209 
211 {
212  /* Main */
213  mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
214  mapper->addMapping(ui->threadsScriptVerif, OptionsModel::ThreadsScriptVerif);
215  mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
216  mapper->addMapping(ui->prune, OptionsModel::Prune);
217  mapper->addMapping(ui->pruneSize, OptionsModel::PruneSize);
218 
219  /* Wallet */
220  mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
221  mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
222 
223  /* Network */
224  mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
225  mapper->addMapping(ui->mapPortNatpmp, OptionsModel::MapPortNatpmp);
226  mapper->addMapping(ui->allowIncoming, OptionsModel::Listen);
227 
228  mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
229  mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
230  mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
231 
232  mapper->addMapping(ui->connectSocksTor, OptionsModel::ProxyUseTor);
233  mapper->addMapping(ui->proxyIpTor, OptionsModel::ProxyIPTor);
234  mapper->addMapping(ui->proxyPortTor, OptionsModel::ProxyPortTor);
235 
236  /* Window */
237 #ifndef Q_OS_MAC
238  if (QSystemTrayIcon::isSystemTrayAvailable()) {
239  mapper->addMapping(ui->showTrayIcon, OptionsModel::ShowTrayIcon);
240  mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
241  }
242  mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
243 #endif
244 
245  /* Display */
246  mapper->addMapping(ui->lang, OptionsModel::Language);
247  mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
248  mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
249 }
250 
252 {
253  ui->okButton->setEnabled(fState);
254 }
255 
257 {
258  if(model)
259  {
260  // confirmation dialog
261  QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
262  tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shut down. Do you want to proceed?"),
263  QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
264 
265  if(btnRetVal == QMessageBox::Cancel)
266  return;
267 
268  /* reset all options and close GUI */
269  model->Reset();
270  QApplication::quit();
271  }
272 }
273 
275 {
276  /* explain the purpose of the config file */
277  QMessageBox::information(this, tr("Configuration options"),
278  tr("The configuration file is used to specify advanced user options which override GUI settings. "
279  "Additionally, any command-line options will override this configuration file."));
280 
281  /* show an error if there was some problem opening the file */
283  QMessageBox::critical(this, tr("Error"), tr("The configuration file could not be opened."));
284 }
285 
287 {
288  mapper->submit();
289  accept();
291 }
292 
294 {
295  reject();
296 }
297 
299 {
300  if (state == Qt::Checked) {
301  ui->minimizeToTray->setEnabled(true);
302  } else {
303  ui->minimizeToTray->setChecked(false);
304  ui->minimizeToTray->setEnabled(false);
305  }
306 }
307 
309 {
310  ui->pruneWarning->setVisible(!ui->pruneWarning->isVisible());
311 }
312 
313 void OptionsDialog::showRestartWarning(bool fPersistent)
314 {
315  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
316 
317  if(fPersistent)
318  {
319  ui->statusLabel->setText(tr("Client restart required to activate changes."));
320  }
321  else
322  {
323  ui->statusLabel->setText(tr("This change would require a client restart."));
324  // clear non-persistent status label after 10 seconds
325  // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
326  QTimer::singleShot(10000, this, &OptionsDialog::clearStatusLabel);
327  }
328 }
329 
331 {
332  ui->statusLabel->clear();
333  if (model && model->isRestartRequired()) {
334  showRestartWarning(true);
335  }
336 }
337 
339 {
340  QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
341  QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
342  if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
343  {
344  setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
346  }
347  else
348  {
349  setOkButtonState(false);
350  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
351  ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
352  }
353 }
354 
356 {
357  proxyType proxy;
358  std::string strProxy;
359  QString strDefaultProxyGUI;
360 
361  model->node().getProxy(NET_IPV4, proxy);
362  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
363  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
364  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);
365 
366  model->node().getProxy(NET_IPV6, proxy);
367  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
368  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
369  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);
370 
371  model->node().getProxy(NET_ONION, proxy);
372  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
373  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
374  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
375 }
376 
378 QValidator(parent)
379 {
380 }
381 
382 QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
383 {
384  Q_UNUSED(pos);
385  // Validate the proxy
386  CService serv(LookupNumeric(input.toStdString(), DEFAULT_GUI_PROXY_PORT));
387  proxyType addrProxy = proxyType(serv, true);
388  if (addrProxy.IsValid())
389  return QValidator::Acceptable;
390 
391  return QValidator::Invalid;
392 }
Ui::OptionsDialog * ui
Definition: optionsdialog.h:73
std::string ToStringPort() const
Definition: netaddress.cpp:972
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
CService LookupNumeric(const std::string &name, int portDefault)
Resolve a service string with a numeric IP to its first corresponding service.
Definition: netbase.cpp:261
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:1286
State validate(QString &input, int &pos) const override
virtual bool getProxy(Network net, proxyType &proxy_info)=0
Get proxy.
IPv4.
Definition: netaddress.h:49
void valueChanged()
void setOkButtonState(bool fState)
const QString & getOverriddenByCommandLine()
Definition: optionsmodel.h:88
#define PACKAGE_NAME
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:30
static constexpr uint64_t GB_BYTES
Definition: guiconstants.h:53
void on_resetButton_clicked()
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:39
void togglePruneWarning(bool enabled)
bool isRestartRequired() const
interfaces::Node & node() const
Definition: optionsmodel.h:98
OptionsModel * model
Definition: optionsdialog.h:74
void on_openBitcoinConfButton_clicked()
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:386
std::string ToStringIP() const
Definition: netaddress.cpp:533
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:523
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:400
CService proxy
Definition: netbase.h:36
bool IsValid() const
Definition: netbase.h:34
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:52
TOR (v2 or v3)
Definition: netaddress.h:55
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:95
if(it !=peer.m_getdata_requests.end() &&!pfrom.fPauseSend)
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:70