From 97d1b964c3b998116f05fd41f26bfe2f724e3150 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 23 Nov 2015 23:54:14 +0000 Subject: Created config/conflists.c for shared config list handling --- src/config/accounts.c | 58 +++++------------------------------ src/config/conflists.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/config/conflists.h | 38 +++++++++++++++++++++++ 3 files changed, 127 insertions(+), 51 deletions(-) create mode 100644 src/config/conflists.c create mode 100644 src/config/conflists.h (limited to 'src') diff --git a/src/config/accounts.c b/src/config/accounts.c index 2d4581ee..234bed1f 100644 --- a/src/config/accounts.c +++ b/src/config/accounts.c @@ -42,6 +42,7 @@ #include "common.h" #include "config/account.h" +#include "config/conflists.h" #include "jid.h" #include "log.h" #include "tools/autocomplete.h" @@ -55,7 +56,6 @@ static Autocomplete enabled_ac; static void _save_accounts(void); static gchar* _get_accounts_file(void); -static void _remove_from_list(GKeyFile *accounts, const char *const account_name, const char *const key, const char *const contact_jid); void accounts_load(void) @@ -604,66 +604,22 @@ accounts_add_otr_policy(const char *const account_name, const char *const contac // check for and remove from other lists if (strcmp(policy, "manual") == 0) { - _remove_from_list(accounts, account_name, "otr.opportunistic", contact_jid); - _remove_from_list(accounts, account_name, "otr.always", contact_jid); + conf_string_list_remove(accounts, account_name, "otr.opportunistic", contact_jid); + conf_string_list_remove(accounts, account_name, "otr.always", contact_jid); } if (strcmp(policy, "opportunistic") == 0) { - _remove_from_list(accounts, account_name, "otr.manual", contact_jid); - _remove_from_list(accounts, account_name, "otr.always", contact_jid); + conf_string_list_remove(accounts, account_name, "otr.manual", contact_jid); + conf_string_list_remove(accounts, account_name, "otr.always", contact_jid); } if (strcmp(policy, "always") == 0) { - _remove_from_list(accounts, account_name, "otr.opportunistic", contact_jid); - _remove_from_list(accounts, account_name, "otr.manual", contact_jid); + conf_string_list_remove(accounts, account_name, "otr.opportunistic", contact_jid); + conf_string_list_remove(accounts, account_name, "otr.manual", contact_jid); } _save_accounts(); } } -static void -_remove_from_list(GKeyFile *accounts, const char *const account_name, const char *const key, const char *const contact_jid) -{ - gsize length; - gchar **list = g_key_file_get_string_list(accounts, account_name, key, &length, NULL); - - if (list) { - int i = 0; - GList *glist = NULL; - gboolean deleted = FALSE; - - for (i = 0; i < length; i++) { - // item found, mark as deleted - if (strcmp(list[i], contact_jid) == 0) { - deleted = TRUE; - } else { - // add item to our g_list - glist = g_list_append(glist, strdup(list[i])); - } - } - - if (deleted) { - if (g_list_length(glist) == 0) { - g_key_file_remove_key(accounts, account_name, key, NULL); - } else { - // create the new list entry - const gchar* new_list[g_list_length(glist)+1]; - GList *curr = glist; - i = 0; - while (curr) { - new_list[i++] = strdup(curr->data); - curr = g_list_next(curr); - } - new_list[i] = NULL; - g_key_file_set_string_list(accounts, account_name, key, new_list, g_list_length(glist)); - } - } - - g_list_free_full(glist, g_free); - } - - g_strfreev(list); -} - void accounts_set_muc_service(const char *const account_name, const char *const value) { diff --git a/src/config/conflists.c b/src/config/conflists.c new file mode 100644 index 00000000..bf5bc482 --- /dev/null +++ b/src/config/conflists.c @@ -0,0 +1,82 @@ +/* + * conflists.c + * + * Copyright (C) 2012 - 2015 James Booth + * + * 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 . + * + * In addition, as a special exception, the copyright holders give permission to + * link the code of portions of this program with the OpenSSL library under + * certain conditions as described in each individual source file, and + * distribute linked combinations including the two. + * + * You must obey the GNU General Public License in all respects for all of the + * code used other than OpenSSL. If you modify file(s) with this exception, you + * may extend this exception to your version of the file(s), but you are not + * obligated to do so. If you do not wish to do so, delete this exception + * statement from your version. If you delete this exception statement from all + * source files in the program, then also delete it here. + * + */ + +#include +#include + +gboolean +conf_string_list_remove(GKeyFile *keyfile, const char *const group, const char *const key, const char *const item) +{ + gsize length; + gchar **list = g_key_file_get_string_list(keyfile, group, key, &length, NULL); + + gboolean deleted = FALSE; + if (list) { + int i = 0; + GList *glist = NULL; + + for (i = 0; i < length; i++) { + // item found, mark as deleted + if (strcmp(list[i], item) == 0) { + deleted = TRUE; + } else { + // add item to our g_list + glist = g_list_append(glist, strdup(list[i])); + } + } + + if (deleted) { + if (g_list_length(glist) == 0) { + g_key_file_remove_key(keyfile, group, key, NULL); + } else { + // create the new list entry + const gchar* new_list[g_list_length(glist)+1]; + GList *curr = glist; + i = 0; + while (curr) { + new_list[i++] = strdup(curr->data); + curr = g_list_next(curr); + } + new_list[i] = NULL; + g_key_file_set_string_list(keyfile, group, key, new_list, g_list_length(glist)); + } + } + + g_list_free_full(glist, g_free); + } + + g_strfreev(list); + + return deleted; +} diff --git a/src/config/conflists.h b/src/config/conflists.h new file mode 100644 index 00000000..f67e58b8 --- /dev/null +++ b/src/config/conflists.h @@ -0,0 +1,38 @@ +/* + * conflists.h + * + * Copyright (C) 2012 - 2015 James Booth + * + * 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 . + * + * In addition, as a special exception, the copyright holders give permission to + * link the code of portions of this program with the OpenSSL library under + * certain conditions as described in each individual source file, and + * distribute linked combinations including the two. + * + * You must obey the GNU General Public License in all respects for all of the + * code used other than OpenSSL. If you modify file(s) with this exception, you + * may extend this exception to your version of the file(s), but you are not + * obligated to do so. If you do not wish to do so, delete this exception + * statement from your version. If you delete this exception statement from all + * source files in the program, then also delete it here. + * + */ + +#include + +gboolean conf_string_list_remove(GKeyFile *keyfile, const char *const group, const char *const key, + const char *const item); -- cgit 1.4.1-2-gfad0 From a2f5e921f2e5fb4158aad14928882e694e5d8a85 Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 24 Nov 2015 00:02:21 +0000 Subject: Added conf_string_list_add --- src/config/accounts.c | 43 +------------------------------------------ src/config/conflists.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/config/conflists.h | 2 ++ 3 files changed, 52 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/config/accounts.c b/src/config/accounts.c index 234bed1f..385e86f3 100644 --- a/src/config/accounts.c +++ b/src/config/accounts.c @@ -558,48 +558,7 @@ accounts_add_otr_policy(const char *const account_name, const char *const contac if (accounts_account_exists(account_name)) { GString *key = g_string_new("otr."); g_string_append(key, policy); - gsize length; - gchar **list = g_key_file_get_string_list(accounts, account_name, key->str, &length, NULL); - GList *glist = NULL; - - // list found - if (list) { - int i = 0; - for (i = 0; i < length; i++) { - // item already in list, exit function - if (strcmp(list[i], contact_jid) == 0) { - g_list_free_full(glist, g_free); - g_strfreev(list); - return; - } - // add item to our g_list - glist = g_list_append(glist, strdup(list[i])); - } - - // item not found, add to our g_list - glist = g_list_append(glist, strdup(contact_jid)); - - // create the new list entry - const gchar* new_list[g_list_length(glist)+1]; - GList *curr = glist; - i = 0; - while (curr) { - new_list[i++] = strdup(curr->data); - curr = g_list_next(curr); - } - new_list[i] = NULL; - g_key_file_set_string_list(accounts, account_name, key->str, new_list, g_list_length(glist)); - - // list not found - } else { - const gchar* new_list[2]; - new_list[0] = strdup(contact_jid); - new_list[1] = NULL; - g_key_file_set_string_list(accounts, account_name, key->str, new_list, 1); - } - - g_strfreev(list); - g_list_free_full(glist, g_free); + conf_string_list_add(accounts, account_name, key->str, contact_jid); g_string_free(key, TRUE); // check for and remove from other lists diff --git a/src/config/conflists.c b/src/config/conflists.c index bf5bc482..474969e6 100644 --- a/src/config/conflists.c +++ b/src/config/conflists.c @@ -35,6 +35,55 @@ #include #include +gboolean +conf_string_list_add(GKeyFile *keyfile, const char *const group, const char *const key, const char *const item) +{ + gsize length; + gchar **list = g_key_file_get_string_list(keyfile, group, key, &length, NULL); + GList *glist = NULL; + + // list found + if (list) { + int i = 0; + for (i = 0; i < length; i++) { + // item already in list, exit function + if (strcmp(list[i], item) == 0) { + g_list_free_full(glist, g_free); + g_strfreev(list); + return FALSE; + } + // add item to our g_list + glist = g_list_append(glist, strdup(list[i])); + } + + // item not found, add to our g_list + glist = g_list_append(glist, strdup(item)); + + // create the new list entry + const gchar* new_list[g_list_length(glist)+1]; + GList *curr = glist; + i = 0; + while (curr) { + new_list[i++] = strdup(curr->data); + curr = g_list_next(curr); + } + new_list[i] = NULL; + g_key_file_set_string_list(keyfile, group, key, new_list, g_list_length(glist)); + + // list not found + } else { + const gchar* new_list[2]; + new_list[0] = strdup(item); + new_list[1] = NULL; + g_key_file_set_string_list(keyfile, group, key, new_list, 1); + } + + g_strfreev(list); + g_list_free_full(glist, g_free); + + return TRUE; +} + gboolean conf_string_list_remove(GKeyFile *keyfile, const char *const group, const char *const key, const char *const item) { diff --git a/src/config/conflists.h b/src/config/conflists.h index f67e58b8..2254732c 100644 --- a/src/config/conflists.h +++ b/src/config/conflists.h @@ -34,5 +34,7 @@ #include +gboolean conf_string_list_add(GKeyFile *keyfile, const char *const group, const char *const key, + const char *const item); gboolean conf_string_list_remove(GKeyFile *keyfile, const char *const group, const char *const key, const char *const item); -- cgit 1.4.1-2-gfad0