Update CMakeLists.txt - play with openssl
[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
162     /* Network */
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     mapper->addMapping(ui->externalSeederCommand, OptionsModel::ExternalSeeder);
174
175
176     /* Window */
177 #ifndef Q_OS_MAC
178     mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
179     mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
180 #endif
181
182     /* Display */
183     mapper->addMapping(ui->lang, OptionsModel::Language);
184     mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
185     mapper->addMapping(ui->displayAddresses, OptionsModel::DisplayAddresses);
186     mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
187     mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
188 }
189
190 void OptionsDialog::enableApplyButton()
191 {
192     ui->applyButton->setEnabled(true);
193 }
194
195 void OptionsDialog::disableApplyButton()
196 {
197     ui->applyButton->setEnabled(false);
198 }
199
200 void OptionsDialog::enableSaveButtons()
201 {
202     /* prevent enabling of the save buttons when data modified, if there is an invalid proxy address present */
203     if(fProxyIpValid && fTorIpValid)
204         setSaveButtonState(true);
205 }
206
207 void OptionsDialog::disableSaveButtons()
208 {
209     setSaveButtonState(false);
210 }
211
212 void OptionsDialog::setSaveButtonState(bool fState)
213 {
214     ui->applyButton->setEnabled(fState);
215     ui->okButton->setEnabled(fState);
216 }
217
218 void OptionsDialog::on_okButton_clicked()
219 {
220     mapper->submit();
221 //    accept();
222     close();
223 }
224
225 void OptionsDialog::on_cancelButton_clicked()
226 {
227 //    reject();
228     close();
229 }
230
231 void OptionsDialog::on_applyButton_clicked()
232 {
233     mapper->submit();
234     disableApplyButton();
235 }
236
237 void OptionsDialog::showRestartWarning_Proxy()
238 {
239     if(!fRestartWarningDisplayed_Proxy)
240     {
241         QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting NovaCoin."), QMessageBox::Ok);
242         fRestartWarningDisplayed_Proxy = true;
243     }
244 }
245
246 void OptionsDialog::showRestartWarning_Tor()
247 {
248     if(!fRestartWarningDisplayed_Proxy)
249     {
250         QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting NovaCoin."), QMessageBox::Ok);
251         fRestartWarningDisplayed_Tor = true;
252     }
253 }
254
255 void OptionsDialog::showRestartWarning_Lang()
256 {
257     if(!fRestartWarningDisplayed_Lang)
258     {
259         QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting NovaCoin."), QMessageBox::Ok);
260         fRestartWarningDisplayed_Lang = true;
261     }
262 }
263
264 void OptionsDialog::showRestartWarning_URL()
265 {
266     if(!fRestartWarningDisplayed_URL)
267     {
268         QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting NovaCoin."), QMessageBox::Ok);
269         fRestartWarningDisplayed_URL = true;
270     }
271 }
272
273
274 void OptionsDialog::updateDisplayUnit()
275 {
276     if(model)
277     {
278         /* Update transactionFee with the current unit */
279         ui->transactionFee->setDisplayUnit(model->getDisplayUnit());
280     }
281 }
282
283 void OptionsDialog::handleProxyIpValid(QValidatedLineEdit *object, bool fState)
284 {
285     // this is used in a check before re-enabling the save buttons
286     fProxyIpValid = fState;
287
288     if(fProxyIpValid)
289     {
290         enableSaveButtons();
291         ui->statusLabel->clear();
292     }
293     else
294     {
295         disableSaveButtons();
296         object->setValid(fProxyIpValid);
297         ui->statusLabel->setStyleSheet("QLabel { color: red; }");
298         ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
299     }
300 }
301
302 void OptionsDialog::handleTorIpValid(QValidatedLineEdit *object, bool fState)
303 {
304     // this is used in a check before re-enabling the save buttons
305     fTorIpValid = fState;
306
307     if(fTorIpValid)
308     {
309         enableSaveButtons();
310         ui->statusLabel->clear();
311     }
312     else
313     {
314         disableSaveButtons();
315         object->setValid(fTorIpValid);
316         ui->statusLabel->setStyleSheet("QLabel { color: red; }");
317         ui->statusLabel->setText(tr("The supplied tor address is invalid."));
318     }
319 }
320
321 bool OptionsDialog::eventFilter(QObject *object, QEvent *event)
322 {
323     if(event->type() == QEvent::FocusOut)
324     {
325         if(object == ui->proxyIp)
326         {
327             CService addr;
328             /* Check proxyIp for a valid IPv4/IPv6 address and emit the proxyIpValid signal */
329             emit proxyIpValid(ui->proxyIp, LookupNumeric(ui->proxyIp->text().toStdString().c_str(), addr));
330         }
331
332         if(object == ui->torIp)
333         {
334             CService addr;
335             /* Check proxyIp for a valid IPv4/IPv6 address and emit the torIpValid signal */
336             emit torIpValid(ui->torIp, LookupNumeric(ui->torIp->text().toStdString().c_str(), addr));
337         }
338     }
339     return QWidget::eventFilter(object, event);
340 }
341
342 void OptionsDialog::keyPressEvent(QKeyEvent *event)
343 {
344 #ifdef ANDROID
345     if(windowType() != Qt::Widget && event->key() == Qt::Key_Back)
346     {
347         close();
348     }
349 #else
350     if(windowType() != Qt::Widget && event->key() == Qt::Key_Escape)
351     {
352         close();
353     }
354 #endif
355 }
356 void OptionsDialog::on_chooseSeeder_clicked()
357 {
358 #if QT_VERSION < 0x050000
359     QString openDir = QDesktopServices::storageLocation(QDesktopServices::ApplicationsLocation);
360 #else
361     QString openDir = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
362 #endif
363
364     QString filename = QFileDialog::getOpenFileName(this, tr("Choose peer collector application"), openDir, tr("Applications (*.*)"));
365     if(!filename.isEmpty()) {
366         ui->externalSeederCommand->setText(filename);
367     }
368 }