Mercurial > minori
annotate src/config.cpp @ 7:07a9095eaeed
Update
Refactored some code, moved some around
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Thu, 24 Aug 2023 23:11:38 -0400 |
parents | 1d82f6e04d7d |
children |
rev | line source |
---|---|
2 | 1 /** |
2 * config.cpp: | |
3 * parses the config | |
4 * | |
5 * much of this is similar to the code used in | |
6 * wgsdk... | |
7 * maybe some of this will be C++-ified someday ;) | |
8 **/ | |
9 #include <filesystem> /* Sorry, C++17 is just sexy. if you have boost you can probably change this */ | |
10 #ifdef MACOSX | |
11 #include <NSSystemDirectories.h> | |
12 #endif | |
13 #include <limits.h> | |
14 #include <cstdlib> | |
15 #include <cstring> | |
16 #include <fstream> | |
17 #include "json.h" | |
18 #include "config.h" | |
19 #include "window.h" | |
20 #include "filesystem.h" | |
21 | |
22 std::map<std::string, enum Themes> StringToTheme = { | |
23 {"Default", OS}, | |
24 {"Light", LIGHT}, | |
25 {"Dark", DARK} | |
26 }; | |
27 | |
28 std::map<enum Themes, std::string> ThemeToString = { | |
29 {OS, "Default"}, | |
30 {LIGHT, "Light"}, | |
31 {DARK, "Dark"} | |
32 }; | |
33 | |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
34 std::map<enum AnimeListServices, std::string> ServiceToString { |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
35 {NONE, "None"}, |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
36 {ANILIST, "AniList"} |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
37 }; |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
38 |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
39 std::map<std::string, enum AnimeListServices> StringToService { |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
40 {"None", NONE}, |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
41 {"AniList", ANILIST} |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
42 }; |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
43 |
7 | 44 std::map<enum AnimeTitleLanguage, std::string> AnimeTitleToStringMap = { |
45 {ROMAJI, "Romaji"}, | |
46 {NATIVE, "Native"}, | |
47 {ENGLISH, "English"} | |
48 }; | |
49 | |
50 std::map<std::string, enum AnimeTitleLanguage> StringToAnimeTitleMap = { | |
51 {"Romaji", ROMAJI}, | |
52 {"Native", NATIVE}, | |
53 {"English", ENGLISH} | |
54 }; | |
55 | |
2 | 56 int Config::Load() { |
57 std::filesystem::path cfg_path = get_config_path(); | |
58 if (!std::filesystem::exists(cfg_path)) | |
59 return 0; | |
60 std::ifstream config_in(cfg_path.string().c_str(), std::ifstream::in); | |
61 auto config_js = nlohmann::json::parse(config_in); | |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
62 service = StringToService[JSON::GetString(config_js, "/General/Service"_json_pointer)]; |
7 | 63 anime_list.language = StringToAnimeTitleMap[JSON::GetString(config_js, "/Anime List/Display only aired episodes"_json_pointer, "Romaji")]; |
64 anime_list.display_aired_episodes = JSON::GetBoolean(config_js, "/Anime List/Display only aired episodes"_json_pointer, true); | |
65 anime_list.display_available_episodes = JSON::GetBoolean(config_js, "/Anime List/Display only available episodes in library"_json_pointer, true); | |
66 anime_list.highlight_anime_if_available = JSON::GetBoolean(config_js, "/Anime List/Highlight anime if available"_json_pointer, true); | |
67 anime_list.highlighted_anime_above_others = JSON::GetBoolean(config_js, "/Anime List/Display highlighted anime above others"_json_pointer); | |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
68 anilist.auth_token = JSON::GetString(config_js, "/Authorization/AniList/Auth Token"_json_pointer); |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
69 anilist.username = JSON::GetString(config_js, "/Authorization/AniList/Username"_json_pointer); |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
70 anilist.user_id = JSON::GetInt(config_js, "/Authorization/AniList/User ID"_json_pointer); |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
71 theme = StringToTheme[JSON::GetString(config_js, "/Appearance/Theme"_json_pointer)]; |
2 | 72 config_in.close(); |
73 return 0; | |
74 } | |
75 | |
76 int Config::Save() { | |
77 std::filesystem::path cfg_path = get_config_path(); | |
78 if (!std::filesystem::exists(cfg_path.parent_path())) | |
79 std::filesystem::create_directories(cfg_path.parent_path()); | |
80 std::ofstream config_out(cfg_path.string().c_str(), std::ofstream::out | std::ofstream::trunc); | |
81 nlohmann::json config_js = { | |
82 {"General", { | |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
83 {"Service", ServiceToString[service]} |
2 | 84 }}, |
7 | 85 {"Anime List", { |
86 {"Title language", AnimeTitleToStringMap[anime_list.language]}, | |
87 {"Display only aired episodes", anime_list.display_aired_episodes}, | |
88 {"Display only available episodes in library", anime_list.display_available_episodes}, | |
89 {"Highlight anime if available", anime_list.highlight_anime_if_available}, | |
90 {"Display highlighted anime above others", anime_list.highlighted_anime_above_others} | |
91 }}, | |
2 | 92 {"Authorization", { |
93 {"AniList", { | |
94 {"Auth Token", anilist.auth_token}, | |
95 {"Username", anilist.username}, | |
96 {"User ID", anilist.user_id} | |
97 }} | |
98 }}, | |
99 {"Appearance", { | |
100 {"Theme", ThemeToString[theme]} | |
101 }} | |
102 }; | |
103 config_out << std::setw(4) << config_js << std::endl; | |
104 config_out.close(); | |
105 return 0; | |
106 } |