annotate src/core/config.cc @ 101:c537996cf67b

*: multitude of config changes 1. theme is now configurable from the settings menu (but you have to restart for it to apply) 2. config is now stored in an INI file, with no method of conversion from json (this repo is private-ish anyway)
author Paper <mrpapersonic@gmail.com>
date Fri, 03 Nov 2023 14:06:02 -0400
parents c4bb49c2f6eb
children b315f3759c56
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 /**
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 * config.cpp:
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3 * parses the config... lol
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 **/
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 #include "core/config.h"
101
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
6 #include "core/strings.h"
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 #include "core/anime.h"
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 #include "core/filesystem.h"
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 #include "core/json.h"
90
c4bb49c2f6eb config: improve handling of vars
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
10 #include "gui/translate/anime.h"
c4bb49c2f6eb config: improve handling of vars
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
11 #include "gui/translate/config.h"
101
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
12 #include "ini.h" // mINI
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
13 #include <algorithm>
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 #include <cstdlib>
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 #include <cstring>
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 #include <filesystem>
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 #include <fstream>
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18 #include <limits.h>
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19
101
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
20 /* I'm not exactly fond of using JSON for a config file, but it's better than
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
21 no config I guess. I'd like to have something more readable, e.g. YAML or
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
22 even INI. */
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
23
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
24 static bool string_to_bool(const std::string& s, bool def = false) {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
25 bool b;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
26 std::istringstream is(Strings::ToLower(s));
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
27 is >> std::boolalpha >> b;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
28 return b;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
29 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
30
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
31 static std::string bool_to_string(bool b) {
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
32 std::ostringstream stream;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
33 stream << std::boolalpha << b;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
34 return stream.str();
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
35 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
36
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 int Config::Load() {
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.Exists())
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 return 0;
101
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
41 mINI::INIFile file(cfg_path.GetPath());
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
42 mINI::INIStructure ini;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
43 file.read(ini);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
44
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
45 service = Translate::ToService(ini.get("General").get("Service"));
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
46 anime_list.language = Translate::ToLanguage(ini.get("Anime List").get("Title language"));
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
47 anime_list.display_aired_episodes = string_to_bool(ini.get("Anime List").get("Display only aired episodes"), true);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
48 anime_list.display_available_episodes = string_to_bool(ini.get("Anime List").get("Display only available episodes in library"), true);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
49 anime_list.highlight_anime_if_available = string_to_bool(ini.get("Anime List").get("Highlight anime if available"), true);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
50 anime_list.highlighted_anime_above_others = string_to_bool(ini.get("Anime List").get("Display highlighted anime above others"));
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
51 anilist.auth_token = ini.get("AniList").get("Auth Token");
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
52 anilist.user_id = Strings::ToInt(ini.get("AniList").get("User ID"));
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
53 theme = Translate::ToTheme(ini.get("Appearance").get("Theme"));
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
54
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 return 0;
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58 int Config::Save() {
61
327568ad9be9 core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
59 Filesystem::Path cfg_path = Filesystem::GetConfigPath();
327568ad9be9 core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
60 if (!cfg_path.GetParent().Exists())
327568ad9be9 core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
61 cfg_path.GetParent().CreateDirectories();
101
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
62
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
63 mINI::INIFile file(cfg_path.GetPath());
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
64 mINI::INIStructure ini;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
65
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
66 ini["General"]["Service"] = Translate::ToString(service);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
67 ini["Anime List"]["Title language"] = Translate::ToString(anime_list.language);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
68 ini["Anime List"]["Display only aired episodes"] = bool_to_string(anime_list.display_aired_episodes);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
69 ini["Anime List"]["Display only available episodes in library"] = bool_to_string(anime_list.display_available_episodes);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
70 ini["Anime List"]["Highlight anime if available"] = bool_to_string(anime_list.highlight_anime_if_available);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
71 ini["Anime List"]["Display highlighted anime above others"] = bool_to_string(anime_list.highlighted_anime_above_others);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
72 ini["AniList"]["Auth Token"] = anilist.auth_token;
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
73 ini["AniList"]["User ID"] = std::to_string(anilist.user_id);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
74 ini["Appearance"]["Theme"] = Translate::ToString(theme);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
75
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
76 file.generate(ini);
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 90
diff changeset
77
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78 return 0;
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
79 }