comparison src/core/config.cc @ 81:9b2b41f83a5e

boring: mass rename to cc because this is a very unix-y project, it makes more sense to use the 'cc' extension
author Paper <mrpapersonic@gmail.com>
date Mon, 23 Oct 2023 12:07:27 -0400
parents src/core/config.cpp@6f7385bd334c
children c4bb49c2f6eb
comparison
equal deleted inserted replaced
80:825506f0e221 81:9b2b41f83a5e
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 Filesystem::Path cfg_path = Filesystem::GetConfigPath();
51 if (!cfg_path.Exists())
52 return 0;
53 std::ifstream config(cfg_path.GetPath(), std::ifstream::in);
54 auto config_js = nlohmann::json::parse(config);
55 service = StringToService[JSON::GetString(config_js, "/General/Service"_json_pointer)];
56 anime_list.language =
57 StringToAnimeTitleMap[JSON::GetString(config_js, "/Anime List/Title language"_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.close();
71 return 0;
72 }
73
74 int Config::Save() {
75 Filesystem::Path cfg_path = Filesystem::GetConfigPath();
76 if (!cfg_path.GetParent().Exists())
77 cfg_path.GetParent().CreateDirectories();
78 std::ofstream config(cfg_path.GetPath(), 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 << std::setw(4) << config_js << std::endl;
104 config.close();
105 return 0;
106 }