diff --git a/abaddon.cpp b/abaddon.cpp index d905b05..06dfab8 100644 --- a/abaddon.cpp +++ b/abaddon.cpp @@ -17,11 +17,11 @@ Abaddon::Abaddon() : m_settings("abaddon.ini") , m_emojis("res/emojis.bin") - , m_discord(m_settings.GetSettingBool("discord", "memory_db", false)) { // stupid but easy + , m_discord(m_settings.GetUseMemoryDB()) { // stupid but easy LoadFromSettings(); // todo: set user agent for non-client(?) - std::string ua = m_settings.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"); + std::string ua = m_settings.GetUserAgent(); m_discord.SetUserAgent(ua); m_discord.signal_gateway_ready().connect(sigc::mem_fun(*this, &Abaddon::DiscordOnReady)); @@ -130,7 +130,7 @@ int Abaddon::StartGTK() { } void Abaddon::LoadFromSettings() { - std::string token = m_settings.GetSettingString("discord", "token"); + std::string token = m_settings.GetDiscordToken(); if (token.size()) { m_discord_token = token; m_discord.UpdateToken(m_discord_token); diff --git a/components/chatmessage.cpp b/components/chatmessage.cpp index 5a076c6..5a883b7 100644 --- a/components/chatmessage.cpp +++ b/components/chatmessage.cpp @@ -622,7 +622,7 @@ void ChatMessageItemContainer::HandleCustomEmojis(Gtk::TextView *tv) { } void ChatMessageItemContainer::HandleEmojis(Gtk::TextView *tv) { - static bool emojis = Abaddon::Get().GetSettings().GetSettingBool("gui", "emojis", true); + static bool emojis = Abaddon::Get().GetSettings().GetShowEmojis(); if (emojis) { HandleStockEmojis(tv); HandleCustomEmojis(tv); @@ -712,8 +712,7 @@ void ChatMessageItemContainer::HandleLinks(Gtk::TextView *tv) { Glib::ustring text = GetText(buf); // i'd like to let this be done thru css like .message-link { color: #bitch; } but idk how - auto &settings = Abaddon::Get().GetSettings(); - static auto link_color = settings.GetSettingString("misc", "linkcolor", "rgba(40, 200, 180, 255)"); + static auto link_color = Abaddon::Get().GetSettings().GetLinkColor(); int startpos = 0; Glib::MatchInfo match; diff --git a/components/memberlist.cpp b/components/memberlist.cpp index 452022c..befc2bd 100644 --- a/components/memberlist.cpp +++ b/components/memberlist.cpp @@ -17,7 +17,7 @@ MemberListUserRow::MemberListUserRow(Snowflake guild_id, const User *data) { m_label->set_single_line_mode(true); m_label->set_ellipsize(Pango::ELLIPSIZE_END); if (data != nullptr) { - static bool show_discriminator = Abaddon::Get().GetSettings().GetSettingBool("gui", "member_list_discriminator", true); + static bool show_discriminator = Abaddon::Get().GetSettings().GetShowMemberListDiscriminators(); std::string display = data->Username; if (show_discriminator) display += "#" + data->Discriminator; diff --git a/filecache.cpp b/filecache.cpp index 2b454f6..3997156 100644 --- a/filecache.cpp +++ b/filecache.cpp @@ -2,8 +2,6 @@ #include "filecache.hpp" #include "murmurhash3.h" -constexpr static const int MaxConcurrentCacheHTTP = 10; - Cache::Cache() { m_tmp_path = std::filesystem::temp_directory_path() / "abaddon-cache"; std::filesystem::create_directories(m_tmp_path); @@ -38,7 +36,7 @@ void Cache::GetFileFromURL(std::string url, callback_type cb) { // needs to be initialized like this or else ::Get() is called recursively if (!m_semaphore) - m_semaphore = std::make_unique(Abaddon::Get().GetSettings().GetSettingInt("http", "concurrent", MaxConcurrentCacheHTTP)); + m_semaphore = std::make_unique(Abaddon::Get().GetSettings().GetCacheHTTPConcurrency()); if (m_callbacks.find(url) != m_callbacks.end()) { m_callbacks[url].push_back(cb); diff --git a/settings.cpp b/settings.cpp index 37a30e4..e5ecd52 100644 --- a/settings.cpp +++ b/settings.cpp @@ -34,3 +34,30 @@ void SettingsManager::Close() { m_ini.SaveFile(m_filename.c_str()); } +bool SettingsManager::GetUseMemoryDB() const { + return GetSettingBool("discord", "memory_db", false); +} + +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"); +} + +std::string SettingsManager::GetDiscordToken() const { + return GetSettingString("discord", "token"); +} + +bool SettingsManager::GetShowMemberListDiscriminators() const { + return GetSettingBool("gui", "member_list_discriminator", true); +} + +bool SettingsManager::GetShowEmojis() const { + return GetSettingBool("gui", "emojis", true); +} + +std::string SettingsManager::GetLinkColor() const { + return GetSettingString("misc", "linkcolor", "rgba(40, 200, 180, 255)"); +} + +int SettingsManager::GetCacheHTTPConcurrency() const { + return GetSettingInt("http", "concurrent", 10); +} diff --git a/settings.hpp b/settings.hpp index cb2e824..c2a292d 100644 --- a/settings.hpp +++ b/settings.hpp @@ -8,21 +8,32 @@ public: SettingsManager(std::string filename); void Close(); - std::string GetSettingString(const std::string §ion, const std::string &key, std::string fallback = "") const; - int GetSettingInt(const std::string §ion, const std::string &key, int fallback) const; - bool GetSettingBool(const std::string §ion, const std::string &key, bool fallback) const; + bool GetUseMemoryDB() const; + std::string GetUserAgent() const; + std::string GetDiscordToken() const; + bool GetShowMemberListDiscriminators() const; + bool GetShowEmojis() const; + std::string GetLinkColor() const; + int GetCacheHTTPConcurrency() const; + + bool IsValid() const; template void SetSetting(std::string section, std::string key, T value) { - if constexpr (std::is_same::value) - m_ini.SetValue(section.c_str(), key.c_str(), value.c_str()); - else - m_ini.SetValue(section.c_str(), key.c_str(), std::to_string(value).c_str()); - + m_ini.SetValue(section.c_str(), key.c_str(), std::to_string(value).c_str()); m_ini.SaveFile(m_filename.c_str()); } - bool IsValid() const; + template<> + void SetSetting(std::string section, std::string key, std::string value) { + m_ini.SetValue(section.c_str(), key.c_str(), value.c_str()); + m_ini.SaveFile(m_filename.c_str()); + } + +private: + std::string GetSettingString(const std::string §ion, const std::string &key, std::string fallback = "") const; + int GetSettingInt(const std::string §ion, const std::string &key, int fallback) const; + bool GetSettingBool(const std::string §ion, const std::string &key, bool fallback) const; private: bool m_ok;