diff --git a/source/editor.cpp b/source/editor.cpp index 0a33a0e0..dacb0b0a 100644 --- a/source/editor.cpp +++ b/source/editor.cpp @@ -41,6 +41,12 @@ #include "live_client.h" #include "live_action.h" +#include +#include +#include + +namespace fs = std::filesystem; + Editor::Editor(CopyBuffer ©buffer) : live_server(nullptr), live_client(nullptr), @@ -475,6 +481,8 @@ void Editor::saveMap(FileName filename, bool showdialog) { std::remove(backup_zones.c_str()); } + deleteOldBackups(map_path + "backups/"); + clearChanges(); } @@ -2090,3 +2098,28 @@ void Editor::SendNodeRequests() { live_client->sendNodeRequests(); } } + +void Editor::deleteOldBackups(const std::string &backup_path) { + int days_to_delete = g_settings.getInteger(Config::DELETE_BACKUP_DAYS); + if (days_to_delete <= 0) { + return; // Se o valor é zero ou negativo, não deletar backups + } + + try { + auto now = std::chrono::system_clock::now(); + for (const auto &entry : fs::directory_iterator(backup_path)) { + if (fs::is_regular_file(entry.status())) { + auto file_time = fs::last_write_time(entry); + auto sctp = std::chrono::time_point_cast(file_time - fs::file_time_type::clock::now() + now); + auto file_age = std::chrono::duration_cast(now - sctp).count() / 24; + + if (file_age > days_to_delete) { + fs::remove(entry); + std::cout << "Deleted old backup: " << entry.path() << std::endl; + } + } + } + } catch (const fs::filesystem_error &e) { + std::cerr << "Error: " << e.what() << std::endl; + } +} diff --git a/source/editor.h b/source/editor.h index ce139b78..2e89ee60 100644 --- a/source/editor.h +++ b/source/editor.h @@ -150,6 +150,7 @@ class Editor { void undraw(const PositionVector &todraw, PositionVector &toborder, bool alt); void ensureBackupDirectoryExists(const std::string &backup_path); + void deleteOldBackups(const std::string &backup_path); protected: void drawInternal(const Position offset, bool alt, bool dodraw); diff --git a/source/preferences.cpp b/source/preferences.cpp index f071f25e..8b4c8b94 100644 --- a/source/preferences.cpp +++ b/source/preferences.cpp @@ -116,6 +116,11 @@ wxNotebookPage* PreferencesWindow::CreateGeneralPage() { grid_sizer->Add(replace_size_spin, 0); SetWindowToolTip(tmptext, replace_size_spin, "How many items you can replace on the map using the Replace Item tool."); + grid_sizer->Add(tmptext = newd wxStaticText(general_page, wxID_ANY, "Delete backup after X days: "), 0); + delete_backup_days_spin = newd wxSpinCtrl(general_page, wxID_ANY, i2ws(g_settings.getInteger(Config::DELETE_BACKUP_DAYS)), wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 0, 365); + grid_sizer->Add(delete_backup_days_spin, 0); + SetWindowToolTip(tmptext, delete_backup_days_spin, "Configure the number of days after which backups will be automatically deleted."); + sizer->Add(grid_sizer, 0, wxALL, 5); sizer->AddSpacer(10); @@ -616,6 +621,7 @@ void PreferencesWindow::Apply() { g_settings.setInteger(Config::UNDO_MEM_SIZE, undo_mem_size_spin->GetValue()); g_settings.setInteger(Config::WORKER_THREADS, worker_threads_spin->GetValue()); g_settings.setInteger(Config::REPLACE_SIZE, replace_size_spin->GetValue()); + g_settings.setInteger(Config::DELETE_BACKUP_DAYS, delete_backup_days_spin->GetValue()); g_settings.setInteger(Config::COPY_POSITION_FORMAT, position_format->GetSelection()); g_settings.setInteger(Config::COPY_AREA_FORMAT, area_format->GetSelection()); if (g_settings.getBoolean(Config::SHOW_TILESET_EDITOR) != enable_tileset_editing_chkbox->GetValue()) { diff --git a/source/preferences.h b/source/preferences.h index 32ea66d7..541b1466 100644 --- a/source/preferences.h +++ b/source/preferences.h @@ -51,6 +51,7 @@ class PreferencesWindow : public wxDialog { wxSpinCtrl* undo_mem_size_spin; wxSpinCtrl* worker_threads_spin; wxSpinCtrl* replace_size_spin; + wxSpinCtrl* delete_backup_days_spin; wxRadioBox* position_format; wxRadioBox* area_format; diff --git a/source/settings.cpp b/source/settings.cpp index 0efd0bd0..0f7cd209 100644 --- a/source/settings.cpp +++ b/source/settings.cpp @@ -272,6 +272,7 @@ void Settings::IO(IOMode mode) { Int(USE_OTGZ, 1); Int(SAVE_WITH_OTB_MAGIC_NUMBER, 0); Int(REPLACE_SIZE, 500); + Int(DELETE_BACKUP_DAYS, 0); Int(COPY_POSITION_FORMAT, 0); Int(COPY_AREA_FORMAT, 0); diff --git a/source/settings.h b/source/settings.h index 3df62478..80b9889e 100644 --- a/source/settings.h +++ b/source/settings.h @@ -95,6 +95,7 @@ namespace Config { USE_OTGZ, SAVE_WITH_OTB_MAGIC_NUMBER, REPLACE_SIZE, + DELETE_BACKUP_DAYS, USE_LARGE_CONTAINER_ICONS, USE_LARGE_CHOOSE_ITEM_ICONS,