From 79b6ce2583ee87454669ee10b06d4030dfaccef9 Mon Sep 17 00:00:00 2001 From: Niels Martin Hansen Date: Sun, 4 May 2014 13:03:04 +0200 Subject: [PATCH] Move style catalog indexing and naming logic --- src/ass_style_storage.cpp | 35 +++++++++++++++++++++++++---------- src/ass_style_storage.h | 11 +++++++++++ src/dialog_style_manager.cpp | 9 +++++---- src/preferences.cpp | 4 ++-- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/ass_style_storage.cpp b/src/ass_style_storage.cpp index 8595a4f01..d6dc6d835 100644 --- a/src/ass_style_storage.cpp +++ b/src/ass_style_storage.cpp @@ -36,11 +36,13 @@ #include "ass_file.h" #include "ass_style.h" +#include "options.h" #include #include #include #include +#include #include @@ -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 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 AssStyleStorage::GetCatalogs() { + std::vector 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)); + } } diff --git a/src/ass_style_storage.h b/src/ass_style_storage.h index 46e4188ea..37f396c6f 100644 --- a/src/ass_style_storage.h +++ b/src/ass_style_storage.h @@ -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 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); diff --git a/src/dialog_style_manager.cpp b/src/dialog_style_manager.cpp index b11a15a4a..b7920e868 100644 --- a/src/dialog_style_manager.cpp +++ b/src/dialog_style_manager.cpp @@ -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()); Store.Save(); CatalogList->Append("Default"); diff --git a/src/preferences.cpp b/src/preferences.cpp index 368c0ffc3..3aa1a291e 100644 --- a/src/preferences.cpp +++ b/src/preferences.cpp @@ -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 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)