From ee9ec0504bc2a9a64f450b54211386009cae70e2 Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Thu, 26 Sep 2024 23:35:05 -0400 Subject: [PATCH] Ask before discarding generated password --- share/translations/keepassxc_en.ts | 8 ++++++++ src/gui/DatabaseWidget.cpp | 4 ++++ src/gui/PasswordGeneratorWidget.cpp | 32 ++++++++++++++++++++++------- src/gui/PasswordGeneratorWidget.h | 1 + 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/share/translations/keepassxc_en.ts b/share/translations/keepassxc_en.ts index 718015913..62b76b4be 100644 --- a/share/translations/keepassxc_en.ts +++ b/share/translations/keepassxc_en.ts @@ -6979,6 +6979,14 @@ Do you want to overwrite it? Characters: %1 + + Confirm Discard + + + + Do you want to discard this password? + + Excluded characters: "0", "1", "l", "I", "O", "|", "﹒", "B", "8", "G", "6" diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 8ec29b951..b3ad68acd 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -1973,7 +1973,11 @@ bool DatabaseWidget::lock() auto parent = modalWidget->parentWidget(); while (parent) { if (parent == this) { + // Preempt any popup questions + // TODO: Figure out a way to "reject" or "accept" instead of specific buttons + MessageBox::setNextAnswer(MessageBox::Discard); modalWidget->close(); + MessageBox::setNextAnswer(MessageBox::NoButton); break; } parent = parent->parentWidget(); diff --git a/src/gui/PasswordGeneratorWidget.cpp b/src/gui/PasswordGeneratorWidget.cpp index c1e82974e..9aee09ee9 100644 --- a/src/gui/PasswordGeneratorWidget.cpp +++ b/src/gui/PasswordGeneratorWidget.cpp @@ -67,7 +67,7 @@ PasswordGeneratorWidget::PasswordGeneratorWidget(QWidget* parent) connect(m_ui->buttonGenerate, SIGNAL(clicked()), SLOT(regeneratePassword())); connect(m_ui->buttonDeleteWordList, SIGNAL(clicked()), SLOT(deleteWordList())); connect(m_ui->buttonAddWordList, SIGNAL(clicked()), SLOT(addWordList())); - connect(m_ui->buttonClose, SIGNAL(clicked()), SIGNAL(closed())); + connect(m_ui->buttonClose, SIGNAL(clicked()), SLOT(confirmClose())); connect(m_ui->sliderLength, SIGNAL(valueChanged(int)), SLOT(passwordLengthChanged(int))); connect(m_ui->spinBoxLength, SIGNAL(valueChanged(int)), SLOT(passwordLengthChanged(int))); @@ -121,9 +121,25 @@ PasswordGeneratorWidget::~PasswordGeneratorWidget() = default; void PasswordGeneratorWidget::closeEvent(QCloseEvent* event) { - // Emits closed signal when clicking X from title bar + confirmClose(); + event->ignore(); +} + +void PasswordGeneratorWidget::confirmClose() +{ + // Only ask for discard confirmation if in popup mode + if (!m_standalone) { + auto result = MessageBox::question(this, + tr("Confirm Discard"), + tr("Do you want to discard this password?"), + MessageBox::Discard | MessageBox::Cancel, + MessageBox::Cancel); + if (result == MessageBox::Cancel) { + return; + } + } + emit closed(); - QWidget::closeEvent(event); } PasswordGeneratorWidget* PasswordGeneratorWidget::popupGenerator(QWidget* parent) @@ -325,10 +341,12 @@ void PasswordGeneratorWidget::updatePasswordLengthLabel(const QString& password) void PasswordGeneratorWidget::applyPassword() { - saveSettings(); - m_passwordGenerated = true; - emit appliedPassword(m_ui->editNewPassword->text()); - emit closed(); + if (!m_standalone) { + saveSettings(); + m_passwordGenerated = true; + emit appliedPassword(m_ui->editNewPassword->text()); + emit closed(); + } } void PasswordGeneratorWidget::copyPassword() diff --git a/src/gui/PasswordGeneratorWidget.h b/src/gui/PasswordGeneratorWidget.h index 2f92a3eca..7a479d4f8 100644 --- a/src/gui/PasswordGeneratorWidget.h +++ b/src/gui/PasswordGeneratorWidget.h @@ -69,6 +69,7 @@ public slots: void setPasswordVisible(bool visible); void deleteWordList(); void addWordList(); + void confirmClose(); protected: void closeEvent(QCloseEvent* event) override;