diff src/config.cpp @ 6:1d82f6e04d7d

Update: add first parts to the settings dialog
author Paper <mrpapersonic@gmail.com>
date Wed, 16 Aug 2023 00:49:17 -0400
parents 23d0d9319a00
children 07a9095eaeed
line wrap: on
line diff
--- a/src/config.cpp	Sat Aug 12 13:10:34 2023 -0400
+++ b/src/config.cpp	Wed Aug 16 00:49:17 2023 -0400
@@ -31,21 +31,27 @@
 	{DARK, "Dark"}
 };
 
+std::map<enum AnimeListServices, std::string> ServiceToString {
+	{NONE, "None"},
+	{ANILIST, "AniList"}
+};
+
+std::map<std::string, enum AnimeListServices> StringToService {
+	{"None", NONE},
+	{"AniList", ANILIST}
+};
+
 int Config::Load() {
 	std::filesystem::path cfg_path = get_config_path();
 	if (!std::filesystem::exists(cfg_path))
 		return 0;
 	std::ifstream config_in(cfg_path.string().c_str(), std::ifstream::in);
 	auto config_js = nlohmann::json::parse(config_in);
-/* this macro will make it easier to edit these in the future, if needed */
-#define GET_CONFIG_VALUE(pointer, location, struct, default) \
-	struct = (config_js.contains(pointer)) ? (location) : (default)
-	GET_CONFIG_VALUE("/General/Service"_json_pointer, (enum AnimeListServices)config_js["General"]["Service"].get<int>(), service, NONE);
-	GET_CONFIG_VALUE("/Authorization/AniList/Auth Token"_json_pointer, config_js["Authorization"]["AniList"]["Auth Token"].get<std::string>(), anilist.auth_token, "");
-	GET_CONFIG_VALUE("/Authorization/AniList/Username"_json_pointer, config_js["Authorization"]["AniList"]["Username"].get<std::string>(), anilist.username, "");
-	GET_CONFIG_VALUE("/Authorization/AniList/User ID"_json_pointer, config_js["Authorization"]["AniList"]["User ID"].get<int>(), anilist.user_id, 0);
-	GET_CONFIG_VALUE("/Appearance/Theme"_json_pointer, StringToTheme[config_js["Appearance"]["Theme"].get<std::string>()], theme, OS);
-#undef GET_CONFIG_VALUE
+	service = StringToService[JSON::GetString(config_js, "/General/Service"_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)];
 	config_in.close();
 	return 0;
 }
@@ -57,7 +63,7 @@
 	std::ofstream config_out(cfg_path.string().c_str(), std::ofstream::out | std::ofstream::trunc);
 	nlohmann::json config_js = {
 		{"General", {
-			{"Service", service}
+			{"Service", ServiceToString[service]}
 		}},
 		{"Authorization", {
 			{"AniList", {