Fabcoin Core  0.16.2
P2P Digital Currency
optionsdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2017 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)
7 #endif
8 
9 #include <optionsdialog.h>
10 #include <ui_optionsdialog.h>
11 
12 #include <fabcoinunits.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 #include <styleSheet.h>
20 
21 #ifdef ENABLE_WALLET
22 #include <wallet/wallet.h> // for CWallet::GetRequiredFee()
23 #endif
24 
25 #include <QDataWidgetMapper>
26 #include <QDir>
27 #include <QIntValidator>
28 #include <QLocale>
29 #include <QMessageBox>
30 #include <QTimer>
31 
32 OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
33  QDialog(parent),
34  ui(new Ui::OptionsDialog),
35  model(0),
36  mapper(0)
37 {
38  ui->setupUi(this);
39 
40  SetObjectStyleSheet(ui->resetButton, StyleSheetNames::ButtonWhite);
41  SetObjectStyleSheet(ui->openFabcoinConfButton, StyleSheetNames::ButtonWhite);
42  SetObjectStyleSheet(ui->okButton, StyleSheetNames::ButtonBlue);
43  SetObjectStyleSheet(ui->cancelButton, StyleSheetNames::ButtonBlue);
44 
45  /* Main elements init */
46  ui->databaseCache->setMinimum(nMinDbCache);
47  ui->databaseCache->setMaximum(nMaxDbCache);
48  ui->threadsScriptVerif->setMinimum(-GetNumCores());
49  ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
50 
51  /* Network elements init */
52 #ifndef USE_UPNP
53  ui->mapPortUpnp->setEnabled(false);
54 #endif
55 
56  ui->proxyIp->setEnabled(false);
57  ui->proxyIpLabel->setEnabled(false);
58  ui->proxyPort->setEnabled(false);
59  ui->proxyPortLabel->setEnabled(false);
60  ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
61 
62  ui->proxyIpTor->setEnabled(false);
63  ui->proxyIpTorLabel->setEnabled(false);
64  ui->proxyPortTor->setEnabled(false);
65  ui->proxyPortTorLabel->setEnabled(false);
66  ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));
67 
68  connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyIp, SLOT(setEnabled(bool)));
69  connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyIpLabel, SLOT(setEnabled(bool)));
70  connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyPort, SLOT(setEnabled(bool)));
71  connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyPortLabel, SLOT(setEnabled(bool)));
72  connect(ui->connectSocks, SIGNAL(toggled(bool)), this, SLOT(updateProxyValidationState()));
73 
74  connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyIpTor, SLOT(setEnabled(bool)));
75  connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyIpTorLabel, SLOT(setEnabled(bool)));
76  connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyPortTor, SLOT(setEnabled(bool)));
77  connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyPortTorLabel, SLOT(setEnabled(bool)));
78  connect(ui->connectSocksTor, SIGNAL(toggled(bool)), this, SLOT(updateProxyValidationState()));
79 
80  /* Window elements init */
81 #ifdef Q_OS_MAC
82  /* remove Window tab on Mac */
83  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
84 #endif
85 
86  /* remove Wallet tab in case of -disablewallet */
87  if (!enableWallet) {
88  ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet));
89  }
90 
91  /* Display elements init */
92  QDir translations(":translations");
93 
94  ui->fabcoinAtStartup->setToolTip(ui->fabcoinAtStartup->toolTip().arg(tr(PACKAGE_NAME)));
95  ui->fabcoinAtStartup->setText(ui->fabcoinAtStartup->text().arg(tr(PACKAGE_NAME)));
96 
97  ui->openFabcoinConfButton->setToolTip(ui->openFabcoinConfButton->toolTip().arg(tr(PACKAGE_NAME)));
98 
99  ui->lang->setToolTip(ui->lang->toolTip().arg(tr(PACKAGE_NAME)));
100  ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
101  for (const QString &langStr : translations.entryList())
102  {
103  QLocale locale(langStr);
104 
106  if(langStr.contains("_"))
107  {
108 #if QT_VERSION >= 0x040800
109 
110  ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
111 #else
112 
113  ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" - ") + QLocale::countryToString(locale.country()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
114 #endif
115  }
116  else
117  {
118 #if QT_VERSION >= 0x040800
119 
120  ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
121 #else
122 
123  ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
124 #endif
125  }
126  }
127 #if QT_VERSION >= 0x040700
128  ui->thirdPartyTxUrls->setPlaceholderText("https://example.com/tx/%s");
129 #endif
130 
131  ui->unit->setModel(new FabcoinUnits(this));
132 
133  /* Widget-to-option mapper */
134  mapper = new QDataWidgetMapper(this);
135  mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
136  mapper->setOrientation(Qt::Vertical);
137 
138  /* setup/change UI elements when proxy IPs are invalid/valid */
141  connect(ui->proxyIp, SIGNAL(validationDidChange(QValidatedLineEdit *)), this, SLOT(updateProxyValidationState()));
142  connect(ui->proxyIpTor, SIGNAL(validationDidChange(QValidatedLineEdit *)), this, SLOT(updateProxyValidationState()));
143  connect(ui->proxyPort, SIGNAL(textChanged(const QString&)), this, SLOT(updateProxyValidationState()));
144  connect(ui->proxyPortTor, SIGNAL(textChanged(const QString&)), this, SLOT(updateProxyValidationState()));
145 }
146 
148 {
149  delete ui;
150 }
151 
153 {
154  this->model = _model;
155 
156  if(_model)
157  {
158  /* check if client restart is needed and show persistent message */
159  if (_model->isRestartRequired())
160  showRestartWarning(true);
161 
162  QString strLabel = _model->getOverriddenByCommandLine();
163  if (strLabel.isEmpty())
164  strLabel = tr("none");
165  ui->overriddenByCommandLineLabel->setText(strLabel);
166 
167  mapper->setModel(_model);
168  setMapper();
169  mapper->toFirst();
170 
172  }
173 
174  /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
175 
176  /* Main */
177  connect(ui->databaseCache, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
178  connect(ui->logEvents, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
179  connect(ui->threadsScriptVerif, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
180  /* Wallet */
181  connect(ui->spendZeroConfChange, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
182  /* Network */
183  connect(ui->allowIncoming, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
184  connect(ui->connectSocks, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
185  connect(ui->connectSocksTor, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
186  /* Display */
187  connect(ui->lang, SIGNAL(valueChanged()), this, SLOT(showRestartWarning()));
188  connect(ui->thirdPartyTxUrls, SIGNAL(textChanged(const QString &)), this, SLOT(showRestartWarning()));
189 }
190 
192 {
193  /* Main */
199 
200  /* Wallet */
206 
207  /* Network */
210 
212  mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
214 
218 
219  /* Window */
220 #ifndef Q_OS_MAC
224 #endif
225 
226  /* Display */
227  mapper->addMapping(ui->lang, OptionsModel::Language);
228  mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
230 }
231 
233 {
234  ui->okButton->setEnabled(fState);
235 }
236 
238 {
239  if(model)
240  {
241  // confirmation dialog
242  QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
243  tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shut down. Do you want to proceed?"),
244  QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
245 
246  if(btnRetVal == QMessageBox::Cancel)
247  return;
248 
249  /* reset all options and close GUI */
250  model->Reset();
251  QApplication::quit();
252  }
253 }
254 
256 {
257  /* explain the purpose of the config file */
258  QMessageBox::information(this, tr("Configuration options"),
259  tr("The configuration file is used to specify advanced user options which override GUI settings. "
260  "Additionally, any command-line options will override this configuration file."));
261 
262  /* show an error if there was some problem opening the file */
264  QMessageBox::critical(this, tr("Error"), tr("The configuration file could not be opened."));
265 }
266 
268 {
269  mapper->submit();
270  accept();
272 }
273 
275 {
276  reject();
277 }
278 
280 {
281  if(fState)
282  {
283  ui->minimizeToTray->setChecked(false);
284  ui->minimizeToTray->setEnabled(false);
285  }
286  else
287  {
288  ui->minimizeToTray->setEnabled(true);
289  }
290 }
291 
292 void OptionsDialog::showRestartWarning(bool fPersistent)
293 {
294  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
295 
296  if(fPersistent)
297  {
298  ui->statusLabel->setText(tr("Client restart required to activate changes."));
299  }
300  else
301  {
302  ui->statusLabel->setText(tr("This change would require a client restart."));
303  // clear non-persistent status label after 10 seconds
304  // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
305  QTimer::singleShot(10000, this, SLOT(clearStatusLabel()));
306  }
307 }
308 
310 {
311  ui->statusLabel->clear();
312  if (model && model->isRestartRequired()) {
313  showRestartWarning(true);
314  }
315 }
316 
318 {
319  QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
320  QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
321  if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
322  {
323  setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
325  }
326  else
327  {
328  setOkButtonState(false);
329  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
330  ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
331  }
332 }
333 
335 {
336  proxyType proxy;
337  std::string strProxy;
338  QString strDefaultProxyGUI;
339 
340  GetProxy(NET_IPV4, proxy);
341  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
342  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
343  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);
344 
345  GetProxy(NET_IPV6, proxy);
346  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
347  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
348  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);
349 
350  GetProxy(NET_TOR, proxy);
351  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
352  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
353  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
354 }
355 
357 QValidator(parent)
358 {
359 }
360 
361 QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
362 {
363  Q_UNUSED(pos);
364  // Validate the proxy
365  CService serv(LookupNumeric(input.toStdString().c_str(), 9050));
366  proxyType addrProxy = proxyType(serv, true);
367  if (addrProxy.IsValid())
368  return QValidator::Acceptable;
369 
370  return QValidator::Invalid;
371 }
QCheckBox * spendZeroConfChange
QTabWidget * tabWidget
Ui::OptionsDialog * ui
Definition: optionsdialog.h:66
QCheckBox * proxyReachIPv6
#define SetObjectStyleSheet(object, name)
Definition: styleSheet.h:10
OptionsDialog(QWidget *parent, bool enableWallet)
Proxy address widget validator, checks for a valid proxy address.
Definition: optionsdialog.h:24
QSpinBox * threadsScriptVerif
void setupUi(QDialog *OptionsDialog)
#define PACKAGE_NAME
QPushButton * cancelButton
std::string ToStringIP() const
Definition: netaddress.cpp:263
QLineEdit * proxyPortTor
CService LookupNumeric(const char *pszName, int portDefault)
Definition: netbase.cpp:169
void setOkButtonState(bool fState)
const QString & getOverriddenByCommandLine()
Definition: optionsmodel.h:74
QCheckBox * minimizeToTray
QValueComboBox * unit
void on_resetButton_clicked()
Line edit that can be marked as "invalid" to show input validation feedback.
QPushButton * openFabcoinConfButton
if(a.IndicesBefore(b, len, lenIndices))
Definition: equihash.cpp:243
QCheckBox * checkForUpdates
QCheckBox * zeroBalanceAddressToken
bool IsValid() const
Definition: netbase.h:34
OptionsModel * model
Definition: optionsdialog.h:67
QLabel * proxyPortTorLabel
QCheckBox * connectSocks
QCheckBox * fabcoinAtStartup
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:140
FabcoinAmountField * reserveBalance
QCheckBox * allowIncoming
void setModel(OptionsModel *model)
QDataWidgetMapper * mapper
Definition: optionsdialog.h:68
void on_okButton_clicked()
void updateDefaultProxyNets()
QCheckBox * proxyReachIPv4
QCheckBox * mapPortUpnp
QValueComboBox * lang
QCheckBox * connectSocksTor
QCheckBox * logEvents
State validate(QString &input, int &pos) const
QLabel * proxyIpTorLabel
void on_hideTrayIcon_stateChanged(int fState)
QLabel * overriddenByCommandLineLabel
QValidatedLineEdit * proxyIpTor
QPushButton * okButton
CService proxy
Definition: netbase.h:36
QCheckBox * hideTrayIcon
QCheckBox * minimizeOnClose
QLineEdit * thirdPartyTxUrls
Interface from Qt to configuration data structure for Fabcoin client.
Definition: optionsmodel.h:22
QLineEdit * proxyPort
QCheckBox * proxyReachTor
QSpinBox * databaseCache
void showRestartWarning(bool fPersistent=false)
void setCheckValidator(const QValidator *v)
void setEnabled(bool enabled)
void on_cancelButton_clicked()
QValidatedLineEdit * proxyIp
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
Definition: netbase.cpp:556
Preferences dialog.
Definition: optionsdialog.h:35
void clearStatusLabel()
std::string ToStringPort() const
Definition: netaddress.cpp:582
int GetNumCores()
Return the number of physical cores available on the current system.
Definition: util.cpp:959
void updateProxyValidationState()
QCheckBox * notUseChangeAddress
bool isRestartRequired()
QCheckBox * coinControlFeatures
Fabcoin unit definitions.
Definition: fabcoinunits.h:49
QPushButton * resetButton
ProxyAddressValidator(QObject *parent)
void on_openFabcoinConfButton_clicked()
bool openFabcoinConf()
Definition: guiutil.cpp:431