From 9d107926409217c53b1a3988556515faec774351 Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Sun, 31 May 2020 10:00:11 -0400 Subject: [PATCH] Update edit entry widget when backing entry changes * Fixes #4060 * Also fix not emitting entryModified() when history is truncated. This possibly fixes other crashes with apply button when the history is truncated and then the history page is shown with a deleted entry. --- src/core/Entry.cpp | 7 +++++++ src/gui/entry/EditEntryWidget.cpp | 14 ++++++++++++++ src/gui/entry/EditEntryWidget.h | 2 ++ tests/gui/TestGui.cpp | 4 ++-- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index fc553f9e5..65a271c2e 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -663,6 +663,7 @@ void Entry::truncateHistory() return; } + bool changed = false; int histMaxItems = db->metadata()->historyMaxItems(); if (histMaxItems > -1) { int historyCount = 0; @@ -674,6 +675,7 @@ void Entry::truncateHistory() if (historyCount > histMaxItems) { delete entry; i.remove(); + changed = true; } } } @@ -697,9 +699,14 @@ void Entry::truncateHistory() if (size > histMaxSize) { delete historyItem; i.remove(); + changed = true; } } } + + if (changed) { + emit entryModified(); + } } bool Entry::equals(const Entry* other, CompareItemOptions options) const diff --git a/src/gui/entry/EditEntryWidget.cpp b/src/gui/entry/EditEntryWidget.cpp index 23fd51670..cfe6fd5a9 100644 --- a/src/gui/entry/EditEntryWidget.cpp +++ b/src/gui/entry/EditEntryWidget.cpp @@ -118,6 +118,14 @@ EditEntryWidget::EditEntryWidget(QWidget* parent) setupHistory(); setupEntryUpdate(); + m_entryModifiedTimer.setSingleShot(true); + m_entryModifiedTimer.setInterval(0); + connect(&m_entryModifiedTimer, &QTimer::timeout, this, [this] { + if (isVisible() && m_entry) { + setForms(m_entry); + } + }); + connect(this, SIGNAL(accepted()), SLOT(acceptEntry())); connect(this, SIGNAL(rejected()), SLOT(cancel())); connect(this, SIGNAL(apply()), SLOT(commitEntry())); @@ -785,6 +793,8 @@ void EditEntryWidget::loadEntry(Entry* entry, m_create = create; m_history = history; + connect(m_entry, &Entry::entryModified, this, [this] { m_entryModifiedTimer.start(); }); + if (history) { setHeadline(QString("%1 \u2B29 %2").arg(parentName, tr("Entry history"))); } else { @@ -1130,6 +1140,10 @@ void EditEntryWidget::cancel() void EditEntryWidget::clear() { + if (m_entry) { + m_entry->disconnect(this); + } + m_entry = nullptr; m_db.reset(); diff --git a/src/gui/entry/EditEntryWidget.h b/src/gui/entry/EditEntryWidget.h index ea630d7eb..3d1835396 100644 --- a/src/gui/entry/EditEntryWidget.h +++ b/src/gui/entry/EditEntryWidget.h @@ -24,6 +24,7 @@ #include #include #include +#include #include "config-keepassx.h" #include "gui/EditWidget.h" @@ -198,6 +199,7 @@ private: QButtonGroup* const m_autoTypeWindowSequenceGroup; QCompleter* const m_usernameCompleter; QStringListModel* const m_usernameCompleterModel; + QTimer m_entryModifiedTimer; Q_DISABLE_COPY(EditEntryWidget) }; diff --git a/tests/gui/TestGui.cpp b/tests/gui/TestGui.cpp index c2ef5e192..e59540a02 100644 --- a/tests/gui/TestGui.cpp +++ b/tests/gui/TestGui.cpp @@ -499,9 +499,9 @@ void TestGui::testEditEntry() // Confirm edit was made QCOMPARE(m_dbWidget->currentMode(), DatabaseWidget::Mode::ViewMode); QCOMPARE(entry->title(), QString("Sample Entry_test")); - QCOMPARE(entry->foregroundColor(), fgColor); + QCOMPARE(entry->foregroundColor().toUpper(), fgColor.toUpper()); QCOMPARE(entryItem.data(Qt::ForegroundRole), QVariant(fgColor)); - QCOMPARE(entry->backgroundColor(), bgColor); + QCOMPARE(entry->backgroundColor().toUpper(), bgColor.toUpper()); QCOMPARE(entryItem.data(Qt::BackgroundRole), QVariant(bgColor)); QCOMPARE(entry->historyItems().size(), ++editCount);