comparison src/core/config.cc @ 369:47c9f8502269

*: clang-format all the things I've edited the formatting a bit. Now pointer asterisks (and reference ampersands) are on the variable instead of the type, as well as having newlines for function braces (but nothing else)
author Paper <paper@tflc.us>
date Fri, 25 Jul 2025 10:16:02 -0400
parents a7d4e5107531
children
comparison
equal deleted inserted replaced
368:6d37a998cf91 369:47c9f8502269
23 #include <iostream> 23 #include <iostream>
24 24
25 /* NOTE: This config file is prone to breakage, as Minori is alpha software and 25 /* NOTE: This config file is prone to breakage, as Minori is alpha software and
26 * as such nothing is constant. */ 26 * as such nothing is constant. */
27 27
28 int Config::Load() { 28 int Config::Load()
29 {
29 std::filesystem::path cfg_path = Filesystem::GetConfigPath(); 30 std::filesystem::path cfg_path = Filesystem::GetConfigPath();
30 31
31 std::ifstream ifs(cfg_path, std::ios_base::binary); 32 std::ifstream ifs(cfg_path, std::ios_base::binary);
32 if (!ifs.good()) 33 if (!ifs.good())
33 return 0; 34 return 0;
34 35
35 toml::value data; 36 toml::value data;
36 37
37 try { 38 try {
38 data = toml::parse(ifs); 39 data = toml::parse(ifs);
39 } catch (const std::exception& ex) { 40 } catch (const std::exception &ex) {
40 std::cerr << "config: failed to parse toml with error " << ex.what() << std::endl; 41 std::cerr << "config: failed to parse toml with error " << ex.what() << std::endl;
41 return 0; 42 return 0;
42 } 43 }
43 44
44 service = Translate::ToService(toml::find_or(data, "General", "Service", "None")); 45 service = Translate::ToService(toml::find_or(data, "General", "Service", "None"));
45 locale.RefreshAvailableLocales(); 46 locale.RefreshAvailableLocales();
46 locale.SetActiveLocale(QLocale(Strings::ToQString(toml::find_or(data, "General", "Locale", "en_US")))); 47 locale.SetActiveLocale(QLocale(Strings::ToQString(toml::find_or(data, "General", "Locale", "en_US"))));
47 48
48
49 anime_list.score_format = Translate::ToScoreFormat(toml::find_or(data, "Anime List", "Score format", "100-point")); 49 anime_list.score_format = Translate::ToScoreFormat(toml::find_or(data, "Anime List", "Score format", "100-point"));
50 anime_list.language = Translate::ToLanguage(toml::find_or(data, "Anime List", "Title language", "Romaji")); 50 anime_list.language = Translate::ToLanguage(toml::find_or(data, "Anime List", "Title language", "Romaji"));
51 anime_list.display_aired_episodes = toml::find_or(data, "Anime List", "Display only aired episodes", true); 51 anime_list.display_aired_episodes = toml::find_or(data, "Anime List", "Display only aired episodes", true);
52 anime_list.display_available_episodes = toml::find_or(data, "Anime List", "Display only available episodes in library", true); 52 anime_list.display_available_episodes =
53 toml::find_or(data, "Anime List", "Display only available episodes in library", true);
53 anime_list.highlight_anime_if_available = toml::find_or(data, "Anime List", "Highlight anime if available", true); 54 anime_list.highlight_anime_if_available = toml::find_or(data, "Anime List", "Highlight anime if available", true);
54 anime_list.highlighted_anime_above_others = 55 anime_list.highlighted_anime_above_others =
55 (anime_list.highlight_anime_if_available) 56 (anime_list.highlight_anime_if_available)
56 ? toml::find_or(data, "Anime List", "Display highlighted anime above others", false) 57 ? toml::find_or(data, "Anime List", "Display highlighted anime above others", false)
57 : false; 58 : false;
58 59
59 auth.anilist.auth_token = toml::find_or(data, "Authentication/AniList", "Auth Token", ""); 60 auth.anilist.auth_token = toml::find_or(data, "Authentication/AniList", "Auth Token", "");
60 auth.anilist.user_id = toml::find_or(data, "Authentication/AniList", "User ID", 0); 61 auth.anilist.user_id = toml::find_or(data, "Authentication/AniList", "User ID", 0);
61 62
62 auth.kitsu.access_token = toml::find_or(data, "Authentication/Kitsu", "Access Token", ""); 63 auth.kitsu.access_token = toml::find_or(data, "Authentication/Kitsu", "Access Token", "");
63 auth.kitsu.access_token_expiration = toml::find_or(data, "Authentication/Kitsu", "Access Token Expiration", static_cast<Time::Timestamp>(0)); 64 auth.kitsu.access_token_expiration =
65 toml::find_or(data, "Authentication/Kitsu", "Access Token Expiration", static_cast<Time::Timestamp>(0));
64 auth.kitsu.refresh_token = toml::find_or(data, "Authentication/Kitsu", "Refresh Token", ""); 66 auth.kitsu.refresh_token = toml::find_or(data, "Authentication/Kitsu", "Refresh Token", "");
65 auth.kitsu.user_id = toml::find_or(data, "Authentication/Kitsu", "User ID", ""); 67 auth.kitsu.user_id = toml::find_or(data, "Authentication/Kitsu", "User ID", "");
66 68
67 torrents.feed_link = toml::find_or(data, "Torrents", "RSS feed", "https://www.tokyotosho.info/rss.php?filter=1,11&zwnj=0"); 69 torrents.feed_link =
70 toml::find_or(data, "Torrents", "RSS feed", "https://www.tokyotosho.info/rss.php?filter=1,11&zwnj=0");
68 71
69 recognition.detect_media_players = toml::find_or(data, "Recognition", "Detect media players", true); 72 recognition.detect_media_players = toml::find_or(data, "Recognition", "Detect media players", true);
70 73
71 { 74 {
72 QFile f(":/players.animone"); 75 QFile f(":/players.animone");
80 83
81 if (!animone::ParsePlayersData(Strings::ToUtf8String(ts.readAll()), players)) 84 if (!animone::ParsePlayersData(Strings::ToUtf8String(ts.readAll()), players))
82 return false; 85 return false;
83 86
84 recognition.players.reserve(players.size()); 87 recognition.players.reserve(players.size());
85 for (const auto& player : players) 88 for (const auto &player : players)
86 recognition.players.push_back({true, player}); 89 recognition.players.push_back({true, player});
87 } 90 }
88 91
89 for (auto& [enabled, player] : recognition.players) { 92 for (auto &[enabled, player] : recognition.players) {
90 switch (player.type) { 93 switch (player.type) {
91 default: 94 default:
92 case animone::PlayerType::Default: 95 case animone::PlayerType::Default:
93 enabled = toml::find_or(data, "Recognition/Players", player.name, true); 96 enabled = toml::find_or(data, "Recognition/Players", player.name, true);
94 break; 97 break;
96 enabled = toml::find_or(data, "Recognition/Browsers", player.name, true); 99 enabled = toml::find_or(data, "Recognition/Browsers", player.name, true);
97 break; 100 break;
98 } 101 }
99 } 102 }
100 103
101
102 theme.SetTheme(Translate::ToTheme(toml::find_or(data, "Appearance", "Theme", "Default"))); 104 theme.SetTheme(Translate::ToTheme(toml::find_or(data, "Appearance", "Theme", "Default")));
103 105
104 if (data.contains("Library") && data["Library"].contains("Folders")) { 106 if (data.contains("Library") && data["Library"].contains("Folders")) {
105 const toml::value& folders = toml::find(data, "Library", "Folders"); 107 const toml::value &folders = toml::find(data, "Library", "Folders");
106 std::vector<std::string> v = toml::get_or<std::vector<std::string>>(folders, {}); 108 std::vector<std::string> v = toml::get_or<std::vector<std::string>>(folders, {});
107 for (const auto& s : v) 109 for (const auto &s : v)
108 if (!library.paths.count(s)) 110 if (!library.paths.count(s))
109 library.paths.insert(s); 111 library.paths.insert(s);
110 } 112 }
111 113
112 library.real_time_monitor = toml::find_or(data, "Library", "Real-time monitor", true); 114 library.real_time_monitor = toml::find_or(data, "Library", "Real-time monitor", true);
113 115
114 return 0; 116 return 0;
115 } 117 }
116 118
117 int Config::Save() { 119 int Config::Save()
120 {
118 std::filesystem::path cfg_path = Filesystem::GetConfigPath(); 121 std::filesystem::path cfg_path = Filesystem::GetConfigPath();
119 Filesystem::CreateDirectories(cfg_path); 122 Filesystem::CreateDirectories(cfg_path);
120 123
121 std::ofstream file(cfg_path); 124 std::ofstream file(cfg_path);
122 if (!file.good()) 125 if (!file.good())
125 toml::value data; 128 toml::value data;
126 129
127 data["Library"]["Folders"] = library.paths; 130 data["Library"]["Folders"] = library.paths;
128 data["Library"]["Real-time monitor"] = library.real_time_monitor; 131 data["Library"]["Real-time monitor"] = library.real_time_monitor;
129 132
130 for (const auto& [enabled, player] : recognition.players) { 133 for (const auto &[enabled, player] : recognition.players) {
131 const std::string section = (player.type == animone::PlayerType::WebBrowser) ? "Recognition/Players" : "Recognition/Browsers"; 134 const std::string section =
135 (player.type == animone::PlayerType::WebBrowser) ? "Recognition/Players" : "Recognition/Browsers";
132 data[section][player.name] = enabled; 136 data[section][player.name] = enabled;
133 } 137 }
134 138
135 data["Recognition"]["Detect media players"] = recognition.detect_media_players; 139 data["Recognition"]["Detect media players"] = recognition.detect_media_players;
136 140