From fc5f504509cac8e4553197f1ccc1d05705e32195 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 15 Jun 2025 17:15:32 +0000 Subject: [PATCH] Implement fix for auto-closing database unlock dialog when file is unavailable Co-authored-by: droidmonkey <2809491+droidmonkey@users.noreply.github.com> --- src/gui/DatabaseOpenWidget.cpp | 7 ++++++- src/gui/DatabaseTabWidget.cpp | 4 +++- tests/gui/TestGui.cpp | 35 ++++++++++++++++++++++++++++++++++ tests/gui/TestGui.h | 1 + 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/gui/DatabaseOpenWidget.cpp b/src/gui/DatabaseOpenWidget.cpp index 7cf5dcb15..167ee32f5 100644 --- a/src/gui/DatabaseOpenWidget.cpp +++ b/src/gui/DatabaseOpenWidget.cpp @@ -257,7 +257,12 @@ void DatabaseOpenWidget::load(const QString& filename) // Read public headers QString error; m_db.reset(new Database()); - m_db->open(m_filename, nullptr, &error); + bool openSuccess = m_db->open(m_filename, nullptr, &error); + + // If opening failed (e.g., file doesn't exist), show an informative message + if (!openSuccess && !error.isEmpty()) { + m_ui->messageWidget->showMessage(error, MessageWidget::MessageType::Warning); + } m_ui->fileNameLabel->setRawText(m_filename); diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp index 2bf18071d..825bceb1e 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -163,9 +163,11 @@ void DatabaseTabWidget::addDatabaseTab(const QString& filePath, QString canonicalFilePath = fileInfo.canonicalFilePath(); if (canonicalFilePath.isEmpty()) { + // Don't return early - continue to show unlock dialog even if file is missing + // This allows user to retry when file becomes available (e.g., cloud storage mounting) emit messageGlobal(tr("Failed to open %1. It either does not exist or is not accessible.").arg(cleanFilePath), MessageWidget::Error); - return; + canonicalFilePath = cleanFilePath; // Use the original path for comparison } for (int i = 0, c = count(); i < c; ++i) { diff --git a/tests/gui/TestGui.cpp b/tests/gui/TestGui.cpp index 96c8b0703..9a0f8c1d0 100644 --- a/tests/gui/TestGui.cpp +++ b/tests/gui/TestGui.cpp @@ -2464,6 +2464,41 @@ void TestGui::testMenuActionStates() QVERIFY(isActionEnabled("actionPasswordGenerator")); } +void TestGui::testOpenMissingDatabaseFile() +{ + // Test that when trying to open a non-existent database file, + // the unlock dialog is still shown (instead of auto-closing) + // This allows user to retry when the file becomes available (e.g., cloud storage mounting) + + const QString nonExistentPath = "/tmp/does_not_exist.kdbx"; + + // Ensure the file doesn't exist + QFile::remove(nonExistentPath); + QVERIFY(!QFile::exists(nonExistentPath)); + + // Record initial tab count + int initialTabCount = m_tabWidget->count(); + + // Try to add database tab with non-existent file + // This should NOT fail but should create a tab and show unlock dialog + m_tabWidget->addDatabaseTab(nonExistentPath); + + // Verify that a tab was created (unlock dialog shown) + QCOMPARE(m_tabWidget->count(), initialTabCount + 1); + + // Get the database widget for the new tab + auto* dbWidget = m_tabWidget->currentDatabaseWidget(); + QVERIFY(dbWidget); + + // Verify the database is in a state where it can be unlocked + // (not closed/rejected due to missing file) + QVERIFY(dbWidget->isLocked()); + + // Close the tab to clean up + m_tabWidget->closeDatabaseTab(m_tabWidget->currentIndex()); + QCOMPARE(m_tabWidget->count(), initialTabCount); +} + void TestGui::addCannedEntries() { // Find buttons diff --git a/tests/gui/TestGui.h b/tests/gui/TestGui.h index 514f7ce95..6e344cdd1 100644 --- a/tests/gui/TestGui.h +++ b/tests/gui/TestGui.h @@ -71,6 +71,7 @@ private slots: void testTrayRestoreHide(); void testShortcutConfig(); void testMenuActionStates(); + void testOpenMissingDatabaseFile(); private: void addCannedEntries();