UI: WindowManager: Refactor password checking

This commit is contained in:
Nodir Temirkhodjaev 2023-05-17 14:08:17 +03:00
parent 6fca7c1709
commit 61bb0d1955
5 changed files with 53 additions and 40 deletions

View File

@ -8,6 +8,7 @@
#include <QPushButton>
#include <form/controls/controlutil.h>
#include <fortsettings.h>
#include <manager/windowmanager.h>
#include <util/guiutil.h>
#include <util/window/widgetwindow.h>
@ -18,6 +19,16 @@ PasswordDialog::PasswordDialog(QWidget *parent) : QDialog(parent)
retranslateUi();
}
QString PasswordDialog::password() const
{
return m_editPassword->text();
}
int PasswordDialog::unlockType() const
{
return m_comboUnlock->currentIndex();
}
void PasswordDialog::retranslateUi()
{
m_labelPassword->setText(tr("Please enter the password:"));
@ -34,8 +45,8 @@ void PasswordDialog::retranslateUi()
void PasswordDialog::retranslateComboUnlock()
{
m_comboUnlock->clear();
m_comboUnlock->addItems(unlockTypeStrings());
m_comboUnlock->setCurrentIndex(0);
m_comboUnlock->addItems(FortSettings::unlockTypeStrings());
m_comboUnlock->setCurrentIndex(FortSettings::UnlockDisabled);
}
void PasswordDialog::setupUi()
@ -107,23 +118,3 @@ void PasswordDialog::setupButtonBox()
QObject::connect(m_buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
QObject::connect(m_buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}
bool PasswordDialog::getPassword(QString &password, int &unlockType, QWidget *parent)
{
PasswordDialog dialog(parent);
WidgetWindow::showWidget(&dialog);
if (dialog.exec() == 0)
return false;
password = dialog.m_editPassword->text();
unlockType = dialog.m_comboUnlock->currentIndex();
return true;
}
QStringList PasswordDialog::unlockTypeStrings()
{
return { tr("Disabled"), tr("Session lockout"), tr("Program exit") };
}

View File

@ -15,9 +15,8 @@ class PasswordDialog : public QDialog
public:
explicit PasswordDialog(QWidget *parent = nullptr);
static bool getPassword(QString &password, int &unlockType, QWidget *parent = nullptr);
static QStringList unlockTypeStrings();
QString password() const;
int unlockType() const;
private:
void retranslateUi();

View File

@ -74,7 +74,7 @@ bool FortSettings::checkPassword(const QString &password) const
bool FortSettings::isPasswordRequired()
{
return hasPassword() && !(m_passwordUnlockType != UnlockDisabled && m_passwordChecked);
return hasPassword() && !(passwordUnlockType() != UnlockDisabled && passwordChecked());
}
void FortSettings::setPasswordChecked(bool checked, int unlockType)
@ -490,3 +490,8 @@ bool FortSettings::canMigrate(QString &viaVersion) const
return true;
}
QStringList FortSettings::unlockTypeStrings()
{
return { tr("Disabled"), tr("Session lockout"), tr("Program exit") };
}

View File

@ -79,6 +79,8 @@ public:
static bool isPortable();
static QString defaultProfilePath(bool isService);
static QStringList unlockTypeStrings();
signals:
void passwordCheckedChanged();

View File

@ -48,14 +48,25 @@ struct MessageBoxArg
const QString title;
};
QMessageBox *createMessageBox(const MessageBoxArg &ba, QWidget *parent)
void setupModalDialog(QDialog *box)
{
auto box = new QMessageBox(ba.icon, ba.title, ba.text, ba.buttons, parent);
box->setAttribute(Qt::WA_DeleteOnClose);
box->setWindowModality(parent ? Qt::WindowModal : Qt::ApplicationModal);
box->setWindowModality(box->parent() ? Qt::WindowModal : Qt::ApplicationModal);
box->setModal(true);
}
QMessageBox *createMessageBox(const MessageBoxArg &ba, QWidget *parent = nullptr)
{
auto box = new QMessageBox(ba.icon, ba.title, ba.text, ba.buttons, parent);
setupModalDialog(box);
return box;
}
PasswordDialog *createPasswordDialog(QWidget *parent = nullptr)
{
auto box = new PasswordDialog(parent);
setupModalDialog(box);
return box;
}
@ -498,31 +509,36 @@ bool WindowManager::widgetVisibleByCheckPassword(QWidget *w)
bool WindowManager::checkPassword()
{
static bool g_passwordDialogOpened = false;
const auto settings = IoC<FortSettings>();
if (!settings->isPasswordRequired())
return true;
if (g_passwordDialogOpened) {
if (isAnyWindowOpen(WindowPasswordDialog)) {
activateModalWidget();
return false;
}
g_passwordDialogOpened = true;
auto box = createPasswordDialog();
QString password;
int unlockType = FortSettings::UnlockDisabled;
const bool ok = PasswordDialog::getPassword(password, unlockType, mainWindow());
connect(box, &QMessageBox::accepted, [=] {
const QString password = box->password();
const int unlockType = box->unlockType();
g_passwordDialogOpened = false;
const bool checked = !password.isEmpty() && IoC<ConfManager>()->checkPassword(password);
const bool checked = ok && !password.isEmpty() && IoC<ConfManager>()->checkPassword(password);
settings->setPasswordChecked(checked, unlockType);
});
settings->setPasswordChecked(checked, unlockType);
windowOpened(WindowPasswordDialog);
return checked;
WidgetWindow::showWidget(box);
box->exec();
windowClosed(WindowPasswordDialog);
return settings->passwordChecked();
}
void WindowManager::showErrorBox(const QString &text, const QString &title, QWidget *parent)