about summary refs log tree commit diff stats
path: root/src/accounts.c
blob: 4bb5ec89bc2681ea8b44f0458da40264d0b8ccca (plain) (blame)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * accounts.c
 *
 * Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com>
 *
 * This file is part of Profanity.
 *
 * Profanity is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Profanity is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Profanity.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdlib.h>
#include <string.h>

#include <glib.h>

#include "accounts.h"
#include "files.h"
#include "log.h"
#include "prof_autocomplete.h"

static gchar *accounts_loc;
static GKeyFile *accounts;

static PAutocomplete all_ac;
static PAutocomplete enabled_ac;

static void _save_accounts(void);

void
accounts_load(void)
{
    log_info("Loading accounts");
    all_ac = p_autocomplete_new();
    enabled_ac = p_autocomplete_new();
    accounts_loc = files_get_accounts_file();

    accounts = g_key_file_new();
    g_key_file_load_from_file(accounts, accounts_loc, G_KEY_FILE_KEEP_COMMENTS,
        NULL);

    // create the logins searchable list for autocompletion
    gsize njids;
    gchar **jids =
        g_key_file_get_groups(accounts, &njids);

    gsize i;
    for (i = 0; i < njids; i++) {
        if (g_key_file_get_boolean(accounts, jids[i], "enabled", NULL)) {
            p_autocomplete_add(enabled_ac, strdup(jids[i]));
        }
        p_autocomplete_add(all_ac, strdup(jids[i]));
    }

    for (i = 0; i < njids; i++) {
        free(jids[i]);
    }
    free(jids);
}

void
accounts_close(void)
{
    p_autocomplete_free(all_ac);
    p_autocomplete_free(enabled_ac);
    g_key_file_free(accounts);
}

char *
accounts_find_enabled(char *prefix)
{
    return p_autocomplete_complete(enabled_ac, prefix);
}

char *
accounts_find_all(char *prefix)
{
    return p_autocomplete_complete(all_ac, prefix);
}

void
accounts_reset_all_search(void)
{
    p_autocomplete_reset(all_ac);
}

void
accounts_reset_enabled_search(void)
{
    p_autocomplete_reset(enabled_ac);
}

void
accounts_add_login(const char *jid, const char *altdomain)
{
    // doesn't yet exist
    if (!g_key_file_has_group(accounts, jid)) {
        g_key_file_set_boolean(accounts, jid, "enabled", TRUE);
        g_key_file_set_string(accounts, jid, "jid", jid);
        if (altdomain != NULL) {
            g_key_file_set_string(accounts, jid, "server", altdomain);
        }

        _save_accounts();
        p_autocomplete_add(all_ac, strdup(jid));
        p_autocomplete_add(enabled_ac, strdup(jid));

    // already exists, update old style accounts
    } else {
        g_key_file_set_string(accounts, jid, "jid", jid);
        _save_accounts();
    }

}

gchar**
accounts_get_list(void)
{
    return g_key_file_get_groups(accounts, NULL);
}

ProfAccount*
accounts_get_account(const char * const name)
{
    if (!g_key_file_has_group(accounts, name)) {
        return NULL;
    } else {
        ProfAccount *account = malloc(sizeof(ProfAccount));
        account->name = strdup(name);
        gchar *jid = g_key_file_get_string(accounts, name, "jid", NULL);
        if (jid != NULL) {
            account->jid = strdup(jid);
        } else {
            account->jid = strdup(name);
            g_key_file_set_string(accounts, name, "jid", name);
            _save_accounts();
        }
        account->enabled = g_key_file_get_boolean(accounts, name, "enabled", NULL);
        gchar *server = g_key_file_get_string(accounts, name, "server", NULL);
        if (server != NULL) {
            account->server = strdup(server);
        } else {
            account->server = NULL;
        }

        return account;
    }
}

void
accounts_free_account(ProfAccount *account)
{
    if (account != NULL) {
        if (account->name != NULL) {
            free(account->name);
            account->name = NULL;
        }
        if (account->jid != NULL) {
            free(account->jid);
            account->jid = NULL;
        }
        if (account->server != NULL) {
            free(account->server);
            account->server = NULL;
        }
        account = NULL;
    }
}

gboolean
accounts_enable(const char * const name)
{
    if (g_key_file_has_group(accounts, name)) {
        g_key_file_set_boolean(accounts, name, "enabled", TRUE);
        _save_accounts();
        p_autocomplete_add(enabled_ac, strdup(name));
        return TRUE;
    } else {
        return FALSE;
    }
}

gboolean
accounts_disable(const char * const name)
{
    if (g_key_file_has_group(accounts, name)) {
        g_key_file_set_boolean(accounts, name, "enabled", FALSE);
        _save_accounts();
        p_autocomplete_remove(enabled_ac, strdup(name));
        return TRUE;
    } else {
        return FALSE;
    }
}

gboolean
accounts_rename(const char * const account_name, const char * const new_name)
{
    if (g_key_file_has_group(accounts, new_name)) {
        return FALSE;
    }

    if (!g_key_file_has_group(accounts, account_name)) {
        return FALSE;
    }

    g_key_file_set_boolean(accounts, new_name, "enabled",
        g_key_file_get_boolean(accounts, account_name, "enabled", NULL));

    char *jid = g_key_file_get_string(accounts, account_name, "jid", NULL);
    if (jid != NULL) {
        g_key_file_set_string(accounts, new_name, "jid", jid);
        free(jid);
    }

    char *server = g_key_file_get_string(accounts, account_name, "server", NULL);
    if (server != NULL) {
        g_key_file_set_string(accounts, new_name, "server", server);
        free(server);
    }

    g_key_file_remove_group(accounts, account_name, NULL);
    _save_accounts();

    p_autocomplete_remove(all_ac, strdup(account_name));
    p_autocomplete_add(all_ac, strdup(new_name));
    if (g_key_file_get_boolean(accounts, new_name, "enabled", NULL)) {
        p_autocomplete_remove(enabled_ac, strdup(account_name));
        p_autocomplete_add(enabled_ac, strdup(new_name));
    }

    return TRUE;
}

gboolean
accounts_account_exists(const char * const account_name)
{
    return g_key_file_has_group(accounts, account_name);

}

void
accounts_set_jid(const char * const account_name, const char * const value)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_set_string(accounts, account_name, "jid", value);
        _save_accounts();
    }
}

void
accounts_set_server(const char * const account_name, const char * const value)
{
    if (accounts_account_exists(account_name)) {
        g_key_file_set_string(accounts, account_name, "server", value);
        _save_accounts();
    }
}

static void
_save_accounts(void)
{
    gsize g_data_size;
    char *g_accounts_data = g_key_file_to_data(accounts, &g_data_size, NULL);
    g_file_set_contents(accounts_loc, g_accounts_data, g_data_size, NULL);
}