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