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
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
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
44 int Config::Load() {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
45 std::filesystem::path cfg_path = get_config_path();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
46 if (!std::filesystem::exists(cfg_path))
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
47 return 0;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
48 std::ifstream config_in(cfg_path.string().c_str(), std::ifstream::in);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
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
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
55 config_in.close();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
56 return 0;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
57 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
58
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
59 int Config::Save() {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
60 std::filesystem::path cfg_path = get_config_path();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
61 if (!std::filesystem::exists(cfg_path.parent_path()))
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
62 std::filesystem::create_directories(cfg_path.parent_path());
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
63 std::ofstream config_out(cfg_path.string().c_str(), std::ofstream::out | std::ofstream::trunc);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
64 nlohmann::json config_js = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
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
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
67 }},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
68 {"Authorization", {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
69 {"AniList", {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
70 {"Auth Token", anilist.auth_token},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
71 {"Username", anilist.username},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
72 {"User ID", anilist.user_id}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
73 }}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
74 }},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
75 {"Appearance", {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
76 {"Theme", ThemeToString[theme]}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
77 }}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
78 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
79 config_out << std::setw(4) << config_js << std::endl;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
80 config_out.close();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
81 return 0;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
82 }