Mercurial > minori
comparison src/core/config.cc @ 120:275da698697d
config: template-ify INI
now it's... slightly less ugly :')
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Wed, 08 Nov 2023 18:13:37 -0500 |
| parents | 39521c47c7a3 |
| children | 0a458cb26ff4 |
comparison
equal
deleted
inserted
replaced
| 119:4eae379cb1ff | 120:275da698697d |
|---|---|
| 15 #include <cstring> | 15 #include <cstring> |
| 16 #include <filesystem> | 16 #include <filesystem> |
| 17 #include <fstream> | 17 #include <fstream> |
| 18 #include <limits.h> | 18 #include <limits.h> |
| 19 | 19 |
| 20 /* I'll use an INI-based config file instead of using an | |
| 21 XML file like Taiga. */ | |
| 22 | |
| 20 int Config::Load() { | 23 int Config::Load() { |
| 21 Filesystem::Path cfg_path = Filesystem::GetConfigPath(); | 24 Filesystem::Path cfg_path = Filesystem::GetConfigPath(); |
| 22 | 25 |
| 23 mINI::INIFile file(cfg_path.GetPath()); | 26 mINI::INIFile file(cfg_path.GetPath()); |
| 24 mINI::INIStructure ini; | 27 mINI::INIStructure ini; |
| 25 file.read(ini); | 28 file.read(ini); |
| 26 | 29 |
| 27 service = Translate::ToService(INI::GetIniString(ini, "General", "Service", "None")); | 30 service = Translate::ToService(INI::GetIniValue<std::string>(ini, "General", "Service", "None")); |
| 31 | |
| 32 anime_list.language = Translate::ToLanguage(INI::GetIniValue<std::string>(ini, "Anime List", "Title language", "Romaji")); | |
| 33 anime_list.display_aired_episodes = INI::GetIniValue<bool>(ini, "Anime List", "Display only aired episodes", true); | |
| 34 anime_list.display_available_episodes = INI::GetIniValue<bool>(ini, "Anime List", "Display only available episodes in library", true); | |
| 35 anime_list.highlight_anime_if_available = INI::GetIniValue<bool>(ini, "Anime List", "Highlight anime if available", true); | |
| 36 anime_list.highlighted_anime_above_others = INI::GetIniValue<bool>(ini, "Anime List", "Display highlighted anime above others", false); | |
| 37 | |
| 38 auth.anilist.auth_token = INI::GetIniValue<std::string>(ini, "Authentication/AniList", "Auth Token", ""); | |
| 39 auth.anilist.user_id = INI::GetIniValue<int>(ini, "Authentication/AniList", "User ID", 0); | |
| 40 | |
| 41 torrents.feed_link = INI::GetIniValue<std::string>(ini, "Torrents", "RSS feed", "https://www.tokyotosho.info/rss.php?filter=1,11&zwnj=0"); | |
| 42 | |
| 28 /* ew */ | 43 /* ew */ |
| 29 locale.SetActiveLocale(QLocale(Strings::ToQString(INI::GetIniString(ini, "General", "Locale", "en_US")))); | 44 locale.SetActiveLocale(QLocale(Strings::ToQString(INI::GetIniValue<std::string>(ini, "General", "Locale", "en_US")))); |
| 30 | 45 |
| 31 anime_list.language = Translate::ToLanguage(INI::GetIniString(ini, "Anime List", "Title language", "Romaji")); | 46 theme.SetTheme(Translate::ToTheme(INI::GetIniValue<std::string>(ini, "Appearance", "Theme", "Default"))); |
| 32 anime_list.display_aired_episodes = Strings::ToBool(INI::GetIniString(ini, "Anime List", "Display only aired episodes", ""), true); | |
| 33 anime_list.display_available_episodes = Strings::ToBool(INI::GetIniString(ini, "Anime List", "Display only available episodes in library", ""), true); | |
| 34 anime_list.highlight_anime_if_available = Strings::ToBool(INI::GetIniString(ini, "Anime List", "Highlight anime if available", ""), true); | |
| 35 anime_list.highlighted_anime_above_others = Strings::ToBool(INI::GetIniString(ini, "Anime List", "Display highlighted anime above others", ""), false); | |
| 36 | |
| 37 anilist.auth_token = INI::GetIniString(ini, "AniList", "Auth Token", ""); | |
| 38 anilist.user_id = Strings::ToInt(INI::GetIniString(ini, "AniList", "User ID", ""), 0); | |
| 39 | |
| 40 torrents.feed_link = INI::GetIniString(ini, "Torrents", "RSS feed", "https://www.tokyotosho.info/rss.php?filter=1,11&zwnj=0"); | |
| 41 | |
| 42 theme.SetTheme(Translate::ToTheme(INI::GetIniString(ini, "Appearance", "Theme", "Default"))); | |
| 43 | 47 |
| 44 return 0; | 48 return 0; |
| 45 } | 49 } |
| 46 | 50 |
| 47 int Config::Save() const { | 51 int Config::Save() const { |
| 50 cfg_path.GetParent().CreateDirectories(); | 54 cfg_path.GetParent().CreateDirectories(); |
| 51 | 55 |
| 52 mINI::INIFile file(cfg_path.GetPath()); | 56 mINI::INIFile file(cfg_path.GetPath()); |
| 53 mINI::INIStructure ini; | 57 mINI::INIStructure ini; |
| 54 | 58 |
| 55 ini["General"]["Service"] = Translate::ToString(service); | 59 INI::SetIniValue(ini, "General", "Service", service); |
| 56 ini["General"]["Locale"] = Strings::ToUtf8String(locale.GetLocale().name()); | 60 INI::SetIniValue(ini, "General", "Locale", locale.GetLocale().name()); |
| 57 ini["Anime List"]["Title language"] = Translate::ToString(anime_list.language); | 61 |
| 58 ini["Anime List"]["Display only aired episodes"] = Strings::ToUtf8String(anime_list.display_aired_episodes); | 62 INI::SetIniValue(ini, "Anime List", "Title language", anime_list.language); |
| 59 ini["Anime List"]["Display only available episodes in library"] = Strings::ToUtf8String(anime_list.display_available_episodes); | 63 INI::SetIniValue(ini, "Anime List", "Display only aired episodes", anime_list.display_aired_episodes); |
| 60 ini["Anime List"]["Highlight anime if available"] = Strings::ToUtf8String(anime_list.highlight_anime_if_available); | 64 INI::SetIniValue(ini, "Anime List", "Display only available episodes in library", anime_list.display_available_episodes); |
| 61 ini["Anime List"]["Display highlighted anime above others"] = Strings::ToUtf8String(anime_list.highlighted_anime_above_others); | 65 INI::SetIniValue(ini, "Anime List", "Highlight anime if available", anime_list.highlight_anime_if_available); |
| 62 ini["AniList"]["Auth Token"] = anilist.auth_token; | 66 INI::SetIniValue(ini, "Anime List", "Display highlighted anime above others", anime_list.highlighted_anime_above_others); |
| 63 ini["AniList"]["User ID"] = std::to_string(anilist.user_id); | 67 |
| 64 ini["Appearance"]["Theme"] = Translate::ToString(theme.GetTheme()); | 68 INI::SetIniValue(ini, "Authentication/AniList", "Auth Token", auth.anilist.auth_token); |
| 65 ini["Torrents"]["RSS feed"] = torrents.feed_link; | 69 INI::SetIniValue(ini, "Authentication/AniList", "User ID", auth.anilist.user_id); |
| 70 | |
| 71 INI::SetIniValue(ini, "Appearance", "Theme", theme.GetTheme()); | |
| 72 | |
| 73 INI::SetIniValue(ini, "Torrents", "RSS feed", torrents.feed_link); | |
| 74 | |
| 75 INI::SetIniValue(ini, "Recognition", "Detect media players", recognition.detect_media_players); | |
| 76 //ini["Recognition"]["Detect streaming media"] = Strings::ToUtf8String(recognition.detect_streaming_media); | |
| 66 | 77 |
| 67 file.write(ini); | 78 file.write(ini); |
| 68 | 79 |
| 69 return 0; | 80 return 0; |
| 70 } | 81 } |
