#include "LoginDialog.h" #include #include #include #include #include #include #include "event/EventCenter.h" #include "dialogs/DialogManager.h" #include "models/User.h" #include "log/UserOperationLog.h" #include "json/jsonobject.h" #include "components/ULineEdit.h" #include "appvals/AppGlobalValues.h" #include "db/SQLHelper.h" namespace { const int LOGIN_LOCK_COUNT = 3; const int LOGIN_LOCK_MINUTIES = 1; } LoginDialog::LoginDialog(QWidget* aParent) : QDialog(aParent) , mVMainLayout(new QVBoxLayout(this)) , mDialogTitle(new QLabel(this)) , mDialogFrame(new QFrame(this)) , mHideFrame(new QFrame(this)) , mTurnoffFrame(new QFrame(this)) , mDialogContentsLayout(new QVBoxLayout(mDialogFrame)) , mAccountEdit(new ULineEdit(mDialogFrame)) , mPasswordEdit(new ULineEdit(mDialogFrame)) , mLoginButton(nullptr) , 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() { } void LoginDialog::initializeAllWidget() { this->setObjectName("loginForm"); setFocusPolicy(Qt::ClickFocus); initializeTitle(); initializeDialogFrame(); initializeLayout(); initializeEdit(); initializeLoginButton(); initializeErrorMessage(); } void LoginDialog::initializeLayout() { mVMainLayout->setContentsMargins(0, 0, 0, 0); mVMainLayout->setSpacing(0); 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; 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")); } }); 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)); 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); } }); mDialogContentsLayout->setSpacing(30); mDialogContentsLayout->setContentsMargins(20, 60, 20, 20); } void LoginDialog::initializeTitle() { mDialogTitle->setObjectName("loginTitle"); mDialogTitle->setText(tr("U S C T")); } void LoginDialog::initializeDialogFrame() { mDialogFrame->setObjectName("loginFrame"); } void LoginDialog::initializeEdit() { mAccountEdit->setObjectName("combobox_UserName"); mAccountEdit->setPlaceholderText(tr("Username")); mAccountEdit->setFocusPolicy(Qt::ClickFocus); mDialogContentsLayout->addWidget(mAccountEdit); mPasswordEdit->setObjectName("edt_Password"); mPasswordEdit->setEchoMode(QLineEdit::Password); mPasswordEdit->setPlaceholderText(tr("Password")); mPasswordEdit->setFocusPolicy(Qt::ClickFocus); mDialogContentsLayout->addWidget(mPasswordEdit); } void LoginDialog::initializeLoginButton() { 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; 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; } } 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(); QString encryptedPassword = strPassWord; // strPassWord = "123456"; QString encryptPwd = User::getEncryptedPassword(strPassWord); if (User::QueryUser(strUserCode, encryptPwd)) { resetLoginFailedCount(); mErrorMessage->setVisible(false); EventCenter::Default()->triggerEvent(GUIEvents::LoginSuccess,nullptr,nullptr); accept(); LOG_USER_OPERATION("Login Sucessful"); JsonObject::Instance()->setDefaultUser(strUserCode); mHideClickCount = 0; } 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); } } } void LoginDialog::accept() { if (JsonObject::Instance()->getAnonymousMode()){ DialogManager::Default()->requestAlertMessage(tr("Anonymous Mode active!"), DialogButtonMode::OkOnly,tr("System mode Notice")); } QDialog::accept(); clearInputData(); mIsRunning = false; } int LoginDialog::exec() { mIsRunning = true; return QDialog::exec(); } bool LoginDialog::isRunning() { return mIsRunning; } void LoginDialog::showEvent(QShowEvent* event) { QDialog::showEvent(event); emit loginDialogShown(); } void LoginDialog::resetLoginFailedCount() { mLoginFailedCount = 0; }