Mercurial > minori
annotate src/core/config.cc @ 114:ab191e28e69d
*: add initial torrent stuff
WOAH!
these checkboxes are a pain in my fucking ass
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Tue, 07 Nov 2023 08:03:42 -0500 |
parents | 2004b41d4a59 |
children | 254b1d2b7096 |
rev | line source |
---|---|
9 | 1 /** |
2 * config.cpp: | |
3 * parses the config... lol | |
4 **/ | |
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 | 7 #include "core/anime.h" |
102 | 8 #include "core/ini.h" |
9 | 9 #include "core/filesystem.h" |
10 #include "core/json.h" | |
90
c4bb49c2f6eb
config: improve handling of vars
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
11 #include "gui/translate/anime.h" |
c4bb49c2f6eb
config: improve handling of vars
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
12 #include "gui/translate/config.h" |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
13 #include <algorithm> |
9 | 14 #include <cstdlib> |
15 #include <cstring> | |
16 #include <filesystem> | |
17 #include <fstream> | |
18 #include <limits.h> | |
19 | |
102 | 20 /* Move these to strings.cc or the translation stuff, please. */ |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
21 static bool string_to_bool(const std::string& s, bool def = false) { |
102 | 22 if (s.length() < 4) |
23 return def; | |
24 std::string l = Strings::ToLower(s); | |
25 if (Strings::BeginningMatchesSubstring(l, "true")) | |
26 return true; | |
27 else if (Strings::BeginningMatchesSubstring(l, "false")) | |
28 return false; | |
29 return def; | |
101
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 |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
32 static std::string bool_to_string(bool b) { |
102 | 33 return b ? "true" : "false"; |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
34 } |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
35 |
9 | 36 int Config::Load() { |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
37 Filesystem::Path cfg_path = Filesystem::GetConfigPath(); |
102 | 38 |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
39 mINI::INIFile file(cfg_path.GetPath()); |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
40 mINI::INIStructure ini; |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
41 file.read(ini); |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
42 |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
43 service = Translate::ToService(ini.get("General").get("Service")); |
108 | 44 locale.SetActiveLocale(QLocale(Strings::ToQString(ini.get("General").get("Locale")))); |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
45 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
|
46 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
|
47 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
|
48 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
|
49 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
|
50 anilist.auth_token = ini.get("AniList").get("Auth Token"); |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
51 anilist.user_id = Strings::ToInt(ini.get("AniList").get("User ID")); |
102 | 52 theme.SetTheme(Translate::ToTheme(ini.get("Appearance").get("Theme"))); |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
53 |
9 | 54 return 0; |
55 } | |
56 | |
57 int Config::Save() { | |
61
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
58 Filesystem::Path cfg_path = Filesystem::GetConfigPath(); |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
59 if (!cfg_path.GetParent().Exists()) |
327568ad9be9
core/fs: finish class-ification of paths
Paper <mrpapersonic@gmail.com>
parents:
15
diff
changeset
|
60 cfg_path.GetParent().CreateDirectories(); |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
61 |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
62 mINI::INIFile file(cfg_path.GetPath()); |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
63 mINI::INIStructure ini; |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
64 |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
65 ini["General"]["Service"] = Translate::ToString(service); |
108 | 66 ini["General"]["Locale"] = Strings::ToUtf8String(locale.GetLocale().name()); |
101
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); |
102 | 74 ini["Appearance"]["Theme"] = Translate::ToString(theme.GetTheme()); |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
75 |
102 | 76 file.write(ini); |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
77 |
9 | 78 return 0; |
79 } |