comparison src/core/config.cpp @ 9:5c0397762b53

INCOMPLETE: megacommit :)
author Paper <mrpapersonic@gmail.com>
date Sun, 10 Sep 2023 03:59:16 -0400
parents
children fc1bf97c528b
comparison
equal deleted inserted replaced
8:b1f73678ef61 9:5c0397762b53
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() {
50 std::filesystem::path cfg_path = get_config_path();
51 if (!std::filesystem::exists(cfg_path))
52 return 0;
53 std::ifstream config_in(cfg_path.string().c_str(), std::ifstream::in);
54 auto config_js = nlohmann::json::parse(config_in);
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)];
70 config_in.close();
71 return 0;
72 }
73
74 int Config::Save() {
75 std::filesystem::path cfg_path = get_config_path();
76 if (!std::filesystem::exists(cfg_path.parent_path()))
77 std::filesystem::create_directories(cfg_path.parent_path());
78 std::ofstream config_out(cfg_path.string().c_str(), std::ofstream::out | std::ofstream::trunc);
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
103 config_out << std::setw(4) << config_js << std::endl;
104 config_out.close();
105 return 0;
106 }