Mercurial > minori
annotate src/core/config.cc @ 153:bd439dd6ffc5
*: make win stuff actually work, rename bsd.cc to xnu.cc
It's been OS X only for ages, and these functions are different between
most BSDs anyway
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 15 Nov 2023 13:28:18 -0500 |
parents | 54744a48a7d7 |
children | 01d259b9c89f |
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" |
147
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
13 |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
14 #include "animia/player.h" |
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 |
120 | 26 /* I'll use an INI-based config file instead of using an |
27 XML file like Taiga. */ | |
28 | |
9 | 29 int Config::Load() { |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
30 std::filesystem::path cfg_path = Filesystem::GetConfigPath(); |
102 | 31 |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
32 mINI::INIFile file(cfg_path.string()); |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
33 mINI::INIStructure ini; |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
34 file.read(ini); |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
35 |
120 | 36 service = Translate::ToService(INI::GetIniValue<std::string>(ini, "General", "Service", "None")); |
37 | |
38 anime_list.language = Translate::ToLanguage(INI::GetIniValue<std::string>(ini, "Anime List", "Title language", "Romaji")); | |
39 anime_list.display_aired_episodes = INI::GetIniValue<bool>(ini, "Anime List", "Display only aired episodes", true); | |
40 anime_list.display_available_episodes = INI::GetIniValue<bool>(ini, "Anime List", "Display only available episodes in library", true); | |
41 anime_list.highlight_anime_if_available = INI::GetIniValue<bool>(ini, "Anime List", "Highlight anime if available", true); | |
42 anime_list.highlighted_anime_above_others = INI::GetIniValue<bool>(ini, "Anime List", "Display highlighted anime above others", false); | |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
108
diff
changeset
|
43 |
120 | 44 auth.anilist.auth_token = INI::GetIniValue<std::string>(ini, "Authentication/AniList", "Auth Token", ""); |
45 auth.anilist.user_id = INI::GetIniValue<int>(ini, "Authentication/AniList", "User ID", 0); | |
46 | |
47 torrents.feed_link = INI::GetIniValue<std::string>(ini, "Torrents", "RSS feed", "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
|
48 |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
49 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
|
50 |
147
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
51 /* 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
|
52 { |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
53 /* 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
|
54 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
|
55 if (!f.exists()) |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
56 return false; |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
57 |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
58 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
|
59 QTextStream ts(&f); |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
60 |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
61 std::vector<animia::Player> players; |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
62 |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
63 if (!animia::ParsePlayersData(Strings::ToUtf8String(ts.readAll()), players)) |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
64 return false; |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
65 |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
66 recognition.players.reserve(players.size()); |
150
ffa535b6d630
*: avoid usage of std::[pair,tuple]
Paper <mrpapersonic@gmail.com>
parents:
149
diff
changeset
|
67 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
|
68 recognition.players.push_back({true, player}); |
150
ffa535b6d630
*: avoid usage of std::[pair,tuple]
Paper <mrpapersonic@gmail.com>
parents:
149
diff
changeset
|
69 } |
147
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
70 } |
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
71 |
149
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
72 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
|
73 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
|
74 default: |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
75 case animia::PlayerType::Default: |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
76 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
|
77 break; |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
78 case animia::PlayerType::WebBrowser: |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
79 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
|
80 break; |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
81 } |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
82 } |
147
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
83 |
120 | 84 /* ew */ |
85 locale.SetActiveLocale(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
|
86 |
120 | 87 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
|
88 |
9 | 89 return 0; |
90 } | |
91 | |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
92 int Config::Save() const { |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
93 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
|
94 Filesystem::CreateDirectories(cfg_path); |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
95 |
135
0a458cb26ff4
filesystem: move to using std::filesystem after C++17 switch
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
96 mINI::INIFile file(cfg_path.string()); |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
97 mINI::INIStructure ini; |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
98 |
120 | 99 INI::SetIniValue(ini, "General", "Service", service); |
100 INI::SetIniValue(ini, "General", "Locale", locale.GetLocale().name()); | |
101 | |
102 INI::SetIniValue(ini, "Anime List", "Title language", anime_list.language); | |
103 INI::SetIniValue(ini, "Anime List", "Display only aired episodes", anime_list.display_aired_episodes); | |
104 INI::SetIniValue(ini, "Anime List", "Display only available episodes in library", anime_list.display_available_episodes); | |
105 INI::SetIniValue(ini, "Anime List", "Highlight anime if available", anime_list.highlight_anime_if_available); | |
106 INI::SetIniValue(ini, "Anime List", "Display highlighted anime above others", anime_list.highlighted_anime_above_others); | |
107 | |
108 INI::SetIniValue(ini, "Authentication/AniList", "Auth Token", auth.anilist.auth_token); | |
109 INI::SetIniValue(ini, "Authentication/AniList", "User ID", auth.anilist.user_id); | |
110 | |
111 INI::SetIniValue(ini, "Appearance", "Theme", theme.GetTheme()); | |
112 | |
113 INI::SetIniValue(ini, "Torrents", "RSS feed", torrents.feed_link); | |
114 | |
115 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
|
116 |
149
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
117 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
|
118 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
|
119 default: |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
120 case animia::PlayerType::Default: |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
121 INI::SetIniValue(ini, "Recognition/Players", 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
|
122 break; |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
123 case animia::PlayerType::WebBrowser: |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
124 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
|
125 break; |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
126 } |
e41505d24733
players: filter out web browsers, they aren't even supported in animia yet
Paper <mrpapersonic@gmail.com>
parents:
147
diff
changeset
|
127 } |
147
6fdf0632c003
track: use a bit of a more sane way to manage recognition
Paper <mrpapersonic@gmail.com>
parents:
135
diff
changeset
|
128 |
102 | 129 file.write(ini); |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
90
diff
changeset
|
130 |
9 | 131 return 0; |
132 } |