Mercurial > minori
annotate src/core/config.cpp @ 66:6481c5aed3e1
posters: add poster widget...
why does AniList call these cover images? they're posters...
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Mon, 02 Oct 2023 05:56:32 -0400 |
parents | 327568ad9be9 |
children | 6f7385bd334c |
rev | line source |
---|---|
9 | 1 /** |
2 * config.cpp: | |
3 * parses the config... lol | |
4 **/ | |
5 #include "core/config.h" | |
6 #include "core/anime.h" | |
7 #include "core/filesystem.h" | |
8 #include "core/json.h" | |
9 #include <cstdlib> | |
10 #include <cstring> | |
11 #include <filesystem> | |
12 #include <fstream> | |
13 #include <limits.h> | |
14 | |
15 std::map<std::string, Themes> StringToTheme = { | |
15 | 16 {"Default", Themes::OS }, |
17 {"Light", Themes::LIGHT}, | |
18 {"Dark", Themes::DARK } | |
9 | 19 }; |
20 | |
21 std::map<Themes, std::string> ThemeToString = { | |
15 | 22 {Themes::OS, "Default"}, |
23 {Themes::LIGHT, "Light" }, | |
24 {Themes::DARK, "Dark" } | |
9 | 25 }; |
26 | |
27 std::map<Anime::Services, std::string> ServiceToString{ | |
15 | 28 {Anime::Services::NONE, "None" }, |
29 {Anime::Services::ANILIST, "AniList"} | |
9 | 30 }; |
31 | |
32 std::map<std::string, Anime::Services> StringToService{ | |
15 | 33 {"None", Anime::Services::NONE }, |
34 {"AniList", Anime::Services::ANILIST} | |
9 | 35 }; |
36 | |
37 std::map<Anime::TitleLanguage, std::string> AnimeTitleToStringMap = { | |
15 | 38 {Anime::TitleLanguage::ROMAJI, "Romaji" }, |
39 {Anime::TitleLanguage::NATIVE, "Native" }, | |
40 {Anime::TitleLanguage::ENGLISH, "English"} | |
9 | 41 }; |
42 | |
43 std::map<std::string, Anime::TitleLanguage> StringToAnimeTitleMap = { | |
15 | 44 {"Romaji", Anime::TitleLanguage::ROMAJI }, |
45 {"Native", Anime::TitleLanguage::NATIVE }, | |
46 {"English", Anime::TitleLanguage::ENGLISH} | |
9 | 47 }; |
48 | |
49 int Config::Load() { | |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
50 Filesystem::Path cfg_path = Filesystem::GetConfigPath(); |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
51 if (!cfg_path.Exists()) |
9 | 52 return 0; |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
53 std::ifstream config(cfg_path.GetPath(), std::ifstream::in); |
11 | 54 auto config_js = nlohmann::json::parse(config); |
9 | 55 service = StringToService[JSON::GetString(config_js, "/General/Service"_json_pointer)]; |
56 anime_list.language = StringToAnimeTitleMap[JSON::GetString( | |
15 | 57 config_js, "/Anime List/Display only aired episodes"_json_pointer, "Romaji")]; |
9 | 58 anime_list.display_aired_episodes = |
15 | 59 JSON::GetBoolean(config_js, "/Anime List/Display only aired episodes"_json_pointer, true); |
9 | 60 anime_list.display_available_episodes = |
15 | 61 JSON::GetBoolean(config_js, "/Anime List/Display only available episodes in library"_json_pointer, true); |
9 | 62 anime_list.highlight_anime_if_available = |
15 | 63 JSON::GetBoolean(config_js, "/Anime List/Highlight anime if available"_json_pointer, true); |
9 | 64 anime_list.highlighted_anime_above_others = |
15 | 65 JSON::GetBoolean(config_js, "/Anime List/Display highlighted anime above others"_json_pointer); |
9 | 66 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); | |
68 anilist.user_id = JSON::GetInt(config_js, "/Authorization/AniList/User ID"_json_pointer); | |
69 theme = StringToTheme[JSON::GetString(config_js, "/Appearance/Theme"_json_pointer)]; | |
11 | 70 config.close(); |
9 | 71 return 0; |
72 } | |
73 | |
74 int Config::Save() { | |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
75 Filesystem::Path cfg_path = Filesystem::GetConfigPath(); |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
76 if (!cfg_path.GetParent().Exists()) |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
77 cfg_path.GetParent().CreateDirectories(); |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
78 std::ofstream config(cfg_path.GetPath(), std::ofstream::out | std::ofstream::trunc); |
9 | 79 // clang-format off |
80 nlohmann::json config_js = { | |
81 {"General", { | |
82 {"Service", ServiceToString[service]} | |
83 }}, | |
84 {"Anime List", { | |
85 {"Title language", AnimeTitleToStringMap[anime_list.language]}, | |
86 {"Display only aired episodes", anime_list.display_aired_episodes}, | |
87 {"Display only available episodes in library", anime_list.display_available_episodes}, | |
88 {"Highlight anime if available", anime_list.highlight_anime_if_available}, | |
89 {"Display highlighted anime above others", anime_list.highlighted_anime_above_others} | |
90 }}, | |
91 {"Authorization", { | |
92 {"AniList", { | |
93 {"Auth Token", anilist.auth_token}, | |
94 {"Username", anilist.username}, | |
95 {"User ID", anilist.user_id} | |
96 }} | |
97 }}, | |
98 {"Appearance", { | |
99 {"Theme", ThemeToString[theme]} | |
100 }} | |
101 }; | |
102 // clang-format on | |
11 | 103 config << std::setw(4) << config_js << std::endl; |
104 config.close(); | |
9 | 105 return 0; |
106 } |