1
0
Fork 0

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)));
}
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 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();
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);
}

View File

@ -22,7 +22,10 @@
namespace agi {
class AudioProvider;
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();

View File

@ -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();

View File

@ -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;
}

View File

@ -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();

View File

@ -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 <libaegisub/make_unique.h>
#include <libaegisub/path.h>
namespace agi {
Context::Context()
@ -43,9 +45,10 @@ Context::Context()
, initialLineState(make_unique<InitialLineState>(this))
, search(make_unique<SearchReplaceEngine>(this))
, dialog(make_unique<DialogManager>())
, path(make_unique<Path>(*config::path))
{
subsController->SetSelectionController(selectionController.get());
}
Context::~Context() {}
Context::~Context() = default;
}

View File

@ -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<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);
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;

View File

@ -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> audioController;
std::unique_ptr<InitialLineState> initialLineState;
std::unique_ptr<SearchReplaceEngine> search;
std::unique_ptr<Path> path;
// Things that should probably be in some sort of UI-context-model
wxWindow *parent = nullptr;

View File

@ -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 (...) {

View File

@ -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());
}