Mercurial > minori
annotate 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 |
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 |
2 | 44 int Config::Load() { |
45 std::filesystem::path cfg_path = get_config_path(); | |
46 if (!std::filesystem::exists(cfg_path)) | |
47 return 0; | |
48 std::ifstream config_in(cfg_path.string().c_str(), std::ifstream::in); | |
49 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
|
50 service = StringToService[JSON::GetString(config_js, "/General/Service"_json_pointer)]; |
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
51 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
|
52 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
|
53 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
|
54 theme = StringToTheme[JSON::GetString(config_js, "/Appearance/Theme"_json_pointer)]; |
2 | 55 config_in.close(); |
56 return 0; | |
57 } | |
58 | |
59 int Config::Save() { | |
60 std::filesystem::path cfg_path = get_config_path(); | |
61 if (!std::filesystem::exists(cfg_path.parent_path())) | |
62 std::filesystem::create_directories(cfg_path.parent_path()); | |
63 std::ofstream config_out(cfg_path.string().c_str(), std::ofstream::out | std::ofstream::trunc); | |
64 nlohmann::json config_js = { | |
65 {"General", { | |
6
1d82f6e04d7d
Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents:
2
diff
changeset
|
66 {"Service", ServiceToString[service]} |
2 | 67 }}, |
68 {"Authorization", { | |
69 {"AniList", { | |
70 {"Auth Token", anilist.auth_token}, | |
71 {"Username", anilist.username}, | |
72 {"User ID", anilist.user_id} | |
73 }} | |
74 }}, | |
75 {"Appearance", { | |
76 {"Theme", ThemeToString[theme]} | |
77 }} | |
78 }; | |
79 config_out << std::setw(4) << config_js << std::endl; | |
80 config_out.close(); | |
81 return 0; | |
82 } |