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