mirror of
https://github.com/profanity-im/profanity.git
synced 2024-12-04 14:46:46 -05:00
Moved logins to accounts module
This commit is contained in:
parent
30e4786180
commit
6826a66f85
@ -10,7 +10,7 @@ profanity_SOURCES = src/command.c src/contact.c src/history.c src/jabber.h \
|
||||
src/chat_session.h src/release.c src/release.h src/room_chat.c \
|
||||
src/room_chat.h src/stanza.c src/stanza.h src/parser.c src/parser.h \
|
||||
src/theme.c src/theme.h src/window.c src/window.h src/xdg_base.c \
|
||||
src/xdg_base.h src/files.c src/files.h
|
||||
src/xdg_base.h src/files.c src/files.h src/accounts.c src/accounts.h
|
||||
|
||||
TESTS = tests/testsuite
|
||||
check_PROGRAMS = tests/testsuite
|
||||
|
99
src/accounts.c
Normal file
99
src/accounts.c
Normal file
@ -0,0 +1,99 @@
|
||||
/*
|
||||
* 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)
|
||||
{
|
||||
if (!g_key_file_has_group(accounts, jid)) {
|
||||
g_key_file_set_boolean(accounts, jid, "enabled", TRUE);
|
||||
_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);
|
||||
}
|
33
src/accounts.h
Normal file
33
src/accounts.h
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* accounts.h
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef ACCOUNTS_H
|
||||
#define ACCOUNTS_H
|
||||
|
||||
void accounts_load(void);
|
||||
void accounts_close(void);
|
||||
|
||||
char * accounts_find_login(char *prefix);
|
||||
void accounts_reset_login_search(void);
|
||||
void accounts_add_login(const char *jid);
|
||||
|
||||
#endif
|
@ -27,6 +27,7 @@
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#include "accounts.h"
|
||||
#include "chat_session.h"
|
||||
#include "command.h"
|
||||
#include "common.h"
|
||||
@ -761,7 +762,7 @@ void
|
||||
cmd_reset_autocomplete()
|
||||
{
|
||||
contact_list_reset_search_attempts();
|
||||
prefs_reset_login_search();
|
||||
accounts_reset_login_search();
|
||||
prefs_reset_boolean_choice();
|
||||
p_autocomplete_reset(help_ac);
|
||||
p_autocomplete_reset(notify_ac);
|
||||
@ -904,7 +905,7 @@ _cmd_complete_parameters(char *input, int *size)
|
||||
_parameter_autocomplete(input, size, "/info",
|
||||
contact_list_find_contact);
|
||||
_parameter_autocomplete(input, size, "/connect",
|
||||
prefs_find_login);
|
||||
accounts_find_login);
|
||||
_parameter_autocomplete_with_ac(input, size, "/sub", sub_ac);
|
||||
_parameter_autocomplete_with_ac(input, size, "/help", help_ac);
|
||||
_parameter_autocomplete_with_ac(input, size, "/who", who_ac);
|
||||
|
13
src/files.c
13
src/files.c
@ -86,6 +86,19 @@ files_get_log_file(void)
|
||||
return result;
|
||||
}
|
||||
|
||||
gchar *
|
||||
files_get_accounts_file(void)
|
||||
{
|
||||
gchar *xdg_data = xdg_get_data_home();
|
||||
GString *logfile = g_string_new(xdg_data);
|
||||
g_string_append(logfile, "/profanity/accounts");
|
||||
gchar *result = strdup(logfile->str);
|
||||
g_free(xdg_data);
|
||||
g_string_free(logfile, TRUE);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
gchar *
|
||||
files_get_themes_dir(void)
|
||||
{
|
||||
|
@ -28,5 +28,6 @@ gchar* files_get_chatlog_dir(void);
|
||||
gchar* files_get_preferences_file(void);
|
||||
gchar* files_get_log_file(void);
|
||||
gchar* files_get_themes_dir(void);
|
||||
gchar* files_get_accounts_file(void);
|
||||
|
||||
#endif
|
||||
|
@ -41,7 +41,6 @@ static gchar *prefs_loc;
|
||||
static GKeyFile *prefs;
|
||||
gint log_maxsize = 0;
|
||||
|
||||
static PAutocomplete login_ac;
|
||||
static PAutocomplete boolean_choice_ac;
|
||||
|
||||
static void _save_prefs(void);
|
||||
@ -52,28 +51,12 @@ prefs_load(void)
|
||||
GError *err;
|
||||
|
||||
log_info("Loading preferences");
|
||||
login_ac = p_autocomplete_new();
|
||||
prefs_loc = files_get_preferences_file();
|
||||
|
||||
prefs = g_key_file_new();
|
||||
g_key_file_load_from_file(prefs, prefs_loc, G_KEY_FILE_KEEP_COMMENTS,
|
||||
NULL);
|
||||
|
||||
// create the logins searchable list for autocompletion
|
||||
gsize njids;
|
||||
gchar **jids =
|
||||
g_key_file_get_string_list(prefs, "connections", "logins", &njids, NULL);
|
||||
|
||||
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);
|
||||
|
||||
err = NULL;
|
||||
log_maxsize = g_key_file_get_integer(prefs, "log", "maxsize", &err);
|
||||
if (err != NULL) {
|
||||
@ -89,23 +72,10 @@ prefs_load(void)
|
||||
void
|
||||
prefs_close(void)
|
||||
{
|
||||
p_autocomplete_clear(login_ac);
|
||||
p_autocomplete_clear(boolean_choice_ac);
|
||||
g_key_file_free(prefs);
|
||||
}
|
||||
|
||||
char *
|
||||
prefs_find_login(char *prefix)
|
||||
{
|
||||
return p_autocomplete_complete(login_ac, prefix);
|
||||
}
|
||||
|
||||
void
|
||||
prefs_reset_login_search(void)
|
||||
{
|
||||
p_autocomplete_reset(login_ac);
|
||||
}
|
||||
|
||||
char *
|
||||
prefs_autocomplete_boolean_choice(char *prefix)
|
||||
{
|
||||
@ -414,48 +384,6 @@ prefs_set_autoaway_check(gboolean value)
|
||||
_save_prefs();
|
||||
}
|
||||
|
||||
void
|
||||
prefs_add_login(const char *jid)
|
||||
{
|
||||
gsize njids;
|
||||
gchar **jids =
|
||||
g_key_file_get_string_list(prefs, "connections", "logins", &njids, NULL);
|
||||
|
||||
// no logins remembered yet
|
||||
if (jids == NULL) {
|
||||
njids = 1;
|
||||
jids = (gchar**) g_malloc(sizeof(gchar *) * 2);
|
||||
jids[0] = g_strdup(jid);
|
||||
jids[1] = NULL;
|
||||
g_key_file_set_string_list(prefs, "connections", "logins",
|
||||
(const gchar * const *)jids, njids);
|
||||
_save_prefs();
|
||||
g_strfreev(jids);
|
||||
|
||||
return;
|
||||
} else {
|
||||
gsize i;
|
||||
for (i = 0; i < njids; i++) {
|
||||
if (strcmp(jid, jids[i]) == 0) {
|
||||
g_strfreev(jids);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// jid not found, add to the list
|
||||
jids = (gchar **) g_realloc(jids, (sizeof(gchar *) * (njids+2)));
|
||||
jids[njids] = g_strdup(jid);
|
||||
njids++;
|
||||
jids[njids] = NULL;
|
||||
g_key_file_set_string_list(prefs, "connections", "logins",
|
||||
(const gchar * const *)jids, njids);
|
||||
_save_prefs();
|
||||
g_strfreev(jids);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
gboolean
|
||||
prefs_get_showsplash(void)
|
||||
{
|
||||
|
@ -28,6 +28,7 @@
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#include "accounts.h"
|
||||
#include "chat_log.h"
|
||||
#include "chat_session.h"
|
||||
#include "command.h"
|
||||
@ -198,7 +199,7 @@ prof_handle_login_success(const char *jid)
|
||||
win_current_page_off();
|
||||
status_bar_print_message(jid);
|
||||
status_bar_refresh();
|
||||
prefs_add_login(jid);
|
||||
accounts_add_login(jid);
|
||||
}
|
||||
|
||||
void
|
||||
@ -508,6 +509,7 @@ _init(const int disable_tls, char *log_level)
|
||||
log_info("Starting Profanity (%s)...", PACKAGE_VERSION);
|
||||
chat_log_init();
|
||||
prefs_load();
|
||||
accounts_load();
|
||||
gchar *theme = prefs_get_theme();
|
||||
theme_load(theme);
|
||||
g_free(theme);
|
||||
|
Loading…
Reference in New Issue
Block a user