Gerrit: Improve button enabling in AuthenticationDialog

* If the text was obviously pasted from the clipboard,
  check instantly
* Otherwise, check two seconds after the last keystroke

Change-Id: I639e2f5dea596afdbb917cf0cffd0cf574d0032f
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
Andre Hartmann
2017-03-04 18:52:07 +01:00
committed by André Hartmann
parent 2f51a42461
commit 4ce5964422
2 changed files with 34 additions and 6 deletions

View File

@@ -31,11 +31,14 @@
#include <utils/fileutils.h>
#include <utils/hostosinfo.h>
#include <QClipboard>
#include <QDir>
#include <QFile>
#include <QGuiApplication>
#include <QPushButton>
#include <QRegularExpression>
#include <QTextStream>
#include <QTimer>
namespace Gerrit {
namespace Internal {
@@ -83,12 +86,19 @@ AuthenticationDialog::AuthenticationDialog(GerritServer *server) :
if (button == anonymous)
m_authenticated = false;
});
QPushButton *okButton = ui->buttonBox->button(QDialogButtonBox::Ok);
okButton->setEnabled(false);
connect(ui->passwordLineEdit, &QLineEdit::editingFinished, this, [this, server, okButton] {
setupCredentials();
const int result = server->testConnection();
okButton->setEnabled(result == 200);
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
connect(ui->passwordLineEdit, &QLineEdit::editingFinished,
this, &AuthenticationDialog::checkCredentials);
m_checkTimer = new QTimer(this);
m_checkTimer->setSingleShot(true);
connect(m_checkTimer, &QTimer::timeout, this, &AuthenticationDialog::checkCredentials);
connect(ui->passwordLineEdit, &QLineEdit::textChanged, [this]() {
if (QGuiApplication::clipboard()->text() == ui->passwordLineEdit->text()) {
checkCredentials();
return;
}
m_checkTimer->start(2000);
});
if (!ui->userLineEdit->text().isEmpty())
ui->passwordLineEdit->setFocus();
@@ -129,6 +139,10 @@ bool AuthenticationDialog::setupCredentials()
bool found = false;
const QString user = ui->userLineEdit->text().trimmed();
const QString password = ui->passwordLineEdit->text().trimmed();
if (user.isEmpty() || password.isEmpty())
return false;
for (QString &line : m_allMachines) {
const QString machine = findEntry(line, "machine");
if (machine == m_server->host) {
@@ -145,5 +159,13 @@ bool AuthenticationDialog::setupCredentials()
return saver.finalize();
}
void AuthenticationDialog::checkCredentials()
{
int result = 400;
if (setupCredentials())
result = m_server->testConnection();
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(result == 200);
}
} // Internal
} // Gerrit

View File

@@ -29,6 +29,10 @@
#include <QCoreApplication>
#include <QDialog>
QT_BEGIN_NAMESPACE
class QTimer;
QT_END_NAMESPACE
namespace Gerrit {
namespace Internal {
@@ -48,11 +52,13 @@ public:
private:
void readExistingConf();
bool setupCredentials();
void checkCredentials();
Ui::AuthenticationDialog *ui = nullptr;
GerritServer *m_server = nullptr;
QString m_netrcFileName;
QStringList m_allMachines;
bool m_authenticated = true;
QTimer *m_checkTimer = nullptr;
};
} // Internal