Mercurial > minori
comparison src/core/config.cc @ 90:c4bb49c2f6eb
config: improve handling of vars
: )
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Tue, 31 Oct 2023 23:41:53 -0400 |
parents | 9b2b41f83a5e |
children | c537996cf67b |
comparison
equal
deleted
inserted
replaced
89:e6fab256ddc4 | 90:c4bb49c2f6eb |
---|---|
4 **/ | 4 **/ |
5 #include "core/config.h" | 5 #include "core/config.h" |
6 #include "core/anime.h" | 6 #include "core/anime.h" |
7 #include "core/filesystem.h" | 7 #include "core/filesystem.h" |
8 #include "core/json.h" | 8 #include "core/json.h" |
9 #include "gui/translate/anime.h" | |
10 #include "gui/translate/config.h" | |
9 #include <cstdlib> | 11 #include <cstdlib> |
10 #include <cstring> | 12 #include <cstring> |
11 #include <filesystem> | 13 #include <filesystem> |
12 #include <fstream> | 14 #include <fstream> |
13 #include <limits.h> | 15 #include <limits.h> |
14 | |
15 std::map<std::string, Themes> StringToTheme = { | |
16 {"Default", Themes::OS }, | |
17 {"Light", Themes::LIGHT}, | |
18 {"Dark", Themes::DARK } | |
19 }; | |
20 | |
21 std::map<Themes, std::string> ThemeToString = { | |
22 {Themes::OS, "Default"}, | |
23 {Themes::LIGHT, "Light" }, | |
24 {Themes::DARK, "Dark" } | |
25 }; | |
26 | |
27 std::map<Anime::Services, std::string> ServiceToString{ | |
28 {Anime::Services::NONE, "None" }, | |
29 {Anime::Services::ANILIST, "AniList"} | |
30 }; | |
31 | |
32 std::map<std::string, Anime::Services> StringToService{ | |
33 {"None", Anime::Services::NONE }, | |
34 {"AniList", Anime::Services::ANILIST} | |
35 }; | |
36 | |
37 std::map<Anime::TitleLanguage, std::string> AnimeTitleToStringMap = { | |
38 {Anime::TitleLanguage::ROMAJI, "Romaji" }, | |
39 {Anime::TitleLanguage::NATIVE, "Native" }, | |
40 {Anime::TitleLanguage::ENGLISH, "English"} | |
41 }; | |
42 | |
43 std::map<std::string, Anime::TitleLanguage> StringToAnimeTitleMap = { | |
44 {"Romaji", Anime::TitleLanguage::ROMAJI }, | |
45 {"Native", Anime::TitleLanguage::NATIVE }, | |
46 {"English", Anime::TitleLanguage::ENGLISH} | |
47 }; | |
48 | 16 |
49 int Config::Load() { | 17 int Config::Load() { |
50 Filesystem::Path cfg_path = Filesystem::GetConfigPath(); | 18 Filesystem::Path cfg_path = Filesystem::GetConfigPath(); |
51 if (!cfg_path.Exists()) | 19 if (!cfg_path.Exists()) |
52 return 0; | 20 return 0; |
53 std::ifstream config(cfg_path.GetPath(), std::ifstream::in); | 21 std::ifstream config(cfg_path.GetPath(), std::ifstream::in); |
54 auto config_js = nlohmann::json::parse(config); | 22 auto config_js = nlohmann::json::parse(config); |
55 service = StringToService[JSON::GetString(config_js, "/General/Service"_json_pointer)]; | 23 service = Translate::ToService(JSON::GetString(config_js, "/General/Service"_json_pointer, "None")); |
56 anime_list.language = | 24 anime_list.language = Translate::ToLanguage(JSON::GetString(config_js, "/Anime List/Title language"_json_pointer, "Romaji")); |
57 StringToAnimeTitleMap[JSON::GetString(config_js, "/Anime List/Title language"_json_pointer, "Romaji")]; | 25 anime_list.display_aired_episodes = JSON::GetBoolean(config_js, "/Anime List/Display only aired episodes"_json_pointer, true); |
58 anime_list.display_aired_episodes = | 26 anime_list.display_available_episodes = JSON::GetBoolean(config_js, "/Anime List/Display only available episodes in library"_json_pointer, true); |
59 JSON::GetBoolean(config_js, "/Anime List/Display only aired episodes"_json_pointer, true); | 27 anime_list.highlight_anime_if_available = JSON::GetBoolean(config_js, "/Anime List/Highlight anime if available"_json_pointer, true); |
60 anime_list.display_available_episodes = | 28 anime_list.highlighted_anime_above_others = JSON::GetBoolean(config_js, "/Anime List/Display highlighted anime above others"_json_pointer); |
61 JSON::GetBoolean(config_js, "/Anime List/Display only available episodes in library"_json_pointer, true); | |
62 anime_list.highlight_anime_if_available = | |
63 JSON::GetBoolean(config_js, "/Anime List/Highlight anime if available"_json_pointer, true); | |
64 anime_list.highlighted_anime_above_others = | |
65 JSON::GetBoolean(config_js, "/Anime List/Display highlighted anime above others"_json_pointer); | |
66 anilist.auth_token = JSON::GetString(config_js, "/Authorization/AniList/Auth Token"_json_pointer); | 29 anilist.auth_token = JSON::GetString(config_js, "/Authorization/AniList/Auth Token"_json_pointer); |
67 anilist.username = JSON::GetString(config_js, "/Authorization/AniList/Username"_json_pointer); | 30 anilist.username = JSON::GetString(config_js, "/Authorization/AniList/Username"_json_pointer); |
68 anilist.user_id = JSON::GetInt(config_js, "/Authorization/AniList/User ID"_json_pointer); | 31 anilist.user_id = JSON::GetInt(config_js, "/Authorization/AniList/User ID"_json_pointer); |
69 theme = StringToTheme[JSON::GetString(config_js, "/Appearance/Theme"_json_pointer)]; | 32 theme = Translate::ToTheme(JSON::GetString(config_js, "/Appearance/Theme"_json_pointer, "Default")); |
70 config.close(); | 33 config.close(); |
71 return 0; | 34 return 0; |
72 } | 35 } |
73 | 36 |
74 int Config::Save() { | 37 int Config::Save() { |
77 cfg_path.GetParent().CreateDirectories(); | 40 cfg_path.GetParent().CreateDirectories(); |
78 std::ofstream config(cfg_path.GetPath(), std::ofstream::out | std::ofstream::trunc); | 41 std::ofstream config(cfg_path.GetPath(), std::ofstream::out | std::ofstream::trunc); |
79 /* clang-format off */ | 42 /* clang-format off */ |
80 nlohmann::json config_js = { | 43 nlohmann::json config_js = { |
81 {"General", { | 44 {"General", { |
82 {"Service", ServiceToString[service]} | 45 {"Service", Translate::ToString(service)} |
83 }}, | 46 }}, |
84 {"Anime List", { | 47 {"Anime List", { |
85 {"Title language", AnimeTitleToStringMap[anime_list.language]}, | 48 {"Title language", Translate::ToString(anime_list.language)}, |
86 {"Display only aired episodes", anime_list.display_aired_episodes}, | 49 {"Display only aired episodes", anime_list.display_aired_episodes}, |
87 {"Display only available episodes in library", anime_list.display_available_episodes}, | 50 {"Display only available episodes in library", anime_list.display_available_episodes}, |
88 {"Highlight anime if available", anime_list.highlight_anime_if_available}, | 51 {"Highlight anime if available", anime_list.highlight_anime_if_available}, |
89 {"Display highlighted anime above others", anime_list.highlighted_anime_above_others} | 52 {"Display highlighted anime above others", anime_list.highlighted_anime_above_others} |
90 }}, | 53 }}, |
94 {"Username", anilist.username}, | 57 {"Username", anilist.username}, |
95 {"User ID", anilist.user_id} | 58 {"User ID", anilist.user_id} |
96 }} | 59 }} |
97 }}, | 60 }}, |
98 {"Appearance", { | 61 {"Appearance", { |
99 {"Theme", ThemeToString[theme]} | 62 {"Theme", Translate::ToString(theme)} |
100 }} | 63 }} |
101 }; | 64 }; |
102 /* clang-format on */ | 65 /* clang-format on */ |
103 config << std::setw(4) << config_js << std::endl; | 66 config << std::setw(4) << config_js << std::endl; |
104 config.close(); | 67 config.close(); |