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 = {
|
|
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
|
|
49 int Config::Load() {
|
11
|
50 std::string cfg_path = Filesystem::GetConfigPath();
|
|
51 if (!Filesystem::Exists(cfg_path))
|
9
|
52 return 0;
|
11
|
53 std::ifstream config(cfg_path.c_str(), std::ifstream::in);
|
|
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(
|
|
57 config_js, "/Anime List/Display only aired episodes"_json_pointer, "Romaji")];
|
|
58 anime_list.display_aired_episodes =
|
|
59 JSON::GetBoolean(config_js, "/Anime List/Display only aired episodes"_json_pointer, true);
|
|
60 anime_list.display_available_episodes =
|
|
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);
|
|
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() {
|
11
|
75 std::string cfg_path = Filesystem::GetConfigPath();
|
|
76 if (!Filesystem::Exists(Filesystem::GetParentDirectory(cfg_path)))
|
|
77 Filesystem::CreateDirectories(Filesystem::GetParentDirectory(cfg_path));
|
|
78 std::ofstream config(cfg_path.c_str(), 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 }
|