Mercurial > minori
annotate src/core/config.cc @ 273:f31305b9f60a
*: various code safety changes
this also makes the code build on Qt 5.7. I can't test it though
because I don't have it working... FAIL!
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Thu, 18 Apr 2024 16:53:17 -0400 |
parents | 862d0d8619f6 |
children | ec0a2b5493f8 |
rev | line source |
---|---|
9 | 1 /** |
2 * config.cpp: | |
3 * parses the config... lol | |
4 **/ | |
5 #include "core/config.h" | |
6 #include "core/anime.h" | |
258 | 7 #include "core/filesystem.h" |
102 | 8 #include "core/ini.h" |
9 | 9 #include "core/json.h" |
258 | 10 #include "core/strings.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" |
147
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
13 |
258 | 14 #include "animone/player.h" |
147
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
15 |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
16 #include <algorithm> |
9 | 17 #include <cstdlib> |
18 #include <cstring> | |
19 #include <filesystem> | |
20 #include <fstream> | |
21 #include <limits.h> | |
22 | |
147
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
23 #include <QFile> |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
24 #include <QTextStream> |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
25 |
250 | 26 #include <iostream> |
27 | |
120 | 28 /* I'll use an INI-based config file instead of using an |
221
53211cb1e7f5
library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents:
198
diff
changeset
|
29 * XML file like Taiga. |
224
7ca56c4ac0bc
about: don't abuse QCoreApplication:tr
Paper <mrpapersonic@gmail.com>
parents:
223
diff
changeset
|
30 * |
7ca56c4ac0bc
about: don't abuse QCoreApplication:tr
Paper <mrpapersonic@gmail.com>
parents:
223
diff
changeset
|
31 * It technically isn't to spec, because I'm making these case-sensitive. |
7ca56c4ac0bc
about: don't abuse QCoreApplication:tr
Paper <mrpapersonic@gmail.com>
parents:
223
diff
changeset
|
32 * Boohoo. |
258 | 33 */ |
120 | 34 |
9 | 35 int Config::Load() { |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
36 std::filesystem::path cfg_path = Filesystem::GetConfigPath(); |
102 | 37 |
228
d030b30526d5
config: remove unused username parameter from anilist auth
Paper <mrpapersonic@gmail.com>
parents:
226
diff
changeset
|
38 mINI::INIFile file(cfg_path.u8string()); |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
39 mINI::INIStructure ini; |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
40 file.read(ini); |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
41 |
120 | 42 service = Translate::ToService(INI::GetIniValue<std::string>(ini, "General", "Service", "None")); |
43 | |
258 | 44 anime_list.score_format = |
45 Translate::ToScoreFormat(INI::GetIniValue<std::string>(ini, "Anime List", "Score format", "POINT_100")); | |
46 anime_list.language = | |
47 Translate::ToLanguage(INI::GetIniValue<std::string>(ini, "Anime List", "Title language", "Romaji")); | |
120 | 48 anime_list.display_aired_episodes = INI::GetIniValue<bool>(ini, "Anime List", "Display only aired episodes", true); |
258 | 49 anime_list.display_available_episodes = |
50 INI::GetIniValue<bool>(ini, "Anime List", "Display only available episodes in library", true); | |
51 anime_list.highlight_anime_if_available = | |
52 INI::GetIniValue<bool>(ini, "Anime List", "Highlight anime if available", true); | |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
53 |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
54 if (anime_list.highlight_anime_if_available) // sanity check |
258 | 55 anime_list.highlighted_anime_above_others = |
56 INI::GetIniValue<bool>(ini, "Anime List", "Display highlighted anime above others", false); | |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
57 else |
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
151
diff
changeset
|
58 anime_list.highlighted_anime_above_others = false; |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
108
diff
changeset
|
59 |
120 | 60 auth.anilist.auth_token = INI::GetIniValue<std::string>(ini, "Authentication/AniList", "Auth Token", ""); |
61 auth.anilist.user_id = INI::GetIniValue<int>(ini, "Authentication/AniList", "User ID", 0); | |
62 | |
258 | 63 torrents.feed_link = INI::GetIniValue<std::string>(ini, "Torrents", "RSS feed", |
64 "https://www.tokyotosho.info/rss.php?filter=1,11&zwnj=0"); | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
108
diff
changeset
|
65 |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
66 recognition.detect_media_players = INI::GetIniValue<bool>(ini, "Recognition", "Detect media players", true); |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
67 |
147
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
68 /* lots of dumb logic to import the player data */ |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
69 { |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
70 /* load the player data */ |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
71 QFile f(":/players.anisthesia"); |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
72 if (!f.exists()) |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
73 return false; |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
74 |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
75 f.open(QFile::ReadOnly | QFile::Text); |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
76 QTextStream ts(&f); |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
77 |
258 | 78 std::vector<animone::Player> players; |
147
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
79 |
258 | 80 if (!animone::ParsePlayersData(Strings::ToUtf8String(ts.readAll()), players)) |
147
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
81 return false; |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
82 |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
83 recognition.players.reserve(players.size()); |
185
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
84 for (const auto& player : players) |
151
54744a48a7d7
last commit part 2: struct init with {} is valid syntax, actually
Paper <mrpapersonic@gmail.com>
parents:
150
diff
changeset
|
85 recognition.players.push_back({true, player}); |
147
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
86 } |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
87 |
149
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
88 for (auto& [enabled, player] : recognition.players) { |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
89 switch (player.type) { |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
90 default: |
258 | 91 case animone::PlayerType::Default: |
149
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
92 enabled = INI::GetIniValue<bool>(ini, "Recognition/Players", player.name, true); |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
93 break; |
258 | 94 case animone::PlayerType::WebBrowser: |
149
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
95 enabled = INI::GetIniValue<bool>(ini, "Recognition/Browsers", player.name, true); |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
96 break; |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
97 } |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
98 } |
147
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
99 |
250 | 100 locale.RefreshAvailableLocales(); |
258 | 101 locale.SetActiveLocale( |
102 QLocale(Strings::ToQString(INI::GetIniValue<std::string>(ini, "General", "Locale", "en_US")))); | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
108
diff
changeset
|
103 |
120 | 104 theme.SetTheme(Translate::ToTheme(INI::GetIniValue<std::string>(ini, "Appearance", "Theme", "Default"))); |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
105 |
226 | 106 { |
107 std::vector<std::string> v = Strings::Split(INI::GetIniValue<std::string>(ini, "Library", "Folders", ""), ";"); | |
250 | 108 for (const auto& s : v) |
109 if (!library.paths.count(s)) | |
110 library.paths.insert(s); | |
226 | 111 } |
112 | |
113 library.real_time_monitor = INI::GetIniValue<bool>(ini, "Library", "Real-time monitor", true); | |
223
84e0a3c4737a
library: implement menu bar buttons
Paper <mrpapersonic@gmail.com>
parents:
221
diff
changeset
|
114 |
9 | 115 return 0; |
116 } | |
117 | |
250 | 118 int Config::Save() { |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
119 std::filesystem::path cfg_path = Filesystem::GetConfigPath(); |
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
120 Filesystem::CreateDirectories(cfg_path); |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
121 |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
122 mINI::INIFile file(cfg_path.string()); |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
123 mINI::INIStructure ini; |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
124 |
120 | 125 INI::SetIniValue(ini, "General", "Service", service); |
126 INI::SetIniValue(ini, "General", "Locale", locale.GetLocale().name()); | |
127 | |
185
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
128 INI::SetIniValue(ini, "Anime List", "Score format", Translate::ToString(anime_list.score_format)); |
120 | 129 INI::SetIniValue(ini, "Anime List", "Title language", anime_list.language); |
130 INI::SetIniValue(ini, "Anime List", "Display only aired episodes", anime_list.display_aired_episodes); | |
258 | 131 INI::SetIniValue(ini, "Anime List", "Display only available episodes in library", |
132 anime_list.display_available_episodes); | |
120 | 133 INI::SetIniValue(ini, "Anime List", "Highlight anime if available", anime_list.highlight_anime_if_available); |
258 | 134 INI::SetIniValue(ini, "Anime List", "Display highlighted anime above others", |
135 anime_list.highlighted_anime_above_others); | |
120 | 136 |
137 INI::SetIniValue(ini, "Authentication/AniList", "Auth Token", auth.anilist.auth_token); | |
138 INI::SetIniValue(ini, "Authentication/AniList", "User ID", auth.anilist.user_id); | |
139 | |
140 INI::SetIniValue(ini, "Appearance", "Theme", theme.GetTheme()); | |
141 | |
142 INI::SetIniValue(ini, "Torrents", "RSS feed", torrents.feed_link); | |
143 | |
144 INI::SetIniValue(ini, "Recognition", "Detect media players", recognition.detect_media_players); | |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
145 |
149
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
146 for (const auto& [enabled, player] : recognition.players) { |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
147 switch (player.type) { |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
148 default: |
258 | 149 case animone::PlayerType::Default: INI::SetIniValue(ini, "Recognition/Players", player.name, enabled); break; |
150 case animone::PlayerType::WebBrowser: | |
149
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
151 INI::SetIniValue(ini, "Recognition/Browsers", player.name, enabled); |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
152 break; |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
153 } |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
154 } |
147
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
155 |
223
84e0a3c4737a
library: implement menu bar buttons
Paper <mrpapersonic@gmail.com>
parents:
221
diff
changeset
|
156 INI::SetIniValue(ini, "Library", "Folders", Strings::Implode(library.paths, ";")); |
226 | 157 INI::SetIniValue(ini, "Library", "Real-time monitor", library.real_time_monitor); |
223
84e0a3c4737a
library: implement menu bar buttons
Paper <mrpapersonic@gmail.com>
parents:
221
diff
changeset
|
158 |
102 | 159 file.write(ini); |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
160 |
9 | 161 return 0; |
162 } |