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_file.h"
#include "ass_style.h" #include "ass_style.h"
#include "options.h"
#include <libaegisub/fs.h> #include <libaegisub/fs.h>
#include <libaegisub/io.h> #include <libaegisub/io.h>
#include <libaegisub/line_iterator.h> #include <libaegisub/line_iterator.h>
#include <libaegisub/make_unique.h> #include <libaegisub/make_unique.h>
#include <libaegisub/path.h>
#include <boost/algorithm/string/predicate.hpp> #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) { void AssStyleStorage::Delete(int idx) {
style.erase(style.begin() + idx); style.erase(style.begin() + idx);
} }
@ -98,15 +105,23 @@ AssStyle *AssStyleStorage::GetStyle(std::string const& name) {
return nullptr; return nullptr;
} }
void AssStyleStorage::ReplaceIntoFile(AssFile &file) { std::vector<std::string> AssStyleStorage::GetCatalogs() {
std::vector<AssStyle*> replaced_styles; std::vector<std::string> catalogs;
for (auto const& s : style) { for (auto const& file : agi::fs::DirectoryIterator(config::path->Decode("?user/catalog/"), "*.sty"))
AssStyle *existing_style = file.GetStyle(s->name); catalogs.push_back(agi::fs::path(file).stem().string());
if (existing_style) return catalogs;
replaced_styles.push_back(existing_style); }
file.Styles.push_back(*new AssStyle(*s));
} bool AssStyleStorage::CatalogExists(std::string const& catalogname) {
for (auto s : replaced_styles) if (catalogname.empty()) return false;
delete s; 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. /// @param filename Catalog filename. Does not have to exist.
void Load(agi::fs::path const& filename); 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 /// Insert all styles into a file, replacing existing styles with the same names
/// @param file File to replace styles in /// @param file File to replace styles in
void ReplaceIntoFile(AssFile &file); void ReplaceIntoFile(AssFile &file);

View file

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

View file

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