Implement fix for auto-closing database unlock dialog when file is unavailable

Co-authored-by: droidmonkey <2809491+droidmonkey@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-06-15 17:15:32 +00:00
committed by Janek Bevendorff
parent 6c963e0000
commit fc5f504509
4 changed files with 45 additions and 2 deletions

View File

@@ -257,7 +257,12 @@ void DatabaseOpenWidget::load(const QString& filename)
// Read public headers // Read public headers
QString error; QString error;
m_db.reset(new Database()); 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); m_ui->fileNameLabel->setRawText(m_filename);

View File

@@ -163,9 +163,11 @@ void DatabaseTabWidget::addDatabaseTab(const QString& filePath,
QString canonicalFilePath = fileInfo.canonicalFilePath(); QString canonicalFilePath = fileInfo.canonicalFilePath();
if (canonicalFilePath.isEmpty()) { 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), emit messageGlobal(tr("Failed to open %1. It either does not exist or is not accessible.").arg(cleanFilePath),
MessageWidget::Error); MessageWidget::Error);
return; canonicalFilePath = cleanFilePath; // Use the original path for comparison
} }
for (int i = 0, c = count(); i < c; ++i) { for (int i = 0, c = count(); i < c; ++i) {

View File

@@ -2464,6 +2464,41 @@ void TestGui::testMenuActionStates()
QVERIFY(isActionEnabled("actionPasswordGenerator")); 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() void TestGui::addCannedEntries()
{ {
// Find buttons // Find buttons

View File

@@ -71,6 +71,7 @@ private slots:
void testTrayRestoreHide(); void testTrayRestoreHide();
void testShortcutConfig(); void testShortcutConfig();
void testMenuActionStates(); void testMenuActionStates();
void testOpenMissingDatabaseFile();
private: private:
void addCannedEntries(); void addCannedEntries();