diff src/core/anime.cc @ 284:e66ffc338d82

anime: refactor title structure to a map
author Paper <paper@paper.us.eu.org>
date Wed, 08 May 2024 16:21:05 -0400
parents 9b6e12c14a1e
children 53e3c015a973
line wrap: on
line diff
--- a/src/core/anime.cc	Wed May 08 16:20:37 2024 -0400
+++ b/src/core/anime.cc	Wed May 08 16:21:05 2024 -0400
@@ -170,19 +170,16 @@
 	return info_.id;
 }
 
-std::string Anime::GetRomajiTitle() const {
-	return info_.title.romaji;
-}
+/* note: this should use std::optional */
+std::optional<std::string> Anime::GetTitle(TitleLanguage language) const {
+	if (info_.titles.find(language) == info_.titles.end())
+		return std::nullopt;
 
-std::string Anime::GetEnglishTitle() const {
-	return info_.title.english;
-}
-
-std::string Anime::GetNativeTitle() const {
-	return info_.title.native;
+	return info_.titles.at(language);
 }
 
 std::vector<std::string> Anime::GetTitleSynonyms() const {
+	/* mainly for the GUI */
 	std::vector<std::string> result;
 
 	auto add_to_synonyms = [this](std::vector<std::string>& vec, std::string key) {
@@ -190,9 +187,9 @@
 			vec.push_back(key);
 	};
 
-	add_to_synonyms(result, info_.title.english);
-	add_to_synonyms(result, info_.title.romaji);
-	add_to_synonyms(result, info_.title.native);
+	for (const auto& lang : TitleLanguages)
+		if (info_.titles.find(lang) != info_.titles.end())
+			add_to_synonyms(result, info_.titles.at(lang));
 
 	for (auto& synonym : info_.synonyms)
 		add_to_synonyms(result, synonym);
@@ -257,16 +254,8 @@
 	info_.id = id;
 }
 
-void Anime::SetRomajiTitle(std::string const& title) {
-	info_.title.romaji = title;
-}
-
-void Anime::SetEnglishTitle(std::string const& title) {
-	info_.title.english = title;
-}
-
-void Anime::SetNativeTitle(std::string const& title) {
-	info_.title.native = title;
+void Anime::SetTitle(TitleLanguage language, const std::string& title) {
+	info_.titles[language] = title;
 }
 
 void Anime::SetTitleSynonyms(std::vector<std::string> const& synonyms) {
@@ -318,12 +307,16 @@
 }
 
 std::string Anime::GetUserPreferredTitle() const {
-	switch (session.config.anime_list.language) {
-		case TitleLanguage::Native: return (GetNativeTitle().empty()) ? GetRomajiTitle() : GetNativeTitle();
-		case TitleLanguage::Romaji: return (GetEnglishTitle().empty()) ? GetRomajiTitle() : GetEnglishTitle();
-		default: break;
-	}
-	return GetRomajiTitle();
+	std::optional<std::string> title = GetTitle(session.config.anime_list.language);
+	if (title.has_value())
+		return title.value();
+
+	title = GetTitle(TitleLanguage::Romaji);
+	if (title.has_value())
+		return title.value();
+
+	/* what? */
+	return std::string();
 }
 
 } // namespace Anime