Dogecoin Core  1.14.2
P2P Digital Currency
optionsdialog.cpp
Go to the documentation of this file.
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.
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 "validation.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::GetRequiredFee()
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 OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
34  QDialog(parent),
35  ui(new Ui::OptionsDialog),
36  model(0),
37  mapper(0)
38 {
39  ui->setupUi(this);
40 
41  /* Main elements init */
42  ui->databaseCache->setMinimum(nMinDbCache);
43  ui->databaseCache->setMaximum(nMaxDbCache);
44  ui->threadsScriptVerif->setMinimum(-GetNumCores());
45  ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
46 
47  /* Network elements init */
48 #ifndef USE_UPNP
49  ui->mapPortUpnp->setEnabled(false);
50 #endif
51 
52  ui->proxyIp->setEnabled(false);
53  ui->proxyPort->setEnabled(false);
54  ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
55 
56  ui->proxyIpTor->setEnabled(false);
57  ui->proxyPortTor->setEnabled(false);
58  ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));
59 
60  connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyIp, SLOT(setEnabled(bool)));
61  connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyPort, SLOT(setEnabled(bool)));
62  connect(ui->connectSocks, SIGNAL(toggled(bool)), this, SLOT(updateProxyValidationState()));
63 
64  connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyIpTor, SLOT(setEnabled(bool)));
65  connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyPortTor, SLOT(setEnabled(bool)));
66  connect(ui->connectSocksTor, SIGNAL(toggled(bool)), this, SLOT(updateProxyValidationState()));
67 
68  /* Window elements init */
69 #ifdef Q_OS_MAC
70  /* remove Window tab on Mac */
71  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
72 #endif
73 
74  /* remove Wallet tab in case of -disablewallet */
75  if (!enableWallet) {
76  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet));
77  }
78 
79  /* Display elements init */
80  QDir translations(":translations");
81 
82  ui->bitcoinAtStartup->setToolTip(ui->bitcoinAtStartup->toolTip().arg(tr(PACKAGE_NAME)));
83  ui->bitcoinAtStartup->setText(ui->bitcoinAtStartup->text().arg(tr(PACKAGE_NAME)));
84 
85  ui->lang->setToolTip(ui->lang->toolTip().arg(tr(PACKAGE_NAME)));
86  ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
87  Q_FOREACH(const QString &langStr, translations.entryList())
88  {
89  QLocale locale(langStr);
90 
92  if(langStr.contains("_"))
93  {
94 #if QT_VERSION >= 0x040800
96  ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
97 #else
99  ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" - ") + QLocale::countryToString(locale.country()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
100 #endif
101  }
102  else
103  {
104 #if QT_VERSION >= 0x040800
106  ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
107 #else
109  ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
110 #endif
111  }
112  }
113 #if QT_VERSION >= 0x040700
114  ui->thirdPartyTxUrls->setPlaceholderText("https://example.com/tx/%s");
115 #endif
116 
117  ui->unit->setModel(new BitcoinUnits(this));
118 
119  /* Widget-to-option mapper */
120  mapper = new QDataWidgetMapper(this);
121  mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
122  mapper->setOrientation(Qt::Vertical);
123 
124  /* setup/change UI elements when proxy IPs are invalid/valid */
125  ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
126  ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
127  connect(ui->proxyIp, SIGNAL(validationDidChange(QValidatedLineEdit *)), this, SLOT(updateProxyValidationState()));
128  connect(ui->proxyIpTor, SIGNAL(validationDidChange(QValidatedLineEdit *)), this, SLOT(updateProxyValidationState()));
129  connect(ui->proxyPort, SIGNAL(textChanged(const QString&)), this, SLOT(updateProxyValidationState()));
130  connect(ui->proxyPortTor, SIGNAL(textChanged(const QString&)), this, SLOT(updateProxyValidationState()));
131 }
132 
134 {
135  delete ui;
136 }
137 
139 {
140  this->model = _model;
141 
142  if(_model)
143  {
144  /* check if client restart is needed and show persistent message */
145  if (_model->isRestartRequired())
146  showRestartWarning(true);
147 
148  QString strLabel = _model->getOverriddenByCommandLine();
149  if (strLabel.isEmpty())
150  strLabel = tr("none");
151  ui->overriddenByCommandLineLabel->setText(strLabel);
152 
153  mapper->setModel(_model);
154  setMapper();
155  mapper->toFirst();
156 
158  }
159 
160  /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
161 
162  /* Main */
163  connect(ui->databaseCache, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
164  connect(ui->threadsScriptVerif, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
165  /* Wallet */
166  connect(ui->spendZeroConfChange, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
167  /* Network */
168  connect(ui->allowIncoming, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
169  connect(ui->connectSocks, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
170  connect(ui->connectSocksTor, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
171  /* Display */
172  connect(ui->lang, SIGNAL(valueChanged()), this, SLOT(showRestartWarning()));
173  connect(ui->thirdPartyTxUrls, SIGNAL(textChanged(const QString &)), this, SLOT(showRestartWarning()));
174 }
175 
177 {
178  /* Main */
179  mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
180  mapper->addMapping(ui->threadsScriptVerif, OptionsModel::ThreadsScriptVerif);
181  mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
182 
183  /* Wallet */
184  mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
185  mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
186 
187  /* Network */
188  mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
189  mapper->addMapping(ui->allowIncoming, OptionsModel::Listen);
190 
191  mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
192  mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
193  mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
194 
195  mapper->addMapping(ui->connectSocksTor, OptionsModel::ProxyUseTor);
196  mapper->addMapping(ui->proxyIpTor, OptionsModel::ProxyIPTor);
197  mapper->addMapping(ui->proxyPortTor, OptionsModel::ProxyPortTor);
198 
199  /* Window */
200 #ifndef Q_OS_MAC
201  mapper->addMapping(ui->hideTrayIcon, OptionsModel::HideTrayIcon);
202  mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
203  mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
204 #endif
205 
206  /* Display */
207  mapper->addMapping(ui->lang, OptionsModel::Language);
208  mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
209  mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
210 }
211 
213 {
214  ui->okButton->setEnabled(fState);
215 }
216 
218 {
219  if(model)
220  {
221  // confirmation dialog
222  QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
223  tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shut down. Do you want to proceed?"),
224  QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
225 
226  if(btnRetVal == QMessageBox::Cancel)
227  return;
228 
229  /* reset all options and close GUI */
230  model->Reset();
231  QApplication::quit();
232  }
233 }
234 
236 {
237  mapper->submit();
238  accept();
240 }
241 
243 {
244  reject();
245 }
246 
248 {
249  if(fState)
250  {
251  ui->minimizeToTray->setChecked(false);
252  ui->minimizeToTray->setEnabled(false);
253  }
254  else
255  {
256  ui->minimizeToTray->setEnabled(true);
257  }
258 }
259 
260 void OptionsDialog::showRestartWarning(bool fPersistent)
261 {
262  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
263 
264  if(fPersistent)
265  {
266  ui->statusLabel->setText(tr("Client restart required to activate changes."));
267  }
268  else
269  {
270  ui->statusLabel->setText(tr("This change would require a client restart."));
271  // clear non-persistent status label after 10 seconds
272  // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
273  QTimer::singleShot(10000, this, SLOT(clearStatusLabel()));
274  }
275 }
276 
278 {
279  ui->statusLabel->clear();
280  if (model && model->isRestartRequired()) {
281  showRestartWarning(true);
282  }
283 }
284 
286 {
287  QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
288  QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
289  if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
290  {
291  setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
293  }
294  else
295  {
296  setOkButtonState(false);
297  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
298  ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
299  }
300 }
301 
303 {
304  proxyType proxy;
305  std::string strProxy;
306  QString strDefaultProxyGUI;
307 
308  GetProxy(NET_IPV4, proxy);
309  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
310  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
311  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);
312 
313  GetProxy(NET_IPV6, proxy);
314  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
315  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
316  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);
317 
318  GetProxy(NET_TOR, proxy);
319  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
320  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
321  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
322 }
323 
325 QValidator(parent)
326 {
327 }
328 
329 QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
330 {
331  Q_UNUSED(pos);
332  // Validate the proxy
333  CService serv(LookupNumeric(input.toStdString().c_str(), 9050));
334  proxyType addrProxy = proxyType(serv, true);
335  if (addrProxy.IsValid())
336  return QValidator::Acceptable;
337 
338  return QValidator::Invalid;
339 }
Bitcoin unit definitions.
Definition: bitcoinunits.h:48
std::string ToStringIP() const
Definition: netaddress.cpp:243
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:134
std::string ToStringPort() const
Definition: netaddress.cpp:554
Preferences dialog.
Definition: optionsdialog.h:36
void setModel(OptionsModel *model)
OptionsModel * model
Definition: optionsdialog.h:66
void on_okButton_clicked()
void updateDefaultProxyNets()
void updateProxyValidationState()
void on_hideTrayIcon_stateChanged(int fState)
void showRestartWarning(bool fPersistent=false)
void on_resetButton_clicked()
Ui::OptionsDialog * ui
Definition: optionsdialog.h:65
OptionsDialog(QWidget *parent, bool enableWallet)
QDataWidgetMapper * mapper
Definition: optionsdialog.h:67
void clearStatusLabel()
void on_cancelButton_clicked()
void setOkButtonState(bool fState)
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:23
bool isRestartRequired()
const QString & getOverriddenByCommandLine()
Definition: optionsmodel.h:69
Proxy address widget validator, checks for a valid proxy address.
Definition: optionsdialog.h:25
ProxyAddressValidator(QObject *parent)
State validate(QString &input, int &pos) const
Line edit that can be marked as "invalid" to show input validation feedback.
bool IsValid() const
Definition: netbase.h:34
CService proxy
Definition: netbase.h:36
@ NET_IPV6
Definition: netaddress.h:23
@ NET_TOR
Definition: netaddress.h:24
@ NET_IPV4
Definition: netaddress.h:22
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
Definition: netbase.cpp:514
CService LookupNumeric(const char *pszName, int portDefault)
Definition: netbase.cpp:183
int GetNumCores()
Return the number of physical cores available on the current system.
Definition: util.cpp:838