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 "config.h"
|
|
|
|
|
|
|
|
#include "dialog_autosave.h"
|
|
|
|
|
|
|
|
#include "compat.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>
|
|
|
|
|
2012-11-19 06:10:36 +01:00
|
|
|
#include <wx/button.h>
|
|
|
|
#include <wx/dir.h>
|
|
|
|
#include <wx/filename.h>
|
|
|
|
#include <wx/listbox.h>
|
|
|
|
#include <wx/sizer.h>
|
2012-11-08 03:53:44 +01:00
|
|
|
#include <wx/statbox.h>
|
|
|
|
|
|
|
|
DialogAutosave::DialogAutosave(wxWindow *parent)
|
2012-11-08 23:35:18 +01:00
|
|
|
: wxDialog(parent, -1, _("Open autosave file"), wxDefaultPosition, wxSize(800, 350), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
|
2012-11-08 03:53:44 +01:00
|
|
|
{
|
2012-11-08 23:33:41 +01:00
|
|
|
SetIcon(GETICON(open_toolbutton_16));
|
|
|
|
|
2012-11-08 03:53:44 +01:00
|
|
|
wxSizer *files_box = new wxStaticBoxSizer(wxVERTICAL, this, _("Files"));
|
|
|
|
file_list = new wxListBox(this, -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());
|
|
|
|
|
|
|
|
wxSizer *versions_box = new wxStaticBoxSizer(wxVERTICAL, this, _("Versions"));
|
|
|
|
version_list = new wxListBox(this, -1);
|
2013-12-12 03:25:13 +01:00
|
|
|
version_list->Bind(wxEVT_LISTBOX_DCLICK, [=](wxCommandEvent&) { 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());
|
|
|
|
|
|
|
|
wxStdDialogButtonSizer *btn_sizer = CreateStdDialogButtonSizer(wxOK | wxCANCEL);
|
|
|
|
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));
|
|
|
|
SetSizer(main_sizer);
|
|
|
|
|
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())
|
2012-11-16 00:55:38 +01:00
|
|
|
it = files_map.insert(std::make_pair(name, name)).first;
|
2012-11-08 15:44:34 +01:00
|
|
|
it->second.versions.emplace_back(wxFileName(directory, fn).GetFullPath(), date, wxString::Format(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
|
|
|
}
|