Add context-specific path tokens

?video, ?audio, and ?script are not global.
This commit is contained in:
Thomas Goyne 2015-08-21 18:17:48 -07:00
parent 932937c343
commit f5f5439808
10 changed files with 48 additions and 34 deletions

View File

@ -55,7 +55,9 @@ std::vector<std::string> GetAudioProviderNames() {
return ::GetClasses(boost::make_iterator_range(std::begin(providers), std::end(providers))); return ::GetClasses(boost::make_iterator_range(std::begin(providers), std::end(providers)));
} }
std::unique_ptr<AudioProvider> GetAudioProvider(fs::path const& filename, BackgroundRunner *br) { std::unique_ptr<agi::AudioProvider> GetAudioProvider(fs::path const& filename,
Path const& path_helper,
BackgroundRunner *br) {
auto preferred = OPT_GET("Audio/Provider")->GetString(); auto preferred = OPT_GET("Audio/Provider")->GetString();
auto sorted = GetSorted(boost::make_iterator_range(std::begin(providers), std::end(providers)), preferred); auto sorted = GetSorted(boost::make_iterator_range(std::begin(providers), std::end(providers)), preferred);
@ -118,7 +120,7 @@ std::unique_ptr<AudioProvider> GetAudioProvider(fs::path const& filename, Backgr
auto path = OPT_GET("Audio/Cache/HD/Location")->GetString(); auto path = OPT_GET("Audio/Cache/HD/Location")->GetString();
if (path == "default") if (path == "default")
path = "?temp"; 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); return CreateHDAudioProvider(std::move(provider), cache_dir);
} }

View File

@ -22,7 +22,10 @@
namespace agi { namespace agi {
class AudioProvider; class AudioProvider;
class BackgroundRunner; class BackgroundRunner;
class Path;
} }
std::unique_ptr<agi::AudioProvider> GetAudioProvider(agi::fs::path const& filename, agi::BackgroundRunner *br); std::unique_ptr<agi::AudioProvider> GetAudioProvider(agi::fs::path const& filename,
agi::Path const& path_helper,
agi::BackgroundRunner *br);
std::vector<std::string> GetAudioProviderNames(); std::vector<std::string> GetAudioProviderNames();

View File

@ -417,8 +417,8 @@ namespace Automation4 {
scripts_string += "|"; scripts_string += "|";
auto scriptfn(script->GetFilename().string()); auto scriptfn(script->GetFilename().string());
auto autobase_rel = config::path->MakeRelative(scriptfn, autobasefn); auto autobase_rel = context->path->MakeRelative(scriptfn, autobasefn);
auto assfile_rel = config::path->MakeRelative(scriptfn, "?script"); auto assfile_rel = context->path->MakeRelative(scriptfn, "?script");
if (autobase_rel.string().size() <= scriptfn.size() && autobase_rel.string().size() <= assfile_rel.string().size()) { if (autobase_rel.string().size() <= scriptfn.size() && autobase_rel.string().size() <= assfile_rel.string().size()) {
scriptfn = "$" + autobase_rel.generic_string(); scriptfn = "$" + autobase_rel.generic_string();

View File

@ -197,8 +197,7 @@ namespace {
int get_keyframes(lua_State *L) int get_keyframes(lua_State *L)
{ {
const agi::Context *c = get_context(L); if (const agi::Context *c = get_context(L))
if (c)
push_value(L, c->project->Keyframes()); push_value(L, c->project->Keyframes());
else else
lua_pushnil(L); lua_pushnil(L);
@ -209,7 +208,10 @@ namespace {
{ {
std::string path = check_string(L, 1); std::string path = check_string(L, 1);
lua_pop(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; return 1;
} }
@ -272,10 +274,10 @@ namespace {
PUSH_FIELD(ar_mode); PUSH_FIELD(ar_mode);
PUSH_FIELD(video_position); PUSH_FIELD(video_position);
#undef PUSH_FIELD #undef PUSH_FIELD
set_field(L, "audio_file", config::path->MakeAbsolute(c->ass->Properties.audio_file, "?script")); set_field(L, "audio_file", c->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, "video_file", c->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, "timecodes_file", c->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, "keyframes_file", c->path->MakeAbsolute(c->ass->Properties.keyframes_file, "?script"));
} }
return 1; return 1;
} }

View File

@ -471,7 +471,7 @@ static void save_snapshot(agi::Context *c, bool raw) {
option = "?script"; option = "?script";
} }
// Find out where the ?specifier points to // 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 where ever that is isn't defined, we can't save there
if ((basepath == "\\") || (basepath == "/")) { if ((basepath == "\\") || (basepath == "/")) {
// So save to the current user's home dir instead // 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 // Actual fixed (possibly relative) path, decode it
else else
basepath = config::path->MakeAbsolute(option, "?user/"); basepath = c->path->MakeAbsolute(option, "?user/");
basepath /= is_dummy ? "dummy" : videoname.stem(); basepath /= is_dummy ? "dummy" : videoname.stem();

View File

@ -21,6 +21,7 @@
#include "auto4_base.h" #include "auto4_base.h"
#include "dialog_manager.h" #include "dialog_manager.h"
#include "initial_line_state.h" #include "initial_line_state.h"
#include "options.h"
#include "project.h" #include "project.h"
#include "search_replace_engine.h" #include "search_replace_engine.h"
#include "selection_controller.h" #include "selection_controller.h"
@ -29,6 +30,7 @@
#include "video_controller.h" #include "video_controller.h"
#include <libaegisub/make_unique.h> #include <libaegisub/make_unique.h>
#include <libaegisub/path.h>
namespace agi { namespace agi {
Context::Context() Context::Context()
@ -43,9 +45,10 @@ Context::Context()
, initialLineState(make_unique<InitialLineState>(this)) , initialLineState(make_unique<InitialLineState>(this))
, search(make_unique<SearchReplaceEngine>(this)) , search(make_unique<SearchReplaceEngine>(this))
, dialog(make_unique<DialogManager>()) , dialog(make_unique<DialogManager>())
, path(make_unique<Path>(*config::path))
{ {
subsController->SetSelectionController(selectionController.get()); subsController->SetSelectionController(selectionController.get());
} }
Context::~Context() {} Context::~Context() = default;
} }

View File

@ -50,6 +50,7 @@
namespace { namespace {
class DialogFontsCollector final : public wxDialog { class DialogFontsCollector final : public wxDialog {
AssFile *subs; AssFile *subs;
agi::Path &path;
wxStyledTextCtrl *collection_log; wxStyledTextCtrl *collection_log;
wxButton *close_btn; wxButton *close_btn;
@ -215,6 +216,7 @@ void FontsCollectorThread(AssFile *subs, agi::fs::path const& destination, FcMod
DialogFontsCollector::DialogFontsCollector(agi::Context *c) DialogFontsCollector::DialogFontsCollector(agi::Context *c)
: wxDialog(c->parent, -1, _("Fonts Collector")) : wxDialog(c->parent, -1, _("Fonts Collector"))
, subs(c->ass.get()) , subs(c->ass.get())
, path(*c->path)
{ {
SetIcon(GETICON(font_collector_button_16)); 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 = new wxRadioBox(this, -1, _("Action"), wxDefaultPosition, wxDefaultSize, countof(modes), modes, 1);
collection_mode->SetSelection(mid<int>(0, OPT_GET("Tool/Fonts Collector/Action")->GetInt(), 4)); collection_mode->SetSelection(mid<int>(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); collection_mode->Enable(2, false);
wxStaticBoxSizer *destination_box = new wxStaticBoxSizer(wxVERTICAL, this, _("Destination")); wxStaticBoxSizer *destination_box = new wxStaticBoxSizer(wxVERTICAL, this, _("Destination"));
dest_label = new wxStaticText(this, -1, " "); 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...")); dest_browse_button = new wxButton(this, -1, _("&Browse..."));
wxSizer *dest_browse_sizer = new wxBoxSizer(wxHORIZONTAL); wxSizer *dest_browse_sizer = new wxBoxSizer(wxHORIZONTAL);
@ -292,7 +294,7 @@ void DialogFontsCollector::OnStart(wxCommandEvent &) {
int action = collection_mode->GetSelection(); int action = collection_mode->GetSelection();
OPT_SET("Tool/Fonts Collector/Action")->SetInt(action); OPT_SET("Tool/Fonts Collector/Action")->SetInt(action);
if (action != CheckFontsOnly) { 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 (action != CopyToZip) {
if (agi::fs::FileExists(dest)) if (agi::fs::FileExists(dest))
@ -397,7 +399,7 @@ void DialogFontsCollector::OnCollectionComplete(wxThreadEvent &) {
start_btn->Enable(); start_btn->Enable();
close_btn->Enable(); close_btn->Enable();
collection_mode->Enable(); collection_mode->Enable();
if (config::path->Decode("?script") == "?script") if (path.Decode("?script") == "?script")
collection_mode->Enable(2, false); collection_mode->Enable(2, false);
wxCommandEvent evt; wxCommandEvent evt;

View File

@ -36,6 +36,7 @@ class wxWindow;
namespace Automation4 { class ScriptManager; } namespace Automation4 { class ScriptManager; }
namespace agi { namespace agi {
class Path;
struct Context { struct Context {
// Note: order here matters quite a bit, as things need to be set up and // 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> audioController; std::unique_ptr<AudioController> audioController;
std::unique_ptr<InitialLineState> initialLineState; std::unique_ptr<InitialLineState> initialLineState;
std::unique_ptr<SearchReplaceEngine> search; std::unique_ptr<SearchReplaceEngine> search;
std::unique_ptr<Path> path;
// Things that should probably be in some sort of UI-context-model // Things that should probably be in some sort of UI-context-model
wxWindow *parent = nullptr; wxWindow *parent = nullptr;

View File

@ -65,10 +65,10 @@ Project::Project(agi::Context *c) : context(c) {
Project::~Project() { } Project::~Project() { }
void Project::UpdateRelativePaths() { void Project::UpdateRelativePaths() {
context->ass->Properties.audio_file = config::path->MakeRelative(audio_file, "?script").generic_string(); context->ass->Properties.audio_file = context->path->MakeRelative(audio_file, "?script").generic_string();
context->ass->Properties.video_file = config::path->MakeRelative(video_file, "?script").generic_string(); context->ass->Properties.video_file = context->path->MakeRelative(video_file, "?script").generic_string();
context->ass->Properties.timecodes_file = config::path->MakeRelative(timecodes_file, "?script").generic_string(); context->ass->Properties.timecodes_file = context->path->MakeRelative(timecodes_file, "?script").generic_string();
context->ass->Properties.keyframes_file = config::path->MakeRelative(keyframes_file, "?script").generic_string(); context->ass->Properties.keyframes_file = context->path->MakeRelative(keyframes_file, "?script").generic_string();
} }
void Project::ReloadAudio() { 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) { void Project::SetPath(agi::fs::path& var, const char *token, const char *mru, agi::fs::path const& value) {
var = value; var = value;
if (*token) if (*token)
config::path->SetToken(token, value); context->path->SetToken(token, value);
if (*mru) if (*mru)
config::mru->Add(mru, value); config::mru->Add(mru, value);
UpdateRelativePaths(); UpdateRelativePaths();
@ -166,7 +166,7 @@ void Project::LoadSubtitles(agi::fs::path path, std::string encoding, bool load_
void Project::CloseSubtitles() { void Project::CloseSubtitles() {
context->subsController->Close(); context->subsController->Close();
config::path->SetToken("?script", ""); context->path->SetToken("?script", "");
LoadUnloadFiles(context->ass->Properties); LoadUnloadFiles(context->ass->Properties);
auto line = &*context->ass->Events.begin(); auto line = &*context->ass->Events.begin();
context->selectionController->SetSelectionAndActive({line}, line); 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(); auto load_linked = OPT_GET("App/Auto/Load Linked Files")->GetInt();
if (!load_linked) return; if (!load_linked) return;
auto audio = config::path->MakeAbsolute(properties.audio_file, "?script"); auto audio = context->path->MakeAbsolute(properties.audio_file, "?script");
auto video = config::path->MakeAbsolute(properties.video_file, "?script"); auto video = context->path->MakeAbsolute(properties.video_file, "?script");
auto timecodes = config::path->MakeAbsolute(properties.timecodes_file, "?script"); auto timecodes = context->path->MakeAbsolute(properties.timecodes_file, "?script");
auto keyframes = config::path->MakeAbsolute(properties.keyframes_file, "?script"); auto keyframes = context->path->MakeAbsolute(properties.keyframes_file, "?script");
if (video == video_file && audio == audio_file && keyframes == keyframes_file && timecodes == timecodes_file) if (video == video_file && audio == audio_file && keyframes == keyframes_file && timecodes == timecodes_file)
return; return;
@ -244,7 +244,7 @@ void Project::DoLoadAudio(agi::fs::path const& path, bool quiet) {
try { try {
try { try {
audio_provider = GetAudioProvider(path, progress); audio_provider = GetAudioProvider(path, *context->path, progress);
} }
catch (agi::UserCancelException const&) { return; } catch (agi::UserCancelException const&) { return; }
catch (...) { catch (...) {

View File

@ -188,7 +188,7 @@ ProjectProperties SubsController::Load(agi::fs::path const& filename, std::strin
if (path_str.empty()) if (path_str.empty())
path = filename.parent_path(); path = filename.parent_path();
else else
path = config::path->Decode(path_str); path = context->path->Decode(path_str);
agi::fs::CreateDirectory(path); agi::fs::CreateDirectory(path);
agi::fs::Copy(filename, path/(filename.stem().string() + ".ORIGINAL" + filename.extension().string())); 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 // Have to set this now for the sake of things that want to save paths
// relative to the script in the header // relative to the script in the header
this->filename = filename; this->filename = filename;
config::path->SetToken("?script", filename.parent_path()); context->path->SetToken("?script", filename.parent_path());
context->ass->CleanExtradata(); context->ass->CleanExtradata();
writer->WriteFile(context->ass.get(), filename, 0, encoding); writer->WriteFile(context->ass.get(), filename, 0, encoding);
@ -256,7 +256,7 @@ void SubsController::AutoSave() {
if (commit_id == autosaved_commit_id) if (commit_id == autosaved_commit_id)
return; 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()) if (directory.empty())
directory = filename.parent_path(); directory = filename.parent_path();
@ -302,7 +302,7 @@ bool SubsController::CanSave() const {
void SubsController::SetFileName(agi::fs::path const& path) { void SubsController::SetFileName(agi::fs::path const& path) {
filename = path; filename = path;
config::path->SetToken("?script", path.parent_path()); context->path->SetToken("?script", path.parent_path());
config::mru->Add("Subtitle", path); config::mru->Add("Subtitle", path);
OPT_SET("Path/Last/Subtitles")->SetString(filename.parent_path().string()); OPT_SET("Path/Last/Subtitles")->SetString(filename.parent_path().string());
} }