Improve handling of read-only files (#3408)

* Fix #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.
This commit is contained in:
Jonathan White 2019-08-30 21:39:37 -04:00 committed by GitHub
parent 3b330ee2d1
commit 2aac83d03b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 10 deletions

View File

@ -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;
}
@ -741,10 +745,6 @@ bool Database::isModified() const
void Database::markAsModified()
{
if (isReadOnly()) {
return;
}
m_modified = true;
if (m_emitModified) {
startModifiedTimer();
@ -780,8 +780,6 @@ Database* Database::databaseByFilePath(const QString& filePath)
void Database::startModifiedTimer()
{
Q_ASSERT(!m_data.isReadOnly);
if (!m_emitModified) {
return;
}

View File

@ -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);
@ -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