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)
6 #include <config/bitcoin-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
OptionsDialog(QWidget * parent,bool enableWallet)31 OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
32 QDialog(parent, GUIUtil::dialog_flags),
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 #ifndef ENABLE_EXTERNAL_SIGNER
96 //: "External signing" means using devices such as hardware wallets.
97 ui->externalSignerPath->setToolTip(tr("Compiled without external signing support (required for external signing)"));
98 ui->externalSignerPath->setEnabled(false);
99 #endif
100 /* Display elements init */
101 QDir translations(":translations");
102
103 ui->bitcoinAtStartup->setToolTip(ui->bitcoinAtStartup->toolTip().arg(PACKAGE_NAME));
104 ui->bitcoinAtStartup->setText(ui->bitcoinAtStartup->text().arg(PACKAGE_NAME));
105
106 ui->openBitcoinConfButton->setToolTip(ui->openBitcoinConfButton->toolTip().arg(PACKAGE_NAME));
107
108 ui->lang->setToolTip(ui->lang->toolTip().arg(PACKAGE_NAME));
109 ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
110 for (const QString &langStr : translations.entryList())
111 {
112 QLocale locale(langStr);
113
114 /** check if the locale name consists of 2 parts (language_country) */
115 if(langStr.contains("_"))
116 {
117 /** display language strings as "native language - native country (locale name)", e.g. "Deutsch - Deutschland (de)" */
118 ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
119 }
120 else
121 {
122 /** display language strings as "native language (locale name)", e.g. "Deutsch (de)" */
123 ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
124 }
125 }
126 ui->unit->setModel(new BitcoinUnits(this));
127
128 /* Widget-to-option mapper */
129 mapper = new QDataWidgetMapper(this);
130 mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
131 mapper->setOrientation(Qt::Vertical);
132
133 GUIUtil::ItemDelegate* delegate = new GUIUtil::ItemDelegate(mapper);
134 connect(delegate, &GUIUtil::ItemDelegate::keyEscapePressed, this, &OptionsDialog::reject);
135 mapper->setItemDelegate(delegate);
136
137 /* setup/change UI elements when proxy IPs are invalid/valid */
138 ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
139 ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
140 connect(ui->proxyIp, &QValidatedLineEdit::validationDidChange, this, &OptionsDialog::updateProxyValidationState);
141 connect(ui->proxyIpTor, &QValidatedLineEdit::validationDidChange, this, &OptionsDialog::updateProxyValidationState);
142 connect(ui->proxyPort, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
143 connect(ui->proxyPortTor, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
144
145 if (!QSystemTrayIcon::isSystemTrayAvailable()) {
146 ui->showTrayIcon->setChecked(false);
147 ui->showTrayIcon->setEnabled(false);
148 ui->minimizeToTray->setChecked(false);
149 ui->minimizeToTray->setEnabled(false);
150 }
151
152 QFont embedded_font{GUIUtil::fixedPitchFont(true)};
153 ui->embeddedFont_radioButton->setText(ui->embeddedFont_radioButton->text().arg(QFontInfo(embedded_font).family()));
154 embedded_font.setWeight(QFont::Bold);
155 ui->embeddedFont_label_1->setFont(embedded_font);
156 ui->embeddedFont_label_9->setFont(embedded_font);
157
158 QFont system_font{GUIUtil::fixedPitchFont(false)};
159 ui->systemFont_radioButton->setText(ui->systemFont_radioButton->text().arg(QFontInfo(system_font).family()));
160 system_font.setWeight(QFont::Bold);
161 ui->systemFont_label_1->setFont(system_font);
162 ui->systemFont_label_9->setFont(system_font);
163 // Checking the embeddedFont_radioButton automatically unchecks the systemFont_radioButton.
164 ui->systemFont_radioButton->setChecked(true);
165
166 GUIUtil::handleCloseWindowShortcut(this);
167 }
168
~OptionsDialog()169 OptionsDialog::~OptionsDialog()
170 {
171 delete ui;
172 }
173
setModel(OptionsModel * _model)174 void OptionsDialog::setModel(OptionsModel *_model)
175 {
176 this->model = _model;
177
178 if(_model)
179 {
180 /* check if client restart is needed and show persistent message */
181 if (_model->isRestartRequired())
182 showRestartWarning(true);
183
184 // Prune values are in GB to be consistent with intro.cpp
185 static constexpr uint64_t nMinDiskSpace = (MIN_DISK_SPACE_FOR_BLOCK_FILES / GB_BYTES) + (MIN_DISK_SPACE_FOR_BLOCK_FILES % GB_BYTES) ? 1 : 0;
186 ui->pruneSize->setRange(nMinDiskSpace, std::numeric_limits<int>::max());
187
188 QString strLabel = _model->getOverriddenByCommandLine();
189 if (strLabel.isEmpty())
190 strLabel = tr("none");
191 ui->overriddenByCommandLineLabel->setText(strLabel);
192
193 mapper->setModel(_model);
194 setMapper();
195 mapper->toFirst();
196
197 updateDefaultProxyNets();
198 }
199
200 /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
201
202 /* Main */
203 connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
204 connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::togglePruneWarning);
205 connect(ui->pruneSize, qOverload<int>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
206 connect(ui->databaseCache, qOverload<int>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
207 connect(ui->externalSignerPath, &QLineEdit::textChanged, [this]{ showRestartWarning(); });
208 connect(ui->threadsScriptVerif, qOverload<int>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
209 /* Wallet */
210 connect(ui->spendZeroConfChange, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
211 /* Network */
212 connect(ui->allowIncoming, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
213 connect(ui->connectSocks, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
214 connect(ui->connectSocksTor, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
215 /* Display */
216 connect(ui->lang, qOverload<>(&QValueComboBox::valueChanged), [this]{ showRestartWarning(); });
217 connect(ui->thirdPartyTxUrls, &QLineEdit::textChanged, [this]{ showRestartWarning(); });
218 }
219
setCurrentTab(OptionsDialog::Tab tab)220 void OptionsDialog::setCurrentTab(OptionsDialog::Tab tab)
221 {
222 QWidget *tab_widget = nullptr;
223 if (tab == OptionsDialog::Tab::TAB_NETWORK) tab_widget = ui->tabNetwork;
224 if (tab == OptionsDialog::Tab::TAB_MAIN) tab_widget = ui->tabMain;
225 if (tab_widget && ui->tabWidget->currentWidget() != tab_widget) {
226 ui->tabWidget->setCurrentWidget(tab_widget);
227 }
228 }
229
setMapper()230 void OptionsDialog::setMapper()
231 {
232 /* Main */
233 mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
234 mapper->addMapping(ui->threadsScriptVerif, OptionsModel::ThreadsScriptVerif);
235 mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
236 mapper->addMapping(ui->prune, OptionsModel::Prune);
237 mapper->addMapping(ui->pruneSize, OptionsModel::PruneSize);
238
239 /* Wallet */
240 mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
241 mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
242 mapper->addMapping(ui->externalSignerPath, OptionsModel::ExternalSignerPath);
243
244 /* Network */
245 mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
246 mapper->addMapping(ui->mapPortNatpmp, OptionsModel::MapPortNatpmp);
247 mapper->addMapping(ui->allowIncoming, OptionsModel::Listen);
248
249 mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
250 mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
251 mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
252
253 mapper->addMapping(ui->connectSocksTor, OptionsModel::ProxyUseTor);
254 mapper->addMapping(ui->proxyIpTor, OptionsModel::ProxyIPTor);
255 mapper->addMapping(ui->proxyPortTor, OptionsModel::ProxyPortTor);
256
257 /* Window */
258 #ifndef Q_OS_MAC
259 if (QSystemTrayIcon::isSystemTrayAvailable()) {
260 mapper->addMapping(ui->showTrayIcon, OptionsModel::ShowTrayIcon);
261 mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
262 }
263 mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
264 #endif
265
266 /* Display */
267 mapper->addMapping(ui->lang, OptionsModel::Language);
268 mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
269 mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
270 mapper->addMapping(ui->embeddedFont_radioButton, OptionsModel::UseEmbeddedMonospacedFont);
271 }
272
setOkButtonState(bool fState)273 void OptionsDialog::setOkButtonState(bool fState)
274 {
275 ui->okButton->setEnabled(fState);
276 }
277
on_resetButton_clicked()278 void OptionsDialog::on_resetButton_clicked()
279 {
280 if(model)
281 {
282 // confirmation dialog
283 QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
284 tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shut down. Do you want to proceed?"),
285 QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
286
287 if(btnRetVal == QMessageBox::Cancel)
288 return;
289
290 /* reset all options and close GUI */
291 model->Reset();
292 QApplication::quit();
293 }
294 }
295
on_openBitcoinConfButton_clicked()296 void OptionsDialog::on_openBitcoinConfButton_clicked()
297 {
298 /* explain the purpose of the config file */
299 QMessageBox::information(this, tr("Configuration options"),
300 tr("The configuration file is used to specify advanced user options which override GUI settings. "
301 "Additionally, any command-line options will override this configuration file."));
302
303 /* show an error if there was some problem opening the file */
304 if (!GUIUtil::openBitcoinConf())
305 QMessageBox::critical(this, tr("Error"), tr("The configuration file could not be opened."));
306 }
307
on_okButton_clicked()308 void OptionsDialog::on_okButton_clicked()
309 {
310 mapper->submit();
311 accept();
312 updateDefaultProxyNets();
313 }
314
on_cancelButton_clicked()315 void OptionsDialog::on_cancelButton_clicked()
316 {
317 reject();
318 }
319
on_showTrayIcon_stateChanged(int state)320 void OptionsDialog::on_showTrayIcon_stateChanged(int state)
321 {
322 if (state == Qt::Checked) {
323 ui->minimizeToTray->setEnabled(true);
324 } else {
325 ui->minimizeToTray->setChecked(false);
326 ui->minimizeToTray->setEnabled(false);
327 }
328 }
329
togglePruneWarning(bool enabled)330 void OptionsDialog::togglePruneWarning(bool enabled)
331 {
332 ui->pruneWarning->setVisible(!ui->pruneWarning->isVisible());
333 }
334
showRestartWarning(bool fPersistent)335 void OptionsDialog::showRestartWarning(bool fPersistent)
336 {
337 ui->statusLabel->setStyleSheet("QLabel { color: red; }");
338
339 if(fPersistent)
340 {
341 ui->statusLabel->setText(tr("Client restart required to activate changes."));
342 }
343 else
344 {
345 ui->statusLabel->setText(tr("This change would require a client restart."));
346 // clear non-persistent status label after 10 seconds
347 // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
348 QTimer::singleShot(10000, this, &OptionsDialog::clearStatusLabel);
349 }
350 }
351
clearStatusLabel()352 void OptionsDialog::clearStatusLabel()
353 {
354 ui->statusLabel->clear();
355 if (model && model->isRestartRequired()) {
356 showRestartWarning(true);
357 }
358 }
359
updateProxyValidationState()360 void OptionsDialog::updateProxyValidationState()
361 {
362 QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
363 QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
364 if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
365 {
366 setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
367 clearStatusLabel();
368 }
369 else
370 {
371 setOkButtonState(false);
372 ui->statusLabel->setStyleSheet("QLabel { color: red; }");
373 ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
374 }
375 }
376
updateDefaultProxyNets()377 void OptionsDialog::updateDefaultProxyNets()
378 {
379 proxyType proxy;
380 std::string strProxy;
381 QString strDefaultProxyGUI;
382
383 model->node().getProxy(NET_IPV4, proxy);
384 strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
385 strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
386 (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);
387
388 model->node().getProxy(NET_IPV6, proxy);
389 strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
390 strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
391 (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);
392
393 model->node().getProxy(NET_ONION, proxy);
394 strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
395 strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
396 (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
397 }
398
ProxyAddressValidator(QObject * parent)399 ProxyAddressValidator::ProxyAddressValidator(QObject *parent) :
400 QValidator(parent)
401 {
402 }
403
validate(QString & input,int & pos) const404 QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
405 {
406 Q_UNUSED(pos);
407 // Validate the proxy
408 CService serv(LookupNumeric(input.toStdString(), DEFAULT_GUI_PROXY_PORT));
409 proxyType addrProxy = proxyType(serv, true);
410 if (addrProxy.IsValid())
411 return QValidator::Acceptable;
412
413 return QValidator::Invalid;
414 }
415