forked from TypesettingTools/Aegisub
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathdialog_autosave.cpp
More file actions
177 lines (143 loc) · 5.54 KB
/
Copy pathdialog_autosave.cpp
File metadata and controls
177 lines (143 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// 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"
#include "format.h"
#include "libresrc/libresrc.h"
#include "options.h"
#include <libaegisub/path.h>
#include <boost/range/adaptor/map.hpp>
#include <map>
#include <string>
#include <vector>
#include <wx/button.h>
#include <wx/dialog.h>
#include <wx/dir.h>
#include <wx/filename.h>
#include <wx/listbox.h>
#include <wx/sizer.h>
#include <wx/string.h>
namespace {
struct Version {
wxString filename;
wxDateTime date;
wxString display;
};
struct AutosaveFile {
wxString name;
std::vector<Version> versions;
};
class DialogAutosave {
wxDialog d;
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;
int ShowModal() { return d.ShowModal(); }
};
DialogAutosave::DialogAutosave(wxWindow *parent)
: d(parent, -1, _("Open autosave file"), wxDefaultPosition, wxSize(800, 350), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
d.SetIcon(GETICON(open_toolbutton_16));
wxSizer *files_box = new wxStaticBoxSizer(wxVERTICAL, &d, _("Files"));
file_list = new wxListBox(&d, -1);
file_list->Bind(wxEVT_LISTBOX, &DialogAutosave::OnSelectFile, this);
files_box->Add(file_list, wxSizerFlags(1).Expand().Border());
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); });
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());
auto *btn_sizer = d.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));
d.SetSizer(main_sizer);
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, "?state/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);
if (file_list->IsEmpty())
btn_sizer->GetAffirmativeButton()->Disable();
else {
file_list->SetSelection(0);
wxCommandEvent evt;
OnSelectFile(evt);
}
}
void DialogAutosave::Populate(std::map<wxString, AutosaveFile> &files_map, std::string const& path, wxString const& filter, wxString const& name_fmt) {
wxString directory(config::path->Decode(path).wstring());
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.insert({name, AutosaveFile{name}}).first;
it->second.versions.push_back(Version{wxFileName(directory, fn).GetFullPath(), date, agi::wxformat(name_fmt, date.Format())});
} 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)
version_list->Append(version.display);
version_list->SetSelection(0);
}
std::string DialogAutosave::ChosenFile() const {
int sel_file = file_list->GetSelection();
if (sel_file < 0) return "";
int sel_version = version_list->GetSelection();
if (sel_version < 0) return "";
return from_wx(files[sel_file].versions[sel_version].filename);
}
}
std::string PickAutosaveFile(wxWindow *parent) {
DialogAutosave dialog(parent);
if (dialog.ShowModal() == wxID_OK)
return dialog.ChosenFile();
return "";
}