Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of read-only files #3408

Merged
merged 2 commits into from
Aug 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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