#include "MasterPasswordDialog.h" #include #include #include #include "VaultManager.h" MasterPasswordDialog::MasterPasswordDialog(bool isSetupMode, QWidget *parent) : QDialog(parent), setupMode(isSetupMode) { setWindowTitle(setupMode ? "Set Master Password" : "Unlock Vault"); setModal(true); auto *layout = new QVBoxLayout(this); auto *passLabel = new QLabel("Master password:", this); layout->addWidget(passLabel); passwordEdit = new QLineEdit(this); passwordEdit->setEchoMode(QLineEdit::Password); layout->addWidget(passwordEdit); if (setupMode) { auto *confirmLabel = new QLabel("Confirm master password:", this); layout->addWidget(confirmLabel); confirmEdit = new QLineEdit(this); confirmEdit->setEchoMode(QLineEdit::Password); layout->addWidget(confirmEdit); } else { confirmEdit = nullptr; } errorLabel = new QLabel(this); QPalette pal = errorLabel->palette(); pal.setColor(QPalette::WindowText, Qt::red); errorLabel->setPalette(pal); errorLabel->hide(); layout->addWidget(errorLabel); buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); buttonBox->button(QDialogButtonBox::Ok)->setText(setupMode ? "Initialize" : "Unlock"); connect(buttonBox, &QDialogButtonBox::accepted, this, &MasterPasswordDialog::handleSubmit); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); layout->addWidget(buttonBox); passwordEdit->setFocus(); } void MasterPasswordDialog::handleSubmit() { QString password = passwordEdit->text(); errorLabel->hide(); if (setupMode) { if (password.isEmpty()) { errorLabel->setText("Password cannot be empty."); errorLabel->show(); return; } if (password != confirmEdit->text()) { errorLabel->setText("Passwords do not match."); errorLabel->show(); return; } if (VaultManager::instance().setup(password)) { accept(); } else { errorLabel->setText("Failed to initialize vault."); errorLabel->show(); } } else { if (VaultManager::instance().unlock(password)) { accept(); } else { errorLabel->setText("Incorrect password."); errorLabel->show(); passwordEdit->clear(); passwordEdit->setFocus(); } } }