Skip to content

Commit 23ad80f

Browse files
Copilotdroidmonkey
andcommitted
Implement fix for auto-closing database unlock dialog when file is unavailable
Co-authored-by: droidmonkey <[email protected]>
1 parent 27db862 commit 23ad80f

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

src/gui/DatabaseOpenWidget.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,12 @@ void DatabaseOpenWidget::load(const QString& filename)
257257
// Read public headers
258258
QString error;
259259
m_db.reset(new Database());
260-
m_db->open(m_filename, nullptr, &error);
260+
bool openSuccess = m_db->open(m_filename, nullptr, &error);
261+
262+
// If opening failed (e.g., file doesn't exist), show an informative message
263+
if (!openSuccess && !error.isEmpty()) {
264+
m_ui->messageWidget->showMessage(error, MessageWidget::MessageType::Warning);
265+
}
261266

262267
m_ui->fileNameLabel->setRawText(m_filename);
263268

src/gui/DatabaseTabWidget.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,11 @@ void DatabaseTabWidget::addDatabaseTab(const QString& filePath,
163163
QString canonicalFilePath = fileInfo.canonicalFilePath();
164164

165165
if (canonicalFilePath.isEmpty()) {
166+
// Don't return early - continue to show unlock dialog even if file is missing
167+
// This allows user to retry when file becomes available (e.g., cloud storage mounting)
166168
emit messageGlobal(tr("Failed to open %1. It either does not exist or is not accessible.").arg(cleanFilePath),
167169
MessageWidget::Error);
168-
return;
170+
canonicalFilePath = cleanFilePath; // Use the original path for comparison
169171
}
170172

171173
for (int i = 0, c = count(); i < c; ++i) {

tests/gui/TestGui.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,6 +2415,41 @@ void TestGui::testMenuActionStates()
24152415
QVERIFY(isActionEnabled("actionPasswordGenerator"));
24162416
}
24172417

2418+
void TestGui::testOpenMissingDatabaseFile()
2419+
{
2420+
// Test that when trying to open a non-existent database file,
2421+
// the unlock dialog is still shown (instead of auto-closing)
2422+
// This allows user to retry when the file becomes available (e.g., cloud storage mounting)
2423+
2424+
const QString nonExistentPath = "/tmp/does_not_exist.kdbx";
2425+
2426+
// Ensure the file doesn't exist
2427+
QFile::remove(nonExistentPath);
2428+
QVERIFY(!QFile::exists(nonExistentPath));
2429+
2430+
// Record initial tab count
2431+
int initialTabCount = m_tabWidget->count();
2432+
2433+
// Try to add database tab with non-existent file
2434+
// This should NOT fail but should create a tab and show unlock dialog
2435+
m_tabWidget->addDatabaseTab(nonExistentPath);
2436+
2437+
// Verify that a tab was created (unlock dialog shown)
2438+
QCOMPARE(m_tabWidget->count(), initialTabCount + 1);
2439+
2440+
// Get the database widget for the new tab
2441+
auto* dbWidget = m_tabWidget->currentDatabaseWidget();
2442+
QVERIFY(dbWidget);
2443+
2444+
// Verify the database is in a state where it can be unlocked
2445+
// (not closed/rejected due to missing file)
2446+
QVERIFY(dbWidget->isLocked());
2447+
2448+
// Close the tab to clean up
2449+
m_tabWidget->closeDatabaseTab(m_tabWidget->currentIndex());
2450+
QCOMPARE(m_tabWidget->count(), initialTabCount);
2451+
}
2452+
24182453
void TestGui::addCannedEntries()
24192454
{
24202455
// Find buttons

tests/gui/TestGui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ private slots:
7171
void testTrayRestoreHide();
7272
void testShortcutConfig();
7373
void testMenuActionStates();
74+
void testOpenMissingDatabaseFile();
7475

7576
private:
7677
void addCannedEntries();

0 commit comments

Comments
 (0)