Skip to content

Commit

Permalink
Remove the .xopp~ backup file after a successful save
Browse files Browse the repository at this point in the history
This is a combination of 4 commits.

* Fixes #3399 Fixes #1498
  Also fixes that no backup file was created anymore after the first save
  of a file
* Cleanup
* Create a backup file on any save where a file already exists
* Update comment
  • Loading branch information
Luca0208 authored and Technius committed Feb 6, 2022
1 parent 9f66a83 commit 91ebfab
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/control/Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2406,7 +2406,7 @@ auto Control::saveAs() -> bool {
return false;
}

// no lock needed, this is an uncritically operation
// no lock needed, this is an uncritical operation
this->doc->setCreateBackupOnSave(false);
return save();
}
Expand Down
12 changes: 10 additions & 2 deletions src/control/jobs/SaveJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ auto SaveJob::save() -> bool {

Util::clearExtensions(filepath, ".pdf");
auto const target = fs::path{filepath}.concat(".xopp");
auto const createBackup = doc->shouldCreateBackupOnSave();

if (doc->shouldCreateBackupOnSave()) {
if (createBackup) {
try {
// Note: The backup must be created for the target as this is the filepath
// which will be written to. Do not use the `filepath` variable!
Expand All @@ -102,7 +103,6 @@ auto SaveJob::save() -> bool {
g_warning("Could not create backup! Failed with %s", fe.what());
return false;
}
doc->setCreateBackupOnSave(false);
}

doc->lock();
Expand All @@ -116,6 +116,14 @@ auto SaveJob::save() -> bool {
g_error("%s", this->lastError.c_str());
}
return false;
} else if (createBackup) {
try {
// If a backup was created it can be removed now since no error occured during the save
fs::remove(fs::path{target} += "~");
} catch (fs::filesystem_error const& fe) { g_warning("Could not delete backup! Failed with %s", fe.what()); }
} else {
doc->setCreateBackupOnSave(true);
}

return true;
}
2 changes: 1 addition & 1 deletion src/control/xojfile/LoadHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ auto LoadHandler::parseXml() -> bool {
return false;
}

doc.setCreateBackupOnSave(this->fileVersion >= 3);
doc.setCreateBackupOnSave(true);

return valid;
}
Expand Down
2 changes: 1 addition & 1 deletion src/model/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class Document {
GtkTreeModel* contentsModel = nullptr;

/**
* create a backup before save, because the original file was an older fileversion
* create a backup before save
*/
bool createBackupOnSave = false;

Expand Down

0 comments on commit 91ebfab

Please sign in to comment.