changeset 90:c4bb49c2f6eb

config: improve handling of vars : )
author Paper <mrpapersonic@gmail.com>
date Tue, 31 Oct 2023 23:41:53 -0400
parents e6fab256ddc4
children 29e2840d9b7b
files CMakeLists.txt include/gui/translate/anime.h src/core/config.cc src/gui/translate/anime.cc
diffstat 4 files changed, 65 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Tue Oct 31 23:06:33 2023 -0400
+++ b/CMakeLists.txt	Tue Oct 31 23:41:53 2023 -0400
@@ -93,6 +93,7 @@
 	# Translate
 	src/gui/translate/anime.cc
 	src/gui/translate/anilist.cc
+	src/gui/translate/config.cc
 
 	# Services (only AniList for now)
 	src/services/services.cc
--- a/include/gui/translate/anime.h	Tue Oct 31 23:06:33 2023 -0400
+++ b/include/gui/translate/anime.h	Tue Oct 31 23:41:53 2023 -0400
@@ -6,5 +6,9 @@
 std::string ToString(const Anime::SeriesFormat format);
 std::string ToString(const Anime::SeriesSeason season);
 std::string ToString(const Anime::SeriesStatus status);
+std::string ToString(const Anime::Services service);
+Anime::Services ToService(const std::string& str);
+std::string ToString(const Anime::TitleLanguage language);
+Anime::TitleLanguage ToLanguage(const std::string& str);
 
 } // namespace Translate
--- a/src/core/config.cc	Tue Oct 31 23:06:33 2023 -0400
+++ b/src/core/config.cc	Tue Oct 31 23:41:53 2023 -0400
@@ -6,67 +6,30 @@
 #include "core/anime.h"
 #include "core/filesystem.h"
 #include "core/json.h"
+#include "gui/translate/anime.h"
+#include "gui/translate/config.h"
 #include <cstdlib>
 #include <cstring>
 #include <filesystem>
 #include <fstream>
 #include <limits.h>
 
-std::map<std::string, Themes> StringToTheme = {
-    {"Default", Themes::OS   },
-    {"Light",   Themes::LIGHT},
-    {"Dark",    Themes::DARK }
-};
-
-std::map<Themes, std::string> ThemeToString = {
-    {Themes::OS,    "Default"},
-    {Themes::LIGHT, "Light"  },
-    {Themes::DARK,  "Dark"   }
-};
-
-std::map<Anime::Services, std::string> ServiceToString{
-    {Anime::Services::NONE,    "None"   },
-    {Anime::Services::ANILIST, "AniList"}
-};
-
-std::map<std::string, Anime::Services> StringToService{
-    {"None",    Anime::Services::NONE   },
-    {"AniList", Anime::Services::ANILIST}
-};
-
-std::map<Anime::TitleLanguage, std::string> AnimeTitleToStringMap = {
-    {Anime::TitleLanguage::ROMAJI,  "Romaji" },
-    {Anime::TitleLanguage::NATIVE,  "Native" },
-    {Anime::TitleLanguage::ENGLISH, "English"}
-};
-
-std::map<std::string, Anime::TitleLanguage> StringToAnimeTitleMap = {
-    {"Romaji",  Anime::TitleLanguage::ROMAJI },
-    {"Native",  Anime::TitleLanguage::NATIVE },
-    {"English", Anime::TitleLanguage::ENGLISH}
-};
-
 int Config::Load() {
 	Filesystem::Path cfg_path = Filesystem::GetConfigPath();
 	if (!cfg_path.Exists())
 		return 0;
 	std::ifstream config(cfg_path.GetPath(), std::ifstream::in);
 	auto config_js = nlohmann::json::parse(config);
-	service = StringToService[JSON::GetString(config_js, "/General/Service"_json_pointer)];
-	anime_list.language =
-	    StringToAnimeTitleMap[JSON::GetString(config_js, "/Anime List/Title language"_json_pointer, "Romaji")];
-	anime_list.display_aired_episodes =
-	    JSON::GetBoolean(config_js, "/Anime List/Display only aired episodes"_json_pointer, true);
-	anime_list.display_available_episodes =
-	    JSON::GetBoolean(config_js, "/Anime List/Display only available episodes in library"_json_pointer, true);
-	anime_list.highlight_anime_if_available =
-	    JSON::GetBoolean(config_js, "/Anime List/Highlight anime if available"_json_pointer, true);
-	anime_list.highlighted_anime_above_others =
-	    JSON::GetBoolean(config_js, "/Anime List/Display highlighted anime above others"_json_pointer);
+	service = Translate::ToService(JSON::GetString(config_js, "/General/Service"_json_pointer, "None"));
+	anime_list.language = Translate::ToLanguage(JSON::GetString(config_js, "/Anime List/Title language"_json_pointer, "Romaji"));
+	anime_list.display_aired_episodes = JSON::GetBoolean(config_js, "/Anime List/Display only aired episodes"_json_pointer, true);
+	anime_list.display_available_episodes = JSON::GetBoolean(config_js, "/Anime List/Display only available episodes in library"_json_pointer, true);
+	anime_list.highlight_anime_if_available = JSON::GetBoolean(config_js, "/Anime List/Highlight anime if available"_json_pointer, true);
+	anime_list.highlighted_anime_above_others = JSON::GetBoolean(config_js, "/Anime List/Display highlighted anime above others"_json_pointer);
 	anilist.auth_token = JSON::GetString(config_js, "/Authorization/AniList/Auth Token"_json_pointer);
 	anilist.username = JSON::GetString(config_js, "/Authorization/AniList/Username"_json_pointer);
 	anilist.user_id = JSON::GetInt(config_js, "/Authorization/AniList/User ID"_json_pointer);
-	theme = StringToTheme[JSON::GetString(config_js, "/Appearance/Theme"_json_pointer)];
+	theme = Translate::ToTheme(JSON::GetString(config_js, "/Appearance/Theme"_json_pointer, "Default"));
 	config.close();
 	return 0;
 }
@@ -79,10 +42,10 @@
 	/* clang-format off */
 	nlohmann::json config_js = {
 		{"General",	{
-			{"Service", ServiceToString[service]}
+			{"Service", Translate::ToString(service)}
 		}},
 		{"Anime List", {
-			{"Title language", AnimeTitleToStringMap[anime_list.language]},
+			{"Title language", Translate::ToString(anime_list.language)},
 			{"Display only aired episodes", anime_list.display_aired_episodes},
 			{"Display only available episodes in library", anime_list.display_available_episodes},
 			{"Highlight anime if available", anime_list.highlight_anime_if_available},
@@ -96,7 +59,7 @@
 			}}
 		}},
 		{"Appearance", {
-			{"Theme", ThemeToString[theme]}
+			{"Theme", Translate::ToString(theme)}
 		}}
 	};
 	/* clang-format on */
--- a/src/gui/translate/anime.cc	Tue Oct 31 23:06:33 2023 -0400
+++ b/src/gui/translate/anime.cc	Tue Oct 31 23:41:53 2023 -0400
@@ -7,19 +7,18 @@
 
 std::string ToString(const Anime::ListStatus status) {
 	switch (status) {
-		case Anime::ListStatus::NOT_IN_LIST: return Strings::ToUtf8String(QCoreApplication::tr("Not in list"));
 		case Anime::ListStatus::CURRENT: return Strings::ToUtf8String(QCoreApplication::tr("Currently watching"));
 		case Anime::ListStatus::PLANNING: return Strings::ToUtf8String(QCoreApplication::tr("Plan to watch"));
 		case Anime::ListStatus::COMPLETED: return Strings::ToUtf8String(QCoreApplication::tr("Completed"));
 		case Anime::ListStatus::DROPPED: return Strings::ToUtf8String(QCoreApplication::tr("Dropped"));
 		case Anime::ListStatus::PAUSED: return Strings::ToUtf8String(QCoreApplication::tr("On hold"));
-		default: return "";
+		default:
+		case Anime::ListStatus::NOT_IN_LIST: return Strings::ToUtf8String(QCoreApplication::tr("Not in list"));
 	}
 }
 
 std::string ToString(const Anime::SeriesFormat format) {
 	switch (format) {
-		case Anime::SeriesFormat::UNKNOWN: return Strings::ToUtf8String(QCoreApplication::tr("Unknown"));
 		case Anime::SeriesFormat::TV: return Strings::ToUtf8String(QCoreApplication::tr("TV"));
 		case Anime::SeriesFormat::TV_SHORT: return Strings::ToUtf8String(QCoreApplication::tr("TV short"));
 		case Anime::SeriesFormat::OVA: return Strings::ToUtf8String(QCoreApplication::tr("OVA"));
@@ -27,31 +26,72 @@
 		case Anime::SeriesFormat::SPECIAL: return Strings::ToUtf8String(QCoreApplication::tr("Special"));
 		case Anime::SeriesFormat::ONA: return Strings::ToUtf8String(QCoreApplication::tr("ONA"));
 		case Anime::SeriesFormat::MUSIC: return Strings::ToUtf8String(QCoreApplication::tr("Music"));
-		default: return "";
+		default:
+		case Anime::SeriesFormat::UNKNOWN: return Strings::ToUtf8String(QCoreApplication::tr("Unknown"));
 	}
 }
 
 std::string ToString(const Anime::SeriesSeason season) {
 	switch (season) {
-		case Anime::SeriesSeason::UNKNOWN: return Strings::ToUtf8String(QCoreApplication::tr("Unknown"));
 		case Anime::SeriesSeason::WINTER: return Strings::ToUtf8String(QCoreApplication::tr("Winter"));
 		case Anime::SeriesSeason::SUMMER: return Strings::ToUtf8String(QCoreApplication::tr("Summer"));
 		case Anime::SeriesSeason::FALL: return Strings::ToUtf8String(QCoreApplication::tr("Fall"));
 		case Anime::SeriesSeason::SPRING: return Strings::ToUtf8String(QCoreApplication::tr("Spring"));
-		default: return "";
+		default:
+		case Anime::SeriesSeason::UNKNOWN: return Strings::ToUtf8String(QCoreApplication::tr("Unknown"));
 	}
 }
 
 std::string ToString(const Anime::SeriesStatus status) {
 	switch (status) {
-		case Anime::SeriesStatus::UNKNOWN: return Strings::ToUtf8String(QCoreApplication::tr("Unknown"));
 		case Anime::SeriesStatus::RELEASING: return Strings::ToUtf8String(QCoreApplication::tr("Currently airing"));
 		case Anime::SeriesStatus::FINISHED: return Strings::ToUtf8String(QCoreApplication::tr("Finished airing"));
 		case Anime::SeriesStatus::NOT_YET_RELEASED: return Strings::ToUtf8String(QCoreApplication::tr("Not yet aired"));
 		case Anime::SeriesStatus::CANCELLED: return Strings::ToUtf8String(QCoreApplication::tr("Cancelled"));
 		case Anime::SeriesStatus::HIATUS: return Strings::ToUtf8String(QCoreApplication::tr("On hiatus"));
-		default: return "";
+		default:
+		case Anime::SeriesStatus::UNKNOWN: return Strings::ToUtf8String(QCoreApplication::tr("Unknown"));
+	}
+}
+
+std::string ToString(const Anime::Services service) {
+	switch (service) {
+		case Anime::Services::ANILIST: return Strings::ToUtf8String(QCoreApplication::tr("AniList"));
+		default:
+		case Anime::Services::NONE:    return Strings::ToUtf8String(QCoreApplication::tr("None"));
 	}
 }
 
+Anime::Services ToService(const std::string& str) {
+	const std::unordered_map<std::string, Anime::Services> map = {
+	    {"None",    Anime::Services::NONE   },
+	    {"AniList", Anime::Services::ANILIST}
+	};
+
+	if (map.find(str) == map.end())
+		return Anime::Services::NONE;
+	return map.at(str);
+}
+
+std::string ToString(const Anime::TitleLanguage language) {
+	switch (language) {
+		case Anime::TitleLanguage::NATIVE:  return Strings::ToUtf8String(QCoreApplication::tr("Native"));
+		case Anime::TitleLanguage::ENGLISH: return Strings::ToUtf8String(QCoreApplication::tr("English"));
+		default:
+		case Anime::TitleLanguage::ROMAJI:  return Strings::ToUtf8String(QCoreApplication::tr("Romaji"));
+	}
+}
+
+Anime::TitleLanguage ToLanguage(const std::string& str) {
+	const std::unordered_map<std::string, Anime::TitleLanguage> map = {
+	    {"Romaji", Anime::TitleLanguage::ROMAJI},
+	    {"Native", Anime::TitleLanguage::NATIVE},
+	    {"English", Anime::TitleLanguage::ENGLISH}
+	};
+
+	if (map.find(str) == map.end())
+		return Anime::TitleLanguage::ROMAJI;
+	return map.at(str);
+}
+
 } // namespace Translate