Bitcoin Core  0.20.99
P2P Digital Currency
optionsdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2019 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 <QSystemTrayIcon>
28 #include <QTimer>
29 
30 OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
31  QDialog(parent),
32  ui(new Ui::OptionsDialog),
33  model(nullptr),
34  mapper(nullptr)
35 {
36  ui->setupUi(this);
37 
38  /* Main elements init */
39  ui->databaseCache->setMinimum(nMinDbCache);
40  ui->databaseCache->setMaximum(nMaxDbCache);
41  ui->threadsScriptVerif->setMinimum(-GetNumCores());
42  ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
43  ui->pruneWarning->setVisible(false);
44  ui->pruneWarning->setStyleSheet("QLabel { color: red; }");
45 
46  ui->pruneSize->setEnabled(false);
47  connect(ui->prune, &QPushButton::toggled, ui->pruneSize, &QWidget::setEnabled);
48 
49  /* Network elements init */
50 #ifndef USE_UPNP
51  ui->mapPortUpnp->setEnabled(false);
52 #endif
53 
54  ui->proxyIp->setEnabled(false);
55  ui->proxyPort->setEnabled(false);
56  ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
57 
58  ui->proxyIpTor->setEnabled(false);
59  ui->proxyPortTor->setEnabled(false);
60  ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));
61 
62  connect(ui->connectSocks, &QPushButton::toggled, ui->proxyIp, &QWidget::setEnabled);
63  connect(ui->connectSocks, &QPushButton::toggled, ui->proxyPort, &QWidget::setEnabled);
64  connect(ui->connectSocks, &QPushButton::toggled, this, &OptionsDialog::updateProxyValidationState);
65 
66  connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyIpTor, &QWidget::setEnabled);
67  connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyPortTor, &QWidget::setEnabled);
68  connect(ui->connectSocksTor, &QPushButton::toggled, this, &OptionsDialog::updateProxyValidationState);
69 
70  /* Window elements init */
71 #ifdef Q_OS_MAC
72  /* remove Window tab on Mac */
73  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
74  /* hide launch at startup option on macOS */
75  ui->bitcoinAtStartup->setVisible(false);
76  ui->verticalLayout_Main->removeWidget(ui->bitcoinAtStartup);
77  ui->verticalLayout_Main->removeItem(ui->horizontalSpacer_0_Main);
78 #endif
79 
80  /* remove Wallet tab and 3rd party-URL textbox in case of -disablewallet */
81  if (!enableWallet) {
82  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet));
83  ui->thirdPartyTxUrlsLabel->setVisible(false);
84  ui->thirdPartyTxUrls->setVisible(false);
85  }
86 
87  /* Display elements init */
88  QDir translations(":translations");
89 
90  ui->bitcoinAtStartup->setToolTip(ui->bitcoinAtStartup->toolTip().arg(PACKAGE_NAME));
91  ui->bitcoinAtStartup->setText(ui->bitcoinAtStartup->text().arg(PACKAGE_NAME));
92 
93  ui->openBitcoinConfButton->setToolTip(ui->openBitcoinConfButton->toolTip().arg(PACKAGE_NAME));
94 
95  ui->lang->setToolTip(ui->lang->toolTip().arg(PACKAGE_NAME));
96  ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
97  for (const QString &langStr : translations.entryList())
98  {
99  QLocale locale(langStr);
100 
102  if(langStr.contains("_"))
103  {
105  ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
106  }
107  else
108  {
110  ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
111  }
112  }
113  ui->unit->setModel(new BitcoinUnits(this));
114 
115  /* Widget-to-option mapper */
116  mapper = new QDataWidgetMapper(this);
117  mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
118  mapper->setOrientation(Qt::Vertical);
119 
121  connect(delegate, &GUIUtil::ItemDelegate::keyEscapePressed, this, &OptionsDialog::reject);
122  mapper->setItemDelegate(delegate);
123 
124  /* setup/change UI elements when proxy IPs are invalid/valid */
125  ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
126  ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
129  connect(ui->proxyPort, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
130  connect(ui->proxyPortTor, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
131 
132  if (!QSystemTrayIcon::isSystemTrayAvailable()) {
133  ui->hideTrayIcon->setChecked(true);
134  ui->hideTrayIcon->setEnabled(false);
135  ui->minimizeToTray->setChecked(false);
136  ui->minimizeToTray->setEnabled(false);
137  }
138 
140 }
141 
143 {
144  delete ui;
145 }
146 
148 {
149  this->model = _model;
150 
151  if(_model)
152  {
153  /* check if client restart is needed and show persistent message */
154  if (_model->isRestartRequired())
155  showRestartWarning(true);
156 
157  // Prune values are in GB to be consistent with intro.cpp
158  static constexpr uint64_t nMinDiskSpace = (MIN_DISK_SPACE_FOR_BLOCK_FILES / GB_BYTES) + (MIN_DISK_SPACE_FOR_BLOCK_FILES % GB_BYTES) ? 1 : 0;
159  ui->pruneSize->setRange(nMinDiskSpace, std::numeric_limits<int>::max());
160 
161  QString strLabel = _model->getOverriddenByCommandLine();
162  if (strLabel.isEmpty())
163  strLabel = tr("none");
164  ui->overriddenByCommandLineLabel->setText(strLabel);
165 
166  mapper->setModel(_model);
167  setMapper();
168  mapper->toFirst();
169 
171  }
172 
173  /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
174 
175  /* Main */
176  connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
177  connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::togglePruneWarning);
178  connect(ui->pruneSize, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
179  connect(ui->databaseCache, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
180  connect(ui->threadsScriptVerif, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
181  /* Wallet */
182  connect(ui->spendZeroConfChange, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
183  /* Network */
184  connect(ui->allowIncoming, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
185  connect(ui->connectSocks, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
186  connect(ui->connectSocksTor, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
187  /* Display */
188  connect(ui->lang, static_cast<void (QValueComboBox::*)()>(&QValueComboBox::valueChanged), [this]{ showRestartWarning(); });
189  connect(ui->thirdPartyTxUrls, &QLineEdit::textChanged, [this]{ showRestartWarning(); });
190 }
191 
193 {
194  QWidget *tab_widget = nullptr;
195  if (tab == OptionsDialog::Tab::TAB_NETWORK) tab_widget = ui->tabNetwork;
196  if (tab == OptionsDialog::Tab::TAB_MAIN) tab_widget = ui->tabMain;
197  if (tab_widget && ui->tabWidget->currentWidget() != tab_widget) {
198  ui->tabWidget->setCurrentWidget(tab_widget);
199  }
200 }
201 
203 {
204  /* Main */
205  mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
206  mapper->addMapping(ui->threadsScriptVerif, OptionsModel::ThreadsScriptVerif);
207  mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
208  mapper->addMapping(ui->prune, OptionsModel::Prune);
209  mapper->addMapping(ui->pruneSize, OptionsModel::PruneSize);
210 
211  /* Wallet */
212  mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
213  mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
214 
215  /* Network */
216  mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
217  mapper->addMapping(ui->allowIncoming, OptionsModel::Listen);
218 
219  mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
220  mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
221  mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
222 
223  mapper->addMapping(ui->connectSocksTor, OptionsModel::ProxyUseTor);
224  mapper->addMapping(ui->proxyIpTor, OptionsModel::ProxyIPTor);
225  mapper->addMapping(ui->proxyPortTor, OptionsModel::ProxyPortTor);
226 
227  /* Window */
228 #ifndef Q_OS_MAC
229  if (QSystemTrayIcon::isSystemTrayAvailable()) {
230  mapper->addMapping(ui->hideTrayIcon, OptionsModel::HideTrayIcon);
231  mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
232  }
233  mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
234 #endif
235 
236  /* Display */
237  mapper->addMapping(ui->lang, OptionsModel::Language);
238  mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
239  mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
240 }
241 
243 {
244  ui->okButton->setEnabled(fState);
245 }
246 
248 {
249  if(model)
250  {
251  // confirmation dialog
252  QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
253  tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shut down. Do you want to proceed?"),
254  QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
255 
256  if(btnRetVal == QMessageBox::Cancel)
257  return;
258 
259  /* reset all options and close GUI */
260  model->Reset();
261  QApplication::quit();
262  }
263 }
264 
266 {
267  /* explain the purpose of the config file */
268  QMessageBox::information(this, tr("Configuration options"),
269  tr("The configuration file is used to specify advanced user options which override GUI settings. "
270  "Additionally, any command-line options will override this configuration file."));
271 
272  /* show an error if there was some problem opening the file */
274  QMessageBox::critical(this, tr("Error"), tr("The configuration file could not be opened."));
275 }
276 
278 {
279  mapper->submit();
280  accept();
282 }
283 
285 {
286  reject();
287 }
288 
290 {
291  if(fState)
292  {
293  ui->minimizeToTray->setChecked(false);
294  ui->minimizeToTray->setEnabled(false);
295  }
296  else
297  {
298  ui->minimizeToTray->setEnabled(true);
299  }
300 }
301 
303 {
304  ui->pruneWarning->setVisible(!ui->pruneWarning->isVisible());
305 }
306 
307 void OptionsDialog::showRestartWarning(bool fPersistent)
308 {
309  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
310 
311  if(fPersistent)
312  {
313  ui->statusLabel->setText(tr("Client restart required to activate changes."));
314  }
315  else
316  {
317  ui->statusLabel->setText(tr("This change would require a client restart."));
318  // clear non-persistent status label after 10 seconds
319  // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
320  QTimer::singleShot(10000, this, &OptionsDialog::clearStatusLabel);
321  }
322 }
323 
325 {
326  ui->statusLabel->clear();
327  if (model && model->isRestartRequired()) {
328  showRestartWarning(true);
329  }
330 }
331 
333 {
334  QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
335  QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
336  if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
337  {
338  setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
340  }
341  else
342  {
343  setOkButtonState(false);
344  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
345  ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
346  }
347 }
348 
350 {
351  proxyType proxy;
352  std::string strProxy;
353  QString strDefaultProxyGUI;
354 
355  model->node().getProxy(NET_IPV4, proxy);
356  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
357  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
358  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);
359 
360  model->node().getProxy(NET_IPV6, proxy);
361  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
362  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
363  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);
364 
365  model->node().getProxy(NET_ONION, proxy);
366  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
367  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
368  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
369 }
370 
372 QValidator(parent)
373 {
374 }
375 
376 QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
377 {
378  Q_UNUSED(pos);
379  // Validate the proxy
380  CService serv(LookupNumeric(input.toStdString(), DEFAULT_GUI_PROXY_PORT));
381  proxyType addrProxy = proxyType(serv, true);
382  if (addrProxy.IsValid())
383  return QValidator::Acceptable;
384 
385  return QValidator::Invalid;
386 }
Ui::OptionsDialog * ui
Definition: optionsdialog.h:73
std::string ToStringPort() const
Definition: netaddress.cpp:963
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:255
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:1262
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:87
#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:97
OptionsModel * model
Definition: optionsdialog.h:74
void on_openBitcoinConfButton_clicked()
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:385
std::string ToStringIP() const
Definition: netaddress.cpp:524
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:501
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)
void on_hideTrayIcon_stateChanged(int fState)
static constexpr uint16_t DEFAULT_GUI_PROXY_PORT
Definition: optionsmodel.h:21
bool openBitcoinConf()
Definition: guiutil.cpp:399
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
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)
ProxyAddressValidator(QObject *parent)
static const int MAX_SCRIPTCHECK_THREADS
Maximum number of dedicated script-checking threads allowed.
Definition: validation.h:70