1
0
Fork 0
Aegisub/src/subtitles_provider.cpp

68 lines
2.3 KiB
C++

// Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Aegisub Project http://www.aegisub.org/
#include "config.h"
#include "include/aegisub/subtitles_provider.h"
#include "factory_manager.h"
#include "options.h"
#include "subtitles_provider_csri.h"
#include "subtitles_provider_libass.h"
namespace {
struct factory {
std::string name;
std::string subtype;
std::unique_ptr<SubtitlesProvider> (*create)(std::string const& subtype, agi::BackgroundRunner *br);
bool hidden;
};
std::vector<factory> const& factories() {
static std::vector<factory> factories;
if (factories.size()) return factories;
#ifdef WITH_CSRI
for (auto const& subtype : csri::List())
factories.push_back(factory{"CSRI/" + subtype, subtype, csri::Create, false});
#endif
factories.push_back(factory{"libass", "", libass::Create, false});
return factories;
}
}
std::vector<std::string> SubtitlesProviderFactory::GetClasses() {
return ::GetClasses(factories());
}
std::unique_ptr<SubtitlesProvider> SubtitlesProviderFactory::GetProvider(agi::BackgroundRunner *br) {
auto preferred = OPT_GET("Subtitle/Provider")->GetString();
auto sorted = GetSorted(factories(), preferred);
std::string error;
for (auto factory : sorted) {
try {
auto provider = factory->create(factory->subtype, br);
if (provider) return provider;
}
catch (agi::UserCancelException const&) { throw; }
catch (std::string const& err) { error += factory->name + ": " + err + "\n"; }
catch (const char *err) { error += factory->name + ": " + std::string(err) + "\n"; }
catch (...) { error += factory->name + ": Unknown error\n"; }
}
throw error;
}