Skip to content

Commit

Permalink
Update edit entry widget when backing entry changes
Browse files Browse the repository at this point in the history
* Fixes keepassxreboot#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.
  • Loading branch information
droidmonkey committed Jun 4, 2020
1 parent 2073f2d commit 9d10792
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/core/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ void Entry::truncateHistory()
return;
}

bool changed = false;
int histMaxItems = db->metadata()->historyMaxItems();
if (histMaxItems > -1) {
int historyCount = 0;
Expand All @@ -674,6 +675,7 @@ void Entry::truncateHistory()
if (historyCount > histMaxItems) {
delete entry;
i.remove();
changed = true;
}
}
}
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/gui/entry/EditEntryWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -1130,6 +1140,10 @@ void EditEntryWidget::cancel()

void EditEntryWidget::clear()
{
if (m_entry) {
m_entry->disconnect(this);
}

m_entry = nullptr;
m_db.reset();

Expand Down
2 changes: 2 additions & 0 deletions src/gui/entry/EditEntryWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QModelIndex>
#include <QPointer>
#include <QScopedPointer>
#include <QTimer>

#include "config-keepassx.h"
#include "gui/EditWidget.h"
Expand Down Expand Up @@ -198,6 +199,7 @@ private slots:
QButtonGroup* const m_autoTypeWindowSequenceGroup;
QCompleter* const m_usernameCompleter;
QStringListModel* const m_usernameCompleterModel;
QTimer m_entryModifiedTimer;

Q_DISABLE_COPY(EditEntryWidget)
};
Expand Down
4 changes: 2 additions & 2 deletions tests/gui/TestGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 9d10792

Please sign in to comment.