comparison src/config.cpp @ 6:1d82f6e04d7d

Update: add first parts to the settings dialog
author Paper <mrpapersonic@gmail.com>
date Wed, 16 Aug 2023 00:49:17 -0400
parents 23d0d9319a00
children 07a9095eaeed
comparison
equal deleted inserted replaced
5:51ae25154b70 6:1d82f6e04d7d
29 {OS, "Default"}, 29 {OS, "Default"},
30 {LIGHT, "Light"}, 30 {LIGHT, "Light"},
31 {DARK, "Dark"} 31 {DARK, "Dark"}
32 }; 32 };
33 33
34 std::map<enum AnimeListServices, std::string> ServiceToString {
35 {NONE, "None"},
36 {ANILIST, "AniList"}
37 };
38
39 std::map<std::string, enum AnimeListServices> StringToService {
40 {"None", NONE},
41 {"AniList", ANILIST}
42 };
43
34 int Config::Load() { 44 int Config::Load() {
35 std::filesystem::path cfg_path = get_config_path(); 45 std::filesystem::path cfg_path = get_config_path();
36 if (!std::filesystem::exists(cfg_path)) 46 if (!std::filesystem::exists(cfg_path))
37 return 0; 47 return 0;
38 std::ifstream config_in(cfg_path.string().c_str(), std::ifstream::in); 48 std::ifstream config_in(cfg_path.string().c_str(), std::ifstream::in);
39 auto config_js = nlohmann::json::parse(config_in); 49 auto config_js = nlohmann::json::parse(config_in);
40 /* this macro will make it easier to edit these in the future, if needed */ 50 service = StringToService[JSON::GetString(config_js, "/General/Service"_json_pointer)];
41 #define GET_CONFIG_VALUE(pointer, location, struct, default) \ 51 anilist.auth_token = JSON::GetString(config_js, "/Authorization/AniList/Auth Token"_json_pointer);
42 struct = (config_js.contains(pointer)) ? (location) : (default) 52 anilist.username = JSON::GetString(config_js, "/Authorization/AniList/Username"_json_pointer);
43 GET_CONFIG_VALUE("/General/Service"_json_pointer, (enum AnimeListServices)config_js["General"]["Service"].get<int>(), service, NONE); 53 anilist.user_id = JSON::GetInt(config_js, "/Authorization/AniList/User ID"_json_pointer);
44 GET_CONFIG_VALUE("/Authorization/AniList/Auth Token"_json_pointer, config_js["Authorization"]["AniList"]["Auth Token"].get<std::string>(), anilist.auth_token, ""); 54 theme = StringToTheme[JSON::GetString(config_js, "/Appearance/Theme"_json_pointer)];
45 GET_CONFIG_VALUE("/Authorization/AniList/Username"_json_pointer, config_js["Authorization"]["AniList"]["Username"].get<std::string>(), anilist.username, "");
46 GET_CONFIG_VALUE("/Authorization/AniList/User ID"_json_pointer, config_js["Authorization"]["AniList"]["User ID"].get<int>(), anilist.user_id, 0);
47 GET_CONFIG_VALUE("/Appearance/Theme"_json_pointer, StringToTheme[config_js["Appearance"]["Theme"].get<std::string>()], theme, OS);
48 #undef GET_CONFIG_VALUE
49 config_in.close(); 55 config_in.close();
50 return 0; 56 return 0;
51 } 57 }
52 58
53 int Config::Save() { 59 int Config::Save() {
55 if (!std::filesystem::exists(cfg_path.parent_path())) 61 if (!std::filesystem::exists(cfg_path.parent_path()))
56 std::filesystem::create_directories(cfg_path.parent_path()); 62 std::filesystem::create_directories(cfg_path.parent_path());
57 std::ofstream config_out(cfg_path.string().c_str(), std::ofstream::out | std::ofstream::trunc); 63 std::ofstream config_out(cfg_path.string().c_str(), std::ofstream::out | std::ofstream::trunc);
58 nlohmann::json config_js = { 64 nlohmann::json config_js = {
59 {"General", { 65 {"General", {
60 {"Service", service} 66 {"Service", ServiceToString[service]}
61 }}, 67 }},
62 {"Authorization", { 68 {"Authorization", {
63 {"AniList", { 69 {"AniList", {
64 {"Auth Token", anilist.auth_token}, 70 {"Auth Token", anilist.auth_token},
65 {"Username", anilist.username}, 71 {"Username", anilist.username},