diff --git a/src/windows/LoginDialog.cpp b/src/windows/LoginDialog.cpp index 018df13..6b6d89e 100644 --- a/src/windows/LoginDialog.cpp +++ b/src/windows/LoginDialog.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "event/EventCenter.h" #include "dialogs/DialogManager.h" @@ -13,6 +14,12 @@ #include "json/jsonobject.h" #include "components/ULineEdit.h" +namespace +{ + const int LOGIN_LOCK_COUNT = 3; + const int LOGIN_LOCK_MINUTIES = 1; +} + LoginDialog::LoginDialog(QWidget* aParent) : QDialog(aParent) , mVMainLayout(new QVBoxLayout(this)) @@ -27,11 +34,15 @@ LoginDialog::LoginDialog(QWidget* aParent) , mErrorMessage(new QLabel(this)) , mIsRunning(false) , mHideClickCount(0) + , mLoginFailedCount(0) + , mLoginFailedTimer(new QTimer(this)) { initializeAllWidget(); setWindowFlags(windowFlags() | Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint| Qt::BypassWindowManagerHint); setGeometry(QApplication::desktop()->screenGeometry()); mAccountEdit->setText(JsonObject::Instance()->defaultUser()); + mLoginFailedTimer->setSingleShot(true); + connect(mLoginFailedTimer, &QTimer::timeout, this, &LoginDialog::resetLoginFailedCount); } LoginDialog::~LoginDialog() @@ -134,7 +145,6 @@ void LoginDialog::initializeLoginButton() void LoginDialog::initializeErrorMessage() { mErrorMessage->setObjectName("warn"); - mErrorMessage->setText("Login failed, username or password error!"); mErrorMessage->setVisible(false); mDialogContentsLayout->addWidget(mErrorMessage, 0, Qt::AlignCenter); } @@ -148,6 +158,13 @@ void LoginDialog::clearInputData() void LoginDialog::doLogin() { + if(mLoginFailedCount >= LOGIN_LOCK_COUNT) + { + mErrorMessage->setText(QString(tr("Login locked. Please retry after %1 minutes.")).arg(LOGIN_LOCK_MINUTIES)); + mErrorMessage->setVisible(true); + return; + } + QString strUserCode = mAccountEdit->text(); QString strPassWord = mPasswordEdit->text(); @@ -157,6 +174,7 @@ void LoginDialog::doLogin() QString encryptPwd = User::getEncryptedPassword(strPassWord); if (User::QueryUser(strUserCode, encryptPwd)) { + resetLoginFailedCount(); mErrorMessage->setVisible(false); accept(); LOG_USER_OPERATION("Login Sucessful"); @@ -166,7 +184,13 @@ void LoginDialog::doLogin() else { LOG_USER_OPERATION(QString("Login Failed, User Name: %1").arg(strUserCode)); + ++mLoginFailedCount; + mErrorMessage->setText(QString(tr("Login failed, username or password error! Remaining retries: %1")).arg(LOGIN_LOCK_COUNT - mLoginFailedCount)); mErrorMessage->setVisible(true); + if(LOGIN_LOCK_COUNT - mLoginFailedCount == 0) + { + mLoginFailedTimer->start(LOGIN_LOCK_MINUTIES * 60000); + } } } @@ -198,5 +222,8 @@ void LoginDialog::showEvent(QShowEvent* event) emit loginDialogShown(); } - +void LoginDialog::resetLoginFailedCount() +{ + mLoginFailedCount = 0; +} diff --git a/src/windows/LoginDialog.h b/src/windows/LoginDialog.h index 4dcb011..4b0dc90 100644 --- a/src/windows/LoginDialog.h +++ b/src/windows/LoginDialog.h @@ -33,6 +33,7 @@ signals: private slots: void doLogin(); + void resetLoginFailedCount(); private: void initializeAllWidget(); @@ -56,6 +57,8 @@ private: QLabel* mErrorMessage; bool mIsRunning; int mHideClickCount; + int mLoginFailedCount; + QTimer* mLoginFailedTimer; }; #endif // LOGINDIALOG_H