4fc9ab8f366d6318bf3723ed956a50049dd819ab
[novacoin.git] / src / qt / optionsdialog.cpp
1 #include "optionsdialog.h"
2 #include "ui_optionsdialog.h"
3
4 #include "bitcoinunits.h"
5 #include "monitoreddatamapper.h"
6 #include "netbase.h"
7 #include "optionsmodel.h"
8 #include "dialogwindowflags.h"
9
10 #include <QDir>
11 #include <QIntValidator>
12 #include <QLocale>
13 #include <QMessageBox>
14 #include <QRegExp>
15 #include <QRegExpValidator>
16 #include <QKeyEvent>
17
18 OptionsDialog::OptionsDialog(QWidget *parent) :
19     QWidget(parent, DIALOGWINDOWHINTS),
20     ui(new Ui::OptionsDialog),
21     model(0),
22     mapper(0),
23     fRestartWarningDisplayed_Proxy(false),
24     fRestartWarningDisplayed_Tor(false),
25     fRestartWarningDisplayed_Lang(false),
26     fRestartWarningDisplayed_URL(false),
27     fProxyIpValid(true),
28     fTorIpValid(true)
29 {
30     ui->setupUi(this);
31
32     /* Network elements init */
33 #ifndef USE_UPNP
34     ui->mapPortUpnp->setEnabled(false);
35 #endif
36
37     ui->proxyIp->setEnabled(false);
38     ui->proxyPort->setEnabled(false);
39     ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
40
41     ui->torIp->setEnabled(false);
42     ui->torPort->setEnabled(false);
43     ui->torPort->setValidator(new QIntValidator(1, 65535, this));
44     ui->TorOnly->setEnabled(false);
45     ui->torName->setEnabled(false);
46
47     ui->socksVersion->setEnabled(false);
48     ui->socksVersion->addItem("5", 5);
49     ui->socksVersion->addItem("4", 4);
50     ui->socksVersion->setCurrentIndex(0);
51
52     connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyIp, SLOT(setEnabled(bool)));
53     connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyPort, SLOT(setEnabled(bool)));
54     connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->socksVersion, SLOT(setEnabled(bool)));
55     connect(ui->connectSocks, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning_Proxy()));
56
57     connect(ui->connectTor, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning_Tor()));
58     connect(ui->connectTor, SIGNAL(toggled(bool)), ui->torIp, SLOT(setEnabled(bool)));
59     connect(ui->connectTor, SIGNAL(toggled(bool)), ui->torPort, SLOT(setEnabled(bool)));
60     connect(ui->connectTor, SIGNAL(toggled(bool)), ui->TorOnly, SLOT(setEnabled(bool)));
61     connect(ui->connectTor, SIGNAL(toggled(bool)), ui->torName, SLOT(setEnabled(bool)));
62     connect(ui->TorOnly, SIGNAL(toggled(bool)), ui->connectSocks, SLOT(setDisabled(bool)));
63
64     ui->proxyIp->installEventFilter(this);
65     ui->torIp->installEventFilter(this);
66
67     /* Window elements init */
68 #ifdef Q_OS_MAC
69     ui->tabWindow->setVisible(false);
70 #endif
71
72     /* Display elements init */
73     QDir translations(":translations");
74     ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
75     foreach(const QString &langStr, translations.entryList())
76     {
77         QLocale locale(langStr);
78
79         /** check if the locale name consists of 2 parts (language_country) */
80         if(langStr.contains("_"))
81         {
82 #if QT_VERSION >= 0x040800
83             /** display language strings as "native language - native country (locale name)", e.g. "Deutsch - Deutschland (de)" */
84             ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
85 #else
86             /** display language strings as "language - country (locale name)", e.g. "German - Germany (de)" */
87             ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" - ") + QLocale::countryToString(locale.country()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
88 #endif
89         }
90         else
91         {
92 #if QT_VERSION >= 0x040800
93             /** display language strings as "native language (locale name)", e.g. "Deutsch (de)" */
94             ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
95 #else
96             /** display language strings as "language (locale name)", e.g. "German (de)" */
97             ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
98 #endif
99         }
100     }
101
102 #if QT_VERSION >= 0x040700
103     ui->thirdPartyTxUrls->setPlaceholderText("https://example.com/tx/%s");
104 #endif
105
106
107     ui->unit->setModel(new BitcoinUnits(this));
108
109     /* Widget-to-option mapper */
110     mapper = new MonitoredDataMapper(this);
111     mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
112     mapper->setOrientation(Qt::Vertical);
113
114     /* enable apply button when data modified */
115     connect(mapper, SIGNAL(viewModified()), this, SLOT(enableApplyButton()));
116     /* disable apply button when new data loaded */
117     connect(mapper, SIGNAL(currentIndexChanged(int)), this, SLOT(disableApplyButton()));
118     /* setup/change UI elements when proxy IP is invalid/valid */
119     connect(this, SIGNAL(proxyIpValid(QValidatedLineEdit *, bool)), this, SLOT(handleProxyIpValid(QValidatedLineEdit *, bool)));
120     /* setup/change UI elements when Tor IP is invalid/valid */
121     connect(this, SIGNAL(torIpValid(QValidatedLineEdit *, bool)), this, SLOT(handleTorIpValid(QValidatedLineEdit *, bool)));
122 }
123
124 OptionsDialog::~OptionsDialog()
125 {
126     delete ui;
127 }
128
129 void OptionsDialog::setModel(OptionsModel *model)
130 {
131     this->model = model;
132
133     if(model)
134     {
135         connect(model, SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
136
137         mapper->setModel(model);
138         setMapper();
139         mapper->toFirst();
140     }
141
142     /* update the display unit, to not use the default ("BTC") */
143     updateDisplayUnit();
144
145     /* warn only when language selection changes by user action (placed here so init via mapper doesn't trigger this) */
146     connect(ui->lang, SIGNAL(valueChanged()), this, SLOT(showRestartWarning_Lang()));
147     connect(ui->thirdPartyTxUrls, SIGNAL(textChanged(const QString &)), this, SLOT(showRestartWarning_URL()));
148
149     /* disable apply button after settings are loaded as there is nothing to save */
150     disableApplyButton();
151 }
152
153 void OptionsDialog::setMapper()
154 {
155     /* Main */
156     mapper->addMapping(ui->transactionFee, OptionsModel::Fee);
157     mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
158     mapper->addMapping(ui->detachDatabases, OptionsModel::DetachDatabases);
159
160     /* Network */
161     mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
162
163     mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
164     mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
165     mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
166     mapper->addMapping(ui->socksVersion, OptionsModel::ProxySocksVersion);
167
168     mapper->addMapping(ui->connectTor, OptionsModel::TorUse);
169     mapper->addMapping(ui->torIp, OptionsModel::TorIP);
170     mapper->addMapping(ui->torPort, OptionsModel::TorPort);
171     mapper->addMapping(ui->TorOnly, OptionsModel::TorOnly);
172     mapper->addMapping(ui->torName, OptionsModel::TorName);
173
174
175     /* Window */
176 #ifndef Q_OS_MAC
177     mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
178     mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
179 #endif
180
181     /* Display */
182     mapper->addMapping(ui->lang, OptionsModel::Language);
183     mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
184     mapper->addMapping(ui->displayAddresses, OptionsModel::DisplayAddresses);
185     mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
186     mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
187 }
188
189 void OptionsDialog::enableApplyButton()
190 {
191     ui->applyButton->setEnabled(true);
192 }
193
194 void OptionsDialog::disableApplyButton()
195 {
196     ui->applyButton->setEnabled(false);
197 }
198
199 void OptionsDialog::enableSaveButtons()
200 {
201     /* prevent enabling of the save buttons when data modified, if there is an invalid proxy address present */
202     if(fProxyIpValid && fTorIpValid)
203         setSaveButtonState(true);
204 }
205
206 void OptionsDialog::disableSaveButtons()
207 {
208     setSaveButtonState(false);
209 }
210
211 void OptionsDialog::setSaveButtonState(bool fState)
212 {
213     ui->applyButton->setEnabled(fState);
214     ui->okButton->setEnabled(fState);
215 }
216
217 void OptionsDialog::on_okButton_clicked()
218 {
219     mapper->submit();
220 //    accept();
221     close();
222 }
223
224 void OptionsDialog::on_cancelButton_clicked()
225 {
226 //    reject();
227     close();
228 }
229
230 void OptionsDialog::on_applyButton_clicked()
231 {
232     mapper->submit();
233     disableApplyButton();
234 }
235
236 void OptionsDialog::showRestartWarning_Proxy()
237 {
238     if(!fRestartWarningDisplayed_Proxy)
239     {
240         QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting NovaCoin."), QMessageBox::Ok);
241         fRestartWarningDisplayed_Proxy = true;
242     }
243 }
244
245 void OptionsDialog::showRestartWarning_Tor()
246 {
247     if(!fRestartWarningDisplayed_Proxy)
248     {
249         QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting NovaCoin."), QMessageBox::Ok);
250         fRestartWarningDisplayed_Tor = true;
251     }
252 }
253
254 void OptionsDialog::showRestartWarning_Lang()
255 {
256     if(!fRestartWarningDisplayed_Lang)
257     {
258         QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting NovaCoin."), QMessageBox::Ok);
259         fRestartWarningDisplayed_Lang = true;
260     }
261 }
262
263 void OptionsDialog::showRestartWarning_URL()
264 {
265     if(!fRestartWarningDisplayed_URL)
266     {
267         QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting NovaCoin."), QMessageBox::Ok);
268         fRestartWarningDisplayed_URL = true;
269     }
270 }
271
272
273 void OptionsDialog::updateDisplayUnit()
274 {
275     if(model)
276     {
277         /* Update transactionFee with the current unit */
278         ui->transactionFee->setDisplayUnit(model->getDisplayUnit());
279     }
280 }
281
282 void OptionsDialog::handleProxyIpValid(QValidatedLineEdit *object, bool fState)
283 {
284     // this is used in a check before re-enabling the save buttons
285     fProxyIpValid = fState;
286
287     if(fProxyIpValid)
288     {
289         enableSaveButtons();
290         ui->statusLabel->clear();
291     }
292     else
293     {
294         disableSaveButtons();
295         object->setValid(fProxyIpValid);
296         ui->statusLabel->setStyleSheet("QLabel { color: red; }");
297         ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
298     }
299 }
300
301 void OptionsDialog::handleTorIpValid(QValidatedLineEdit *object, bool fState)
302 {
303     // this is used in a check before re-enabling the save buttons
304     fTorIpValid = fState;
305
306     if(fTorIpValid)
307     {
308         enableSaveButtons();
309         ui->statusLabel->clear();
310     }
311     else
312     {
313         disableSaveButtons();
314         object->setValid(fTorIpValid);
315         ui->statusLabel->setStyleSheet("QLabel { color: red; }");
316         ui->statusLabel->setText(tr("The supplied tor address is invalid."));
317     }
318 }
319
320 bool OptionsDialog::eventFilter(QObject *object, QEvent *event)
321 {
322     if(event->type() == QEvent::FocusOut)
323     {
324         if(object == ui->proxyIp)
325         {
326             CService addr;
327             /* Check proxyIp for a valid IPv4/IPv6 address and emit the proxyIpValid signal */
328             emit proxyIpValid(ui->proxyIp, LookupNumeric(ui->proxyIp->text().toStdString().c_str(), addr));
329         }
330
331         if(object == ui->torIp)
332         {
333             CService addr;
334             /* Check proxyIp for a valid IPv4/IPv6 address and emit the torIpValid signal */
335             emit torIpValid(ui->torIp, LookupNumeric(ui->torIp->text().toStdString().c_str(), addr));
336         }
337     }
338     return QWidget::eventFilter(object, event);
339 }
340
341 void OptionsDialog::keyPressEvent(QKeyEvent *event)
342 {
343 #ifdef ANDROID
344     if(windowType() != Qt::Widget && event->key() == Qt::Key_Back)
345     {
346         close();
347     }
348 #else
349     if(windowType() != Qt::Widget && event->key() == Qt::Key_Escape)
350     {
351         close();
352     }
353 #endif
354 }