Skip to content

Commit d16d1b7

Browse files
committed
[Qt] refactor and optimize proxy settings behavior
1 parent 6876a78 commit d16d1b7

File tree

4 files changed

+60
-53
lines changed

4 files changed

+60
-53
lines changed

src/qt/optionsdialog.cpp

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
3434
QDialog(parent),
3535
ui(new Ui::OptionsDialog),
3636
model(0),
37-
mapper(0),
38-
fProxyIpsValid(true)
37+
mapper(0)
3938
{
4039
ui->setupUi(this);
4140

@@ -64,9 +63,6 @@ OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
6463
connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyIpTor, SLOT(setEnabled(bool)));
6564
connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyPortTor, SLOT(setEnabled(bool)));
6665

67-
ui->proxyIp->installEventFilter(this);
68-
ui->proxyIpTor->installEventFilter(this);
69-
7066
/* Window elements init */
7167
#ifdef Q_OS_MAC
7268
/* remove Window tab on Mac */
@@ -119,7 +115,10 @@ OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
119115
mapper->setOrientation(Qt::Vertical);
120116

121117
/* setup/change UI elements when proxy IPs are invalid/valid */
122-
connect(this, SIGNAL(proxyIpChecks(QValidatedLineEdit *, int)), this, SLOT(doProxyIpChecks(QValidatedLineEdit *, int)));
118+
ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
119+
ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
120+
connect(ui->proxyIp, SIGNAL(validationDidChange(QValidatedLineEdit *)), this, SLOT(updateProxyValidationState(QValidatedLineEdit *)));
121+
connect(ui->proxyIpTor, SIGNAL(validationDidChange(QValidatedLineEdit *)), this, SLOT(updateProxyValidationState(QValidatedLineEdit *)));
123122
}
124123

125124
OptionsDialog::~OptionsDialog()
@@ -200,18 +199,6 @@ void OptionsDialog::setMapper()
200199
mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
201200
}
202201

203-
void OptionsDialog::enableOkButton()
204-
{
205-
/* prevent enabling of the OK button when data modified, if there is an invalid proxy address present */
206-
if(fProxyIpsValid)
207-
setOkButtonState(true);
208-
}
209-
210-
void OptionsDialog::disableOkButton()
211-
{
212-
setOkButtonState(false);
213-
}
214-
215202
void OptionsDialog::setOkButtonState(bool fState)
216203
{
217204
ui->okButton->setEnabled(fState);
@@ -269,24 +256,19 @@ void OptionsDialog::clearStatusLabel()
269256
ui->statusLabel->clear();
270257
}
271258

272-
void OptionsDialog::doProxyIpChecks(QValidatedLineEdit *pUiProxyIp, int nProxyPort)
259+
void OptionsDialog::updateProxyValidationState(QValidatedLineEdit *pUiProxyIp)
273260
{
274-
Q_UNUSED(nProxyPort);
275-
276-
CService addrProxy;
277-
278-
/* Check for a valid IPv4 / IPv6 address */
279-
if (!(fProxyIpsValid = LookupNumeric(pUiProxyIp->text().toStdString().c_str(), addrProxy)))
261+
QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
262+
if (pUiProxyIp->isValid())
280263
{
281-
disableOkButton();
282-
pUiProxyIp->setValid(false);
283-
ui->statusLabel->setStyleSheet("QLabel { color: red; }");
284-
ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
264+
setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
265+
ui->statusLabel->clear();
285266
}
286267
else
287268
{
288-
enableOkButton();
289-
ui->statusLabel->clear();
269+
setOkButtonState(false);
270+
ui->statusLabel->setStyleSheet("QLabel { color: red; }");
271+
ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
290272
}
291273
}
292274

@@ -312,18 +294,18 @@ void OptionsDialog::updateDefaultProxyNets()
312294
(strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
313295
}
314296

315-
bool OptionsDialog::eventFilter(QObject *object, QEvent *event)
297+
ProxyAddressValidator::ProxyAddressValidator(QObject *parent) :
298+
QValidator(parent)
316299
{
317-
if(event->type() == QEvent::FocusOut)
318-
{
319-
if(object == ui->proxyIp)
320-
{
321-
Q_EMIT proxyIpChecks(ui->proxyIp, ui->proxyPort->text().toInt());
322-
}
323-
else if(object == ui->proxyIpTor)
324-
{
325-
Q_EMIT proxyIpChecks(ui->proxyIpTor, ui->proxyPortTor->text().toInt());
326-
}
327-
}
328-
return QDialog::eventFilter(object, event);
300+
}
301+
302+
QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
303+
{
304+
Q_UNUSED(pos);
305+
// Validate the proxy
306+
proxyType addrProxy = proxyType(CService(input.toStdString(), 9050), true);
307+
if (addrProxy.IsValid())
308+
return QValidator::Acceptable;
309+
310+
return QValidator::Invalid;
329311
}

src/qt/optionsdialog.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_QT_OPTIONSDIALOG_H
77

88
#include <QDialog>
9+
#include <QValidator>
910

1011
class OptionsModel;
1112
class QValidatedLineEdit;
@@ -18,6 +19,18 @@ namespace Ui {
1819
class OptionsDialog;
1920
}
2021

22+
/** Proxy address widget validator, checks for a valid proxy address.
23+
*/
24+
class ProxyAddressValidator : public QValidator
25+
{
26+
Q_OBJECT
27+
28+
public:
29+
explicit ProxyAddressValidator(QObject *parent);
30+
31+
State validate(QString &input, int &pos) const;
32+
};
33+
2134
/** Preferences dialog. */
2235
class OptionsDialog : public QDialog
2336
{
@@ -30,14 +43,7 @@ class OptionsDialog : public QDialog
3043
void setModel(OptionsModel *model);
3144
void setMapper();
3245

33-
protected:
34-
bool eventFilter(QObject *object, QEvent *event);
35-
3646
private Q_SLOTS:
37-
/* enable OK button */
38-
void enableOkButton();
39-
/* disable OK button */
40-
void disableOkButton();
4147
/* set OK button state (enabled / disabled) */
4248
void setOkButtonState(bool fState);
4349
void on_resetButton_clicked();
@@ -46,7 +52,7 @@ private Q_SLOTS:
4652

4753
void showRestartWarning(bool fPersistent = false);
4854
void clearStatusLabel();
49-
void doProxyIpChecks(QValidatedLineEdit *pUiProxyIp, int nProxyPort);
55+
void updateProxyValidationState(QValidatedLineEdit *pUiProxyIp);
5056
/* query the networks, for which the default proxy is used */
5157
void updateDefaultProxyNets();
5258

@@ -57,7 +63,6 @@ private Q_SLOTS:
5763
Ui::OptionsDialog *ui;
5864
OptionsModel *model;
5965
QDataWidgetMapper *mapper;
60-
bool fProxyIpsValid;
6166
};
6267

6368
#endif // BITCOIN_QT_OPTIONSDIALOG_H

src/qt/qvalidatedlineedit.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,25 @@ void QValidatedLineEdit::checkValidity()
9999
}
100100
else
101101
setValid(false);
102+
103+
Q_EMIT validationDidChange(this);
102104
}
103105

104106
void QValidatedLineEdit::setCheckValidator(const QValidator *v)
105107
{
106108
checkValidator = v;
107109
}
110+
111+
bool QValidatedLineEdit::isValid()
112+
{
113+
// use checkValidator in case the QValidatedLineEdit is disabled
114+
if (checkValidator)
115+
{
116+
QString address = text();
117+
int pos = 0;
118+
if (checkValidator->validate(address, pos) == QValidator::Acceptable)
119+
return true;
120+
}
121+
122+
return valid;
123+
}

src/qt/qvalidatedlineedit.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class QValidatedLineEdit : public QLineEdit
1818
explicit QValidatedLineEdit(QWidget *parent);
1919
void clear();
2020
void setCheckValidator(const QValidator *v);
21+
bool isValid();
2122

2223
protected:
2324
void focusInEvent(QFocusEvent *evt);
@@ -31,6 +32,9 @@ public Q_SLOTS:
3132
void setValid(bool valid);
3233
void setEnabled(bool enabled);
3334

35+
Q_SIGNALS:
36+
void validationDidChange(QValidatedLineEdit *validatedLineEdit);
37+
3438
private Q_SLOTS:
3539
void markValid();
3640
void checkValidity();

0 commit comments

Comments
 (0)