Move style catalog indexing and naming logic

This commit is contained in:
Niels Martin Hansen 2014-05-04 13:03:04 +02:00
parent cdb6da4b2f
commit 79b6ce2583
4 changed files with 43 additions and 16 deletions

View File

@ -36,11 +36,13 @@
#include "ass_file.h"
#include "ass_style.h"
#include "options.h"
#include <libaegisub/fs.h>
#include <libaegisub/io.h>
#include <libaegisub/line_iterator.h>
#include <libaegisub/make_unique.h>
#include <libaegisub/path.h>
#include <boost/algorithm/string/predicate.hpp>
@ -79,6 +81,11 @@ void AssStyleStorage::Load(agi::fs::path const& filename) {
}
}
void AssStyleStorage::LoadCatalog(std::string const& catalogname) {
auto filename = config::path->Decode("?user/catalog/" + catalogname + ".sty");
Load(filename);
}
void AssStyleStorage::Delete(int idx) {
style.erase(style.begin() + idx);
}
@ -98,15 +105,23 @@ AssStyle *AssStyleStorage::GetStyle(std::string const& name) {
return nullptr;
}
void AssStyleStorage::ReplaceIntoFile(AssFile &file) {
std::vector<AssStyle*> replaced_styles;
for (auto const& s : style) {
AssStyle *existing_style = file.GetStyle(s->name);
if (existing_style)
replaced_styles.push_back(existing_style);
file.Styles.push_back(*new AssStyle(*s));
}
for (auto s : replaced_styles)
delete s;
std::vector<std::string> AssStyleStorage::GetCatalogs() {
std::vector<std::string> catalogs;
for (auto const& file : agi::fs::DirectoryIterator(config::path->Decode("?user/catalog/"), "*.sty"))
catalogs.push_back(agi::fs::path(file).stem().string());
return catalogs;
}
bool AssStyleStorage::CatalogExists(std::string const& catalogname) {
if (catalogname.empty()) return false;
auto filename = config::path->Decode("?user/catalog/" + catalogname + ".sty");
return agi::fs::FileExists(filename);
}
void AssStyleStorage::ReplaceIntoFile(AssFile &file) {
for (auto const& s : style) {
delete file.GetStyle(s->name);
file.Styles.push_back(*new AssStyle(*s));
}
}

View File

@ -79,6 +79,17 @@ public:
/// @param filename Catalog filename. Does not have to exist.
void Load(agi::fs::path const& filename);
/// Load stored styles from a file in the default location
/// @param catalogname Basename for the catalog file. Does not have to exist.
void LoadCatalog(std::string const& catalogname);
/// Make a list of all existing style catalogs in the default location
static std::vector<std::string> GetCatalogs();
/// Check whether the name catalog exists in the default location
/// @param catalogname Basename for the catalog file to check for.
static bool CatalogExists(std::string const& catalogname);
/// Insert all styles into a file, replacing existing styles with the same names
/// @param file File to replace styles in
void ReplaceIntoFile(AssFile &file);

View File

@ -320,7 +320,7 @@ void DialogStyleManager::UpdateStorage() {
void DialogStyleManager::OnChangeCatalog() {
std::string catalog(from_wx(CatalogList->GetStringSelection()));
c->ass->SetScriptInfo("Last Style Storage", catalog);
Store.Load(config::path->Decode("?user/catalog/" + catalog + ".sty"));
Store.LoadCatalog(catalog);
UpdateStorage();
}
@ -328,12 +328,13 @@ void DialogStyleManager::LoadCatalog() {
CatalogList->Clear();
// Get saved style catalogs
for (auto const& file : agi::fs::DirectoryIterator(config::path->Decode("?user/catalog/"), "*.sty"))
CatalogList->Append(agi::fs::path(file).stem().wstring());
auto catalogs = AssStyleStorage::GetCatalogs();
for (auto const& c : catalogs)
CatalogList->Append(c);
// Create a default storage if there are none
if (CatalogList->IsListEmpty()) {
Store.Load(config::path->Decode("?user/catalog/Default.sty"));
Store.LoadCatalog("Default");
Store.push_back(agi::make_unique<AssStyle>());
Store.Save();
CatalogList->Append("Default");

View File

@ -18,6 +18,7 @@
#include "preferences.h"
#include "ass_style_storage.h"
#include "audio_renderer_waveform.h"
#include "colour_button.h"
#include "command/command.h"
@ -131,8 +132,7 @@ General_DefaultStyles::General_DefaultStyles(wxTreebook *book, Preferences *pare
// Always include one named "Default" even if it doesn't exist (ensure there is at least one on the list)
catalogs_set.insert("Default");
// Include all catalog files that exist
for (auto const& file : agi::fs::DirectoryIterator(config::path->Decode("?user/catalog/"), "*.sty"))
catalogs_set.insert(agi::fs::path(file).stem().string());
[&](std::vector<std::string> const& l){ catalogs_set.insert(l.begin(), l.end()); } (AssStyleStorage::GetCatalogs());
// Include all catalogs named in the existing configuration
static const char *formats[] = { "ASS", "SRT", "TTXT", "TXT" };
for (auto formatname : formats)