From 884688001554c3879147f3e50b919130636c359a Mon Sep 17 00:00:00 2001 From: Dmytro <121412908+dmaslenko@users.noreply.github.com> Date: Sun, 29 Jan 2023 06:24:10 -0800 Subject: [PATCH] Fix db history when adding new db (#9022) Fixes https://github.com/keepassxreboot/keepassxc/issues/8375 --- src/core/Database.cpp | 2 +- src/gui/DatabaseTabWidget.cpp | 13 +++++++++++++ src/gui/DatabaseTabWidget.h | 1 + tests/TestDatabase.cpp | 28 ++++++++++++++++++++++++++++ tests/TestDatabase.h | 1 + 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/core/Database.cpp b/src/core/Database.cpp index 508cbe51c..c42419536 100644 --- a/src/core/Database.cpp +++ b/src/core/Database.cpp @@ -275,8 +275,8 @@ bool Database::saveAs(const QString& filePath, SaveAction action, const QString& bool isNewFile = !QFile::exists(realFilePath); bool ok = AsyncTask::runAndWaitForFuture([&] { return performSave(realFilePath, action, backupFilePath, error); }); if (ok) { - markAsClean(); setFilePath(filePath); + markAsClean(); if (isNewFile) { QFile::setPermissions(realFilePath, QFile::ReadUser | QFile::WriteUser); } diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp index 8d95aaef1..531d14212 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -244,6 +244,7 @@ void DatabaseTabWidget::addDatabaseTab(DatabaseWidget* dbWidget, bool inBackgrou SLOT(updateTabName())); connect(dbWidget, SIGNAL(databaseModified()), SLOT(updateTabName())); connect(dbWidget, SIGNAL(databaseSaved()), SLOT(updateTabName())); + connect(dbWidget, SIGNAL(databaseSaved()), SLOT(updateLastDatabases())); connect(dbWidget, SIGNAL(databaseUnlocked()), SLOT(updateTabName())); connect(dbWidget, SIGNAL(databaseUnlocked()), SLOT(emitDatabaseLockChanged())); connect(dbWidget, SIGNAL(databaseLocked()), SLOT(updateTabName())); @@ -829,6 +830,18 @@ void DatabaseTabWidget::updateLastDatabases(const QString& filename) } } +void DatabaseTabWidget::updateLastDatabases() +{ + auto dbWidget = currentDatabaseWidget(); + + if (dbWidget) { + auto filePath = dbWidget->database()->filePath(); + if (!filePath.isEmpty()) { + updateLastDatabases(filePath); + } + } +} + void DatabaseTabWidget::emitActiveDatabaseChanged() { emit activeDatabaseChanged(currentDatabaseWidget()); diff --git a/src/gui/DatabaseTabWidget.h b/src/gui/DatabaseTabWidget.h index 38a1822bf..3a6791a80 100644 --- a/src/gui/DatabaseTabWidget.h +++ b/src/gui/DatabaseTabWidget.h @@ -105,6 +105,7 @@ private slots: void emitDatabaseLockChanged(); void handleDatabaseUnlockDialogFinished(bool accepted, DatabaseWidget* dbWidget); void handleExportError(const QString& reason); + void updateLastDatabases(); private: QSharedPointer execNewDatabaseWizard(); diff --git a/tests/TestDatabase.cpp b/tests/TestDatabase.cpp index 3b5864273..9ab9de785 100644 --- a/tests/TestDatabase.cpp +++ b/tests/TestDatabase.cpp @@ -99,6 +99,34 @@ void TestDatabase::testSave() QVERIFY(!QFile::exists(backupFilePath)); } +void TestDatabase::testSaveAs() +{ + TemporaryFile tempFile; + QVERIFY(tempFile.copyFromFile(dbFileName)); + + auto db = QSharedPointer::create(); + auto key = QSharedPointer::create(); + key->addKey(QSharedPointer::create("a")); + + QString error; + QVERIFY(db->open(tempFile.fileName(), key, &error)); + + // Happy path case when try to save as new DB. + QSignalSpy spyFilePathChanged(db.data(), SIGNAL(filePathChanged(const QString&, const QString&))); + QString newDbFileName = QStringLiteral(KEEPASSX_TEST_DATA_DIR).append("/SaveAsNewDatabase.kdbx"); + QVERIFY2(db->saveAs(newDbFileName, Database::Atomic, QString(), &error), error.toLatin1()); + QVERIFY(!db->isModified()); + QCOMPARE(spyFilePathChanged.count(), 1); + QVERIFY(QFile::exists(newDbFileName)); + QFile::remove(newDbFileName); + QVERIFY(!QFile::exists(newDbFileName)); + + // Negative case when try to save not initialized DB. + db->releaseData(); + QVERIFY2(!db->saveAs(newDbFileName, Database::Atomic, QString(), &error), error.toLatin1()); + QCOMPARE(error, QString("Could not save, database has not been initialized!")); +} + void TestDatabase::testSignals() { TemporaryFile tempFile; diff --git a/tests/TestDatabase.h b/tests/TestDatabase.h index 511703849..9f4bfab56 100644 --- a/tests/TestDatabase.h +++ b/tests/TestDatabase.h @@ -29,6 +29,7 @@ private slots: void initTestCase(); void testOpen(); void testSave(); + void testSaveAs(); void testSignals(); void testEmptyRecycleBinOnDisabled(); void testEmptyRecycleBinOnNotCreated();