Skip to content

Commit

Permalink
Improve handling of read-only files (keepassxreboot#3408)
Browse files Browse the repository at this point in the history
* Fix keepassxreboot#3407
* Read-only files now disable auto-save and show as modified correctly. This allows the GUI to prompt to "save-as" instead of silently discarding changes when the read-only database is locked or closed.
  • Loading branch information
droidmonkey authored and scoroi committed Nov 10, 2019
1 parent bb0852c commit 37e16df
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
14 changes: 6 additions & 8 deletions src/core/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,12 @@ bool Database::save(QString* error, bool atomic, bool backup)
*/
bool Database::save(const QString& filePath, QString* error, bool atomic, bool backup)
{
Q_ASSERT(!m_data.isReadOnly);
if (m_data.isReadOnly) {
// Disallow saving to the same file if read-only
if (m_data.isReadOnly && filePath == m_data.filePath) {
Q_ASSERT_X(false, "Database::save", "Could not save, database file is read-only.");
if (error) {
*error = tr("Could not save, database file is read-only.");
}
return false;
}

Expand Down Expand Up @@ -741,10 +745,6 @@ bool Database::isModified() const

void Database::markAsModified()
{
if (isReadOnly()) {
return;
}

m_modified = true;
if (m_emitModified) {
startModifiedTimer();
Expand Down Expand Up @@ -780,8 +780,6 @@ Database* Database::databaseByFilePath(const QString& filePath)

void Database::startModifiedTimer()
{
Q_ASSERT(!m_data.isReadOnly);

if (!m_emitModified) {
return;
}
Expand Down
7 changes: 5 additions & 2 deletions src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,10 @@ void DatabaseWidget::unlockDatabase(bool accepted)
}
replaceDatabase(db);
if (db->isReadOnly()) {
showMessage(tr("File opened in read only mode."), MessageWidget::Warning, false, -1);
showMessage(tr("This database is opened in read-only mode. Autosave is disabled."),
MessageWidget::Warning,
false,
-1);
}

restoreGroupEntryFocus(m_groupBeforeLock, m_entryBeforeLock);
Expand Down Expand Up @@ -1225,7 +1228,7 @@ void DatabaseWidget::onGroupChanged(Group* group)

void DatabaseWidget::onDatabaseModified()
{
if (!m_blockAutoSave && config()->get("AutoSaveAfterEveryChange").toBool()) {
if (!m_blockAutoSave && config()->get("AutoSaveAfterEveryChange").toBool() && !m_db->isReadOnly()) {
save();
} else {
// Only block once, then reset
Expand Down

0 comments on commit 37e16df

Please sign in to comment.