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

@@ -1,62 +1,55 @@
#pragma once
#include <string>
#include <type_traits>
#include <SimpleIni.h>
#include <glibmm/keyfile.h>
class SettingsManager {
public:
SettingsManager(std::string filename);
void Reload();
struct Settings {
// [discord]
std::string APIBaseURL { "https://discord.com/api/v9" };
std::string GatewayURL { "wss://gateway.discord.gg/?v=9&encoding=json&compress=zlib-stream" };
std::string DiscordToken;
bool UseMemoryDB { false };
bool Prefetch { false };
// [gui]
std::string MainCSS { "main.css" };
bool AnimatedGuildHoverOnly { true };
bool ShowAnimations { true };
bool ShowCustomEmojis { true };
bool ShowMemberListDiscriminators { true };
bool ShowOwnerCrown { true };
bool SaveState { true };
#ifdef _WIN32
bool ShowStockEmojis { false };
#else
bool ShowStockEmojis { true };
#endif
// [http]
int CacheHTTPConcurrency { 20 };
std::string UserAgent { "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36" };
// [style]
// TODO: convert to StyleProperty
std::string LinkColor { "rgba(40, 200, 180, 255)" };
std::string ChannelsExpanderColor { "rgba(255, 83, 112, 255)" };
std::string NSFWChannelColor { "#ed6666" };
};
SettingsManager(std::string_view filename);
void Close();
bool GetUseMemoryDB() const;
std::string GetUserAgent() const;
std::string GetDiscordToken() const;
bool GetShowMemberListDiscriminators() const;
bool GetShowStockEmojis() const;
bool GetShowCustomEmojis() const;
int GetCacheHTTPConcurrency() const;
bool GetPrefetch() const;
std::string GetMainCSS() const;
bool GetShowAnimations() const;
bool GetShowOwnerCrown() const;
std::string GetGatewayURL() const;
std::string GetAPIBaseURL() const;
bool GetAnimatedGuildHoverOnly() const;
bool GetSaveState() const;
// i would like to use Gtk::StyleProperty for this, but it will not work on windows
// #1 it's missing from the project files for the version used by vcpkg
// #2 it's still broken and doesn't function even when added to the solution
// #3 it's a massive pain in the ass to try and bump the version to a functioning version
// because they switch build systems to nmake/meson (took months to get merged in vcpkg)
// #4 c++ build systems sucks
// three options are: use gtk4 with updated vcpkg, try and port it myself, or use msys2 instead of vcpkg
// im leaning towards msys
std::string GetLinkColor() const;
std::string GetChannelsExpanderColor() const;
std::string GetNSFWChannelColor() const;
bool IsValid() const;
template<typename T>
void SetSetting(std::string section, std::string key, T value) {
m_ini.SetValue(section.c_str(), key.c_str(), std::to_string(value).c_str());
m_ini.SaveFile(m_filename.c_str());
}
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());
}
Settings &GetSettings();
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;
void ReadSettings();
private:
bool m_ok;
std::string m_filename;
CSimpleIniA m_ini;
Glib::KeyFile m_file;
Settings m_settings;
Settings m_read_settings;
};