Bitcoin Core  0.19.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 }
139 
141 {
142  delete ui;
143 }
144 
146 {
147  this->model = _model;
148 
149  if(_model)
150  {
151  /* check if client restart is needed and show persistent message */
152  if (_model->isRestartRequired())
153  showRestartWarning(true);
154 
155  // Prune values are in GB to be consistent with intro.cpp
156  static constexpr uint64_t nMinDiskSpace = (MIN_DISK_SPACE_FOR_BLOCK_FILES / GB_BYTES) + (MIN_DISK_SPACE_FOR_BLOCK_FILES % GB_BYTES) ? 1 : 0;
157  ui->pruneSize->setRange(nMinDiskSpace, std::numeric_limits<int>::max());
158 
159  QString strLabel = _model->getOverriddenByCommandLine();
160  if (strLabel.isEmpty())
161  strLabel = tr("none");
162  ui->overriddenByCommandLineLabel->setText(strLabel);
163 
164  mapper->setModel(_model);
165  setMapper();
166  mapper->toFirst();
167 
169  }
170 
171  /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
172 
173  /* Main */
174  connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
175  connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::togglePruneWarning);
176  connect(ui->pruneSize, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
177  connect(ui->databaseCache, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
178  connect(ui->threadsScriptVerif, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
179  /* Wallet */
180  connect(ui->spendZeroConfChange, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
181  /* Network */
182  connect(ui->allowIncoming, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
183  connect(ui->connectSocks, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
184  connect(ui->connectSocksTor, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
185  /* Display */
186  connect(ui->lang, static_cast<void (QValueComboBox::*)()>(&QValueComboBox::valueChanged), [this]{ showRestartWarning(); });
187  connect(ui->thirdPartyTxUrls, &QLineEdit::textChanged, [this]{ showRestartWarning(); });
188 }
189 
191 {
192  QWidget *tab_widget = nullptr;
193  if (tab == OptionsDialog::Tab::TAB_NETWORK) tab_widget = ui->tabNetwork;
194  if (tab == OptionsDialog::Tab::TAB_MAIN) tab_widget = ui->tabMain;
195  if (tab_widget && ui->tabWidget->currentWidget() != tab_widget) {
196  ui->tabWidget->setCurrentWidget(tab_widget);
197  }
198 }
199 
201 {
202  /* Main */
203  mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
204  mapper->addMapping(ui->threadsScriptVerif, OptionsModel::ThreadsScriptVerif);
205  mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
206  mapper->addMapping(ui->prune, OptionsModel::Prune);
207  mapper->addMapping(ui->pruneSize, OptionsModel::PruneSize);
208 
209  /* Wallet */
210  mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
211  mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
212 
213  /* Network */
214  mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
215  mapper->addMapping(ui->allowIncoming, OptionsModel::Listen);
216 
217  mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
218  mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
219  mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
220 
221  mapper->addMapping(ui->connectSocksTor, OptionsModel::ProxyUseTor);
222  mapper->addMapping(ui->proxyIpTor, OptionsModel::ProxyIPTor);
223  mapper->addMapping(ui->proxyPortTor, OptionsModel::ProxyPortTor);
224 
225  /* Window */
226 #ifndef Q_OS_MAC
227  if (QSystemTrayIcon::isSystemTrayAvailable()) {
228  mapper->addMapping(ui->hideTrayIcon, OptionsModel::HideTrayIcon);
229  mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
230  }
231  mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
232 #endif
233 
234  /* Display */
235  mapper->addMapping(ui->lang, OptionsModel::Language);
236  mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
237  mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
238 }
239 
241 {
242  ui->okButton->setEnabled(fState);
243 }
244 
246 {
247  if(model)
248  {
249  // confirmation dialog
250  QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
251  tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shut down. Do you want to proceed?"),
252  QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
253 
254  if(btnRetVal == QMessageBox::Cancel)
255  return;
256 
257  /* reset all options and close GUI */
258  model->Reset();
259  QApplication::quit();
260  }
261 }
262 
264 {
265  /* explain the purpose of the config file */
266  QMessageBox::information(this, tr("Configuration options"),
267  tr("The configuration file is used to specify advanced user options which override GUI settings. "
268  "Additionally, any command-line options will override this configuration file."));
269 
270  /* show an error if there was some problem opening the file */
272  QMessageBox::critical(this, tr("Error"), tr("The configuration file could not be opened."));
273 }
274 
276 {
277  mapper->submit();
278  accept();
280 }
281 
283 {
284  reject();
285 }
286 
288 {
289  if(fState)
290  {
291  ui->minimizeToTray->setChecked(false);
292  ui->minimizeToTray->setEnabled(false);
293  }
294  else
295  {
296  ui->minimizeToTray->setEnabled(true);
297  }
298 }
299 
301 {
302  ui->pruneWarning->setVisible(!ui->pruneWarning->isVisible());
303 }
304 
305 void OptionsDialog::showRestartWarning(bool fPersistent)
306 {
307  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
308 
309  if(fPersistent)
310  {
311  ui->statusLabel->setText(tr("Client restart required to activate changes."));
312  }
313  else
314  {
315  ui->statusLabel->setText(tr("This change would require a client restart."));
316  // clear non-persistent status label after 10 seconds
317  // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
318  QTimer::singleShot(10000, this, &OptionsDialog::clearStatusLabel);
319  }
320 }
321 
323 {
324  ui->statusLabel->clear();
325  if (model && model->isRestartRequired()) {
326  showRestartWarning(true);
327  }
328 }
329 
331 {
332  QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
333  QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
334  if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
335  {
336  setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
338  }
339  else
340  {
341  setOkButtonState(false);
342  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
343  ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
344  }
345 }
346 
348 {
349  proxyType proxy;
350  std::string strProxy;
351  QString strDefaultProxyGUI;
352 
353  model->node().getProxy(NET_IPV4, proxy);
354  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
355  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
356  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);
357 
358  model->node().getProxy(NET_IPV6, proxy);
359  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
360  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
361  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);
362 
363  model->node().getProxy(NET_ONION, proxy);
364  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
365  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
366  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
367 }
368 
370 QValidator(parent)
371 {
372 }
373 
374 QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
375 {
376  Q_UNUSED(pos);
377  // Validate the proxy
378  CService serv(LookupNumeric(input.toStdString(), DEFAULT_GUI_PROXY_PORT));
379  proxyType addrProxy = proxyType(serv, true);
380  if (addrProxy.IsValid())
381  return QValidator::Acceptable;
382 
383  return QValidator::Invalid;
384 }
Ui::OptionsDialog * ui
Definition: optionsdialog.h:73
std::string ToStringPort() const
Definition: netaddress.cpp:736
if(expired !=0)
Definition: validation.cpp:316
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:253
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:1123
virtual bool getProxy(Network net, proxyType &proxy_info)=0
Get proxy.
void valueChanged()
void setOkButtonState(bool fState)
const QString & getOverriddenByCommandLine()
Definition: optionsmodel.h:84
#define PACKAGE_NAME
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:30
static constexpr uint64_t GB_BYTES
Definition: guiconstants.h:52
void on_resetButton_clicked()
Line edit that can be marked as "invalid" to show input validation feedback.
void togglePruneWarning(bool enabled)
bool isRestartRequired() const
interfaces::Node & node() const
Definition: optionsmodel.h:94
OptionsModel * model
Definition: optionsdialog.h:74
void on_openBitcoinConfButton_clicked()
std::string ToStringIP() const
Definition: netaddress.cpp:333
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:150
State validate(QString &input, int &pos) const
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)
bool openBitcoinConf()
Definition: guiutil.cpp:390
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:36
static const int64_t nMaxDbCache
max. -dbcache (MiB)
Definition: txdb.h:28
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:193
ProxyAddressValidator(QObject *parent)
static constexpr unsigned short DEFAULT_GUI_PROXY_PORT
Definition: optionsmodel.h:18
static const int MAX_SCRIPTCHECK_THREADS
Maximum number of dedicated script-checking threads allowed.
Definition: validation.h:80