comparison 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
comparison
equal deleted inserted replaced
100:f5940a575d83 101:c537996cf67b
1 /** 1 /**
2 * config.cpp: 2 * config.cpp:
3 * parses the config... lol 3 * parses the config... lol
4 **/ 4 **/
5 #include "core/config.h" 5 #include "core/config.h"
6 #include "core/strings.h"
6 #include "core/anime.h" 7 #include "core/anime.h"
7 #include "core/filesystem.h" 8 #include "core/filesystem.h"
8 #include "core/json.h" 9 #include "core/json.h"
9 #include "gui/translate/anime.h" 10 #include "gui/translate/anime.h"
10 #include "gui/translate/config.h" 11 #include "gui/translate/config.h"
12 #include "ini.h" // mINI
13 #include <algorithm>
11 #include <cstdlib> 14 #include <cstdlib>
12 #include <cstring> 15 #include <cstring>
13 #include <filesystem> 16 #include <filesystem>
14 #include <fstream> 17 #include <fstream>
15 #include <limits.h> 18 #include <limits.h>
16 19
20 /* I'm not exactly fond of using JSON for a config file, but it's better than
21 no config I guess. I'd like to have something more readable, e.g. YAML or
22 even INI. */
23
24 static bool string_to_bool(const std::string& s, bool def = false) {
25 bool b;
26 std::istringstream is(Strings::ToLower(s));
27 is >> std::boolalpha >> b;
28 return b;
29 }
30
31 static std::string bool_to_string(bool b) {
32 std::ostringstream stream;
33 stream << std::boolalpha << b;
34 return stream.str();
35 }
36
17 int Config::Load() { 37 int Config::Load() {
18 Filesystem::Path cfg_path = Filesystem::GetConfigPath(); 38 Filesystem::Path cfg_path = Filesystem::GetConfigPath();
19 if (!cfg_path.Exists()) 39 if (!cfg_path.Exists())
20 return 0; 40 return 0;
21 std::ifstream config(cfg_path.GetPath(), std::ifstream::in); 41 mINI::INIFile file(cfg_path.GetPath());
22 auto config_js = nlohmann::json::parse(config); 42 mINI::INIStructure ini;
23 service = Translate::ToService(JSON::GetString(config_js, "/General/Service"_json_pointer, "None")); 43 file.read(ini);
24 anime_list.language = Translate::ToLanguage(JSON::GetString(config_js, "/Anime List/Title language"_json_pointer, "Romaji")); 44
25 anime_list.display_aired_episodes = JSON::GetBoolean(config_js, "/Anime List/Display only aired episodes"_json_pointer, true); 45 service = Translate::ToService(ini.get("General").get("Service"));
26 anime_list.display_available_episodes = JSON::GetBoolean(config_js, "/Anime List/Display only available episodes in library"_json_pointer, true); 46 anime_list.language = Translate::ToLanguage(ini.get("Anime List").get("Title language"));
27 anime_list.highlight_anime_if_available = JSON::GetBoolean(config_js, "/Anime List/Highlight anime if available"_json_pointer, true); 47 anime_list.display_aired_episodes = string_to_bool(ini.get("Anime List").get("Display only aired episodes"), true);
28 anime_list.highlighted_anime_above_others = JSON::GetBoolean(config_js, "/Anime List/Display highlighted anime above others"_json_pointer); 48 anime_list.display_available_episodes = string_to_bool(ini.get("Anime List").get("Display only available episodes in library"), true);
29 anilist.auth_token = JSON::GetString(config_js, "/Authorization/AniList/Auth Token"_json_pointer); 49 anime_list.highlight_anime_if_available = string_to_bool(ini.get("Anime List").get("Highlight anime if available"), true);
30 anilist.username = JSON::GetString(config_js, "/Authorization/AniList/Username"_json_pointer); 50 anime_list.highlighted_anime_above_others = string_to_bool(ini.get("Anime List").get("Display highlighted anime above others"));
31 anilist.user_id = JSON::GetInt(config_js, "/Authorization/AniList/User ID"_json_pointer); 51 anilist.auth_token = ini.get("AniList").get("Auth Token");
32 theme = Translate::ToTheme(JSON::GetString(config_js, "/Appearance/Theme"_json_pointer, "Default")); 52 anilist.user_id = Strings::ToInt(ini.get("AniList").get("User ID"));
33 config.close(); 53 theme = Translate::ToTheme(ini.get("Appearance").get("Theme"));
54
34 return 0; 55 return 0;
35 } 56 }
36 57
37 int Config::Save() { 58 int Config::Save() {
38 Filesystem::Path cfg_path = Filesystem::GetConfigPath(); 59 Filesystem::Path cfg_path = Filesystem::GetConfigPath();
39 if (!cfg_path.GetParent().Exists()) 60 if (!cfg_path.GetParent().Exists())
40 cfg_path.GetParent().CreateDirectories(); 61 cfg_path.GetParent().CreateDirectories();
41 std::ofstream config(cfg_path.GetPath(), std::ofstream::out | std::ofstream::trunc); 62
42 /* clang-format off */ 63 mINI::INIFile file(cfg_path.GetPath());
43 nlohmann::json config_js = { 64 mINI::INIStructure ini;
44 {"General", { 65
45 {"Service", Translate::ToString(service)} 66 ini["General"]["Service"] = Translate::ToString(service);
46 }}, 67 ini["Anime List"]["Title language"] = Translate::ToString(anime_list.language);
47 {"Anime List", { 68 ini["Anime List"]["Display only aired episodes"] = bool_to_string(anime_list.display_aired_episodes);
48 {"Title language", Translate::ToString(anime_list.language)}, 69 ini["Anime List"]["Display only available episodes in library"] = bool_to_string(anime_list.display_available_episodes);
49 {"Display only aired episodes", anime_list.display_aired_episodes}, 70 ini["Anime List"]["Highlight anime if available"] = bool_to_string(anime_list.highlight_anime_if_available);
50 {"Display only available episodes in library", anime_list.display_available_episodes}, 71 ini["Anime List"]["Display highlighted anime above others"] = bool_to_string(anime_list.highlighted_anime_above_others);
51 {"Highlight anime if available", anime_list.highlight_anime_if_available}, 72 ini["AniList"]["Auth Token"] = anilist.auth_token;
52 {"Display highlighted anime above others", anime_list.highlighted_anime_above_others} 73 ini["AniList"]["User ID"] = std::to_string(anilist.user_id);
53 }}, 74 ini["Appearance"]["Theme"] = Translate::ToString(theme);
54 {"Authorization", { 75
55 {"AniList", { 76 file.generate(ini);
56 {"Auth Token", anilist.auth_token}, 77
57 {"Username", anilist.username},
58 {"User ID", anilist.user_id}
59 }}
60 }},
61 {"Appearance", {
62 {"Theme", Translate::ToString(theme)}
63 }}
64 };
65 /* clang-format on */
66 config << std::setw(4) << config_js << std::endl;
67 config.close();
68 return 0; 78 return 0;
69 } 79 }