Merge #11683: tests: Remove unused mininode functions {ser,deser}_int_vector(......
[bitcoinplatinum.git] / src / qt / optionsdialog.cpp
blobd7aa8bc38bf75cb185bfe5f755d13df706cd243b
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 <qt/optionsdialog.h>
10 #include <qt/forms/ui_optionsdialog.h>
12 #include <qt/bitcoinunits.h>
13 #include <qt/guiutil.h>
14 #include <qt/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 #include <QDataWidgetMapper>
21 #include <QDir>
22 #include <QIntValidator>
23 #include <QLocale>
24 #include <QMessageBox>
25 #include <QTimer>
27 OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
28 QDialog(parent),
29 ui(new Ui::OptionsDialog),
30 model(0),
31 mapper(0)
33 ui->setupUi(this);
35 /* Main elements init */
36 ui->databaseCache->setMinimum(nMinDbCache);
37 ui->databaseCache->setMaximum(nMaxDbCache);
38 ui->threadsScriptVerif->setMinimum(-GetNumCores());
39 ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
41 /* Network elements init */
42 #ifndef USE_UPNP
43 ui->mapPortUpnp->setEnabled(false);
44 #endif
46 ui->proxyIp->setEnabled(false);
47 ui->proxyPort->setEnabled(false);
48 ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
50 ui->proxyIpTor->setEnabled(false);
51 ui->proxyPortTor->setEnabled(false);
52 ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));
54 connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyIp, SLOT(setEnabled(bool)));
55 connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyPort, SLOT(setEnabled(bool)));
56 connect(ui->connectSocks, SIGNAL(toggled(bool)), this, SLOT(updateProxyValidationState()));
58 connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyIpTor, SLOT(setEnabled(bool)));
59 connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyPortTor, SLOT(setEnabled(bool)));
60 connect(ui->connectSocksTor, SIGNAL(toggled(bool)), this, SLOT(updateProxyValidationState()));
62 /* Window elements init */
63 #ifdef Q_OS_MAC
64 /* remove Window tab on Mac */
65 ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
66 #endif
68 /* remove Wallet tab in case of -disablewallet */
69 if (!enableWallet) {
70 ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet));
73 /* Display elements init */
74 QDir translations(":translations");
76 ui->bitcoinAtStartup->setToolTip(ui->bitcoinAtStartup->toolTip().arg(tr(PACKAGE_NAME)));
77 ui->bitcoinAtStartup->setText(ui->bitcoinAtStartup->text().arg(tr(PACKAGE_NAME)));
79 ui->openBitcoinConfButton->setToolTip(ui->openBitcoinConfButton->toolTip().arg(tr(PACKAGE_NAME)));
81 ui->lang->setToolTip(ui->lang->toolTip().arg(tr(PACKAGE_NAME)));
82 ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
83 for (const QString &langStr : translations.entryList())
85 QLocale locale(langStr);
87 /** check if the locale name consists of 2 parts (language_country) */
88 if(langStr.contains("_"))
90 #if QT_VERSION >= 0x040800
91 /** display language strings as "native language - native country (locale name)", e.g. "Deutsch - Deutschland (de)" */
92 ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
93 #else
94 /** display language strings as "language - country (locale name)", e.g. "German - Germany (de)" */
95 ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" - ") + QLocale::countryToString(locale.country()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
96 #endif
98 else
100 #if QT_VERSION >= 0x040800
101 /** display language strings as "native language (locale name)", e.g. "Deutsch (de)" */
102 ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
103 #else
104 /** display language strings as "language (locale name)", e.g. "German (de)" */
105 ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
106 #endif
109 #if QT_VERSION >= 0x040700
110 ui->thirdPartyTxUrls->setPlaceholderText("https://example.com/tx/%s");
111 #endif
113 ui->unit->setModel(new BitcoinUnits(this));
115 /* Widget-to-option mapper */
116 mapper = new QDataWidgetMapper(this);
117 mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
118 mapper->setOrientation(Qt::Vertical);
120 /* setup/change UI elements when proxy IPs are invalid/valid */
121 ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
122 ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
123 connect(ui->proxyIp, SIGNAL(validationDidChange(QValidatedLineEdit *)), this, SLOT(updateProxyValidationState()));
124 connect(ui->proxyIpTor, SIGNAL(validationDidChange(QValidatedLineEdit *)), this, SLOT(updateProxyValidationState()));
125 connect(ui->proxyPort, SIGNAL(textChanged(const QString&)), this, SLOT(updateProxyValidationState()));
126 connect(ui->proxyPortTor, SIGNAL(textChanged(const QString&)), this, SLOT(updateProxyValidationState()));
129 OptionsDialog::~OptionsDialog()
131 delete ui;
134 void OptionsDialog::setModel(OptionsModel *_model)
136 this->model = _model;
138 if(_model)
140 /* check if client restart is needed and show persistent message */
141 if (_model->isRestartRequired())
142 showRestartWarning(true);
144 QString strLabel = _model->getOverriddenByCommandLine();
145 if (strLabel.isEmpty())
146 strLabel = tr("none");
147 ui->overriddenByCommandLineLabel->setText(strLabel);
149 mapper->setModel(_model);
150 setMapper();
151 mapper->toFirst();
153 updateDefaultProxyNets();
156 /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
158 /* Main */
159 connect(ui->databaseCache, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
160 connect(ui->threadsScriptVerif, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
161 /* Wallet */
162 connect(ui->spendZeroConfChange, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
163 /* Network */
164 connect(ui->allowIncoming, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
165 connect(ui->connectSocks, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
166 connect(ui->connectSocksTor, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
167 /* Display */
168 connect(ui->lang, SIGNAL(valueChanged()), this, SLOT(showRestartWarning()));
169 connect(ui->thirdPartyTxUrls, SIGNAL(textChanged(const QString &)), this, SLOT(showRestartWarning()));
172 void OptionsDialog::setMapper()
174 /* Main */
175 mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
176 mapper->addMapping(ui->threadsScriptVerif, OptionsModel::ThreadsScriptVerif);
177 mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
179 /* Wallet */
180 mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
181 mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
183 /* Network */
184 mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
185 mapper->addMapping(ui->allowIncoming, OptionsModel::Listen);
187 mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
188 mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
189 mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
191 mapper->addMapping(ui->connectSocksTor, OptionsModel::ProxyUseTor);
192 mapper->addMapping(ui->proxyIpTor, OptionsModel::ProxyIPTor);
193 mapper->addMapping(ui->proxyPortTor, OptionsModel::ProxyPortTor);
195 /* Window */
196 #ifndef Q_OS_MAC
197 mapper->addMapping(ui->hideTrayIcon, OptionsModel::HideTrayIcon);
198 mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
199 mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
200 #endif
202 /* Display */
203 mapper->addMapping(ui->lang, OptionsModel::Language);
204 mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
205 mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
208 void OptionsDialog::setOkButtonState(bool fState)
210 ui->okButton->setEnabled(fState);
213 void OptionsDialog::on_resetButton_clicked()
215 if(model)
217 // confirmation dialog
218 QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
219 tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shut down. Do you want to proceed?"),
220 QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
222 if(btnRetVal == QMessageBox::Cancel)
223 return;
225 /* reset all options and close GUI */
226 model->Reset();
227 QApplication::quit();
231 void OptionsDialog::on_openBitcoinConfButton_clicked()
233 /* explain the purpose of the config file */
234 QMessageBox::information(this, tr("Configuration options"),
235 tr("The configuration file is used to specify advanced user options which override GUI settings. "
236 "Additionally, any command-line options will override this configuration file."));
238 /* show an error if there was some problem opening the file */
239 if (!GUIUtil::openBitcoinConf())
240 QMessageBox::critical(this, tr("Error"), tr("The configuration file could not be opened."));
243 void OptionsDialog::on_okButton_clicked()
245 mapper->submit();
246 accept();
247 updateDefaultProxyNets();
250 void OptionsDialog::on_cancelButton_clicked()
252 reject();
255 void OptionsDialog::on_hideTrayIcon_stateChanged(int fState)
257 if(fState)
259 ui->minimizeToTray->setChecked(false);
260 ui->minimizeToTray->setEnabled(false);
262 else
264 ui->minimizeToTray->setEnabled(true);
268 void OptionsDialog::showRestartWarning(bool fPersistent)
270 ui->statusLabel->setStyleSheet("QLabel { color: red; }");
272 if(fPersistent)
274 ui->statusLabel->setText(tr("Client restart required to activate changes."));
276 else
278 ui->statusLabel->setText(tr("This change would require a client restart."));
279 // clear non-persistent status label after 10 seconds
280 // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
281 QTimer::singleShot(10000, this, SLOT(clearStatusLabel()));
285 void OptionsDialog::clearStatusLabel()
287 ui->statusLabel->clear();
288 if (model && model->isRestartRequired()) {
289 showRestartWarning(true);
293 void OptionsDialog::updateProxyValidationState()
295 QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
296 QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
297 if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
299 setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
300 clearStatusLabel();
302 else
304 setOkButtonState(false);
305 ui->statusLabel->setStyleSheet("QLabel { color: red; }");
306 ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
310 void OptionsDialog::updateDefaultProxyNets()
312 proxyType proxy;
313 std::string strProxy;
314 QString strDefaultProxyGUI;
316 GetProxy(NET_IPV4, proxy);
317 strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
318 strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
319 (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);
321 GetProxy(NET_IPV6, proxy);
322 strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
323 strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
324 (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);
326 GetProxy(NET_TOR, proxy);
327 strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
328 strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
329 (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
332 ProxyAddressValidator::ProxyAddressValidator(QObject *parent) :
333 QValidator(parent)
337 QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
339 Q_UNUSED(pos);
340 // Validate the proxy
341 CService serv(LookupNumeric(input.toStdString().c_str(), 9050));
342 proxyType addrProxy = proxyType(serv, true);
343 if (addrProxy.IsValid())
344 return QValidator::Acceptable;
346 return QValidator::Invalid;