comparison src/core/config.cc @ 102:b315f3759c56

*: big patch 1. use a wrapper for mINI that enables case sensitivity (personal preference) 2. rename dark_theme.cc to theme.cc and change it to be a class 3. include the "dep" folder so we don't have stupidity in json.h or ini.h 4. I think the graph was also tweaked a lot in this, nothing is constexpr and size is found at runtime...
author Paper <mrpapersonic@gmail.com>
date Fri, 03 Nov 2023 21:32:52 -0400
parents c537996cf67b
children 2004b41d4a59
comparison
equal deleted inserted replaced
101:c537996cf67b 102:b315f3759c56
3 * parses the config... lol 3 * parses the config... lol
4 **/ 4 **/
5 #include "core/config.h" 5 #include "core/config.h"
6 #include "core/strings.h" 6 #include "core/strings.h"
7 #include "core/anime.h" 7 #include "core/anime.h"
8 #include "core/ini.h"
8 #include "core/filesystem.h" 9 #include "core/filesystem.h"
9 #include "core/json.h" 10 #include "core/json.h"
10 #include "gui/translate/anime.h" 11 #include "gui/translate/anime.h"
11 #include "gui/translate/config.h" 12 #include "gui/translate/config.h"
12 #include "ini.h" // mINI
13 #include <algorithm> 13 #include <algorithm>
14 #include <cstdlib> 14 #include <cstdlib>
15 #include <cstring> 15 #include <cstring>
16 #include <filesystem> 16 #include <filesystem>
17 #include <fstream> 17 #include <fstream>
18 #include <limits.h> 18 #include <limits.h>
19 19
20 /* I'm not exactly fond of using JSON for a config file, but it's better than 20 /* Move these to strings.cc or the translation stuff, please. */
21 no config I guess. I'd like to have something more readable, e.g. YAML or
22 even INI. */
23
24 static bool string_to_bool(const std::string& s, bool def = false) { 21 static bool string_to_bool(const std::string& s, bool def = false) {
25 bool b; 22 if (s.length() < 4)
26 std::istringstream is(Strings::ToLower(s)); 23 return def;
27 is >> std::boolalpha >> b; 24 std::string l = Strings::ToLower(s);
28 return b; 25 if (Strings::BeginningMatchesSubstring(l, "true"))
26 return true;
27 else if (Strings::BeginningMatchesSubstring(l, "false"))
28 return false;
29 return def;
29 } 30 }
30 31
31 static std::string bool_to_string(bool b) { 32 static std::string bool_to_string(bool b) {
32 std::ostringstream stream; 33 return b ? "true" : "false";
33 stream << std::boolalpha << b;
34 return stream.str();
35 } 34 }
36 35
37 int Config::Load() { 36 int Config::Load() {
38 Filesystem::Path cfg_path = Filesystem::GetConfigPath(); 37 Filesystem::Path cfg_path = Filesystem::GetConfigPath();
39 if (!cfg_path.Exists()) 38
40 return 0;
41 mINI::INIFile file(cfg_path.GetPath()); 39 mINI::INIFile file(cfg_path.GetPath());
42 mINI::INIStructure ini; 40 mINI::INIStructure ini;
43 file.read(ini); 41 file.read(ini);
44 42
45 service = Translate::ToService(ini.get("General").get("Service")); 43 service = Translate::ToService(ini.get("General").get("Service"));
48 anime_list.display_available_episodes = string_to_bool(ini.get("Anime List").get("Display only available episodes in library"), true); 46 anime_list.display_available_episodes = string_to_bool(ini.get("Anime List").get("Display only available episodes in library"), true);
49 anime_list.highlight_anime_if_available = string_to_bool(ini.get("Anime List").get("Highlight anime if available"), true); 47 anime_list.highlight_anime_if_available = string_to_bool(ini.get("Anime List").get("Highlight anime if available"), true);
50 anime_list.highlighted_anime_above_others = string_to_bool(ini.get("Anime List").get("Display highlighted anime above others")); 48 anime_list.highlighted_anime_above_others = string_to_bool(ini.get("Anime List").get("Display highlighted anime above others"));
51 anilist.auth_token = ini.get("AniList").get("Auth Token"); 49 anilist.auth_token = ini.get("AniList").get("Auth Token");
52 anilist.user_id = Strings::ToInt(ini.get("AniList").get("User ID")); 50 anilist.user_id = Strings::ToInt(ini.get("AniList").get("User ID"));
53 theme = Translate::ToTheme(ini.get("Appearance").get("Theme")); 51 theme.SetTheme(Translate::ToTheme(ini.get("Appearance").get("Theme")));
54 52
55 return 0; 53 return 0;
56 } 54 }
57 55
58 int Config::Save() { 56 int Config::Save() {
69 ini["Anime List"]["Display only available episodes in library"] = bool_to_string(anime_list.display_available_episodes); 67 ini["Anime List"]["Display only available episodes in library"] = bool_to_string(anime_list.display_available_episodes);
70 ini["Anime List"]["Highlight anime if available"] = bool_to_string(anime_list.highlight_anime_if_available); 68 ini["Anime List"]["Highlight anime if available"] = bool_to_string(anime_list.highlight_anime_if_available);
71 ini["Anime List"]["Display highlighted anime above others"] = bool_to_string(anime_list.highlighted_anime_above_others); 69 ini["Anime List"]["Display highlighted anime above others"] = bool_to_string(anime_list.highlighted_anime_above_others);
72 ini["AniList"]["Auth Token"] = anilist.auth_token; 70 ini["AniList"]["Auth Token"] = anilist.auth_token;
73 ini["AniList"]["User ID"] = std::to_string(anilist.user_id); 71 ini["AniList"]["User ID"] = std::to_string(anilist.user_id);
74 ini["Appearance"]["Theme"] = Translate::ToString(theme); 72 ini["Appearance"]["Theme"] = Translate::ToString(theme.GetTheme());
75 73
76 file.generate(ini); 74 file.write(ini);
77 75
78 return 0; 76 return 0;
79 } 77 }