Line data Source code
1 : // Copyright (c) 2011-2013 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 "optionsdialog.h"
10 : #include "ui_optionsdialog.h"
11 :
12 : #include "bitcoinunits.h"
13 : #include "guiutil.h"
14 : #include "optionsmodel.h"
15 :
16 : #include "main.h" // for DEFAULT_SCRIPTCHECK_THREADS and MAX_SCRIPTCHECK_THREADS
17 : #include "netbase.h"
18 : #include "txdb.h" // for -dbcache defaults
19 :
20 : #ifdef ENABLE_WALLET
21 : #include "wallet/wallet.h" // for CWallet::minTxFee
22 : #endif
23 :
24 : #include <boost/thread.hpp>
25 :
26 : #include <QDataWidgetMapper>
27 : #include <QDir>
28 : #include <QIntValidator>
29 : #include <QLocale>
30 : #include <QMessageBox>
31 : #include <QTimer>
32 :
33 0 : OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
34 : QDialog(parent),
35 0 : ui(new Ui::OptionsDialog),
36 : model(0),
37 : mapper(0),
38 0 : fProxyIpsValid(true)
39 : {
40 0 : ui->setupUi(this);
41 :
42 : /* Main elements init */
43 0 : ui->databaseCache->setMinimum(nMinDbCache);
44 0 : ui->databaseCache->setMaximum(nMaxDbCache);
45 0 : ui->threadsScriptVerif->setMinimum(-GetNumCores());
46 0 : ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
47 :
48 : /* Network elements init */
49 : #ifndef USE_UPNP
50 : ui->mapPortUpnp->setEnabled(false);
51 : #endif
52 :
53 0 : ui->proxyIp->setEnabled(false);
54 0 : ui->proxyPort->setEnabled(false);
55 0 : ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
56 :
57 0 : ui->proxyIpTor->setEnabled(false);
58 0 : ui->proxyPortTor->setEnabled(false);
59 0 : ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));
60 :
61 0 : connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyIp, SLOT(setEnabled(bool)));
62 0 : connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyPort, SLOT(setEnabled(bool)));
63 :
64 0 : connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyIpTor, SLOT(setEnabled(bool)));
65 0 : connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyPortTor, SLOT(setEnabled(bool)));
66 :
67 0 : ui->proxyIp->installEventFilter(this);
68 0 : ui->proxyIpTor->installEventFilter(this);
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 : #endif
75 :
76 : /* remove Wallet tab in case of -disablewallet */
77 0 : if (!enableWallet) {
78 0 : ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet));
79 : }
80 :
81 : /* Display elements init */
82 0 : QDir translations(":translations");
83 0 : ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
84 0 : Q_FOREACH(const QString &langStr, translations.entryList())
85 : {
86 0 : QLocale locale(langStr);
87 :
88 : /** check if the locale name consists of 2 parts (language_country) */
89 0 : if(langStr.contains("_"))
90 : {
91 : #if QT_VERSION >= 0x040800
92 : /** display language strings as "native language - native country (locale name)", e.g. "Deutsch - Deutschland (de)" */
93 0 : ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
94 : #else
95 : /** display language strings as "language - country (locale name)", e.g. "German - Germany (de)" */
96 : ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" - ") + QLocale::countryToString(locale.country()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
97 : #endif
98 : }
99 : else
100 : {
101 : #if QT_VERSION >= 0x040800
102 : /** display language strings as "native language (locale name)", e.g. "Deutsch (de)" */
103 0 : ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
104 : #else
105 : /** display language strings as "language (locale name)", e.g. "German (de)" */
106 : ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
107 : #endif
108 : }
109 : }
110 : #if QT_VERSION >= 0x040700
111 0 : ui->thirdPartyTxUrls->setPlaceholderText("https://example.com/tx/%s");
112 : #endif
113 :
114 0 : ui->unit->setModel(new BitcoinUnits(this));
115 :
116 : /* Widget-to-option mapper */
117 0 : mapper = new QDataWidgetMapper(this);
118 0 : mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
119 0 : mapper->setOrientation(Qt::Vertical);
120 :
121 : /* setup/change UI elements when proxy IPs are invalid/valid */
122 0 : connect(this, SIGNAL(proxyIpChecks(QValidatedLineEdit *, int)), this, SLOT(doProxyIpChecks(QValidatedLineEdit *, int)));
123 0 : }
124 :
125 0 : OptionsDialog::~OptionsDialog()
126 : {
127 0 : delete ui;
128 0 : }
129 :
130 0 : void OptionsDialog::setModel(OptionsModel *model)
131 : {
132 0 : this->model = model;
133 :
134 0 : if(model)
135 : {
136 : /* check if client restart is needed and show persistent message */
137 0 : if (model->isRestartRequired())
138 0 : showRestartWarning(true);
139 :
140 0 : QString strLabel = model->getOverriddenByCommandLine();
141 0 : if (strLabel.isEmpty())
142 0 : strLabel = tr("none");
143 0 : ui->overriddenByCommandLineLabel->setText(strLabel);
144 :
145 0 : mapper->setModel(model);
146 0 : setMapper();
147 0 : mapper->toFirst();
148 :
149 0 : updateDefaultProxyNets();
150 : }
151 :
152 : /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
153 :
154 : /* Main */
155 0 : connect(ui->databaseCache, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
156 0 : connect(ui->threadsScriptVerif, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
157 : /* Wallet */
158 0 : connect(ui->spendZeroConfChange, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
159 : /* Network */
160 0 : connect(ui->allowIncoming, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
161 0 : connect(ui->connectSocks, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
162 0 : connect(ui->connectSocksTor, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
163 : /* Display */
164 0 : connect(ui->lang, SIGNAL(valueChanged()), this, SLOT(showRestartWarning()));
165 0 : connect(ui->thirdPartyTxUrls, SIGNAL(textChanged(const QString &)), this, SLOT(showRestartWarning()));
166 0 : }
167 :
168 0 : void OptionsDialog::setMapper()
169 : {
170 : /* Main */
171 0 : mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
172 0 : mapper->addMapping(ui->threadsScriptVerif, OptionsModel::ThreadsScriptVerif);
173 0 : mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
174 :
175 : /* Wallet */
176 0 : mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
177 0 : mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
178 :
179 : /* Network */
180 0 : mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
181 0 : mapper->addMapping(ui->allowIncoming, OptionsModel::Listen);
182 :
183 0 : mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
184 0 : mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
185 0 : mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
186 :
187 0 : mapper->addMapping(ui->connectSocksTor, OptionsModel::ProxyUseTor);
188 0 : mapper->addMapping(ui->proxyIpTor, OptionsModel::ProxyIPTor);
189 0 : mapper->addMapping(ui->proxyPortTor, OptionsModel::ProxyPortTor);
190 :
191 : /* Window */
192 : #ifndef Q_OS_MAC
193 0 : mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
194 0 : mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
195 : #endif
196 :
197 : /* Display */
198 0 : mapper->addMapping(ui->lang, OptionsModel::Language);
199 0 : mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
200 0 : mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
201 0 : }
202 :
203 0 : void OptionsDialog::enableOkButton()
204 : {
205 : /* prevent enabling of the OK button when data modified, if there is an invalid proxy address present */
206 0 : if(fProxyIpsValid)
207 : setOkButtonState(true);
208 0 : }
209 :
210 0 : void OptionsDialog::disableOkButton()
211 : {
212 : setOkButtonState(false);
213 0 : }
214 :
215 0 : void OptionsDialog::setOkButtonState(bool fState)
216 : {
217 0 : ui->okButton->setEnabled(fState);
218 0 : }
219 :
220 0 : void OptionsDialog::on_resetButton_clicked()
221 : {
222 0 : if(model)
223 : {
224 : // confirmation dialog
225 : QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
226 0 : tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shut down. Do you want to proceed?"),
227 0 : QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
228 :
229 0 : if(btnRetVal == QMessageBox::Cancel)
230 0 : return;
231 :
232 : /* reset all options and close GUI */
233 0 : model->Reset();
234 0 : QApplication::quit();
235 : }
236 : }
237 :
238 0 : void OptionsDialog::on_okButton_clicked()
239 : {
240 0 : mapper->submit();
241 0 : accept();
242 0 : updateDefaultProxyNets();
243 0 : }
244 :
245 0 : void OptionsDialog::on_cancelButton_clicked()
246 : {
247 0 : reject();
248 0 : }
249 :
250 0 : void OptionsDialog::showRestartWarning(bool fPersistent)
251 : {
252 0 : ui->statusLabel->setStyleSheet("QLabel { color: red; }");
253 :
254 0 : if(fPersistent)
255 : {
256 0 : ui->statusLabel->setText(tr("Client restart required to activate changes."));
257 : }
258 : else
259 : {
260 0 : ui->statusLabel->setText(tr("This change would require a client restart."));
261 : // clear non-persistent status label after 10 seconds
262 : // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
263 0 : QTimer::singleShot(10000, this, SLOT(clearStatusLabel()));
264 : }
265 0 : }
266 :
267 0 : void OptionsDialog::clearStatusLabel()
268 : {
269 0 : ui->statusLabel->clear();
270 0 : }
271 :
272 0 : void OptionsDialog::doProxyIpChecks(QValidatedLineEdit *pUiProxyIp, int nProxyPort)
273 : {
274 : Q_UNUSED(nProxyPort);
275 :
276 0 : CService addrProxy;
277 :
278 : /* Check for a valid IPv4 / IPv6 address */
279 0 : if (!(fProxyIpsValid = LookupNumeric(pUiProxyIp->text().toStdString().c_str(), addrProxy)))
280 : {
281 : disableOkButton();
282 0 : pUiProxyIp->setValid(false);
283 0 : ui->statusLabel->setStyleSheet("QLabel { color: red; }");
284 0 : ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
285 : }
286 : else
287 : {
288 : enableOkButton();
289 0 : ui->statusLabel->clear();
290 : }
291 0 : }
292 :
293 0 : void OptionsDialog::updateDefaultProxyNets()
294 : {
295 : proxyType proxy;
296 : std::string strProxy;
297 0 : QString strDefaultProxyGUI;
298 :
299 0 : GetProxy(NET_IPV4, proxy);
300 0 : strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
301 0 : strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
302 0 : (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);
303 :
304 0 : GetProxy(NET_IPV6, proxy);
305 0 : strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
306 0 : strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
307 0 : (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);
308 :
309 0 : GetProxy(NET_TOR, proxy);
310 0 : strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
311 0 : strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
312 0 : (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
313 0 : }
314 :
315 0 : bool OptionsDialog::eventFilter(QObject *object, QEvent *event)
316 : {
317 0 : if(event->type() == QEvent::FocusOut)
318 : {
319 0 : if(object == ui->proxyIp)
320 : {
321 0 : Q_EMIT proxyIpChecks(ui->proxyIp, ui->proxyPort->text().toInt());
322 : }
323 0 : else if(object == ui->proxyIpTor)
324 : {
325 0 : Q_EMIT proxyIpChecks(ui->proxyIpTor, ui->proxyPortTor->text().toInt());
326 : }
327 : }
328 0 : return QDialog::eventFilter(object, event);
329 0 : }
|