abstract away settings some more

This commit is contained in:
ouwou 2020-12-17 01:40:02 -05:00
parent 315a4a8df8
commit 58d0424dfc
6 changed files with 54 additions and 19 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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<Semaphore>(Abaddon::Get().GetSettings().GetSettingInt("http", "concurrent", MaxConcurrentCacheHTTP));
m_semaphore = std::make_unique<Semaphore>(Abaddon::Get().GetSettings().GetCacheHTTPConcurrency());
if (m_callbacks.find(url) != m_callbacks.end()) {
m_callbacks[url].push_back(cb);

View File

@ -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);
}

View File

@ -8,21 +8,32 @@ public:
SettingsManager(std::string filename);
void Close();
std::string GetSettingString(const std::string &section, const std::string &key, std::string fallback = "") const;
int GetSettingInt(const std::string &section, const std::string &key, int fallback) const;
bool GetSettingBool(const std::string &section, 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<typename T>
void SetSetting(std::string section, std::string key, T value) {
if constexpr (std::is_same<T, std::string>::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>(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 &section, const std::string &key, std::string fallback = "") const;
int GetSettingInt(const std::string &section, const std::string &key, int fallback) const;
bool GetSettingBool(const std::string &section, const std::string &key, bool fallback) const;
private:
bool m_ok;