From 75452361ef39bf6776c9cefb1ce3056ced6a280a Mon Sep 17 00:00:00 2001 From: Bruno Lamonato Date: Mon, 19 Aug 2024 10:15:49 -0300 Subject: [PATCH 1/5] init --- source/editor.cpp | 28 ++++++++++++++++++++++++++++ source/editor.h | 1 + 2 files changed, 29 insertions(+) diff --git a/source/editor.cpp b/source/editor.cpp index 0a33a0e0..f0272ed4 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/", 30); + clearChanges(); } @@ -2090,3 +2098,23 @@ void Editor::SendNodeRequests() { live_client->sendNodeRequests(); } } + +void Editor::deleteOldBackups(const std::string &backup_path, int days_old) { + 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_old) { + 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..6c7445cc 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, int days_old); protected: void drawInternal(const Position offset, bool alt, bool dodraw); From be9c2729df80be8ee3082b8686ad02972da413ae Mon Sep 17 00:00:00 2001 From: Bruno Lamonato Date: Mon, 19 Aug 2024 10:41:00 -0300 Subject: [PATCH 2/5] delete bkps --- source/editor.cpp | 11 ++++++++--- source/editor.h | 2 +- source/preferences.cpp | 7 +++++++ source/preferences.h | 1 + source/settings.h | 1 + 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/source/editor.cpp b/source/editor.cpp index f0272ed4..dacb0b0a 100644 --- a/source/editor.cpp +++ b/source/editor.cpp @@ -481,7 +481,7 @@ void Editor::saveMap(FileName filename, bool showdialog) { std::remove(backup_zones.c_str()); } - deleteOldBackups(map_path + "backups/", 30); + deleteOldBackups(map_path + "backups/"); clearChanges(); } @@ -2099,7 +2099,12 @@ void Editor::SendNodeRequests() { } } -void Editor::deleteOldBackups(const std::string &backup_path, int days_old) { +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)) { @@ -2108,7 +2113,7 @@ void Editor::deleteOldBackups(const std::string &backup_path, int days_old) { 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_old) { + if (file_age > days_to_delete) { fs::remove(entry); std::cout << "Deleted old backup: " << entry.path() << std::endl; } diff --git a/source/editor.h b/source/editor.h index 6c7445cc..2e89ee60 100644 --- a/source/editor.h +++ b/source/editor.h @@ -150,7 +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, int days_old); + 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..c89a882e 100644 --- a/source/preferences.cpp +++ b/source/preferences.cpp @@ -116,6 +116,12 @@ 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 +622,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.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, From 797e2920d2743257dfd947b1be9bc27ca4f076d7 Mon Sep 17 00:00:00 2001 From: Bruno Lamonato Date: Tue, 20 Aug 2024 01:25:17 -0300 Subject: [PATCH 3/5] trigger GitHub actions From ba019dfdebbdc6e523dfe3f9e33fe05cd4b24df4 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Tue, 20 Aug 2024 13:12:15 +0000 Subject: [PATCH 4/5] Code format - (Clang-format) --- source/preferences.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/source/preferences.cpp b/source/preferences.cpp index c89a882e..8b4c8b94 100644 --- a/source/preferences.cpp +++ b/source/preferences.cpp @@ -121,7 +121,6 @@ wxNotebookPage* PreferencesWindow::CreateGeneralPage() { 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); From f6231cc7d2ba3a3d49a923af5aaf99f828989bac Mon Sep 17 00:00:00 2001 From: Bruno Lamonato Date: Tue, 20 Aug 2024 22:32:53 -0300 Subject: [PATCH 5/5] Update settings.cpp --- source/settings.cpp | 1 + 1 file changed, 1 insertion(+) 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);