summaryrefslogtreecommitdiff
path: root/src/settings.cpp
blob: 6afc35107255e4db14c128e187a94a90bd8f5b69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "settings.hpp"
#include <filesystem>
#include <fstream>
#include <glibmm/miscutils.h>

#ifdef WITH_KEYCHAIN
    #include <keychain/keychain.h>
#endif

const std::string KeychainPackage = "com.github.uowuo.abaddon";
const std::string KeychainService = "abaddon-client-token";
const std::string KeychainUser = "";

SettingsManager::SettingsManager(const std::string &filename)
    : m_filename(filename) {
    if (!std::filesystem::exists(filename)) {
        std::fstream fs;
        fs.open(filename, std::ios::out);
        fs.close();
    }

    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::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 (...) {}

    SMSTR("discord", "api_base", APIBaseURL);
    SMSTR("discord", "gateway", GatewayURL);
    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);
    SMBOOL("gui", "unreads", Unreads);
    SMINT("http", "concurrent", CacheHTTPConcurrency);
    SMSTR("http", "user_agent", UserAgent);
    SMSTR("style", "expandercolor", ChannelsExpanderColor);
    SMSTR("style", "linkcolor", LinkColor);
    SMSTR("style", "nsfwchannelcolor", NSFWChannelColor);
    SMSTR("style", "channelcolor", ChannelColor);
    SMSTR("style", "mentionbadgecolor", MentionBadgeColor);
    SMSTR("style", "mentionbadgetextcolor", MentionBadgeTextColor);
    SMSTR("style", "unreadcolor", UnreadIndicatorColor);

#ifdef WITH_KEYCHAIN
    keychain::Error error {};

    // convert to keychain if present in normal settings
    SMSTR("discord", "token", DiscordToken);

    if (!m_settings.DiscordToken.empty()) {
        keychain::Error set_error {};

        keychain::setPassword(KeychainPackage, KeychainService, KeychainUser, m_settings.DiscordToken, set_error);
        if (set_error) {
            printf("keychain error setting token: %s\n", set_error.message.c_str());
        } else {
            m_file.remove_key("discord", "token");
        }
    }

    m_settings.DiscordToken = keychain::getPassword(KeychainPackage, KeychainService, KeychainUser, error);
    if (error && error.type != keychain::ErrorType::NotFound) {
        printf("keychain error reading token: %s (%d)\n", error.message.c_str(), error.code);
    }

#else
    SMSTR("discord", "token", DiscordToken);
#endif

#undef SMBOOL
#undef SMSTR
#undef SMINT

    m_read_settings = m_settings;
}

bool SettingsManager::IsValid() const {
    return m_ok;
}

SettingsManager::Settings &SettingsManager::GetSettings() {
    return m_settings;
}

void SettingsManager::Close() {
    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);

        SMSTR("discord", "api_base", APIBaseURL);
        SMSTR("discord", "gateway", GatewayURL);
        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);
        SMBOOL("gui", "unreads", Unreads);
        SMINT("http", "concurrent", CacheHTTPConcurrency);
        SMSTR("http", "user_agent", UserAgent);
        SMSTR("style", "expandercolor", ChannelsExpanderColor);
        SMSTR("style", "linkcolor", LinkColor);
        SMSTR("style", "nsfwchannelcolor", NSFWChannelColor);
        SMSTR("style", "channelcolor", ChannelColor);
        SMSTR("style", "mentionbadgecolor", MentionBadgeColor);
        SMSTR("style", "mentionbadgetextcolor", MentionBadgeTextColor);
        SMSTR("style", "unreadcolor", UnreadIndicatorColor);

#ifdef WITH_KEYCHAIN
        keychain::Error error {};

        keychain::setPassword(KeychainPackage, KeychainService, KeychainUser, m_settings.DiscordToken, error);
        if (error) {
            printf("keychain error setting token: %s\n", error.message.c_str());
        }
#else
        SMSTR("discord", "token", DiscordToken);
#endif

#undef SMSTR
#undef SMBOOL
#undef SMINT

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