From eb823e66d39c9c6162f4c6b21e2d27f9e6203ab5 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Thu, 8 Nov 2012 06:44:34 -0800 Subject: [PATCH] Add backups and crash recovery files to the autosave dialog. Closes #657. --- aegisub/src/dialog_autosave.cpp | 76 +++++++++++++++++++-------------- aegisub/src/dialog_autosave.h | 9 ++-- 2 files changed, 48 insertions(+), 37 deletions(-) diff --git a/aegisub/src/dialog_autosave.cpp b/aegisub/src/dialog_autosave.cpp index 5be8f447b..3a8ce4793 100644 --- a/aegisub/src/dialog_autosave.cpp +++ b/aegisub/src/dialog_autosave.cpp @@ -32,7 +32,6 @@ DialogAutosave::DialogAutosave(wxWindow *parent) : wxDialog(parent, -1, _("Open autosave file"), wxDefaultPosition, wxSize(800, 350)) -, directory(StandardPaths::DecodePath(to_wx(OPT_GET("Path/Auto/Save")->GetString()))) { wxSizer *files_box = new wxStaticBoxSizer(wxVERTICAL, this, _("Files")); file_list = new wxListBox(this, -1); @@ -56,34 +55,10 @@ DialogAutosave::DialogAutosave(wxWindow *parent) main_sizer->Add(btn_sizer, wxSizerFlags().Expand().Border(wxALL & ~wxTOP)); SetSizer(main_sizer); - Populate(); - if (file_list->IsEmpty()) - btn_sizer->GetAffirmativeButton()->Disable(); -} - -void DialogAutosave::Populate() { - wxDir dir; - if (!dir.Open(directory)) return; - - wxString fn; - if (!dir.GetFirst(&fn, "*.AUTOSAVE.ass", wxDIR_FILES)) - return; - std::map files_map; - do { - wxString date_str; - wxString name = fn.Left(fn.size() - 13).BeforeLast('.', &date_str); - if (!name) continue; - - wxDateTime date; - if (!date.ParseFormat(date_str, "%Y-%m-%d-%H-%M-%S")) - continue; - - auto it = files_map.find(name); - if (it == files_map.end()) - it = files_map.emplace(name, name).first; - it->second.versions.emplace_back(fn, date); - } while (dir.GetNext(&fn)); + Populate(files_map, OPT_GET("Path/Auto/Save")->GetString(), ".AUTOSAVE.ass", "%s"); + Populate(files_map, OPT_GET("Path/Auto/Backup")->GetString(), ".ORIGINAL.ass", _("%s [ORIGINAL BACKUP]")); + Populate(files_map, "?user/recovered", ".ass", _("%s [RECOVERED]")); for (auto& file : files_map | boost::adaptors::map_values) files.emplace_back(std::move(file)); @@ -98,10 +73,45 @@ void DialogAutosave::Populate() { for (auto const& file : files) file_list->Append(file.name); - file_list->SetSelection(0); - wxCommandEvent evt; - OnSelectFile(evt); + if (file_list->IsEmpty()) + btn_sizer->GetAffirmativeButton()->Disable(); + else { + file_list->SetSelection(0); + wxCommandEvent evt; + OnSelectFile(evt); + } +} + +void DialogAutosave::Populate(std::map &files_map, std::string const& path, wxString const& filter, wxString const& name_fmt) { + wxString directory(StandardPaths::DecodePath(to_wx(path))); + + wxDir dir; + if (!dir.Open(directory)) return; + + wxString fn; + if (!dir.GetFirst(&fn, "*" + filter, wxDIR_FILES)) + return; + + do { + wxDateTime date; + + wxString date_str; + wxString name = fn.Left(fn.size() - filter.size()).BeforeLast('.', &date_str); + if (!name) + name = date_str; + else { + if (!date.ParseFormat(date_str, "%Y-%m-%d-%H-%M-%S")) + name += "." + date_str; + } + if (!date.IsValid()) + date = wxFileName(directory, fn).GetModificationTime(); + + auto it = files_map.find(name); + if (it == files_map.end()) + it = files_map.emplace(name, name).first; + it->second.versions.emplace_back(wxFileName(directory, fn).GetFullPath(), date, wxString::Format(name_fmt, date.Format())); + } while (dir.GetNext(&fn)); } void DialogAutosave::OnSelectFile(wxCommandEvent&) { @@ -110,7 +120,7 @@ void DialogAutosave::OnSelectFile(wxCommandEvent&) { if (sel_file < 0) return; for (auto const& version : files[sel_file].versions) - version_list->Append(version.date.Format()); + version_list->Append(version.display); version_list->SetSelection(0); } @@ -121,5 +131,5 @@ wxString DialogAutosave::ChosenFile() const { int sel_version = version_list->GetSelection(); if (sel_version < 0) return ""; - return wxFileName(directory, files[sel_file].versions[sel_version].filename).GetFullPath(); + return files[sel_file].versions[sel_version].filename; } diff --git a/aegisub/src/dialog_autosave.h b/aegisub/src/dialog_autosave.h index b4fc0d38e..4ae1361f2 100644 --- a/aegisub/src/dialog_autosave.h +++ b/aegisub/src/dialog_autosave.h @@ -16,6 +16,7 @@ #ifndef AGI_PRE #include +#include #include #include @@ -28,8 +29,9 @@ class DialogAutosave : public wxDialog { struct Version { wxString filename; wxDateTime date; - Version(wxString const& filename, wxDateTime const& date) - : filename(filename), date(date) { } + wxString display; + Version(wxString const& filename, wxDateTime const& date, wxString const& display) + : filename(filename), date(date), display(display) { } }; struct AutosaveFile { @@ -38,13 +40,12 @@ class DialogAutosave : public wxDialog { AutosaveFile(wxString const& name) : name(name) { } }; - wxString directory; std::vector files; wxListBox *file_list; wxListBox *version_list; - void Populate(); + void Populate(std::map &files_map, std::string const& path, wxString const& filter, wxString const& name_fmt); void OnSelectFile(wxCommandEvent&); public: