Raven Core  3.0.0
P2P Digital Currency
optionsdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #if defined(HAVE_CONFIG_H)
7 #include "config/raven-config.h"
8 #endif
9 
10 #include "optionsdialog.h"
11 #include "ui_optionsdialog.h"
12 
13 #include "ravenunits.h"
14 #include "guiutil.h"
15 #include "optionsmodel.h"
16 
17 #include "validation.h" // for DEFAULT_SCRIPTCHECK_THREADS and MAX_SCRIPTCHECK_THREADS
18 #include "netbase.h"
19 #include "txdb.h" // for -dbcache defaults
20 
21 #include <QDataWidgetMapper>
22 #include <QDir>
23 #include <QIntValidator>
24 #include <QLocale>
25 #include <QMessageBox>
26 #include <QTimer>
27 
28 OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
29  QDialog(parent),
30  ui(new Ui::OptionsDialog),
31  model(0),
32  mapper(0)
33 {
34  ui->setupUi(this);
35 
36  /* Main elements init */
37  ui->databaseCache->setMinimum(nMinDbCache);
38  ui->databaseCache->setMaximum(nMaxDbCache);
39  ui->threadsScriptVerif->setMinimum(-GetNumCores());
40  ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
41 
42  /* Network elements init */
43 #ifndef USE_UPNP
44  ui->mapPortUpnp->setEnabled(false);
45 #endif
46 
47  ui->proxyIp->setEnabled(false);
48  ui->proxyPort->setEnabled(false);
49  ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
50 
51  ui->proxyIpTor->setEnabled(false);
52  ui->proxyPortTor->setEnabled(false);
53  ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));
54 
55  connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyIp, SLOT(setEnabled(bool)));
56  connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyPort, SLOT(setEnabled(bool)));
57  connect(ui->connectSocks, SIGNAL(toggled(bool)), this, SLOT(updateProxyValidationState()));
58 
59  connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyIpTor, SLOT(setEnabled(bool)));
60  connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyPortTor, SLOT(setEnabled(bool)));
61  connect(ui->connectSocksTor, SIGNAL(toggled(bool)), this, SLOT(updateProxyValidationState()));
62 
63  /* Window elements init */
64 #ifdef Q_OS_MAC
65  /* remove Window tab on Mac */
66  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
67 #endif
68 
69  /* remove Wallet tab in case of -disablewallet */
70  if (!enableWallet) {
71  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet));
72  }
73 
74  /* Display elements init */
75  QDir translations(":translations");
76 
77  ui->ravenAtStartup->setToolTip(ui->ravenAtStartup->toolTip().arg(tr(PACKAGE_NAME)));
78  ui->ravenAtStartup->setText(ui->ravenAtStartup->text().arg(tr(PACKAGE_NAME)));
79 
80  ui->openRavenConfButton->setToolTip(ui->openRavenConfButton->toolTip().arg(tr(PACKAGE_NAME)));
81 
82  ui->lang->setToolTip(ui->lang->toolTip().arg(tr(PACKAGE_NAME)));
83  ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
84  for (const QString &langStr : translations.entryList())
85  {
86  QLocale locale(langStr);
87 
89  if(langStr.contains("_"))
90  {
91 #if QT_VERSION >= 0x040800
92 
93  ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
94 #else
95 
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 
103  ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
104 #else
105 
106  ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
107 #endif
108  }
109  }
110 #if QT_VERSION >= 0x040700
111  ui->thirdPartyTxUrls->setPlaceholderText("https://example.com/tx/%s");
112 #endif
113 
114  ui->unit->setModel(new RavenUnits(this));
115 
116  /* Widget-to-option mapper */
117  mapper = new QDataWidgetMapper(this);
118  mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
119  mapper->setOrientation(Qt::Vertical);
120 
121  /* setup/change UI elements when proxy IPs are invalid/valid */
122  ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
123  ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
124  connect(ui->proxyIp, SIGNAL(validationDidChange(QValidatedLineEdit *)), this, SLOT(updateProxyValidationState()));
125  connect(ui->proxyIpTor, SIGNAL(validationDidChange(QValidatedLineEdit *)), this, SLOT(updateProxyValidationState()));
126  connect(ui->proxyPort, SIGNAL(textChanged(const QString&)), this, SLOT(updateProxyValidationState()));
127  connect(ui->proxyPortTor, SIGNAL(textChanged(const QString&)), this, SLOT(updateProxyValidationState()));
128 }
129 
131 {
132  delete ui;
133 }
134 
136 {
137  this->model = _model;
138 
139  if(_model)
140  {
141  /* check if client restart is needed and show persistent message */
142  if (_model->isRestartRequired())
143  showRestartWarning(true);
144 
145  QString strLabel = _model->getOverriddenByCommandLine();
146  if (strLabel.isEmpty())
147  strLabel = tr("none");
148  ui->overriddenByCommandLineLabel->setText(strLabel);
149 
150  mapper->setModel(_model);
151  setMapper();
152  mapper->toFirst();
153 
155  }
156 
157  /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
158 
159  /* Main */
160  connect(ui->databaseCache, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
161  connect(ui->threadsScriptVerif, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
162  /* Wallet */
163  connect(ui->spendZeroConfChange, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
164  /* Network */
165  connect(ui->allowIncoming, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
166  connect(ui->connectSocks, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
167  connect(ui->connectSocksTor, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
168  /* Display */
169  connect(ui->lang, SIGNAL(valueChanged()), this, SLOT(showRestartWarning()));
170  connect(ui->thirdPartyTxUrls, SIGNAL(textChanged(const QString &)), this, SLOT(showRestartWarning()));
171  connect(ui->darkModeCheckBox, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
172 }
173 
175 {
176  /* Main */
177  mapper->addMapping(ui->ravenAtStartup, OptionsModel::StartAtStartup);
178  mapper->addMapping(ui->threadsScriptVerif, OptionsModel::ThreadsScriptVerif);
179  mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
180 
181  /* Wallet */
182  mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
183  mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
184  mapper->addMapping(ui->customFeeFeatures, OptionsModel::CustomFeeFeatures);
185  mapper->addMapping(ui->darkModeCheckBox, OptionsModel::DarkModeEnabled);
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  /* explain the purpose of the config file */
238  QMessageBox::information(this, tr("Configuration options"),
239  tr("The configuration file is used to specify advanced user options which override GUI settings. "
240  "Additionally, any command-line options will override this configuration file."));
241 
242  /* show an error if there was some problem opening the file */
243  if (!GUIUtil::openRavenConf())
244  QMessageBox::critical(this, tr("Error"), tr("The configuration file could not be opened."));
245 }
246 
248 {
249  mapper->submit();
250  accept();
252 }
253 
255 {
256  reject();
257 }
258 
260 {
261  if(fState)
262  {
263  ui->minimizeToTray->setChecked(false);
264  ui->minimizeToTray->setEnabled(false);
265  }
266  else
267  {
268  ui->minimizeToTray->setEnabled(true);
269  }
270 }
271 
272 void OptionsDialog::showRestartWarning(bool fPersistent)
273 {
274  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
275 
276  if(fPersistent)
277  {
278  ui->statusLabel->setText(tr("Client restart required to activate changes."));
279  }
280  else
281  {
282  ui->statusLabel->setText(tr("This change would require a client restart."));
283  // clear non-persistent status label after 10 seconds
284  // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
285  QTimer::singleShot(10000, this, SLOT(clearStatusLabel()));
286  }
287 }
288 
290 {
291  ui->statusLabel->clear();
292  if (model && model->isRestartRequired()) {
293  showRestartWarning(true);
294  }
295 }
296 
298 {
299  QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
300  QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
301  if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
302  {
303  setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
305  }
306  else
307  {
308  setOkButtonState(false);
309  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
310  ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
311  }
312 }
313 
315 {
316  proxyType proxy;
317  std::string strProxy;
318  QString strDefaultProxyGUI;
319 
320  GetProxy(NET_IPV4, proxy);
321  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
322  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
323  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);
324 
325  GetProxy(NET_IPV6, proxy);
326  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
327  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
328  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);
329 
330  GetProxy(NET_TOR, proxy);
331  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
332  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
333  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
334 }
335 
337 QValidator(parent)
338 {
339 }
340 
341 QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
342 {
343  Q_UNUSED(pos);
344  // Validate the proxy
345  CService serv(LookupNumeric(input.toStdString().c_str(), 9050));
346  proxyType addrProxy = proxyType(serv, true);
347  if (addrProxy.IsValid())
348  return QValidator::Acceptable;
349 
350  return QValidator::Invalid;
351 }
Ui::OptionsDialog * ui
Definition: optionsdialog.h:67
std::string ToStringPort() const
Definition: netaddress.cpp:583
OptionsDialog(QWidget *parent, bool enableWallet)
Proxy address widget validator, checks for a valid proxy address.
Definition: optionsdialog.h:25
CService LookupNumeric(const char *pszName, int portDefault)
Definition: netbase.cpp:170
void setOkButtonState(bool fState)
const QString & getOverriddenByCommandLine()
Definition: optionsmodel.h:74
void on_resetButton_clicked()
Line edit that can be marked as "invalid" to show input validation feedback.
#define PACKAGE_NAME
Definition: raven-config.h:350
bool openRavenConf()
Definition: guiutil.cpp:497
bool isRestartRequired() const
OptionsModel * model
Definition: optionsdialog.h:68
std::string ToStringIP() const
Definition: netaddress.cpp:264
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:141
State validate(QString &input, int &pos) const
void setModel(OptionsModel *model)
QDataWidgetMapper * mapper
Definition: optionsdialog.h:69
void on_okButton_clicked()
void updateDefaultProxyNets()
void on_hideTrayIcon_stateChanged(int fState)
CService proxy
Definition: netbase.h:37
bool IsValid() const
Definition: netbase.h:35
Interface from Qt to configuration data structure for Raven client.
Definition: optionsmodel.h:23
void on_openRavenConfButton_clicked()
void showRestartWarning(bool fPersistent=false)
void on_cancelButton_clicked()
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
Definition: netbase.cpp:552
Preferences dialog.
Definition: optionsdialog.h:36
void clearStatusLabel()
int GetNumCores()
Return the number of physical cores available on the current system.
Definition: util.cpp:908
void updateProxyValidationState()
Raven unit definitions.
Definition: ravenunits.h:53
ProxyAddressValidator(QObject *parent)