Prevent new entry loss on database file reload

* Fix #3651

* Correct data loss when the database reloads due to a file change while creating a new entry. The issue occurred due to the "new parent group" pointer being invalid after the database is reloaded following merge.

* Also fix re-selecting entries following database file reload. If the entry was moved out of the current group it would result in an assert hit. This fix prevents recursively looking for the entry.
This commit is contained in:
Jonathan White 2019-10-22 22:47:45 -04:00
parent b8830dfd32
commit af263fd80d
3 changed files with 24 additions and 4 deletions

View File

@ -585,13 +585,18 @@ QList<Entry*> Group::referencesRecursive(const Entry* entry) const
[entry](const Entry* e) { return e->hasReferencesTo(entry->uuid()); }); [entry](const Entry* e) { return e->hasReferencesTo(entry->uuid()); });
} }
Entry* Group::findEntryByUuid(const QUuid& uuid) const Entry* Group::findEntryByUuid(const QUuid& uuid, bool recursive) const
{ {
if (uuid.isNull()) { if (uuid.isNull()) {
return nullptr; return nullptr;
} }
for (Entry* entry : entriesRecursive(false)) { auto entries = m_entries;
if (recursive) {
entries = entriesRecursive(false);
}
for (auto entry : entries) {
if (entry->uuid() == uuid) { if (entry->uuid() == uuid) {
return entry; return entry;
} }

View File

@ -114,7 +114,7 @@ public:
static const QString RootAutoTypeSequence; static const QString RootAutoTypeSequence;
Group* findChildByName(const QString& name); Group* findChildByName(const QString& name);
Entry* findEntryByUuid(const QUuid& uuid) const; Entry* findEntryByUuid(const QUuid& uuid, bool recursive = true) const;
Entry* findEntryByPath(const QString& entryPath); Entry* findEntryByPath(const QString& entryPath);
Entry* findEntryBySearchTerm(const QString& term, EntryReferenceType referenceType); Entry* findEntryBySearchTerm(const QString& term, EntryReferenceType referenceType);
Group* findGroupByUuid(const QUuid& uuid); Group* findGroupByUuid(const QUuid& uuid);

View File

@ -381,6 +381,12 @@ void DatabaseWidget::createEntry()
void DatabaseWidget::replaceDatabase(QSharedPointer<Database> db) void DatabaseWidget::replaceDatabase(QSharedPointer<Database> db)
{ {
// Save off new parent UUID which will be valid when creating a new entry
QUuid newParentUuid;
if (m_newParent) {
newParentUuid = m_newParent->uuid();
}
// TODO: instead of increasing the ref count temporarily, there should be a clean // TODO: instead of increasing the ref count temporarily, there should be a clean
// break from the old database. Without this crashes occur due to the change // break from the old database. Without this crashes occur due to the change
// signals triggering dangling pointers. // signals triggering dangling pointers.
@ -390,6 +396,15 @@ void DatabaseWidget::replaceDatabase(QSharedPointer<Database> db)
m_groupView->changeDatabase(m_db); m_groupView->changeDatabase(m_db);
processAutoOpen(); processAutoOpen();
// Restore the new parent group pointer, if not found default to the root group
// this prevents data loss when merging a database while creating a new entry
if (!newParentUuid.isNull()) {
m_newParent = m_db->rootGroup()->findGroupByUuid(newParentUuid);
if (!m_newParent) {
m_newParent = m_db->rootGroup();
}
}
emit databaseReplaced(oldDb, m_db); emit databaseReplaced(oldDb, m_db);
#if defined(WITH_XC_KEESHARE) #if defined(WITH_XC_KEESHARE)
@ -1480,7 +1495,7 @@ void DatabaseWidget::restoreGroupEntryFocus(const QUuid& groupUuid, const QUuid&
auto group = m_db->rootGroup()->findGroupByUuid(groupUuid); auto group = m_db->rootGroup()->findGroupByUuid(groupUuid);
if (group) { if (group) {
m_groupView->setCurrentGroup(group); m_groupView->setCurrentGroup(group);
auto entry = group->findEntryByUuid(entryUuid); auto entry = group->findEntryByUuid(entryUuid, false);
if (entry) { if (entry) {
m_entryView->setCurrentEntry(entry); m_entryView->setCurrentEntry(entry);
} }