Files
GUI/src/windows/LoginDialog.cpp

246 lines
7.7 KiB
C++
Raw Normal View History

2022-06-08 17:06:38 +08:00
#include "LoginDialog.h"
#include <QVBoxLayout>
2022-07-20 16:45:33 +08:00
#include <QApplication>
2022-07-28 16:27:51 +08:00
#include <QDesktopWidget>
2022-06-08 17:06:38 +08:00
#include <QToolButton>
#include <QtWidgets/QLabel>
2023-09-25 14:35:34 +08:00
#include <QTimer>
2022-06-08 17:06:38 +08:00
#include "event/EventCenter.h"
2023-09-08 10:20:10 +08:00
#include "dialogs/DialogManager.h"
2022-06-08 17:06:38 +08:00
#include "models/User.h"
#include "log/UserOperationLog.h"
#include "json/jsonobject.h"
#include "components/ULineEdit.h"
#include "appvals/AppGlobalValues.h"
#include "db/SQLHelper.h"
2022-06-08 17:06:38 +08:00
2023-09-25 14:35:34 +08:00
namespace
{
const int LOGIN_LOCK_COUNT = 3;
const int LOGIN_LOCK_MINUTIES = 1;
}
2022-06-08 17:06:38 +08:00
LoginDialog::LoginDialog(QWidget* aParent)
: QDialog(aParent)
, mVMainLayout(new QVBoxLayout(this))
, mDialogTitle(new QLabel(this))
, mDialogFrame(new QFrame(this))
2023-09-11 16:23:59 +08:00
, mHideFrame(new QFrame(this))
2023-09-08 10:20:10 +08:00
, mTurnoffFrame(new QFrame(this))
2022-06-08 17:06:38 +08:00
, mDialogContentsLayout(new QVBoxLayout(mDialogFrame))
, mAccountEdit(new ULineEdit(mDialogFrame))
, mPasswordEdit(new ULineEdit(mDialogFrame))
2022-06-08 17:06:38 +08:00
, mLoginButton(nullptr)
, mErrorMessage(new QLabel(this))
2023-08-21 14:22:41 +08:00
, mIsRunning(false)
2023-09-11 16:23:59 +08:00
, mHideClickCount(0)
2023-09-25 14:35:34 +08:00
, mLoginFailedCount(0)
, mLoginFailedTimer(new QTimer(this))
2022-06-08 17:06:38 +08:00
{
initializeAllWidget();
2022-07-28 16:27:51 +08:00
setWindowFlags(windowFlags() | Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint| Qt::BypassWindowManagerHint);
2022-07-20 16:45:33 +08:00
setGeometry(QApplication::desktop()->screenGeometry());
2022-06-08 17:06:38 +08:00
mAccountEdit->setText(JsonObject::Instance()->defaultUser());
2023-09-25 14:35:34 +08:00
mLoginFailedTimer->setSingleShot(true);
connect(mLoginFailedTimer, &QTimer::timeout, this, &LoginDialog::resetLoginFailedCount);
2022-06-08 17:06:38 +08:00
}
LoginDialog::~LoginDialog()
{
}
void LoginDialog::initializeAllWidget()
{
2022-07-12 16:13:09 +08:00
this->setObjectName("loginForm");
2022-07-20 17:05:50 +08:00
setFocusPolicy(Qt::ClickFocus);
2022-06-08 17:06:38 +08:00
initializeTitle();
initializeDialogFrame();
initializeLayout();
initializeEdit();
initializeLoginButton();
initializeErrorMessage();
}
void LoginDialog::initializeLayout()
{
mVMainLayout->setContentsMargins(0, 0, 0, 0);
mVMainLayout->setSpacing(0);
2023-09-11 16:23:59 +08:00
mVMainLayout->addWidget(mHideFrame, 0, Qt::AlignLeft);
QHBoxLayout * hLayout1 = new QHBoxLayout(mHideFrame);
hLayout1->setDirection(QBoxLayout::RightToLeft);
QToolButton * hideBtn = new QToolButton(mHideFrame);
hideBtn->setObjectName("HideBtn");
hLayout1->addWidget(hideBtn);
connect(hideBtn, &QToolButton::clicked, [=]()
{
mHideClickCount++;
if (mHideClickCount == 5){
mHideClickCount = 0;
2023-09-13 14:27:37 +08:00
DialogResult result = DialogManager::Default()->requestResetAdminPwd();
if (result.ResultCode != QDialog::Accepted) return;
DialogManager::Default()->requestAlertMessage(QString(tr("New password:%1")).arg(result.ResultData.toString()),
DialogButtonMode::OkOnly,tr("Reset success"));
2023-09-11 16:23:59 +08:00
}
});
2022-06-08 17:06:38 +08:00
mVMainLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding));
mVMainLayout->addWidget(mDialogTitle, 0, Qt::AlignCenter);
mVMainLayout->addWidget(mDialogFrame, 0, Qt::AlignCenter);
mVMainLayout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding));
2023-09-08 10:20:10 +08:00
mTurnoffFrame->setObjectName("TurnoffFrame");
mVMainLayout->addWidget(mTurnoffFrame, 0, Qt::AlignRight);
QHBoxLayout * hLayout = new QHBoxLayout(mTurnoffFrame);
hLayout->setDirection(QBoxLayout::RightToLeft);
QToolButton * turnOffBtn = new QToolButton(mTurnoffFrame);
turnOffBtn->setObjectName("TurnoffBtn");
hLayout->addWidget(turnOffBtn);
connect(turnOffBtn, &QToolButton::clicked, []()
{
if(DialogManager::Default()->requestAlertMessage(QString(tr("Shut down now ?")), DialogButtonMode::OkAndCancel,tr("Shut Down")) == QDialog::Accepted)
{
LOG_USER_OPERATION("Shut Down")
EventCenter::Default()->triggerEvent(GUIEvents::RequestShutdown, nullptr, nullptr);
}
});
2022-06-08 17:06:38 +08:00
mDialogContentsLayout->setSpacing(30);
mDialogContentsLayout->setContentsMargins(20, 60, 20, 20);
}
void LoginDialog::initializeTitle()
{
2023-09-11 16:23:59 +08:00
mDialogTitle->setObjectName("loginTitle");
2022-06-08 17:06:38 +08:00
mDialogTitle->setText(tr("U S C T"));
}
void LoginDialog::initializeDialogFrame()
{
2022-07-12 16:13:09 +08:00
mDialogFrame->setObjectName("loginFrame");
2022-06-08 17:06:38 +08:00
}
void LoginDialog::initializeEdit()
{
mAccountEdit->setObjectName("combobox_UserName");
mAccountEdit->setPlaceholderText(tr("Username"));
mAccountEdit->setFocusPolicy(Qt::ClickFocus);
2022-06-08 17:06:38 +08:00
mDialogContentsLayout->addWidget(mAccountEdit);
mPasswordEdit->setObjectName("edt_Password");
mPasswordEdit->setEchoMode(QLineEdit::Password);
mPasswordEdit->setPlaceholderText(tr("Password"));
mPasswordEdit->setFocusPolicy(Qt::ClickFocus);
2022-06-08 17:06:38 +08:00
mDialogContentsLayout->addWidget(mPasswordEdit);
}
void LoginDialog::initializeLoginButton()
{
2022-07-14 16:59:30 +08:00
auto btnLogin = new QToolButton(this);
btnLogin->setObjectName("btnlogin");
mDialogContentsLayout->addWidget(btnLogin);
btnLogin->setText(tr("Login"));
mDialogContentsLayout->removeWidget(btnLogin);
mDialogContentsLayout->addWidget(btnLogin, 0, Qt::AlignCenter);
mLoginButton = btnLogin;
2022-06-08 17:06:38 +08:00
connect(mLoginButton, SIGNAL(clicked()), this, SLOT(doLogin()));
}
void LoginDialog::initializeErrorMessage()
{
mErrorMessage->setObjectName("warn");
mErrorMessage->setVisible(false);
mDialogContentsLayout->addWidget(mErrorMessage, 0, Qt::AlignCenter);
}
void LoginDialog::clearInputData()
{
mAccountEdit->clear();
mPasswordEdit->clear();
}
void LoginDialog::doLogin()
{
if (!(AppGlobalValues::DBconnected().toBool())){
bool sqlConnected= SQLHelper::Open();
if (sqlConnected)
{
AppGlobalValues::setDBconnected(true);
}
else{
mErrorMessage->setText(QString(tr("Can't connect db. Please reboot the device and retry, or call for the service help.")).arg(LOGIN_LOCK_MINUTIES));
mErrorMessage->setVisible(true);
return;
}
}
2023-09-25 14:35:34 +08:00
if(mLoginFailedCount >= LOGIN_LOCK_COUNT)
{
mErrorMessage->setText(QString(tr("Login locked. Please retry after %1 minutes.")).arg(LOGIN_LOCK_MINUTIES));
mErrorMessage->setVisible(true);
return;
}
2022-06-08 17:06:38 +08:00
QString strUserCode = mAccountEdit->text();
QString strPassWord = mPasswordEdit->text();
QString encryptedPassword = strPassWord;
// strPassWord = "123456";
2022-06-08 17:06:38 +08:00
QString encryptPwd = User::getEncryptedPassword(strPassWord);
if (User::QueryUser(strUserCode, encryptPwd))
{
2023-09-25 14:35:34 +08:00
resetLoginFailedCount();
2022-07-14 16:59:30 +08:00
mErrorMessage->setVisible(false);
EventCenter::Default()->triggerEvent(GUIEvents::LoginSuccess,nullptr,nullptr);
2022-06-08 17:06:38 +08:00
accept();
2023-09-05 16:32:38 +08:00
LOG_USER_OPERATION("Login Sucessful");
2022-06-08 17:06:38 +08:00
JsonObject::Instance()->setDefaultUser(strUserCode);
2023-09-11 16:23:59 +08:00
mHideClickCount = 0;
2022-06-08 17:06:38 +08:00
}
else
{
2023-09-05 16:32:38 +08:00
LOG_USER_OPERATION(QString("Login Failed, User Name: %1").arg(strUserCode));
2023-09-25 14:35:34 +08:00
++mLoginFailedCount;
mErrorMessage->setText(QString(tr("Login failed, username or password error! Remaining retries: %1")).arg(LOGIN_LOCK_COUNT - mLoginFailedCount));
2022-06-08 17:06:38 +08:00
mErrorMessage->setVisible(true);
2023-09-25 14:35:34 +08:00
if(LOGIN_LOCK_COUNT - mLoginFailedCount == 0)
{
mLoginFailedTimer->start(LOGIN_LOCK_MINUTIES * 60000);
}
2022-06-08 17:06:38 +08:00
}
}
2023-08-21 14:22:41 +08:00
void LoginDialog::accept()
{
2023-09-15 11:42:40 +08:00
if (JsonObject::Instance()->getAnonymousMode()){
DialogManager::Default()->requestAlertMessage(tr("Anonymous Mode active!"),
DialogButtonMode::OkOnly,tr("System mode Notice"));
}
2022-07-20 16:45:33 +08:00
QDialog::accept();
clearInputData();
2023-08-21 14:22:41 +08:00
mIsRunning = false;
}
int LoginDialog::exec()
{
mIsRunning = true;
return QDialog::exec();
}
bool LoginDialog::isRunning()
{
return mIsRunning;
2022-07-20 16:45:33 +08:00
}
2023-08-31 14:26:54 +08:00
void LoginDialog::showEvent(QShowEvent* event)
{
QDialog::showEvent(event);
emit loginDialogShown();
}
2023-09-25 14:35:34 +08:00
void LoginDialog::resetLoginFailedCount()
{
mLoginFailedCount = 0;
}
2022-06-08 17:06:38 +08:00