From 831ffea0df32dbd6f1cd87fd3e5be4ea8248906c Mon Sep 17 00:00:00 2001 From: Christian Heinemann Date: Wed, 30 Oct 2024 22:27:21 +0100 Subject: [PATCH] allow deletion of specific savepoints --- source/Gui/AutosaveWindow.cpp | 16 ++++++++++++++ source/Gui/AutosaveWindow.h | 1 + .../SavepointTableService.cpp | 22 ++++++++++++++----- .../SavepointTableService.h | 3 ++- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/source/Gui/AutosaveWindow.cpp b/source/Gui/AutosaveWindow.cpp index 198199c73..396c36dcd 100644 --- a/source/Gui/AutosaveWindow.cpp +++ b/source/Gui/AutosaveWindow.cpp @@ -113,6 +113,7 @@ void AutosaveWindow::processToolbar() ImGui::SameLine(); ImGui::BeginDisabled(!static_cast(_selectedEntry) || _selectedEntry->state != SavepointState_Persisted); if (AlienImGui::ToolbarButton(ICON_FA_TRASH)) { + onDeleteSavepoint(_selectedEntry); } AlienImGui::Tooltip("Delete save point"); ImGui::EndDisabled(); @@ -283,9 +284,23 @@ void AutosaveWindow::onCreateSavepoint() SavepointTableService::get().insertEntryAtFront(_savepointTable.value(), entry); } +void AutosaveWindow::onDeleteSavepoint(SavepointEntry const& entry) +{ + printOverlayMessage("Deleting save point ..."); + + SavepointTableService::get().deleteEntry(_savepointTable.value(), entry); + + if (entry->state != SavepointState_Persisted) { + scheduleDeleteNonPersistentSavepoint({entry}); + } + _selectedEntry.reset(); +} + void AutosaveWindow::processCleanup() { if (_scheduleCleanup) { + printOverlayMessage("Cleaning up save points ..."); + auto nonPersistentEntries = SavepointTableService::get().truncate(_savepointTable.value(), 0); scheduleDeleteNonPersistentSavepoint(nonPersistentEntries); _scheduleCleanup = false; @@ -359,6 +374,7 @@ void AutosaveWindow::updateSavepointTableFromFile() } else { _savepointTable.reset(); } + _selectedEntry.reset(); } std::string AutosaveWindow::getSavepointFilename() const diff --git a/source/Gui/AutosaveWindow.h b/source/Gui/AutosaveWindow.h index 032f2b28e..1e2a20f75 100644 --- a/source/Gui/AutosaveWindow.h +++ b/source/Gui/AutosaveWindow.h @@ -27,6 +27,7 @@ class AutosaveWindow : public AlienWindow void processStatusBar(); void onCreateSavepoint(); + void onDeleteSavepoint(SavepointEntry const& entry); void scheduleDeleteNonPersistentSavepoint(std::vector const& entries); void processDeleteNonPersistentSavepoint(); diff --git a/source/PersisterInterface/SavepointTableService.cpp b/source/PersisterInterface/SavepointTableService.cpp index 08a6b7f20..64ae98c9c 100644 --- a/source/PersisterInterface/SavepointTableService.cpp +++ b/source/PersisterInterface/SavepointTableService.cpp @@ -80,7 +80,7 @@ std::vector SavepointTableService::truncate(SavepointTable& tabl } entries.erase(entries.begin() + newSize, entries.end()); - writeToFile(table); + updateFile(table); return result; } @@ -88,21 +88,31 @@ void SavepointTableService::insertEntryAtFront(SavepointTable& table, SavepointE { table._entries.emplace_front(entry); ++table._sequenceNumber; - writeToFile(table); + updateFile(table); } void SavepointTableService::updateEntry(SavepointTable& table, int row, SavepointEntry const& newEntry) const { table._entries.at(row) = newEntry; - writeToFile(table); + updateFile(table); } -void SavepointTableService::writeToFile(SavepointTable& table) const +void SavepointTableService::deleteEntry(SavepointTable& table, SavepointEntry const& entry) const +{ + if (entry->state == SavepointState_Persisted) { + SerializerService::get().deleteSimulation(entry->filename); + } + + table._entries.erase(std::remove(table._entries.begin(), table._entries.end(), entry), table._entries.end()); + updateFile(table); +} + +void SavepointTableService::updateFile(SavepointTable& table) const { try { std::ofstream stream(table.getFilename(), std::ios::binary); if (!stream) { - throw std::runtime_error("Could not access savepoint table file: " + table.getFilename().string()); + throw std::runtime_error("Could not access save point table file: " + table.getFilename().string()); } boost::property_tree::ptree tree; encodeDecode(tree, table, ParserTask::Encode); @@ -147,7 +157,7 @@ void SavepointTableService::encodeDecode(boost::property_tree::ptree& tree, Save JsonParser::encodeDecode(tree, entry->state, 0, "state", task); JsonParser::encodeDecode(tree, entry->timestamp, std::string(), "timestamp", task); JsonParser::encodeDecode(tree, entry->name, std::string(), "name", task); - JsonParser::encodeDecode(tree, entry->timestep, 0ull, "timestep", task); + JsonParser::encodeDecode(tree, entry->timestep, uint64_t(0), "timestep", task); } void SavepointTableService::encodeDecode(boost::property_tree::ptree& tree, std::filesystem::path& path, std::string const& node, ParserTask task) const diff --git a/source/PersisterInterface/SavepointTableService.h b/source/PersisterInterface/SavepointTableService.h index fc65de4cd..ec05efd5e 100644 --- a/source/PersisterInterface/SavepointTableService.h +++ b/source/PersisterInterface/SavepointTableService.h @@ -23,9 +23,10 @@ class SavepointTableService std::vector truncate(SavepointTable& table, int newSize) const; //returns non-persistent entries void insertEntryAtFront(SavepointTable& table, SavepointEntry const& entry) const; void updateEntry(SavepointTable& table, int row, SavepointEntry const& newEntry) const; + void deleteEntry(SavepointTable& table, SavepointEntry const& entry) const; private: - void writeToFile(SavepointTable& table) const; + void updateFile(SavepointTable& table) const; void encodeDecode(boost::property_tree::ptree& tree, SavepointTable& table, ParserTask task) const; void encodeDecode(boost::property_tree::ptree& tree, std::deque& entries, ParserTask task) const; void encodeDecode(boost::property_tree::ptree& tree, SavepointEntry& entry, ParserTask task) const;