Make FcMode an enum class and simplify the data flow

This commit is contained in:
Thomas Goyne 2015-10-31 20:12:24 -08:00
parent fe2925408c
commit e8cdfc57a9

View file

@ -48,9 +48,18 @@
#include <wx/zipstrm.h> #include <wx/zipstrm.h>
namespace { namespace {
enum class FcMode {
CheckFontsOnly = 0,
CopyToFolder = 1,
CopyToScriptFolder = 2,
CopyToZip = 3,
SymlinkToFolder = 4
};
class DialogFontsCollector final : public wxDialog { class DialogFontsCollector final : public wxDialog {
AssFile *subs; AssFile *subs;
agi::Path &path; agi::Path &path;
FcMode mode = FcMode::CheckFontsOnly;
wxStyledTextCtrl *collection_log; wxStyledTextCtrl *collection_log;
wxButton *close_btn; wxButton *close_btn;
@ -73,14 +82,6 @@ public:
DialogFontsCollector(agi::Context *c); DialogFontsCollector(agi::Context *c);
}; };
enum FcMode {
CheckFontsOnly = 0,
CopyToFolder = 1,
CopyToScriptFolder = 2,
CopyToZip = 3,
SymlinkToFolder = 4
};
using color_str_pair = std::pair<int, wxString>; using color_str_pair = std::pair<int, wxString>;
wxDEFINE_EVENT(EVT_ADD_TEXT, ValueEvent<color_str_pair>); wxDEFINE_EVENT(EVT_ADD_TEXT, ValueEvent<color_str_pair>);
wxDEFINE_EVENT(EVT_COLLECTION_DONE, wxThreadEvent); wxDEFINE_EVENT(EVT_COLLECTION_DONE, wxThreadEvent);
@ -99,17 +100,17 @@ void FontsCollectorThread(AssFile *subs, agi::fs::path const& destination, FcMod
// Copy fonts // Copy fonts
switch (oper) { switch (oper) {
case CheckFontsOnly: case FcMode::CheckFontsOnly:
collector->AddPendingEvent(wxThreadEvent(EVT_COLLECTION_DONE)); collector->AddPendingEvent(wxThreadEvent(EVT_COLLECTION_DONE));
return; return;
case SymlinkToFolder: case FcMode::SymlinkToFolder:
AppendText(_("Symlinking fonts to folder...\n"), 0); AppendText(_("Symlinking fonts to folder...\n"), 0);
break; break;
case CopyToScriptFolder: case FcMode::CopyToScriptFolder:
case CopyToFolder: case FcMode::CopyToFolder:
AppendText(_("Copying fonts to folder...\n"), 0); AppendText(_("Copying fonts to folder...\n"), 0);
break; break;
case CopyToZip: case FcMode::CopyToZip:
AppendText(_("Copying fonts to archive...\n"), 0); AppendText(_("Copying fonts to archive...\n"), 0);
break; break;
} }
@ -117,7 +118,7 @@ void FontsCollectorThread(AssFile *subs, agi::fs::path const& destination, FcMod
// Open zip stream if saving to compressed archive // Open zip stream if saving to compressed archive
std::unique_ptr<wxFFileOutputStream> out; std::unique_ptr<wxFFileOutputStream> out;
std::unique_ptr<wxZipOutputStream> zip; std::unique_ptr<wxZipOutputStream> zip;
if (oper == CopyToZip) { if (oper == FcMode::CopyToZip) {
try { try {
agi::fs::CreateDirectory(destination.parent_path()); agi::fs::CreateDirectory(destination.parent_path());
} }
@ -148,14 +149,14 @@ void FontsCollectorThread(AssFile *subs, agi::fs::path const& destination, FcMod
total_size += agi::fs::Size(path); total_size += agi::fs::Size(path);
switch (oper) { switch (oper) {
case SymlinkToFolder: case FcMode::SymlinkToFolder:
case CopyToScriptFolder: case FcMode::CopyToScriptFolder:
case CopyToFolder: { case FcMode::CopyToFolder: {
auto dest = destination/path.filename(); auto dest = destination/path.filename();
if (agi::fs::FileExists(dest)) if (agi::fs::FileExists(dest))
ret = 2; ret = 2;
#ifndef _WIN32 #ifndef _WIN32
else if (oper == SymlinkToFolder) { else if (oper == FcMode::SymlinkToFolder) {
// returns 0 on success, -1 on error... // returns 0 on success, -1 on error...
if (symlink(path.c_str(), dest.c_str())) if (symlink(path.c_str(), dest.c_str()))
ret = 0; ret = 0;
@ -175,7 +176,7 @@ void FontsCollectorThread(AssFile *subs, agi::fs::path const& destination, FcMod
} }
break; break;
case CopyToZip: { case FcMode::CopyToZip: {
wxFFileInputStream in(path.wstring()); wxFFileInputStream in(path.wstring());
if (!in.IsOk()) if (!in.IsOk())
ret = false; ret = false;
@ -291,12 +292,10 @@ void DialogFontsCollector::OnStart(wxCommandEvent &) {
collection_log->SetReadOnly(true); collection_log->SetReadOnly(true);
agi::fs::path dest; agi::fs::path dest;
int action = collection_mode->GetSelection(); if (mode != FcMode::CheckFontsOnly) {
OPT_SET("Tool/Fonts Collector/Action")->SetInt(action); dest = path.Decode(mode == FcMode::CopyToScriptFolder ? "?script/" : from_wx(dest_ctrl->GetValue()));
if (action != CheckFontsOnly) {
dest = path.Decode(action == CopyToScriptFolder ? "?script/" : from_wx(dest_ctrl->GetValue()));
if (action != CopyToZip) { if (mode != FcMode::CopyToZip) {
if (agi::fs::FileExists(dest)) if (agi::fs::FileExists(dest))
wxMessageBox(_("Invalid destination."), _("Error"), wxOK | wxICON_ERROR | wxCENTER, this); wxMessageBox(_("Invalid destination."), _("Error"), wxOK | wxICON_ERROR | wxCENTER, this);
try { try {
@ -313,7 +312,7 @@ void DialogFontsCollector::OnStart(wxCommandEvent &) {
} }
} }
if (action != CheckFontsOnly) if (mode != FcMode::CheckFontsOnly)
OPT_SET("Path/Fonts Collector Destination")->SetString(dest.string()); OPT_SET("Path/Fonts Collector Destination")->SetString(dest.string());
// Disable the UI while it runs as we don't support canceling // Disable the UI while it runs as we don't support canceling
@ -325,12 +324,12 @@ void DialogFontsCollector::OnStart(wxCommandEvent &) {
collection_mode->Enable(false); collection_mode->Enable(false);
dest_label->Enable(false); dest_label->Enable(false);
FontsCollectorThread(subs, dest, static_cast<FcMode>(action), GetEventHandler()); FontsCollectorThread(subs, dest, mode, GetEventHandler());
} }
void DialogFontsCollector::OnBrowse(wxCommandEvent &) { void DialogFontsCollector::OnBrowse(wxCommandEvent &) {
wxString dest; wxString dest;
if (collection_mode->GetSelection() == CopyToZip) { if (mode == FcMode::CopyToZip) {
dest = wxFileSelector( dest = wxFileSelector(
_("Select archive file name"), _("Select archive file name"),
dest_ctrl->GetValue(), dest_ctrl->GetValue(),
@ -345,11 +344,12 @@ void DialogFontsCollector::OnBrowse(wxCommandEvent &) {
dest_ctrl->SetValue(dest); dest_ctrl->SetValue(dest);
} }
void DialogFontsCollector::OnRadio(wxCommandEvent &) { void DialogFontsCollector::OnRadio(wxCommandEvent &evt) {
int value = collection_mode->GetSelection(); OPT_SET("Tool/Fonts Collector/Action")->SetInt(evt.GetInt());
mode = static_cast<FcMode>(evt.GetInt());
wxString dst = dest_ctrl->GetValue(); wxString dst = dest_ctrl->GetValue();
if (value == CheckFontsOnly || value == CopyToScriptFolder) { if (mode == FcMode::CheckFontsOnly || mode == FcMode::CopyToScriptFolder) {
dest_ctrl->Enable(false); dest_ctrl->Enable(false);
dest_browse_button->Enable(false); dest_browse_button->Enable(false);
dest_label->Enable(false); dest_label->Enable(false);
@ -360,7 +360,7 @@ void DialogFontsCollector::OnRadio(wxCommandEvent &) {
dest_browse_button->Enable(true); dest_browse_button->Enable(true);
dest_label->Enable(true); dest_label->Enable(true);
if (value == CopyToFolder || value == SymlinkToFolder) { if (mode == FcMode::CopyToFolder || mode == FcMode::SymlinkToFolder) {
dest_label->SetLabel(_("Choose the folder where the fonts will be collected to. It will be created if it doesn't exist.")); dest_label->SetLabel(_("Choose the folder where the fonts will be collected to. It will be created if it doesn't exist."));
// Remove filename from browse box // Remove filename from browse box