Mercurial > minori
annotate src/core/config.cc @ 90:c4bb49c2f6eb
config: improve handling of vars
: )
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Tue, 31 Oct 2023 23:41:53 -0400 |
parents | 9b2b41f83a5e |
children | c537996cf67b |
rev | line source |
---|---|
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" | |
90
c4bb49c2f6eb
config: improve handling of vars
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
9 #include "gui/translate/anime.h" |
c4bb49c2f6eb
config: improve handling of vars
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
10 #include "gui/translate/config.h" |
9 | 11 #include <cstdlib> |
12 #include <cstring> | |
13 #include <filesystem> | |
14 #include <fstream> | |
15 #include <limits.h> | |
16 | |
17 int Config::Load() { | |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
18 Filesystem::Path cfg_path = Filesystem::GetConfigPath(); |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
19 if (!cfg_path.Exists()) |
9 | 20 return 0; |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
21 std::ifstream config(cfg_path.GetPath(), std::ifstream::in); |
11 | 22 auto config_js = nlohmann::json::parse(config); |
90
c4bb49c2f6eb
config: improve handling of vars
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
23 service = Translate::ToService(JSON::GetString(config_js, "/General/Service"_json_pointer, "None")); |
c4bb49c2f6eb
config: improve handling of vars
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
24 anime_list.language = Translate::ToLanguage(JSON::GetString(config_js, "/Anime List/Title language"_json_pointer, "Romaji")); |
c4bb49c2f6eb
config: improve handling of vars
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
25 anime_list.display_aired_episodes = JSON::GetBoolean(config_js, "/Anime List/Display only aired episodes"_json_pointer, true); |
c4bb49c2f6eb
config: improve handling of vars
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
26 anime_list.display_available_episodes = JSON::GetBoolean(config_js, "/Anime List/Display only available episodes in library"_json_pointer, true); |
c4bb49c2f6eb
config: improve handling of vars
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
27 anime_list.highlight_anime_if_available = JSON::GetBoolean(config_js, "/Anime List/Highlight anime if available"_json_pointer, true); |
c4bb49c2f6eb
config: improve handling of vars
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
28 anime_list.highlighted_anime_above_others = JSON::GetBoolean(config_js, "/Anime List/Display highlighted anime above others"_json_pointer); |
9 | 29 anilist.auth_token = JSON::GetString(config_js, "/Authorization/AniList/Auth Token"_json_pointer); |
30 anilist.username = JSON::GetString(config_js, "/Authorization/AniList/Username"_json_pointer); | |
31 anilist.user_id = JSON::GetInt(config_js, "/Authorization/AniList/User ID"_json_pointer); | |
90
c4bb49c2f6eb
config: improve handling of vars
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
32 theme = Translate::ToTheme(JSON::GetString(config_js, "/Appearance/Theme"_json_pointer, "Default")); |
11 | 33 config.close(); |
9 | 34 return 0; |
35 } | |
36 | |
37 int Config::Save() { | |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
38 Filesystem::Path cfg_path = Filesystem::GetConfigPath(); |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
39 if (!cfg_path.GetParent().Exists()) |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
40 cfg_path.GetParent().CreateDirectories(); |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
41 std::ofstream config(cfg_path.GetPath(), std::ofstream::out | std::ofstream::trunc); |
77 | 42 /* clang-format off */ |
9 | 43 nlohmann::json config_js = { |
44 {"General", { | |
90
c4bb49c2f6eb
config: improve handling of vars
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
45 {"Service", Translate::ToString(service)} |
9 | 46 }}, |
47 {"Anime List", { | |
90
c4bb49c2f6eb
config: improve handling of vars
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
48 {"Title language", Translate::ToString(anime_list.language)}, |
9 | 49 {"Display only aired episodes", anime_list.display_aired_episodes}, |
50 {"Display only available episodes in library", anime_list.display_available_episodes}, | |
51 {"Highlight anime if available", anime_list.highlight_anime_if_available}, | |
52 {"Display highlighted anime above others", anime_list.highlighted_anime_above_others} | |
53 }}, | |
54 {"Authorization", { | |
55 {"AniList", { | |
56 {"Auth Token", anilist.auth_token}, | |
57 {"Username", anilist.username}, | |
58 {"User ID", anilist.user_id} | |
59 }} | |
60 }}, | |
61 {"Appearance", { | |
90
c4bb49c2f6eb
config: improve handling of vars
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
62 {"Theme", Translate::ToString(theme)} |
9 | 63 }} |
64 }; | |
77 | 65 /* clang-format on */ |
11 | 66 config << std::setw(4) << config_js << std::endl; |
67 config.close(); | |
9 | 68 return 0; |
69 } |