Anoncoin  0.9.4
P2P Digital Currency
optionsdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 The Bitcoin developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #if defined(HAVE_CONFIG_H)
8 #endif
9 
10 #include "optionsdialog.h"
11 #include "ui_optionsdialog.h"
12 
13 #include "anoncoinunits.h"
14 #include "guiutil.h"
15 #include "monitoreddatamapper.h"
16 #include "optionsmodel.h"
17 
18 #include "main.h" // for CTransaction::nMinTxFee and MAX_SCRIPTCHECK_THREADS
19 #include "netbase.h"
20 #include "txdb.h" // for -dbcache defaults
21 
22 #include <QDir>
23 #include <QIntValidator>
24 #include <QLocale>
25 #include <QMessageBox>
26 #include <QTimer>
27 
28 OptionsDialog::OptionsDialog(QWidget *parent) :
29  QDialog(parent),
30  ui(new Ui::OptionsDialog),
31  model(0),
32  mapper(0),
33  fProxyIpValid(true)
34 #ifdef ENABLE_I2PSAM
35  , fRestartWarningDisplayed_I2P(false)
36  , tabI2P(new I2POptionsWidget()) // Add the I2P OptionsWidget tab
37 #endif
38 {
39  ui->setupUi(this);
40  GUIUtil::restoreWindowGeometry("nOptionsDialogWindow", this->size(), this);
41 
42  /* Main elements init */
43  ui->databaseCache->setMinimum(nMinDbCache);
44  ui->databaseCache->setMaximum(nMaxDbCache);
45  ui->threadsScriptVerif->setMinimum(-(int)boost::thread::hardware_concurrency());
46  ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
47 
48  /* Network elements init */
49 #ifndef USE_UPNP
50  ui->mapPortUpnp->setEnabled(false);
51 #endif
52 
53  ui->proxyIp->setEnabled(false);
54  ui->proxyPort->setEnabled(false);
55  ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
56 
57  connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyIp, SLOT(setEnabled(bool)));
58  connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyPort, SLOT(setEnabled(bool)));
59 
60  ui->proxyIp->installEventFilter(this);
61 
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
67 
68  /* Display elements init */
69  QDir translations(":translations");
70  ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
71  foreach(const QString &langStr, translations.entryList())
72  {
73  QLocale locale(langStr);
74 
76  if(langStr.contains("_"))
77  {
78 #if QT_VERSION >= 0x040800
79 
80  ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
81 #else
82 
83  ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" - ") + QLocale::countryToString(locale.country()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
84 #endif
85  }
86  else
87  {
88 #if QT_VERSION >= 0x040800
89 
90  ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
91 #else
92 
93  ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
94 #endif
95  }
96  }
97 #if QT_VERSION >= 0x040700
98  ui->thirdPartyTxUrls->setPlaceholderText("https://example.com/tx/%s");
99 #endif
100 
101  ui->unit->setModel(new AnoncoinUnits(this));
103 
104  /* Widget-to-option mapper */
105  mapper = new MonitoredDataMapper(this);
106  mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
107  mapper->setOrientation(Qt::Vertical);
108 
109  /* setup/change UI elements when proxy IP is invalid/valid */
110  connect(this, SIGNAL(proxyIpChecks(QValidatedLineEdit *, int)), this, SLOT(doProxyIpChecks(QValidatedLineEdit *, int)));
111 #ifdef ENABLE_I2PSAM
112  ui->tabWidget->addTab(tabI2P, QString("I2P"));
113 #endif
114 }
115 
117 {
118  GUIUtil::saveWindowGeometry("nOptionsDialogWindow", this);
119  delete ui;
120 }
121 
123 {
124  this->model = model;
125 
126  if(model)
127  {
128  /* check if client restart is needed and show persistent message */
129  if (model->isRestartRequired())
130  showRestartWarning(true);
131 
132  QString strLabel = model->getOverriddenByCommandLine();
133  if (strLabel.isEmpty())
134  strLabel = tr("none");
135  ui->overriddenByCommandLineLabel->setText(strLabel);
136 
137  connect(model, SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
138 
139  mapper->setModel(model);
140  setMapper();
141  mapper->toFirst();
142  }
143 
144  /* update the display unit, to not use the default ("ANC") */
146 
147  /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
148 
149  /* Main */
150  connect(ui->databaseCache, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
151  connect(ui->threadsScriptVerif, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
152  /* Wallet */
153  connect(ui->spendZeroConfChange, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
154  /* Network */
155  connect(ui->connectSocks, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
156  /* Display */
157  connect(ui->lang, SIGNAL(valueChanged()), this, SLOT(showRestartWarning()));
158  connect(ui->thirdPartyTxUrls, SIGNAL(textChanged(const QString &)), this, SLOT(showRestartWarning()));
159 #ifdef ENABLE_I2PSAM
160  QObject::connect(tabI2P, SIGNAL(settingsChanged()), this, SLOT(showRestartWarning_I2P()));
161 #endif
162 }
163 
164 #ifdef ENABLE_I2PSAM
165 // ToDo: Double check me. Ported from v0.8.6 anoncoin code, all things related to tabI2P created here,
166 // and the associated i2poptionswidget. Would prefer to see it simplely added as a widget tab,
167 // in the optionsdialog.ui base file, then remove i2poptionswidget.ui from the build dependancies
168 // and remove some of the code changes that were required here to upgrade to 0.9.4.
169 //
170 // GR Note: Needed to add this override, guess to implement the dynamic tab created for I2P configuraton
171 //
173 {
174  if (clientModel) tabI2P->setModel(clientModel);
175 }
176 #endif // ENABLE_I2PSAM
177 
179 {
180  /* Main */
184 
185  /* Wallet */
189 
190  /* Network */
192 
196 
197  /* Window */
198 #ifndef Q_OS_MAC
201 #endif
202 
203  /* Display */
207 #ifdef ENABLE_I2PSAM
209 #endif
210 }
211 
213 {
214  /* prevent enabling of the OK button when data modified, if there is an invalid proxy address present */
215  if(fProxyIpValid)
216  setOkButtonState(true);
217 }
218 
220 {
221  setOkButtonState(false);
222 }
223 
225 {
226  ui->okButton->setEnabled(fState);
227 }
228 
230 {
231  if(model)
232  {
233  // confirmation dialog
234  QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
235  tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shutdown, do you want to proceed?"),
236  QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
237 
238  if(btnRetVal == QMessageBox::Cancel)
239  return;
240 #ifdef ENABLE_I2PSAM
241  /* disable restart warning messages display */
242  // ToDo: Check that this overall model functionality is correct.
243  // GR Note: The code behavior here is different between 0.8.5.6 and 0.9.3, or I'm not understanding what
244  // was added and/or changed.
245  // Not sure if this fRestartWarningDisplayed_I2P flag matters give the app quits. and I didn't setup any
246  // special resetwarning flags for the lang or proxy tabs....
248 #endif
249  /* reset all options and close GUI */
250  model->Reset();
251  QApplication::quit();
252 
253 #ifdef ENABLE_I2PSAM
254  /* re-enable restart warning messages display */
256 #endif
257  }
258 }
259 
261 {
262  mapper->submit();
263  accept();
264 }
265 
267 {
268  reject();
269 }
270 
271 void OptionsDialog::showRestartWarning(bool fPersistent)
272 {
273  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
274 
275  if(fPersistent)
276  {
277  ui->statusLabel->setText(tr("Client restart required to activate changes."));
278  }
279  else
280  {
281  ui->statusLabel->setText(tr("This change would require a client restart."));
282  // clear non-persistent status label after 10 seconds
283  // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
284  QTimer::singleShot(10000, this, SLOT(clearStatusLabel()));
285  }
286 }
287 
288 #ifdef ENABLE_I2PSAM
290 {
292  {
293  QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting Anoncoin."), QMessageBox::Ok);
295  }
296 }
297 #endif // ENABLE_I2PSAM
298 
300 {
301  ui->statusLabel->clear();
302 }
303 
305 {
306  if(model)
307  {
308  /* Update transactionFee with the current unit */
310  }
311 }
312 
313 void OptionsDialog::doProxyIpChecks(QValidatedLineEdit *pUiProxyIp, int nProxyPort)
314 {
315  Q_UNUSED(nProxyPort);
316 
317  const std::string strAddrProxy = pUiProxyIp->text().toStdString();
318  CService addrProxy;
319 
320  /* Check for a valid IPv4 / IPv6 address */
321  if (!(fProxyIpValid = LookupNumeric(strAddrProxy.c_str(), addrProxy)))
322  {
323  disableOkButton();
324  pUiProxyIp->setValid(false);
325  ui->statusLabel->setStyleSheet("QLabel { color: red; }");
326  ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
327  }
328  else
329  {
330  enableOkButton();
331  ui->statusLabel->clear();
332  }
333 }
334 
335 bool OptionsDialog::eventFilter(QObject *object, QEvent *event)
336 {
337  if(event->type() == QEvent::FocusOut)
338  {
339  if(object == ui->proxyIp)
340  {
341  emit proxyIpChecks(ui->proxyIp, ui->proxyPort->text().toInt());
342  }
343  }
344  return QDialog::eventFilter(object, event);
345 }
QCheckBox * spendZeroConfChange
QTabWidget * tabWidget
Ui::OptionsDialog * ui
Definition: optionsdialog.h:58
OptionsDialog(QWidget *parent)
I2POptionsWidget * tabI2P
Definition: optionsdialog.h:64
void setDisplayUnit(int unit)
Change unit used to display amount.
void showRestartWarning_I2P()
QSpinBox * threadsScriptVerif
Anoncoin unit definitions.
Definition: anoncoinunits.h:15
void setupUi(QDialog *OptionsDialog)
void setOkButtonState(bool fState)
const QString & getOverriddenByCommandLine()
Definition: optionsmodel.h:91
bool LookupNumeric(const char *pszName, CService &addr, int portDefault)
Definition: netbase.cpp:253
QCheckBox * minimizeToTray
QValueComboBox * unit
void on_resetButton_clicked()
Line edit that can be marked as "invalid" to show input validation feedback.
static int64_t nMinTxFee
Fees smaller than this (in satoshi) are considered zero fee (for transaction creation) ...
Definition: core.h:182
void saveWindowGeometry(const QString &strSetting, QWidget *parent)
Save window size and position.
Definition: guiutil.cpp:738
void setModel(ClientModel *model)
void updateDisplayUnit()
OptionsModel * model
Definition: optionsdialog.h:59
MonitoredDataMapper * mapper
Definition: optionsdialog.h:60
QCheckBox * connectSocks
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netbase.h:109
void setModel(OptionsModel *model)
void on_okButton_clicked()
QCheckBox * anoncoinAtStartup
int getDisplayUnit()
Definition: optionsmodel.h:87
QCheckBox * mapPortUpnp
void restoreWindowGeometry(const QString &strSetting, const QSize &defaultSize, QWidget *parent)
Restore window size and position.
Definition: guiutil.cpp:745
void proxyIpChecks(QValidatedLineEdit *pUiProxyIp, int nProxyPort)
Model for Anoncoin network client.
Definition: clientmodel.h:45
QValueComboBox * lang
QLabel * overriddenByCommandLineLabel
QPushButton * okButton
QCheckBox * minimizeOnClose
QLineEdit * thirdPartyTxUrls
Interface from Qt to configuration data structure for Anoncoin client.
Definition: optionsmodel.h:27
void doProxyIpChecks(QValidatedLineEdit *pUiProxyIp, int nProxyPort)
void addMapping(QWidget *widget, int section)
QLineEdit * proxyPort
AnoncoinAmountField * transactionFee
#define ENABLE_I2PSAM
bool eventFilter(QObject *object, QEvent *event)
void setSingleStep(qint64 step)
Set single step in satoshis.
QSpinBox * databaseCache
void showRestartWarning(bool fPersistent=false)
void setEnabled(bool enabled)
Data to Widget mapper that watches for edits and notifies listeners when a field is edited...
void on_cancelButton_clicked()
QValidatedLineEdit * proxyIp
bool fRestartWarningDisplayed_I2P
Definition: optionsdialog.h:63
Preferences dialog.
Definition: optionsdialog.h:22
void clearStatusLabel()
void setMapper(MonitoredDataMapper &mapper)
bool isRestartRequired()
void setClientModel(ClientModel *clientModel)
QCheckBox * coinControlFeatures
void setValid(bool valid)
void disableOkButton()