about summary refs log tree commit diff stats
path: root/src/accounts.c
blob: 905904d3fd7b3fbb53b8d8e11811931a38c1d8fb (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
/*
 * accounts.c
 *
 * Copyright (C) 2012 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 "files.h"
#include "log.h"
#include "prof_autocomplete.h"

static gchar *accounts_loc;
static GKeyFile *accounts;

static PAutocomplete login_ac;

static void _save_accounts(void);

void
accounts_load(void)
{
    log_info("Loading accounts");
    login_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++) {
        p_autocomplete_add(login_ac, strdup(jids[i]));
    }

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

void
accounts_close(void)
{
    p_autocomplete_clear(login_ac);
    g_key_file_free(accounts);
}

char *
accounts_find_login(char *prefix)
{
    return p_autocomplete_complete(login_ac, prefix);
}

void
accounts_reset_login_search(void)
{
    p_autocomplete_reset(login_ac);
}

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

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