Skip to content

Commit

Permalink
Better approach for 93f2532 and 28db040.
Browse files Browse the repository at this point in the history
Do not require to explicitly call FileSystemTemporary::discard_changes().
Instead, I just added a flag to FileSystemTemporary class (keep_files_when_destroyed),
which defines how to deal with temporary files after class instance is destroyed.
  • Loading branch information
morevnaproject committed Feb 20, 2020
1 parent 4df4085 commit e03d878
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
5 changes: 4 additions & 1 deletion synfig-core/src/synfig/filesystemtemporary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,17 @@ FileSystemTemporary::FileSystemTemporary(const String &tag, const String &tempor
tag(tag),
temporary_directory(temporary_directory.empty() ? get_system_temporary_directory() : temporary_directory),
temporary_filename_base(generate_temporary_filename_base(tag)),
keep_files_when_destroyed(false),
autosave(true)
{
set_sub_file_system(sub_file_system);
}

FileSystemTemporary::~FileSystemTemporary()
{

if (!keep_files_when_destroyed) {
discard_changes();
}
}

String
Expand Down
3 changes: 3 additions & 0 deletions synfig-core/src/synfig/filesystemtemporary.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ namespace synfig

std::map<String, String> meta;

bool keep_files_when_destroyed;
bool autosave;

static bool save_changes(
Expand Down Expand Up @@ -125,6 +126,8 @@ namespace synfig
const std::map<String, String>& get_metadata() const;
void set_metadata(const std::map<String, String> &data);

void set_keep_files_when_destroyed(bool value)
{ keep_files_when_destroyed = value; }
bool get_autosave() const
{ return autosave; }
void set_autosave(bool value)
Expand Down
8 changes: 3 additions & 5 deletions synfig-studio/src/gui/autorecover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,9 @@ AutoRecover::clear_backups()
// FileSystemTemporary will clear opened temporary files in destructor
String filename = App::get_temporary_directory() + ETL_DIRECTORY_SEPARATOR + *i;
bool s = false;
try {
FileSystemTemporary temporary_filesystem = FileSystemTemporary("");
s = temporary_filesystem.open_temporary(filename);
temporary_filesystem.discard_changes();
} catch (...) {
try { s = FileSystemTemporary("").open_temporary(filename); }
catch (...)
{
synfig::warning("Autobackup file is not recoverable. Forcing to remove.");
}
if (!s)
Expand Down
4 changes: 2 additions & 2 deletions synfig-studio/src/gui/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,9 @@ Instance::close(bool remove_temporary_files)
studio::App::instance_list.front()->canvas_view_list().front()->present();
}

if (remove_temporary_files) {
if (!remove_temporary_files) {
FileSystemTemporary::Handle temporary_filesystem = FileSystemTemporary::Handle::cast_dynamic(get_canvas()->get_file_system());
temporary_filesystem->discard_changes();
temporary_filesystem->set_keep_files_when_destroyed(true);
}
}

Expand Down
1 change: 0 additions & 1 deletion synfig-studio/src/synfigapp/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,6 @@ Instance::save_as(const synfig::String &file_name)
new_temporary_filesystem->set_meta("as", new_canvas_filename);
new_temporary_filesystem->set_meta("truncate", "0");
new_canvas_filesystem = new_temporary_filesystem;
previous_temporary_filesystem->discard_changes();
}

new_canvas_identifier = new_canvas_filesystem->get_identifier(CanvasFileNaming::project_file(new_canvas_filename));
Expand Down

0 comments on commit e03d878

Please sign in to comment.