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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
1 /**
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
2 * config.cpp:
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
3 * parses the config
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
4 *
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
5 * much of this is similar to the code used in
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
6 * wgsdk...
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
7 * maybe some of this will be C++-ified someday ;)
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
8 **/
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
9 #include <filesystem> /* Sorry, C++17 is just sexy. if you have boost you can probably change this */
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
10 #ifdef MACOSX
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
11 #include <NSSystemDirectories.h>
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
12 #endif
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
13 #include <limits.h>
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
14 #include <cstdlib>
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
15 #include <cstring>
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
16 #include <fstream>
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
17 #include "json.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
18 #include "config.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
19 #include "window.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
20 #include "filesystem.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
21
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
22 std::map<std::string, enum Themes> StringToTheme = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
23 {"Default", OS},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
24 {"Light", LIGHT},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
25 {"Dark", DARK}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
26 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
27
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
28 std::map<enum Themes, std::string> ThemeToString = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
29 {OS, "Default"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
30 {LIGHT, "Light"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
31 {DARK, "Dark"}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
32 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
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
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
44 std::map<enum AnimeTitleLanguage, std::string> AnimeTitleToStringMap = {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
45 {ROMAJI, "Romaji"},
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
46 {NATIVE, "Native"},
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
47 {ENGLISH, "English"}
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
48 };
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
49
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
50 std::map<std::string, enum AnimeTitleLanguage> StringToAnimeTitleMap = {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
51 {"Romaji", ROMAJI},
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
52 {"Native", NATIVE},
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
53 {"English", ENGLISH}
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
54 };
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
55
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
56 int Config::Load() {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
57 std::filesystem::path cfg_path = get_config_path();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
58 if (!std::filesystem::exists(cfg_path))
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
59 return 0;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
60 std::ifstream config_in(cfg_path.string().c_str(), std::ifstream::in);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
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
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
63 anime_list.language = StringToAnimeTitleMap[JSON::GetString(config_js, "/Anime List/Display only aired episodes"_json_pointer, "Romaji")];
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
64 anime_list.display_aired_episodes = JSON::GetBoolean(config_js, "/Anime List/Display only aired episodes"_json_pointer, true);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
65 anime_list.display_available_episodes = JSON::GetBoolean(config_js, "/Anime List/Display only available episodes in library"_json_pointer, true);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
66 anime_list.highlight_anime_if_available = JSON::GetBoolean(config_js, "/Anime List/Highlight anime if available"_json_pointer, true);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
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
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
72 config_in.close();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
73 return 0;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
74 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
75
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
76 int Config::Save() {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
77 std::filesystem::path cfg_path = get_config_path();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
78 if (!std::filesystem::exists(cfg_path.parent_path()))
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
79 std::filesystem::create_directories(cfg_path.parent_path());
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
80 std::ofstream config_out(cfg_path.string().c_str(), std::ofstream::out | std::ofstream::trunc);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
81 nlohmann::json config_js = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
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
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
84 }},
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
85 {"Anime List", {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
86 {"Title language", AnimeTitleToStringMap[anime_list.language]},
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
87 {"Display only aired episodes", anime_list.display_aired_episodes},
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
88 {"Display only available episodes in library", anime_list.display_available_episodes},
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
89 {"Highlight anime if available", anime_list.highlight_anime_if_available},
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
90 {"Display highlighted anime above others", anime_list.highlighted_anime_above_others}
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
91 }},
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
92 {"Authorization", {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
93 {"AniList", {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
94 {"Auth Token", anilist.auth_token},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
95 {"Username", anilist.username},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
96 {"User ID", anilist.user_id}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
97 }}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
98 }},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
99 {"Appearance", {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
100 {"Theme", ThemeToString[theme]}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
101 }}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
102 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
103 config_out << std::setw(4) << config_js << std::endl;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
104 config_out.close();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
105 return 0;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
106 }