diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/accounts.c | 99 | ||||
-rw-r--r-- | src/accounts.h | 33 | ||||
-rw-r--r-- | src/command.c | 5 | ||||
-rw-r--r-- | src/files.c | 13 | ||||
-rw-r--r-- | src/files.h | 1 | ||||
-rw-r--r-- | src/preferences.c | 72 | ||||
-rw-r--r-- | src/profanity.c | 4 |
7 files changed, 152 insertions, 75 deletions
diff --git a/src/accounts.c b/src/accounts.c new file mode 100644 index 00000000..93179367 --- /dev/null +++ b/src/accounts.c @@ -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); +} diff --git a/src/accounts.h b/src/accounts.h new file mode 100644 index 00000000..d8fa6377 --- /dev/null +++ b/src/accounts.h @@ -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 diff --git a/src/command.c b/src/command.c index 86cf58fc..68f43648 100644 --- a/src/command.c +++ b/src/command.c @@ -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); diff --git a/src/files.c b/src/files.c index 96480b3d..b98cfbee 100644 --- a/src/files.c +++ b/src/files.c @@ -87,6 +87,19 @@ files_get_log_file(void) } 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) { gchar *xdg_config = xdg_get_config_home(); diff --git a/src/files.h b/src/files.h index ade7075b..bba707b6 100644 --- a/src/files.h +++ b/src/files.h @@ -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 diff --git a/src/preferences.c b/src/preferences.c index 497c90c2..7de61dbd 100644 --- a/src/preferences.c +++ b/src/preferences.c @@ -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,24 +72,11 @@ 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) { return p_autocomplete_complete(boolean_choice_ac, 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) { diff --git a/src/profanity.c b/src/profanity.c index 8f4befb1..92ceae44 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -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); |