Remove unused variable int64_t nEnd
[bitcoinplatinum.git] / src / qt / optionsdialog.cpp
blobb80b6541dddd2ab3fdfd61f5e657cd18a3a8fd50
1 // Copyright (c) 2011-2016 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.
5 #if defined(HAVE_CONFIG_H)
6 #include "config/bitcoin-config.h"
7 #endif
9 #include "optionsdialog.h"
10 #include "ui_optionsdialog.h"
12 #include "bitcoinunits.h"
13 #include "guiutil.h"
14 #include "optionsmodel.h"
16 #include "validation.h" // for DEFAULT_SCRIPTCHECK_THREADS and MAX_SCRIPTCHECK_THREADS
17 #include "netbase.h"
18 #include "txdb.h" // for -dbcache defaults
20 #ifdef ENABLE_WALLET
21 #include "wallet/wallet.h" // for CWallet::GetRequiredFee()
22 #endif
24 #include <QDataWidgetMapper>
25 #include <QDir>
26 #include <QIntValidator>
27 #include <QLocale>
28 #include <QMessageBox>
29 #include <QTimer>
31 OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
32 QDialog(parent),
33 ui(new Ui::OptionsDialog),
34 model(0),
35 mapper(0)
37 ui->setupUi(this);
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);
45 /* Network elements init */
46 #ifndef USE_UPNP
47 ui->mapPortUpnp->setEnabled(false);
48 #endif
50 ui->proxyIp->setEnabled(false);
51 ui->proxyPort->setEnabled(false);
52 ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
54 ui->proxyIpTor->setEnabled(false);
55 ui->proxyPortTor->setEnabled(false);
56 ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));
58 connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyIp, SLOT(setEnabled(bool)));
59 connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyPort, SLOT(setEnabled(bool)));
60 connect(ui->connectSocks, SIGNAL(toggled(bool)), this, SLOT(updateProxyValidationState()));
62 connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyIpTor, SLOT(setEnabled(bool)));
63 connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyPortTor, SLOT(setEnabled(bool)));
64 connect(ui->connectSocksTor, SIGNAL(toggled(bool)), this, SLOT(updateProxyValidationState()));
66 /* Window elements init */
67 #ifdef Q_OS_MAC
68 /* remove Window tab on Mac */
69 ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
70 #endif
72 /* remove Wallet tab in case of -disablewallet */
73 if (!enableWallet) {
74 ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet));
77 /* Display elements init */
78 QDir translations(":translations");
80 ui->bitcoinAtStartup->setToolTip(ui->bitcoinAtStartup->toolTip().arg(tr(PACKAGE_NAME)));
81 ui->bitcoinAtStartup->setText(ui->bitcoinAtStartup->text().arg(tr(PACKAGE_NAME)));
83 ui->lang->setToolTip(ui->lang->toolTip().arg(tr(PACKAGE_NAME)));
84 ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
85 for (const QString &langStr : translations.entryList())
87 QLocale locale(langStr);
89 /** check if the locale name consists of 2 parts (language_country) */
90 if(langStr.contains("_"))
92 #if QT_VERSION >= 0x040800
93 /** display language strings as "native language - native country (locale name)", e.g. "Deutsch - Deutschland (de)" */
94 ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
95 #else
96 /** display language strings as "language - country (locale name)", e.g. "German - Germany (de)" */
97 ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" - ") + QLocale::countryToString(locale.country()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
98 #endif
100 else
102 #if QT_VERSION >= 0x040800
103 /** display language strings as "native language (locale name)", e.g. "Deutsch (de)" */
104 ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
105 #else
106 /** display language strings as "language (locale name)", e.g. "German (de)" */
107 ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
108 #endif
111 #if QT_VERSION >= 0x040700
112 ui->thirdPartyTxUrls->setPlaceholderText("https://example.com/tx/%s");
113 #endif
115 ui->unit->setModel(new BitcoinUnits(this));
117 /* Widget-to-option mapper */
118 mapper = new QDataWidgetMapper(this);
119 mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
120 mapper->setOrientation(Qt::Vertical);
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));
125 connect(ui->proxyIp, SIGNAL(validationDidChange(QValidatedLineEdit *)), this, SLOT(updateProxyValidationState()));
126 connect(ui->proxyIpTor, SIGNAL(validationDidChange(QValidatedLineEdit *)), this, SLOT(updateProxyValidationState()));
127 connect(ui->proxyPort, SIGNAL(textChanged(const QString&)), this, SLOT(updateProxyValidationState()));
128 connect(ui->proxyPortTor, SIGNAL(textChanged(const QString&)), this, SLOT(updateProxyValidationState()));
131 OptionsDialog::~OptionsDialog()
133 delete ui;
136 void OptionsDialog::setModel(OptionsModel *_model)
138 this->model = _model;
140 if(_model)
142 /* check if client restart is needed and show persistent message */
143 if (_model->isRestartRequired())
144 showRestartWarning(true);
146 QString strLabel = _model->getOverriddenByCommandLine();
147 if (strLabel.isEmpty())
148 strLabel = tr("none");
149 ui->overriddenByCommandLineLabel->setText(strLabel);
151 mapper->setModel(_model);
152 setMapper();
153 mapper->toFirst();
155 updateDefaultProxyNets();
158 /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
160 /* Main */
161 connect(ui->databaseCache, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
162 connect(ui->threadsScriptVerif, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
163 /* Wallet */
164 connect(ui->spendZeroConfChange, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
165 /* Network */
166 connect(ui->allowIncoming, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
167 connect(ui->connectSocks, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
168 connect(ui->connectSocksTor, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
169 /* Display */
170 connect(ui->lang, SIGNAL(valueChanged()), this, SLOT(showRestartWarning()));
171 connect(ui->thirdPartyTxUrls, SIGNAL(textChanged(const QString &)), this, SLOT(showRestartWarning()));
174 void OptionsDialog::setMapper()
176 /* Main */
177 mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
178 mapper->addMapping(ui->threadsScriptVerif, OptionsModel::ThreadsScriptVerif);
179 mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
181 /* Wallet */
182 mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
183 mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
185 /* Network */
186 mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
187 mapper->addMapping(ui->allowIncoming, OptionsModel::Listen);
189 mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
190 mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
191 mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
193 mapper->addMapping(ui->connectSocksTor, OptionsModel::ProxyUseTor);
194 mapper->addMapping(ui->proxyIpTor, OptionsModel::ProxyIPTor);
195 mapper->addMapping(ui->proxyPortTor, OptionsModel::ProxyPortTor);
197 /* Window */
198 #ifndef Q_OS_MAC
199 mapper->addMapping(ui->hideTrayIcon, OptionsModel::HideTrayIcon);
200 mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
201 mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
202 #endif
204 /* Display */
205 mapper->addMapping(ui->lang, OptionsModel::Language);
206 mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
207 mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
210 void OptionsDialog::setOkButtonState(bool fState)
212 ui->okButton->setEnabled(fState);
215 void OptionsDialog::on_resetButton_clicked()
217 if(model)
219 // confirmation dialog
220 QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
221 tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shut down. Do you want to proceed?"),
222 QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
224 if(btnRetVal == QMessageBox::Cancel)
225 return;
227 /* reset all options and close GUI */
228 model->Reset();
229 QApplication::quit();
233 void OptionsDialog::on_openBitcoinConfButton_clicked()
235 /* explain the purpose of the config file */
236 QMessageBox::information(this, tr("Configuration options"),
237 tr("The configuration file is used to specify advanced user options which override GUI settings. "
238 "Additionally, any command-line options will override this configuration file."));
240 /* show an error if there was some problem opening the file */
241 if (!GUIUtil::openBitcoinConf())
242 QMessageBox::critical(this, tr("Error"), tr("The configuration file could not be opened."));
245 void OptionsDialog::on_okButton_clicked()
247 mapper->submit();
248 accept();
249 updateDefaultProxyNets();
252 void OptionsDialog::on_cancelButton_clicked()
254 reject();
257 void OptionsDialog::on_hideTrayIcon_stateChanged(int fState)
259 if(fState)
261 ui->minimizeToTray->setChecked(false);
262 ui->minimizeToTray->setEnabled(false);
264 else
266 ui->minimizeToTray->setEnabled(true);
270 void OptionsDialog::showRestartWarning(bool fPersistent)
272 ui->statusLabel->setStyleSheet("QLabel { color: red; }");
274 if(fPersistent)
276 ui->statusLabel->setText(tr("Client restart required to activate changes."));
278 else
280 ui->statusLabel->setText(tr("This change would require a client restart."));
281 // clear non-persistent status label after 10 seconds
282 // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
283 QTimer::singleShot(10000, this, SLOT(clearStatusLabel()));
287 void OptionsDialog::clearStatusLabel()
289 ui->statusLabel->clear();
290 if (model && model->isRestartRequired()) {
291 showRestartWarning(true);
295 void OptionsDialog::updateProxyValidationState()
297 QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
298 QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
299 if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
301 setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
302 clearStatusLabel();
304 else
306 setOkButtonState(false);
307 ui->statusLabel->setStyleSheet("QLabel { color: red; }");
308 ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
312 void OptionsDialog::updateDefaultProxyNets()
314 proxyType proxy;
315 std::string strProxy;
316 QString strDefaultProxyGUI;
318 GetProxy(NET_IPV4, proxy);
319 strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
320 strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
321 (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);
323 GetProxy(NET_IPV6, proxy);
324 strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
325 strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
326 (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);
328 GetProxy(NET_TOR, proxy);
329 strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
330 strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
331 (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
334 ProxyAddressValidator::ProxyAddressValidator(QObject *parent) :
335 QValidator(parent)
339 QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
341 Q_UNUSED(pos);
342 // Validate the proxy
343 CService serv(LookupNumeric(input.toStdString().c_str(), 9050));
344 proxyType addrProxy = proxyType(serv, true);
345 if (addrProxy.IsValid())
346 return QValidator::Acceptable;
348 return QValidator::Invalid;