BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
optionsdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2018 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/guiutil.h>
14 #include <qt/optionsmodel.h>
15 
16 #include <interfaces/node.h>
17 #include <validation.h> // for DEFAULT_SCRIPTCHECK_THREADS and MAX_SCRIPTCHECK_THREADS
18 #include <netbase.h>
19 #include <txdb.h> // for -dbcache defaults
20 
21 #include <QDataWidgetMapper>
22 #include <QDir>
23 #include <QIntValidator>
24 #include <QLocale>
25 #include <QMessageBox>
26 #include <QTimer>
27 
28 OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
29  QDialog(parent),
30  ui(new Ui::OptionsDialog),
31  model(0),
32  mapper(0)
33 {
34  ui->setupUi(this);
35 
36  /* Main elements init */
37  ui->databaseCache->setMinimum(nMinDbCache);
38  ui->databaseCache->setMaximum(nMaxDbCache);
39  static const uint64_t GiB = 1024 * 1024 * 1024;
40  static const uint64_t nMinDiskSpace = MIN_DISK_SPACE_FOR_BLOCK_FILES / GiB +
41  (MIN_DISK_SPACE_FOR_BLOCK_FILES % GiB) ? 1 : 0;
42  ui->pruneSize->setMinimum(nMinDiskSpace);
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 
56  ui->proxyIp->setEnabled(false);
57  ui->proxyPort->setEnabled(false);
58  ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
59 
60  ui->proxyIpTor->setEnabled(false);
61  ui->proxyPortTor->setEnabled(false);
62  ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));
63 
64  connect(ui->connectSocks, &QPushButton::toggled, ui->proxyIp, &QWidget::setEnabled);
65  connect(ui->connectSocks, &QPushButton::toggled, ui->proxyPort, &QWidget::setEnabled);
66  connect(ui->connectSocks, &QPushButton::toggled, this, &OptionsDialog::updateProxyValidationState);
67 
68  connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyIpTor, &QWidget::setEnabled);
69  connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyPortTor, &QWidget::setEnabled);
70  connect(ui->connectSocksTor, &QPushButton::toggled, this, &OptionsDialog::updateProxyValidationState);
71 
72  /* Window elements init */
73 #ifdef Q_OS_MAC
74  /* remove Window tab on Mac */
75  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
76 #endif
77 
78  /* remove Wallet tab in case of -disablewallet */
79  if (!enableWallet) {
80  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet));
81  }
82 
83  /* Display elements init */
84  QDir translations(":translations");
85 
86  ui->bitcoinAtStartup->setToolTip(ui->bitcoinAtStartup->toolTip().arg(tr(PACKAGE_NAME)));
87  ui->bitcoinAtStartup->setText(ui->bitcoinAtStartup->text().arg(tr(PACKAGE_NAME)));
88 
89  ui->openBitcoinConfButton->setToolTip(ui->openBitcoinConfButton->toolTip().arg(tr(PACKAGE_NAME)));
90 
91  ui->lang->setToolTip(ui->lang->toolTip().arg(tr(PACKAGE_NAME)));
92  ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
93  for (const QString &langStr : translations.entryList())
94  {
95  QLocale locale(langStr);
96 
98  if(langStr.contains("_"))
99  {
101  ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
102  }
103  else
104  {
106  ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
107  }
108  }
109  ui->thirdPartyTxUrls->setPlaceholderText("https://example.com/tx/%s");
110 
111  ui->unit->setModel(new BitcoinUnits(this));
112 
113  /* Widget-to-option mapper */
114  mapper = new QDataWidgetMapper(this);
115  mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
116  mapper->setOrientation(Qt::Vertical);
117 
119  connect(delegate, &GUIUtil::ItemDelegate::keyEscapePressed, this, &OptionsDialog::reject);
120  mapper->setItemDelegate(delegate);
121 
122  /* setup/change UI elements when proxy IPs are invalid/valid */
123  ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
124  ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
127  connect(ui->proxyPort, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
128  connect(ui->proxyPortTor, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
129 }
130 
132 {
133  delete ui;
134 }
135 
137 {
138  this->model = _model;
139 
140  if(_model)
141  {
142  /* check if client restart is needed and show persistent message */
143  if (_model->isRestartRequired())
144  showRestartWarning(true);
145 
146  QString strLabel = _model->getOverriddenByCommandLine();
147  if (strLabel.isEmpty())
148  strLabel = tr("none");
149  ui->overriddenByCommandLineLabel->setText(strLabel);
150 
151  mapper->setModel(_model);
152  setMapper();
153  mapper->toFirst();
154 
156  }
157 
158  /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
159 
160  /* Main */
161  connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
162  connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::togglePruneWarning);
163  connect(ui->pruneSize, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
164  connect(ui->databaseCache, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
165  connect(ui->threadsScriptVerif, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
166  /* Wallet */
167  connect(ui->spendZeroConfChange, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
168  /* Network */
169  connect(ui->allowIncoming, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
170  connect(ui->connectSocks, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
171  connect(ui->connectSocksTor, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
172  /* Display */
173  connect(ui->lang, static_cast<void (QValueComboBox::*)()>(&QValueComboBox::valueChanged), [this]{ showRestartWarning(); });
174  connect(ui->thirdPartyTxUrls, &QLineEdit::textChanged, [this]{ showRestartWarning(); });
175 }
176 
178 {
179  QWidget *tab_widget = nullptr;
180  if (tab == OptionsDialog::Tab::TAB_NETWORK) tab_widget = ui->tabNetwork;
181  if (tab == OptionsDialog::Tab::TAB_MAIN) tab_widget = ui->tabMain;
182  if (tab_widget && ui->tabWidget->currentWidget() != tab_widget) {
183  ui->tabWidget->setCurrentWidget(tab_widget);
184  }
185 }
186 
188 {
189  /* Main */
190  mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
191  mapper->addMapping(ui->threadsScriptVerif, OptionsModel::ThreadsScriptVerif);
192  mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
193  mapper->addMapping(ui->prune, OptionsModel::Prune);
194  mapper->addMapping(ui->pruneSize, OptionsModel::PruneSize);
195 
196  /* Wallet */
197  mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
198  mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
199 
200  /* Network */
201  mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
202  mapper->addMapping(ui->allowIncoming, OptionsModel::Listen);
203 
204  mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
205  mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
206  mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
207 
208  mapper->addMapping(ui->connectSocksTor, OptionsModel::ProxyUseTor);
209  mapper->addMapping(ui->proxyIpTor, OptionsModel::ProxyIPTor);
210  mapper->addMapping(ui->proxyPortTor, OptionsModel::ProxyPortTor);
211 
212  /* Window */
213 #ifndef Q_OS_MAC
214  mapper->addMapping(ui->hideTrayIcon, OptionsModel::HideTrayIcon);
215  mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
216  mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
217 #endif
218 
219  /* Display */
220  mapper->addMapping(ui->lang, OptionsModel::Language);
221  mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
222  mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
223 }
224 
226 {
227  ui->okButton->setEnabled(fState);
228 }
229 
231 {
232  if(model)
233  {
234  // confirmation dialog
235  QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
236  tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shut down. Do you want to proceed?"),
237  QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
238 
239  if(btnRetVal == QMessageBox::Cancel)
240  return;
241 
242  /* reset all options and close GUI */
243  model->Reset();
244  QApplication::quit();
245  }
246 }
247 
249 {
250  /* explain the purpose of the config file */
251  QMessageBox::information(this, tr("Configuration options"),
252  tr("The configuration file is used to specify advanced user options which override GUI settings. "
253  "Additionally, any command-line options will override this configuration file."));
254 
255  /* show an error if there was some problem opening the file */
257  QMessageBox::critical(this, tr("Error"), tr("The configuration file could not be opened."));
258 }
259 
261 {
262  mapper->submit();
263  accept();
265 }
266 
268 {
269  reject();
270 }
271 
273 {
274  if(fState)
275  {
276  ui->minimizeToTray->setChecked(false);
277  ui->minimizeToTray->setEnabled(false);
278  }
279  else
280  {
281  ui->minimizeToTray->setEnabled(true);
282  }
283 }
284 
286 {
287  ui->pruneWarning->setVisible(!ui->pruneWarning->isVisible());
288 }
289 
290 void OptionsDialog::showRestartWarning(bool fPersistent)
291 {
292  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
293 
294  if(fPersistent)
295  {
296  ui->statusLabel->setText(tr("Client restart required to activate changes."));
297  }
298  else
299  {
300  ui->statusLabel->setText(tr("This change would require a client restart."));
301  // clear non-persistent status label after 10 seconds
302  // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
303  QTimer::singleShot(10000, this, &OptionsDialog::clearStatusLabel);
304  }
305 }
306 
308 {
309  ui->statusLabel->clear();
310  if (model && model->isRestartRequired()) {
311  showRestartWarning(true);
312  }
313 }
314 
316 {
317  QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
318  QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
319  if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
320  {
321  setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
323  }
324  else
325  {
326  setOkButtonState(false);
327  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
328  ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
329  }
330 }
331 
333 {
334  proxyType proxy;
335  std::string strProxy;
336  QString strDefaultProxyGUI;
337 
338  model->node().getProxy(NET_IPV4, proxy);
339  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
340  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
341  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);
342 
343  model->node().getProxy(NET_IPV6, proxy);
344  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
345  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
346  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);
347 
348  model->node().getProxy(NET_ONION, proxy);
349  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
350  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
351  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
352 }
353 
355 QValidator(parent)
356 {
357 }
358 
359 QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
360 {
361  Q_UNUSED(pos);
362  // Validate the proxy
363  CService serv(LookupNumeric(input.toStdString().c_str(), DEFAULT_GUI_PROXY_PORT));
364  proxyType addrProxy = proxyType(serv, true);
365  if (addrProxy.IsValid())
366  return QValidator::Acceptable;
367 
368  return QValidator::Invalid;
369 }
Ui::OptionsDialog * ui
Definition: optionsdialog.h:73
std::string ToStringPort() const
Definition: netaddress.cpp:560
OptionsDialog(QWidget *parent, bool enableWallet)
Bitcoin unit definitions.
Definition: bitcoinunits.h:47
Proxy address widget validator, checks for a valid proxy address.
Definition: optionsdialog.h:24
virtual bool getProxy(Network net, proxyType &proxy_info)=0
Get proxy.
CService LookupNumeric(const char *pszName, int portDefault)
Definition: netbase.cpp:168
void valueChanged()
void setOkButtonState(bool fState)
const QString & getOverriddenByCommandLine()
Definition: optionsmodel.h:78
#define PACKAGE_NAME
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:84
OptionsModel * model
Definition: optionsdialog.h:74
if(!params[0].isNull()) nMinDepth
void on_openBitcoinConfButton_clicked()
std::string ToStringIP() const
Definition: netaddress.cpp:254
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:142
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:365
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:29
void showRestartWarning(bool fPersistent=false)
void on_cancelButton_clicked()
Preferences dialog.
Definition: optionsdialog.h:35
void clearStatusLabel()
int GetNumCores()
Return the number of cores available on the current system.
Definition: util.cpp:1233
void updateProxyValidationState()
ProxyAddressValidator(QObject *parent)