2012-11-08 03:53:44 +01:00
|
|
|
// Copyright (c) 2012, Thomas Goyne <plorkyeran@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
#include "compat.h"
|
2014-05-29 17:28:37 +02:00
|
|
|
#include "format.h"
|
2012-11-08 23:33:41 +01:00
|
|
|
#include "libresrc/libresrc.h"
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2013-01-30 04:35:37 +01:00
|
|
|
|
|
|
|
#include <libaegisub/path.h>
|
2012-11-08 03:53:44 +01:00
|
|
|
|
|
|
|
#include <boost/range/adaptor/map.hpp>
|
|
|
|
#include <map>
|
2014-05-22 21:07:15 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2012-11-19 06:10:36 +01:00
|
|
|
#include <wx/button.h>
|
2014-05-22 21:07:15 +02:00
|
|
|
#include <wx/dialog.h>
|
2012-11-19 06:10:36 +01:00
|
|
|
#include <wx/dir.h>
|
|
|
|
#include <wx/filename.h>
|
|
|
|
#include <wx/listbox.h>
|
|
|
|
#include <wx/sizer.h>
|
2014-05-22 21:07:15 +02:00
|
|
|
#include <wx/string.h>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct Version {
|
|
|
|
wxString filename;
|
|
|
|
wxDateTime date;
|
|
|
|
wxString display;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AutosaveFile {
|
|
|
|
wxString name;
|
|
|
|
std::vector<Version> versions;
|
|
|
|
};
|
|
|
|
|
2014-05-31 02:05:25 +02:00
|
|
|
class DialogAutosave {
|
|
|
|
wxDialog d;
|
2014-05-22 21:07:15 +02:00
|
|
|
std::vector<AutosaveFile> files;
|
|
|
|
|
|
|
|
wxListBox *file_list;
|
|
|
|
wxListBox *version_list;
|
|
|
|
|
|
|
|
void Populate(std::map<wxString, AutosaveFile> &files_map, std::string const& path, wxString const& filter, wxString const& name_fmt);
|
|
|
|
void OnSelectFile(wxCommandEvent&);
|
|
|
|
|
|
|
|
public:
|
|
|
|
DialogAutosave(wxWindow *parent);
|
|
|
|
std::string ChosenFile() const;
|
2014-05-31 02:05:25 +02:00
|
|
|
|
|
|
|
int ShowModal() { return d.ShowModal(); }
|
2014-05-22 21:07:15 +02:00
|
|
|
};
|
2012-11-08 03:53:44 +01:00
|
|
|
|
|
|
|
DialogAutosave::DialogAutosave(wxWindow *parent)
|
2014-05-31 02:05:25 +02:00
|
|
|
: d(parent, -1, _("Open autosave file"), wxDefaultPosition, wxSize(800, 350), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
|
2012-11-08 03:53:44 +01:00
|
|
|
{
|
2014-05-31 02:05:25 +02:00
|
|
|
d.SetIcon(GETICON(open_toolbutton_16));
|
2012-11-08 23:33:41 +01:00
|
|
|
|
2014-05-31 02:05:25 +02:00
|
|
|
wxSizer *files_box = new wxStaticBoxSizer(wxVERTICAL, &d, _("Files"));
|
|
|
|
file_list = new wxListBox(&d, -1);
|
2013-12-12 03:25:13 +01:00
|
|
|
file_list->Bind(wxEVT_LISTBOX, &DialogAutosave::OnSelectFile, this);
|
2012-11-08 03:53:44 +01:00
|
|
|
files_box->Add(file_list, wxSizerFlags(1).Expand().Border());
|
|
|
|
|
2014-05-31 02:05:25 +02:00
|
|
|
wxSizer *versions_box = new wxStaticBoxSizer(wxVERTICAL, &d, _("Versions"));
|
|
|
|
version_list = new wxListBox(&d, -1);
|
|
|
|
version_list->Bind(wxEVT_LISTBOX_DCLICK, [=](wxCommandEvent&) { d.EndModal(wxID_OK); });
|
2012-11-08 03:53:44 +01:00
|
|
|
versions_box->Add(version_list, wxSizerFlags(1).Expand().Border());
|
|
|
|
|
|
|
|
wxSizer *boxes_sizer = new wxBoxSizer(wxHORIZONTAL);
|
|
|
|
boxes_sizer->Add(files_box, wxSizerFlags(1).Expand().Border());
|
|
|
|
boxes_sizer->Add(versions_box, wxSizerFlags(1).Expand().Border());
|
|
|
|
|
2014-05-31 02:05:25 +02:00
|
|
|
auto *btn_sizer = d.CreateStdDialogButtonSizer(wxOK | wxCANCEL);
|
2012-11-08 03:53:44 +01:00
|
|
|
btn_sizer->GetAffirmativeButton()->SetLabelText(_("Open"));
|
|
|
|
|
|
|
|
wxSizer *main_sizer = new wxBoxSizer(wxVERTICAL);
|
|
|
|
main_sizer->Add(boxes_sizer, wxSizerFlags(1).Expand().Border());
|
|
|
|
main_sizer->Add(btn_sizer, wxSizerFlags().Expand().Border(wxALL & ~wxTOP));
|
2014-05-31 02:05:25 +02:00
|
|
|
d.SetSizer(main_sizer);
|
2012-11-08 03:53:44 +01:00
|
|
|
|
2012-11-08 15:44:34 +01:00
|
|
|
std::map<wxString, AutosaveFile> files_map;
|
|
|
|
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));
|
|
|
|
|
|
|
|
for (auto& file : files) {
|
|
|
|
sort(begin(file.versions), end(file.versions),
|
|
|
|
[](Version const& a, Version const& b) { return a.date > b.date; });
|
|
|
|
}
|
|
|
|
|
|
|
|
sort(begin(files), end(files),
|
|
|
|
[](AutosaveFile const& a, AutosaveFile const& b) { return a.versions[0].date > b.versions[0].date; });
|
|
|
|
|
|
|
|
for (auto const& file : files)
|
|
|
|
file_list->Append(file.name);
|
|
|
|
|
2012-11-08 03:53:44 +01:00
|
|
|
if (file_list->IsEmpty())
|
|
|
|
btn_sizer->GetAffirmativeButton()->Disable();
|
2012-11-08 15:44:34 +01:00
|
|
|
else {
|
|
|
|
file_list->SetSelection(0);
|
|
|
|
wxCommandEvent evt;
|
|
|
|
OnSelectFile(evt);
|
|
|
|
}
|
2012-11-08 03:53:44 +01:00
|
|
|
}
|
|
|
|
|
2012-11-08 15:44:34 +01:00
|
|
|
void DialogAutosave::Populate(std::map<wxString, AutosaveFile> &files_map, std::string const& path, wxString const& filter, wxString const& name_fmt) {
|
2013-01-30 04:35:37 +01:00
|
|
|
wxString directory(config::path->Decode(path).wstring());
|
2012-11-08 15:44:34 +01:00
|
|
|
|
2012-11-08 03:53:44 +01:00
|
|
|
wxDir dir;
|
|
|
|
if (!dir.Open(directory)) return;
|
|
|
|
|
|
|
|
wxString fn;
|
2012-11-08 15:44:34 +01:00
|
|
|
if (!dir.GetFirst(&fn, "*" + filter, wxDIR_FILES))
|
2012-11-08 03:53:44 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
do {
|
|
|
|
wxDateTime date;
|
2012-11-08 15:44:34 +01:00
|
|
|
|
|
|
|
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();
|
2012-11-08 03:53:44 +01:00
|
|
|
|
|
|
|
auto it = files_map.find(name);
|
|
|
|
if (it == files_map.end())
|
2014-05-25 16:41:21 +02:00
|
|
|
it = files_map.insert({name, AutosaveFile{name}}).first;
|
2014-05-29 17:28:37 +02:00
|
|
|
it->second.versions.push_back(Version{wxFileName(directory, fn).GetFullPath(), date, agi::wxformat(name_fmt, date.Format())});
|
2012-11-08 03:53:44 +01:00
|
|
|
} while (dir.GetNext(&fn));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogAutosave::OnSelectFile(wxCommandEvent&) {
|
|
|
|
version_list->Clear();
|
|
|
|
int sel_file = file_list->GetSelection();
|
|
|
|
if (sel_file < 0) return;
|
|
|
|
|
|
|
|
for (auto const& version : files[sel_file].versions)
|
2012-11-08 15:44:34 +01:00
|
|
|
version_list->Append(version.display);
|
2012-11-08 03:53:44 +01:00
|
|
|
version_list->SetSelection(0);
|
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string DialogAutosave::ChosenFile() const {
|
2012-11-08 03:53:44 +01:00
|
|
|
int sel_file = file_list->GetSelection();
|
|
|
|
if (sel_file < 0) return "";
|
|
|
|
|
|
|
|
int sel_version = version_list->GetSelection();
|
|
|
|
if (sel_version < 0) return "";
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
return from_wx(files[sel_file].versions[sel_version].filename);
|
2012-11-08 03:53:44 +01:00
|
|
|
}
|
2014-05-22 21:07:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string PickAutosaveFile(wxWindow *parent) {
|
|
|
|
DialogAutosave dialog(parent);
|
|
|
|
if (dialog.ShowModal() == wxID_OK)
|
|
|
|
return dialog.ChosenFile();
|
|
|
|
return "";
|
|
|
|
}
|