diff src/core/config.cc @ 101:c537996cf67b

*: multitude of config changes 1. theme is now configurable from the settings menu (but you have to restart for it to apply) 2. config is now stored in an INI file, with no method of conversion from json (this repo is private-ish anyway)
author Paper <mrpapersonic@gmail.com>
date Fri, 03 Nov 2023 14:06:02 -0400
parents c4bb49c2f6eb
children b315f3759c56
line wrap: on
line diff
--- a/src/core/config.cc	Fri Nov 03 09:43:04 2023 -0400
+++ b/src/core/config.cc	Fri Nov 03 14:06:02 2023 -0400
@@ -3,34 +3,55 @@
  * parses the config... lol
  **/
 #include "core/config.h"
+#include "core/strings.h"
 #include "core/anime.h"
 #include "core/filesystem.h"
 #include "core/json.h"
 #include "gui/translate/anime.h"
 #include "gui/translate/config.h"
+#include "ini.h" // mINI
+#include <algorithm>
 #include <cstdlib>
 #include <cstring>
 #include <filesystem>
 #include <fstream>
 #include <limits.h>
 
+/* I'm not exactly fond of using JSON for a config file, but it's better than
+   no config I guess. I'd like to have something more readable, e.g. YAML or
+   even INI. */
+
+static bool string_to_bool(const std::string& s, bool def = false) {
+	bool b;
+    std::istringstream is(Strings::ToLower(s));
+    is >> std::boolalpha >> b;
+    return b;
+}
+
+static std::string bool_to_string(bool b) {
+	std::ostringstream stream;
+	stream << std::boolalpha << b;
+	return stream.str();
+}
+
 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 = 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 = Translate::ToTheme(JSON::GetString(config_js, "/Appearance/Theme"_json_pointer, "Default"));
-	config.close();
+	mINI::INIFile file(cfg_path.GetPath());
+	mINI::INIStructure ini;
+	file.read(ini);
+
+	service = Translate::ToService(ini.get("General").get("Service"));
+	anime_list.language = Translate::ToLanguage(ini.get("Anime List").get("Title language"));
+	anime_list.display_aired_episodes = string_to_bool(ini.get("Anime List").get("Display only aired episodes"), true);
+	anime_list.display_available_episodes = string_to_bool(ini.get("Anime List").get("Display only available episodes in library"), true);
+	anime_list.highlight_anime_if_available = string_to_bool(ini.get("Anime List").get("Highlight anime if available"), true);
+	anime_list.highlighted_anime_above_others = string_to_bool(ini.get("Anime List").get("Display highlighted anime above others"));
+	anilist.auth_token = ini.get("AniList").get("Auth Token");
+	anilist.user_id = Strings::ToInt(ini.get("AniList").get("User ID"));
+	theme = Translate::ToTheme(ini.get("Appearance").get("Theme"));
+
 	return 0;
 }
 
@@ -38,32 +59,21 @@
 	Filesystem::Path cfg_path = Filesystem::GetConfigPath();
 	if (!cfg_path.GetParent().Exists())
 		cfg_path.GetParent().CreateDirectories();
-	std::ofstream config(cfg_path.GetPath(), std::ofstream::out | std::ofstream::trunc);
-	/* clang-format off */
-	nlohmann::json config_js = {
-		{"General",	{
-			{"Service", Translate::ToString(service)}
-		}},
-		{"Anime List", {
-			{"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},
-			{"Display highlighted anime above others", anime_list.highlighted_anime_above_others}
-		}},
-		{"Authorization", {
-			{"AniList", {
-				{"Auth Token", anilist.auth_token},
-				{"Username", anilist.username},
-				{"User ID", anilist.user_id}
-			}}
-		}},
-		{"Appearance", {
-			{"Theme", Translate::ToString(theme)}
-		}}
-	};
-	/* clang-format on */
-	config << std::setw(4) << config_js << std::endl;
-	config.close();
+
+	mINI::INIFile file(cfg_path.GetPath());
+	mINI::INIStructure ini;
+
+	ini["General"]["Service"] = Translate::ToString(service);
+	ini["Anime List"]["Title language"] = Translate::ToString(anime_list.language);
+	ini["Anime List"]["Display only aired episodes"] = bool_to_string(anime_list.display_aired_episodes);
+	ini["Anime List"]["Display only available episodes in library"] = bool_to_string(anime_list.display_available_episodes);
+	ini["Anime List"]["Highlight anime if available"] = bool_to_string(anime_list.highlight_anime_if_available);
+	ini["Anime List"]["Display highlighted anime above others"] = bool_to_string(anime_list.highlighted_anime_above_others);
+	ini["AniList"]["Auth Token"] = anilist.auth_token;
+	ini["AniList"]["User ID"] = std::to_string(anilist.user_id);
+	ini["Appearance"]["Theme"] = Translate::ToString(theme);
+
+	file.generate(ini);
+
 	return 0;
 }