remove SimpleIni as a dependency

use Glib::KeyFile instead which is basically the same file format
also read into and save from struct once, cuz its faster and less redundant
This commit is contained in:
ouwou
2021-11-24 03:14:41 -05:00
parent a51a54bc59
commit 4326c5e29b
22 changed files with 181 additions and 230 deletions

View File

@@ -2,7 +2,7 @@
#include <filesystem>
#include <fstream>
SettingsManager::SettingsManager(std::string filename)
SettingsManager::SettingsManager(std::string_view filename)
: m_filename(filename) {
if (!std::filesystem::exists(filename)) {
std::fstream fs;
@@ -10,106 +10,106 @@ SettingsManager::SettingsManager(std::string filename)
fs.close();
}
auto rc = m_ini.LoadFile(filename.c_str());
m_ok = rc == SI_OK;
try {
m_ok = m_file.load_from_file(m_filename, Glib::KEY_FILE_KEEP_COMMENTS);
} catch (const Glib::Error &e) {
fprintf(stderr, "error opening settings KeyFile: %s\n", e.what().c_str());
m_ok = false;
}
if (m_ok) ReadSettings();
}
void SettingsManager::Reload() {
m_ok = m_ini.LoadFile(m_filename.c_str()) == SI_OK;
}
void SettingsManager::ReadSettings() {
#define SMBOOL(section, key, var) \
try { \
m_settings.var = m_file.get_boolean(section, key); \
} catch (...) {}
#define SMSTR(section, key, var) \
try { \
m_settings.var = m_file.get_string(section, key); \
} catch (...) {}
#define SMINT(section, key, var) \
try { \
m_settings.var = m_file.get_integer(section, key); \
} catch (...) {}
std::string SettingsManager::GetSettingString(const std::string &section, const std::string &key, std::string fallback) const {
return m_ini.GetValue(section.c_str(), key.c_str(), fallback.c_str());
}
SMSTR("discord", "api_base", APIBaseURL);
SMSTR("discord", "gateway", GatewayURL);
SMSTR("discord", "token", DiscordToken);
SMBOOL("discord", "memory_db", UseMemoryDB);
SMBOOL("discord", "prefetch", Prefetch);
SMSTR("gui", "css", MainCSS);
SMBOOL("gui", "animated_guild_hover_only", AnimatedGuildHoverOnly);
SMBOOL("gui", "animations", ShowAnimations);
SMBOOL("gui", "custom_emojis", ShowCustomEmojis);
SMBOOL("gui", "member_list_discriminator", ShowMemberListDiscriminators);
SMBOOL("gui", "owner_crown", ShowOwnerCrown);
SMBOOL("gui", "save_state", SaveState);
SMBOOL("gui", "stock_emojis", ShowStockEmojis);
SMINT("http", "concurrent", CacheHTTPConcurrency);
SMSTR("http", "user_agent", UserAgent);
SMSTR("style", "expandercolor", ChannelsExpanderColor);
SMSTR("style", "linkcolor", LinkColor);
SMSTR("style", "nsfwchannelcolor", NSFWChannelColor);
int SettingsManager::GetSettingInt(const std::string &section, const std::string &key, int fallback) const {
return std::stoul(GetSettingString(section, key, std::to_string(fallback)));
}
#undef SMBOOL
#undef SMSTR
#undef SMINT
bool SettingsManager::GetSettingBool(const std::string &section, const std::string &key, bool fallback) const {
return GetSettingString(section, key, fallback ? "true" : "false") != "false";
m_read_settings = m_settings;
}
bool SettingsManager::IsValid() const {
return m_ok;
}
SettingsManager::Settings &SettingsManager::GetSettings() {
return m_settings;
}
void SettingsManager::Close() {
m_ini.SaveFile(m_filename.c_str());
}
if (m_ok) {
// save anything that changed
// (futureproofing since only DiscordToken can actually change)
#define SMSTR(section, key, var) \
if (m_settings.var != m_read_settings.var) \
m_file.set_string(section, key, m_settings.var);
#define SMBOOL(section, key, var) \
if (m_settings.var != m_read_settings.var) \
m_file.set_boolean(section, key, m_settings.var);
#define SMINT(section, key, var) \
if (m_settings.var != m_read_settings.var) \
m_file.set_integer(section, key, m_settings.var);
bool SettingsManager::GetUseMemoryDB() const {
return GetSettingBool("discord", "memory_db", false);
}
SMSTR("discord", "api_base", APIBaseURL);
SMSTR("discord", "gateway", GatewayURL);
SMSTR("discord", "token", DiscordToken);
SMBOOL("discord", "memory_db", UseMemoryDB);
SMBOOL("discord", "prefetch", Prefetch);
SMSTR("gui", "css", MainCSS);
SMBOOL("gui", "animated_guild_hover_only", AnimatedGuildHoverOnly);
SMBOOL("gui", "animations", ShowAnimations);
SMBOOL("gui", "custom_emojis", ShowCustomEmojis);
SMBOOL("gui", "member_list_discriminator", ShowMemberListDiscriminators);
SMBOOL("gui", "owner_crown", ShowOwnerCrown);
SMBOOL("gui", "save_state", SaveState);
SMBOOL("gui", "stock_emojis", ShowStockEmojis);
SMINT("http", "concurrent", CacheHTTPConcurrency);
SMSTR("http", "user_agent", UserAgent);
SMSTR("style", "expandercolor", ChannelsExpanderColor);
SMSTR("style", "linkcolor", LinkColor);
SMSTR("style", "nsfwchannelcolor", NSFWChannelColor);
std::string SettingsManager::GetUserAgent() const {
return GetSettingString("http", "user_agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36");
}
#undef SMSTR
#undef SMBOOL
#undef SMINT
std::string SettingsManager::GetDiscordToken() const {
return GetSettingString("discord", "token");
}
bool SettingsManager::GetShowMemberListDiscriminators() const {
return GetSettingBool("gui", "member_list_discriminator", true);
}
bool SettingsManager::GetShowStockEmojis() const {
#ifdef _WIN32
return GetSettingBool("gui", "stock_emojis", false);
#else
return GetSettingBool("gui", "stock_emojis", true);
#endif
}
bool SettingsManager::GetShowCustomEmojis() const {
return GetSettingBool("gui", "custom_emojis", true);
}
std::string SettingsManager::GetLinkColor() const {
return GetSettingString("style", "linkcolor", "rgba(40, 200, 180, 255)");
}
std::string SettingsManager::GetChannelsExpanderColor() const {
return GetSettingString("style", "expandercolor", "rgba(255, 83, 112, 255)");
}
std::string SettingsManager::GetNSFWChannelColor() const {
return GetSettingString("style", "nsfwchannelcolor", "#ed6666");
}
int SettingsManager::GetCacheHTTPConcurrency() const {
return GetSettingInt("http", "concurrent", 20);
}
bool SettingsManager::GetPrefetch() const {
return GetSettingBool("discord", "prefetch", false);
}
std::string SettingsManager::GetMainCSS() const {
return GetSettingString("gui", "css", "main.css");
}
bool SettingsManager::GetShowAnimations() const {
return GetSettingBool("gui", "animations", true);
}
bool SettingsManager::GetShowOwnerCrown() const {
return GetSettingBool("gui", "owner_crown", true);
}
std::string SettingsManager::GetGatewayURL() const {
return GetSettingString("discord", "gateway", "wss://gateway.discord.gg/?v=9&encoding=json&compress=zlib-stream");
}
std::string SettingsManager::GetAPIBaseURL() const {
return GetSettingString("discord", "api_base", "https://discord.com/api/v9");
}
bool SettingsManager::GetAnimatedGuildHoverOnly() const {
return GetSettingBool("gui", "animated_guild_hover_only", true);
}
bool SettingsManager::GetSaveState() const {
return GetSettingBool("gui", "save_state", true);
try {
if (!m_file.save_to_file(m_filename))
fputs("failed to save settings KeyFile", stderr);
} catch (const Glib::Error &e) {
fprintf(stderr, "failed to save settings KeyFile: %s\n", e.what().c_str());
}
}
}