Skip to content

Commit c28d393

Browse files
committed
Merge pull request #7025
2a8e8c2 [Qt] don't allow to store invalid proxy ports (Jonas Schnelli) d16d1b7 [Qt] refactor and optimize proxy settings behavior (Jonas Schnelli)
2 parents 5d5ef3a + 2a8e8c2 commit c28d393

File tree

4 files changed

+65
-53
lines changed

4 files changed

+65
-53
lines changed

src/qt/optionsdialog.cpp

Lines changed: 31 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

@@ -60,12 +59,11 @@ OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
6059

6160
connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyIp, SLOT(setEnabled(bool)));
6261
connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyPort, SLOT(setEnabled(bool)));
62+
connect(ui->connectSocks, SIGNAL(toggled(bool)), this, SLOT(updateProxyValidationState()));
6363

6464
connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyIpTor, SLOT(setEnabled(bool)));
6565
connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyPortTor, SLOT(setEnabled(bool)));
66-
67-
ui->proxyIp->installEventFilter(this);
68-
ui->proxyIpTor->installEventFilter(this);
66+
connect(ui->connectSocksTor, SIGNAL(toggled(bool)), this, SLOT(updateProxyValidationState()));
6967

7068
/* Window elements init */
7169
#ifdef Q_OS_MAC
@@ -119,7 +117,12 @@ OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
119117
mapper->setOrientation(Qt::Vertical);
120118

121119
/* setup/change UI elements when proxy IPs are invalid/valid */
122-
connect(this, SIGNAL(proxyIpChecks(QValidatedLineEdit *, int)), this, SLOT(doProxyIpChecks(QValidatedLineEdit *, int)));
120+
ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
121+
ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
122+
connect(ui->proxyIp, SIGNAL(validationDidChange(QValidatedLineEdit *)), this, SLOT(updateProxyValidationState()));
123+
connect(ui->proxyIpTor, SIGNAL(validationDidChange(QValidatedLineEdit *)), this, SLOT(updateProxyValidationState()));
124+
connect(ui->proxyPort, SIGNAL(textChanged(const QString&)), this, SLOT(updateProxyValidationState()));
125+
connect(ui->proxyPortTor, SIGNAL(textChanged(const QString&)), this, SLOT(updateProxyValidationState()));
123126
}
124127

125128
OptionsDialog::~OptionsDialog()
@@ -200,18 +203,6 @@ void OptionsDialog::setMapper()
200203
mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
201204
}
202205

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-
215206
void OptionsDialog::setOkButtonState(bool fState)
216207
{
217208
ui->okButton->setEnabled(fState);
@@ -269,24 +260,20 @@ void OptionsDialog::clearStatusLabel()
269260
ui->statusLabel->clear();
270261
}
271262

272-
void OptionsDialog::doProxyIpChecks(QValidatedLineEdit *pUiProxyIp, int nProxyPort)
263+
void OptionsDialog::updateProxyValidationState()
273264
{
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)))
265+
QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
266+
QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
267+
if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
280268
{
281-
disableOkButton();
282-
pUiProxyIp->setValid(false);
283-
ui->statusLabel->setStyleSheet("QLabel { color: red; }");
284-
ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
269+
setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
270+
ui->statusLabel->clear();
285271
}
286272
else
287273
{
288-
enableOkButton();
289-
ui->statusLabel->clear();
274+
setOkButtonState(false);
275+
ui->statusLabel->setStyleSheet("QLabel { color: red; }");
276+
ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
290277
}
291278
}
292279

@@ -312,18 +299,18 @@ void OptionsDialog::updateDefaultProxyNets()
312299
(strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
313300
}
314301

315-
bool OptionsDialog::eventFilter(QObject *object, QEvent *event)
302+
ProxyAddressValidator::ProxyAddressValidator(QObject *parent) :
303+
QValidator(parent)
316304
{
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);
305+
}
306+
307+
QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
308+
{
309+
Q_UNUSED(pos);
310+
// Validate the proxy
311+
proxyType addrProxy = proxyType(CService(input.toStdString(), 9050), true);
312+
if (addrProxy.IsValid())
313+
return QValidator::Acceptable;
314+
315+
return QValidator::Invalid;
329316
}

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();
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)