From f5f5439808693009e6d759917cc7c7ddc6f602e9 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Fri, 21 Aug 2015 18:17:48 -0700 Subject: [PATCH] Add context-specific path tokens ?video, ?audio, and ?script are not global. --- src/audio_provider_factory.cpp | 6 ++++-- src/audio_provider_factory.h | 5 ++++- src/auto4_base.cpp | 4 ++-- src/auto4_lua.cpp | 16 +++++++++------- src/command/video.cpp | 4 ++-- src/context.cpp | 5 ++++- src/dialog_fonts_collector.cpp | 10 ++++++---- src/include/aegisub/context.h | 2 ++ src/project.cpp | 22 +++++++++++----------- src/subs_controller.cpp | 8 ++++---- 10 files changed, 48 insertions(+), 34 deletions(-) diff --git a/src/audio_provider_factory.cpp b/src/audio_provider_factory.cpp index 0e5fe3589..887783644 100644 --- a/src/audio_provider_factory.cpp +++ b/src/audio_provider_factory.cpp @@ -55,7 +55,9 @@ std::vector GetAudioProviderNames() { return ::GetClasses(boost::make_iterator_range(std::begin(providers), std::end(providers))); } -std::unique_ptr GetAudioProvider(fs::path const& filename, BackgroundRunner *br) { +std::unique_ptr GetAudioProvider(fs::path const& filename, + Path const& path_helper, + BackgroundRunner *br) { auto preferred = OPT_GET("Audio/Provider")->GetString(); auto sorted = GetSorted(boost::make_iterator_range(std::begin(providers), std::end(providers)), preferred); @@ -118,7 +120,7 @@ std::unique_ptr GetAudioProvider(fs::path const& filename, Backgr auto path = OPT_GET("Audio/Cache/HD/Location")->GetString(); if (path == "default") path = "?temp"; - auto cache_dir = config::path->MakeAbsolute(config::path->Decode(path), "?temp"); + auto cache_dir = path_helper.MakeAbsolute(path_helper.Decode(path), "?temp"); return CreateHDAudioProvider(std::move(provider), cache_dir); } diff --git a/src/audio_provider_factory.h b/src/audio_provider_factory.h index 1883c5828..b419d6d0a 100644 --- a/src/audio_provider_factory.h +++ b/src/audio_provider_factory.h @@ -22,7 +22,10 @@ namespace agi { class AudioProvider; class BackgroundRunner; + class Path; } -std::unique_ptr GetAudioProvider(agi::fs::path const& filename, agi::BackgroundRunner *br); +std::unique_ptr GetAudioProvider(agi::fs::path const& filename, + agi::Path const& path_helper, + agi::BackgroundRunner *br); std::vector GetAudioProviderNames(); diff --git a/src/auto4_base.cpp b/src/auto4_base.cpp index a890a6e57..388e1fa3d 100644 --- a/src/auto4_base.cpp +++ b/src/auto4_base.cpp @@ -417,8 +417,8 @@ namespace Automation4 { scripts_string += "|"; auto scriptfn(script->GetFilename().string()); - auto autobase_rel = config::path->MakeRelative(scriptfn, autobasefn); - auto assfile_rel = config::path->MakeRelative(scriptfn, "?script"); + auto autobase_rel = context->path->MakeRelative(scriptfn, autobasefn); + auto assfile_rel = context->path->MakeRelative(scriptfn, "?script"); if (autobase_rel.string().size() <= scriptfn.size() && autobase_rel.string().size() <= assfile_rel.string().size()) { scriptfn = "$" + autobase_rel.generic_string(); diff --git a/src/auto4_lua.cpp b/src/auto4_lua.cpp index baa345101..3ce9f2adb 100644 --- a/src/auto4_lua.cpp +++ b/src/auto4_lua.cpp @@ -197,8 +197,7 @@ namespace { int get_keyframes(lua_State *L) { - const agi::Context *c = get_context(L); - if (c) + if (const agi::Context *c = get_context(L)) push_value(L, c->project->Keyframes()); else lua_pushnil(L); @@ -209,7 +208,10 @@ namespace { { std::string path = check_string(L, 1); lua_pop(L, 1); - push_value(L, config::path->Decode(path)); + if (const agi::Context *c = get_context(L)) + push_value(L, c->path->Decode(path)); + else + push_value(L, config::path->Decode(path)); return 1; } @@ -272,10 +274,10 @@ namespace { PUSH_FIELD(ar_mode); PUSH_FIELD(video_position); #undef PUSH_FIELD - set_field(L, "audio_file", config::path->MakeAbsolute(c->ass->Properties.audio_file, "?script")); - set_field(L, "video_file", config::path->MakeAbsolute(c->ass->Properties.video_file, "?script")); - set_field(L, "timecodes_file", config::path->MakeAbsolute(c->ass->Properties.timecodes_file, "?script")); - set_field(L, "keyframes_file", config::path->MakeAbsolute(c->ass->Properties.keyframes_file, "?script")); + set_field(L, "audio_file", c->path->MakeAbsolute(c->ass->Properties.audio_file, "?script")); + set_field(L, "video_file", c->path->MakeAbsolute(c->ass->Properties.video_file, "?script")); + set_field(L, "timecodes_file", c->path->MakeAbsolute(c->ass->Properties.timecodes_file, "?script")); + set_field(L, "keyframes_file", c->path->MakeAbsolute(c->ass->Properties.keyframes_file, "?script")); } return 1; } diff --git a/src/command/video.cpp b/src/command/video.cpp index ea1a8a026..fb2bcb0ba 100644 --- a/src/command/video.cpp +++ b/src/command/video.cpp @@ -471,7 +471,7 @@ static void save_snapshot(agi::Context *c, bool raw) { option = "?script"; } // Find out where the ?specifier points to - basepath = config::path->Decode(option); + basepath = c->path->Decode(option); // If where ever that is isn't defined, we can't save there if ((basepath == "\\") || (basepath == "/")) { // So save to the current user's home dir instead @@ -480,7 +480,7 @@ static void save_snapshot(agi::Context *c, bool raw) { } // Actual fixed (possibly relative) path, decode it else - basepath = config::path->MakeAbsolute(option, "?user/"); + basepath = c->path->MakeAbsolute(option, "?user/"); basepath /= is_dummy ? "dummy" : videoname.stem(); diff --git a/src/context.cpp b/src/context.cpp index 8923d0884..2366d76ee 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -21,6 +21,7 @@ #include "auto4_base.h" #include "dialog_manager.h" #include "initial_line_state.h" +#include "options.h" #include "project.h" #include "search_replace_engine.h" #include "selection_controller.h" @@ -29,6 +30,7 @@ #include "video_controller.h" #include +#include namespace agi { Context::Context() @@ -43,9 +45,10 @@ Context::Context() , initialLineState(make_unique(this)) , search(make_unique(this)) , dialog(make_unique()) +, path(make_unique(*config::path)) { subsController->SetSelectionController(selectionController.get()); } -Context::~Context() {} +Context::~Context() = default; } diff --git a/src/dialog_fonts_collector.cpp b/src/dialog_fonts_collector.cpp index 3989d3998..ef31e799e 100644 --- a/src/dialog_fonts_collector.cpp +++ b/src/dialog_fonts_collector.cpp @@ -50,6 +50,7 @@ namespace { class DialogFontsCollector final : public wxDialog { AssFile *subs; + agi::Path &path; wxStyledTextCtrl *collection_log; wxButton *close_btn; @@ -215,6 +216,7 @@ void FontsCollectorThread(AssFile *subs, agi::fs::path const& destination, FcMod DialogFontsCollector::DialogFontsCollector(agi::Context *c) : wxDialog(c->parent, -1, _("Fonts Collector")) , subs(c->ass.get()) +, path(*c->path) { SetIcon(GETICON(font_collector_button_16)); @@ -230,13 +232,13 @@ DialogFontsCollector::DialogFontsCollector(agi::Context *c) collection_mode = new wxRadioBox(this, -1, _("Action"), wxDefaultPosition, wxDefaultSize, countof(modes), modes, 1); collection_mode->SetSelection(mid(0, OPT_GET("Tool/Fonts Collector/Action")->GetInt(), 4)); - if (config::path->Decode("?script") == "?script") + if (c->path->Decode("?script") == "?script") collection_mode->Enable(2, false); wxStaticBoxSizer *destination_box = new wxStaticBoxSizer(wxVERTICAL, this, _("Destination")); dest_label = new wxStaticText(this, -1, " "); - dest_ctrl = new wxTextCtrl(this, -1, config::path->Decode(OPT_GET("Path/Fonts Collector Destination")->GetString()).wstring()); + dest_ctrl = new wxTextCtrl(this, -1, c->path->Decode(OPT_GET("Path/Fonts Collector Destination")->GetString()).wstring()); dest_browse_button = new wxButton(this, -1, _("&Browse...")); wxSizer *dest_browse_sizer = new wxBoxSizer(wxHORIZONTAL); @@ -292,7 +294,7 @@ void DialogFontsCollector::OnStart(wxCommandEvent &) { int action = collection_mode->GetSelection(); OPT_SET("Tool/Fonts Collector/Action")->SetInt(action); if (action != CheckFontsOnly) { - dest = config::path->Decode(action == CopyToScriptFolder ? "?script/" : from_wx(dest_ctrl->GetValue())); + dest = path.Decode(action == CopyToScriptFolder ? "?script/" : from_wx(dest_ctrl->GetValue())); if (action != CopyToZip) { if (agi::fs::FileExists(dest)) @@ -397,7 +399,7 @@ void DialogFontsCollector::OnCollectionComplete(wxThreadEvent &) { start_btn->Enable(); close_btn->Enable(); collection_mode->Enable(); - if (config::path->Decode("?script") == "?script") + if (path.Decode("?script") == "?script") collection_mode->Enable(2, false); wxCommandEvent evt; diff --git a/src/include/aegisub/context.h b/src/include/aegisub/context.h index bda867974..361dcf9c7 100644 --- a/src/include/aegisub/context.h +++ b/src/include/aegisub/context.h @@ -36,6 +36,7 @@ class wxWindow; namespace Automation4 { class ScriptManager; } namespace agi { +class Path; struct Context { // Note: order here matters quite a bit, as things need to be set up and @@ -50,6 +51,7 @@ struct Context { std::unique_ptr audioController; std::unique_ptr initialLineState; std::unique_ptr search; + std::unique_ptr path; // Things that should probably be in some sort of UI-context-model wxWindow *parent = nullptr; diff --git a/src/project.cpp b/src/project.cpp index 2d1a404a7..5ed6578d4 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -65,10 +65,10 @@ Project::Project(agi::Context *c) : context(c) { Project::~Project() { } void Project::UpdateRelativePaths() { - context->ass->Properties.audio_file = config::path->MakeRelative(audio_file, "?script").generic_string(); - context->ass->Properties.video_file = config::path->MakeRelative(video_file, "?script").generic_string(); - context->ass->Properties.timecodes_file = config::path->MakeRelative(timecodes_file, "?script").generic_string(); - context->ass->Properties.keyframes_file = config::path->MakeRelative(keyframes_file, "?script").generic_string(); + context->ass->Properties.audio_file = context->path->MakeRelative(audio_file, "?script").generic_string(); + context->ass->Properties.video_file = context->path->MakeRelative(video_file, "?script").generic_string(); + context->ass->Properties.timecodes_file = context->path->MakeRelative(timecodes_file, "?script").generic_string(); + context->ass->Properties.keyframes_file = context->path->MakeRelative(keyframes_file, "?script").generic_string(); } void Project::ReloadAudio() { @@ -94,7 +94,7 @@ void Project::ShowError(std::string const& message) { void Project::SetPath(agi::fs::path& var, const char *token, const char *mru, agi::fs::path const& value) { var = value; if (*token) - config::path->SetToken(token, value); + context->path->SetToken(token, value); if (*mru) config::mru->Add(mru, value); UpdateRelativePaths(); @@ -166,7 +166,7 @@ void Project::LoadSubtitles(agi::fs::path path, std::string encoding, bool load_ void Project::CloseSubtitles() { context->subsController->Close(); - config::path->SetToken("?script", ""); + context->path->SetToken("?script", ""); LoadUnloadFiles(context->ass->Properties); auto line = &*context->ass->Events.begin(); context->selectionController->SetSelectionAndActive({line}, line); @@ -176,10 +176,10 @@ void Project::LoadUnloadFiles(ProjectProperties properties) { auto load_linked = OPT_GET("App/Auto/Load Linked Files")->GetInt(); if (!load_linked) return; - auto audio = config::path->MakeAbsolute(properties.audio_file, "?script"); - auto video = config::path->MakeAbsolute(properties.video_file, "?script"); - auto timecodes = config::path->MakeAbsolute(properties.timecodes_file, "?script"); - auto keyframes = config::path->MakeAbsolute(properties.keyframes_file, "?script"); + auto audio = context->path->MakeAbsolute(properties.audio_file, "?script"); + auto video = context->path->MakeAbsolute(properties.video_file, "?script"); + auto timecodes = context->path->MakeAbsolute(properties.timecodes_file, "?script"); + auto keyframes = context->path->MakeAbsolute(properties.keyframes_file, "?script"); if (video == video_file && audio == audio_file && keyframes == keyframes_file && timecodes == timecodes_file) return; @@ -244,7 +244,7 @@ void Project::DoLoadAudio(agi::fs::path const& path, bool quiet) { try { try { - audio_provider = GetAudioProvider(path, progress); + audio_provider = GetAudioProvider(path, *context->path, progress); } catch (agi::UserCancelException const&) { return; } catch (...) { diff --git a/src/subs_controller.cpp b/src/subs_controller.cpp index 74cfbca39..e69af3bf3 100644 --- a/src/subs_controller.cpp +++ b/src/subs_controller.cpp @@ -188,7 +188,7 @@ ProjectProperties SubsController::Load(agi::fs::path const& filename, std::strin if (path_str.empty()) path = filename.parent_path(); else - path = config::path->Decode(path_str); + path = context->path->Decode(path_str); agi::fs::CreateDirectory(path); agi::fs::Copy(filename, path/(filename.stem().string() + ".ORIGINAL" + filename.extension().string())); } @@ -209,7 +209,7 @@ void SubsController::Save(agi::fs::path const& filename, std::string const& enco // Have to set this now for the sake of things that want to save paths // relative to the script in the header this->filename = filename; - config::path->SetToken("?script", filename.parent_path()); + context->path->SetToken("?script", filename.parent_path()); context->ass->CleanExtradata(); writer->WriteFile(context->ass.get(), filename, 0, encoding); @@ -256,7 +256,7 @@ void SubsController::AutoSave() { if (commit_id == autosaved_commit_id) return; - auto directory = config::path->Decode(OPT_GET("Path/Auto/Save")->GetString()); + auto directory = context->path->Decode(OPT_GET("Path/Auto/Save")->GetString()); if (directory.empty()) directory = filename.parent_path(); @@ -302,7 +302,7 @@ bool SubsController::CanSave() const { void SubsController::SetFileName(agi::fs::path const& path) { filename = path; - config::path->SetToken("?script", path.parent_path()); + context->path->SetToken("?script", path.parent_path()); config::mru->Add("Subtitle", path); OPT_SET("Path/Last/Subtitles")->SetString(filename.parent_path().string()); }