From cd3e1fc27e8baa51e0a0ab1a662cc8650f330721 Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Sat, 17 Feb 2018 22:16:45 -0500 Subject: [PATCH] Ask to apply generated password when commiting an entry edit * Rename saveEntry to commitEntry to accurately capture its purpose * Add message to user when commit is successful * Made all inline messages in edit entry view 2 sec visibility --- src/gui/DatabaseWidget.cpp | 13 +++++---- src/gui/EditWidget.cpp | 3 +- src/gui/PasswordGeneratorWidget.cpp | 5 ++++ src/gui/PasswordGeneratorWidget.h | 8 ++++-- src/gui/entry/EditEntryWidget.cpp | 44 ++++++++++++++++++++--------- src/gui/entry/EditEntryWidget.h | 2 +- 6 files changed, 51 insertions(+), 24 deletions(-) diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 23760668..fda7586d 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -776,21 +776,18 @@ void DatabaseWidget::switchToView(bool accepted) m_newGroup->setParent(m_newParent); m_groupView->setCurrentGroup(m_newGroup); m_groupView->expandGroup(m_newParent); - } - else { + } else { delete m_newGroup; } m_newGroup = nullptr; m_newParent = nullptr; - } - else if (m_newEntry) { + } else if (m_newEntry) { if (accepted) { m_newEntry->setGroup(m_newParent); m_entryView->setFocus(); m_entryView->setCurrentEntry(m_newEntry); - } - else { + } else { delete m_newEntry; } @@ -798,6 +795,10 @@ void DatabaseWidget::switchToView(bool accepted) m_newParent = nullptr; } + if (accepted) { + showMessage(tr("Entry updated successfully."), MessageWidget::Positive, false, 2000); + } + setCurrentWidget(m_mainWidget); } diff --git a/src/gui/EditWidget.cpp b/src/gui/EditWidget.cpp index daa2f792..65c6306e 100644 --- a/src/gui/EditWidget.cpp +++ b/src/gui/EditWidget.cpp @@ -119,7 +119,8 @@ bool EditWidget::readOnly() const void EditWidget::showMessage(const QString& text, MessageWidget::MessageType type) { - m_ui->messageWidget->showMessage(text, type); + m_ui->messageWidget->setCloseButtonVisible(false); + m_ui->messageWidget->showMessage(text, type, 2000); } void EditWidget::hideMessage() diff --git a/src/gui/PasswordGeneratorWidget.cpp b/src/gui/PasswordGeneratorWidget.cpp index e6cb0a92..7a93f86b 100644 --- a/src/gui/PasswordGeneratorWidget.cpp +++ b/src/gui/PasswordGeneratorWidget.cpp @@ -151,6 +151,11 @@ void PasswordGeneratorWidget::setStandaloneMode(bool standalone) } } +QString PasswordGeneratorWidget::getGeneratedPassword() +{ + return m_ui->editNewPassword->text(); +} + void PasswordGeneratorWidget::keyPressEvent(QKeyEvent* e) { if (e->key() == Qt::Key_Escape && m_standalone == true) { diff --git a/src/gui/PasswordGeneratorWidget.h b/src/gui/PasswordGeneratorWidget.h index 3d6d27a5..ed441437 100644 --- a/src/gui/PasswordGeneratorWidget.h +++ b/src/gui/PasswordGeneratorWidget.h @@ -49,16 +49,18 @@ public: void saveSettings(); void reset(); void setStandaloneMode(bool standalone); -public Q_SLOTS: + QString getGeneratedPassword(); + +public slots: void regeneratePassword(); + void applyPassword(); + void copyPassword(); signals: void appliedPassword(const QString& password); void dialogTerminated(); private slots: - void applyPassword(); - void copyPassword(); void updateButtonsEnabled(const QString& password); void updatePasswordStrength(const QString& password); void togglePasswordShown(bool hidden); diff --git a/src/gui/entry/EditEntryWidget.cpp b/src/gui/entry/EditEntryWidget.cpp index 4cb5e21e..c3055757 100644 --- a/src/gui/entry/EditEntryWidget.cpp +++ b/src/gui/entry/EditEntryWidget.cpp @@ -98,7 +98,7 @@ EditEntryWidget::EditEntryWidget(QWidget* parent) connect(this, SIGNAL(accepted()), SLOT(acceptEntry())); connect(this, SIGNAL(rejected()), SLOT(cancel())); - connect(this, SIGNAL(apply()), SLOT(saveEntry())); + connect(this, SIGNAL(apply()), SLOT(commitEntry())); connect(m_iconsWidget, SIGNAL(messageEditEntry(QString, MessageWidget::MessageType)), SLOT(showMessage(QString, MessageWidget::MessageType))); connect(m_iconsWidget, SIGNAL(messageEditEntryDismiss()), SLOT(hideMessage())); @@ -128,7 +128,7 @@ void EditEntryWidget::setupMain() QAction *action = new QAction(this); action->setShortcut(Qt::CTRL | Qt::Key_Return); - connect(action, SIGNAL(triggered()), this, SLOT(saveEntry())); + connect(action, SIGNAL(triggered()), this, SLOT(commitEntry())); this->addAction(action); m_mainUi->passwordGenerator->hide(); @@ -683,20 +683,39 @@ void EditEntryWidget::setForms(const Entry* entry, bool restore) m_mainUi->titleEdit->setFocus(); } -void EditEntryWidget::saveEntry() +/** + * Commit the form values to in-memory database representation + * + * @return true is commit successful, otherwise false + */ +bool EditEntryWidget::commitEntry() { if (m_history) { clear(); hideMessage(); emit editFinished(false); - return; + return true; } if (!passwordsEqual()) { showMessage(tr("Different passwords supplied."), MessageWidget::Error); - return; + return false; } + // Ask the user to apply the generator password, if open + if (m_mainUi->togglePasswordGeneratorButton->isChecked() && + m_mainUi->passwordGenerator->getGeneratedPassword() != m_mainUi->passwordEdit->text()) { + auto answer = MessageBox::question(this, tr("Apply generated password?"), + tr("Do you want to apply the generated password to this entry?"), + QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); + if (answer == QMessageBox::Yes) { + m_mainUi->passwordGenerator->applyPassword(); + } + } + + // Hide the password generator + m_mainUi->togglePasswordGeneratorButton->setChecked(false); + if (m_advancedUi->attributesView->currentIndex().isValid() && m_advancedUi->attributesEdit->isEnabled()) { QString key = m_attributesModel->keyByIndex(m_advancedUi->attributesView->currentIndex()); m_entryAttributes->set(key, m_advancedUi->attributesEdit->toPlainText(), @@ -734,19 +753,18 @@ void EditEntryWidget::saveEntry() updateSSHAgent(); } #endif + + showMessage(tr("Entry updated successfully."), MessageWidget::Positive); + return true; } void EditEntryWidget::acceptEntry() { - // Check if passwords are mismatched first to prevent saving - if (!passwordsEqual()) { - showMessage(tr("Different passwords supplied."), MessageWidget::Error); - return; + if (commitEntry()) { + clear(); + hideMessage(); + emit editFinished(true); } - - saveEntry(); - clear(); - emit editFinished(true); } void EditEntryWidget::updateEntryData(Entry* entry) const diff --git a/src/gui/entry/EditEntryWidget.h b/src/gui/entry/EditEntryWidget.h index 63b77323..66d89dbf 100644 --- a/src/gui/entry/EditEntryWidget.h +++ b/src/gui/entry/EditEntryWidget.h @@ -74,7 +74,7 @@ signals: private slots: void acceptEntry(); - void saveEntry(); + bool commitEntry(); void cancel(); void togglePasswordGeneratorButton(bool checked); void setGeneratedPassword(const QString& password);