mirror of
https://github.com/profanity-im/profanity.git
synced 2024-09-01 19:24:15 -04:00
Keep count of group members
This commit is contained in:
parent
1f723a60b3
commit
c4a1d69a0c
@ -55,6 +55,7 @@ static Autocomplete fulljid_ac;
|
||||
|
||||
// groups
|
||||
static Autocomplete groups_ac;
|
||||
GHashTable *group_count;
|
||||
|
||||
// contacts, indexed on barejid
|
||||
static GHashTable *contacts;
|
||||
@ -76,11 +77,11 @@ roster_clear(void)
|
||||
autocomplete_clear(fulljid_ac);
|
||||
autocomplete_clear(groups_ac);
|
||||
g_hash_table_destroy(contacts);
|
||||
contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free,
|
||||
(GDestroyNotify)p_contact_free);
|
||||
contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, (GDestroyNotify)p_contact_free);
|
||||
g_hash_table_destroy(name_to_barejid);
|
||||
name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
|
||||
g_free);
|
||||
name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
||||
g_hash_table_destroy(group_count);
|
||||
group_count = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
|
||||
}
|
||||
|
||||
gboolean
|
||||
@ -179,10 +180,9 @@ roster_init(void)
|
||||
barejid_ac = autocomplete_new();
|
||||
fulljid_ac = autocomplete_new();
|
||||
groups_ac = autocomplete_new();
|
||||
contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free,
|
||||
(GDestroyNotify)p_contact_free);
|
||||
name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
|
||||
g_free);
|
||||
contacts = g_hash_table_new_full(g_str_hash, (GEqualFunc)_key_equals, g_free, (GDestroyNotify)p_contact_free);
|
||||
name_to_barejid = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
||||
group_count = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
|
||||
}
|
||||
|
||||
void
|
||||
@ -230,6 +230,23 @@ roster_remove(const char *const name, const char *const barejid)
|
||||
resources = g_list_next(resources);
|
||||
}
|
||||
g_list_free(resources);
|
||||
|
||||
GSList *groups = p_contact_groups(contact);
|
||||
GSList *curr = groups;
|
||||
while (curr) {
|
||||
gchar *group = curr->data;
|
||||
if (g_hash_table_contains(group_count, group)) {
|
||||
int count = GPOINTER_TO_INT(g_hash_table_lookup(group_count, group));
|
||||
count--;
|
||||
if (count < 1) {
|
||||
g_hash_table_remove(group_count, group);
|
||||
autocomplete_remove(groups_ac, group);
|
||||
} else {
|
||||
g_hash_table_insert(group_count, strdup(group), GINT_TO_POINTER(count));
|
||||
}
|
||||
}
|
||||
curr = g_slist_next(curr);
|
||||
}
|
||||
}
|
||||
|
||||
// remove the contact
|
||||
@ -253,14 +270,51 @@ roster_update(const char *const barejid, const char *const name, GSList *groups,
|
||||
}
|
||||
|
||||
p_contact_set_name(contact, new_name);
|
||||
p_contact_set_groups(contact, groups);
|
||||
_replace_name(current_name, new_name, barejid);
|
||||
|
||||
// add groups
|
||||
while (groups) {
|
||||
autocomplete_add(groups_ac, groups->data);
|
||||
groups = g_slist_next(groups);
|
||||
GSList *curr_new_group = groups;
|
||||
while (curr_new_group) {
|
||||
char *new_group = curr_new_group->data;
|
||||
|
||||
// contact added to group
|
||||
if (!p_contact_in_group(contact, new_group)) {
|
||||
|
||||
// group doesn't yet exist
|
||||
if (!g_hash_table_contains(group_count, new_group)) {
|
||||
g_hash_table_insert(group_count, strdup(new_group), GINT_TO_POINTER(1));
|
||||
autocomplete_add(groups_ac, curr_new_group->data);
|
||||
|
||||
// increment count
|
||||
} else {
|
||||
int count = GPOINTER_TO_INT(g_hash_table_lookup(group_count, new_group));
|
||||
g_hash_table_insert(group_count, strdup(new_group), GINT_TO_POINTER(count + 1));
|
||||
}
|
||||
}
|
||||
curr_new_group = g_slist_next(curr_new_group);
|
||||
}
|
||||
|
||||
GSList *old_groups = p_contact_groups(contact);
|
||||
GSList *curr_old_group = old_groups;
|
||||
while (curr_old_group) {
|
||||
char *old_group = curr_old_group->data;
|
||||
// removed from group
|
||||
if (!g_slist_find_custom(groups, old_group, (GCompareFunc)g_strcmp0)) {
|
||||
if (g_hash_table_contains(group_count, old_group)) {
|
||||
int count = GPOINTER_TO_INT(g_hash_table_lookup(group_count, old_group));
|
||||
count--;
|
||||
if (count < 1) {
|
||||
g_hash_table_remove(group_count, old_group);
|
||||
autocomplete_remove(groups_ac, old_group);
|
||||
} else {
|
||||
g_hash_table_insert(group_count, strdup(old_group), GINT_TO_POINTER(count));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
curr_old_group = g_slist_next(curr_old_group);
|
||||
}
|
||||
|
||||
p_contact_set_groups(contact, groups);
|
||||
}
|
||||
|
||||
gboolean
|
||||
@ -276,9 +330,18 @@ roster_add(const char *const barejid, const char *const name, GSList *groups, co
|
||||
pending_out);
|
||||
|
||||
// add groups
|
||||
while (groups) {
|
||||
autocomplete_add(groups_ac, groups->data);
|
||||
groups = g_slist_next(groups);
|
||||
GSList *curr_new_group = groups;
|
||||
while (curr_new_group) {
|
||||
char *new_group = curr_new_group->data;
|
||||
if (g_hash_table_contains(group_count, new_group)) {
|
||||
int count = GPOINTER_TO_INT(g_hash_table_lookup(group_count, new_group));
|
||||
g_hash_table_insert(group_count, strdup(new_group), GINT_TO_POINTER(count + 1));
|
||||
} else {
|
||||
g_hash_table_insert(group_count, strdup(new_group), GINT_TO_POINTER(1));
|
||||
autocomplete_add(groups_ac, new_group);
|
||||
}
|
||||
|
||||
curr_new_group = g_slist_next(curr_new_group);
|
||||
}
|
||||
|
||||
g_hash_table_insert(contacts, strdup(barejid), contact);
|
||||
|
@ -14,6 +14,7 @@ void empty_list_when_none_added(void **state)
|
||||
roster_init();
|
||||
GSList *list = roster_get_contacts();
|
||||
assert_null(list);
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -23,6 +24,7 @@ void contains_one_element(void **state)
|
||||
roster_add("James", NULL, NULL, NULL, FALSE);
|
||||
GSList *list = roster_get_contacts();
|
||||
assert_int_equal(1, g_slist_length(list));
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -34,6 +36,7 @@ void first_element_correct(void **state)
|
||||
PContact james = list->data;
|
||||
|
||||
assert_string_equal("James", p_contact_barejid(james));
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -45,6 +48,7 @@ void contains_two_elements(void **state)
|
||||
GSList *list = roster_get_contacts();
|
||||
|
||||
assert_int_equal(2, g_slist_length(list));
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -60,6 +64,7 @@ void first_and_second_elements_correct(void **state)
|
||||
|
||||
assert_string_equal("Dave", p_contact_barejid(first));
|
||||
assert_string_equal("James", p_contact_barejid(second));
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -72,6 +77,7 @@ void contains_three_elements(void **state)
|
||||
GSList *list = roster_get_contacts();
|
||||
|
||||
assert_int_equal(3, g_slist_length(list));
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -89,6 +95,7 @@ void first_three_elements_correct(void **state)
|
||||
assert_string_equal("James", p_contact_barejid(james));
|
||||
assert_string_equal("Dave", p_contact_barejid(dave));
|
||||
assert_string_equal("Bob", p_contact_barejid(bob));
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -108,6 +115,7 @@ void add_twice_at_beginning_adds_once(void **state)
|
||||
assert_string_equal("Bob", p_contact_barejid(first));
|
||||
assert_string_equal("Dave", p_contact_barejid(second));
|
||||
assert_string_equal("James", p_contact_barejid(third));
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -127,6 +135,7 @@ void add_twice_in_middle_adds_once(void **state)
|
||||
assert_string_equal("Bob", p_contact_barejid(first));
|
||||
assert_string_equal("Dave", p_contact_barejid(second));
|
||||
assert_string_equal("James", p_contact_barejid(third));
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -146,6 +155,7 @@ void add_twice_at_end_adds_once(void **state)
|
||||
assert_string_equal("Bob", p_contact_barejid(first));
|
||||
assert_string_equal("Dave", p_contact_barejid(second));
|
||||
assert_string_equal("James", p_contact_barejid(third));
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -162,6 +172,7 @@ void find_first_exists(void **state)
|
||||
assert_string_equal("Bob", result);
|
||||
free(result);
|
||||
free(search);
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -175,6 +186,7 @@ void find_second_exists(void **state)
|
||||
char *result = roster_contact_autocomplete("Dav");
|
||||
assert_string_equal("Dave", result);
|
||||
free(result);
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -188,6 +200,7 @@ void find_third_exists(void **state)
|
||||
char *result = roster_contact_autocomplete("Ja");
|
||||
assert_string_equal("James", result);
|
||||
free(result);
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -200,6 +213,7 @@ void find_returns_null(void **state)
|
||||
|
||||
char *result = roster_contact_autocomplete("Mike");
|
||||
assert_null(result);
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -208,6 +222,7 @@ void find_on_empty_returns_null(void **state)
|
||||
roster_init();
|
||||
char *result = roster_contact_autocomplete("James");
|
||||
assert_null(result);
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -223,6 +238,7 @@ void find_twice_returns_second_when_two_match(void **state)
|
||||
assert_string_equal("Jamie", result2);
|
||||
free(result1);
|
||||
free(result2);
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -251,6 +267,7 @@ void find_five_times_finds_fifth(void **state)
|
||||
free(result3);
|
||||
free(result4);
|
||||
free(result5);
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
@ -267,5 +284,133 @@ void find_twice_returns_first_when_two_match_and_reset(void **state)
|
||||
assert_string_equal("James", result2);
|
||||
free(result1);
|
||||
free(result2);
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
void add_contact_with_no_group_returns_no_groups(void **state)
|
||||
{
|
||||
roster_init();
|
||||
roster_add("person@server.org", NULL, NULL, NULL, FALSE);
|
||||
|
||||
GSList *groups_res = roster_get_groups();
|
||||
assert_int_equal(g_slist_length(groups_res), 0);
|
||||
|
||||
g_slist_free_full(groups_res, g_free);
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
void add_contact_with_group_returns_group(void **state)
|
||||
{
|
||||
roster_init();
|
||||
|
||||
GSList *groups = NULL;
|
||||
groups = g_slist_append(groups, strdup("friends"));
|
||||
roster_add("person@server.org", NULL, groups, NULL, FALSE);
|
||||
|
||||
GSList *groups_res = roster_get_groups();
|
||||
assert_int_equal(g_slist_length(groups_res), 1);
|
||||
|
||||
GSList *found = g_slist_find_custom(groups_res, "friends", g_strcmp0);
|
||||
assert_true(found != NULL);
|
||||
assert_string_equal(found->data, "friends");
|
||||
|
||||
g_slist_free_full(groups_res, g_free);
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
void add_contact_with_two_groups_returns_groups(void **state)
|
||||
{
|
||||
roster_init();
|
||||
|
||||
GSList *groups = NULL;
|
||||
groups = g_slist_append(groups, strdup("friends"));
|
||||
groups = g_slist_append(groups, strdup("work"));
|
||||
roster_add("person@server.org", NULL, groups, NULL, FALSE);
|
||||
|
||||
GSList *groups_res = roster_get_groups();
|
||||
assert_int_equal(g_slist_length(groups_res), 2);
|
||||
|
||||
GSList *found = g_slist_find_custom(groups_res, "friends", g_strcmp0);
|
||||
assert_true(found != NULL);
|
||||
assert_string_equal(found->data, "friends");
|
||||
found = g_slist_find_custom(groups_res, "work", g_strcmp0);
|
||||
assert_true(found != NULL);
|
||||
assert_string_equal(found->data, "work");
|
||||
|
||||
g_slist_free_full(groups_res, g_free);
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
void add_contact_with_three_groups_returns_groups(void **state)
|
||||
{
|
||||
roster_init();
|
||||
|
||||
GSList *groups = NULL;
|
||||
groups = g_slist_append(groups, strdup("friends"));
|
||||
groups = g_slist_append(groups, strdup("work"));
|
||||
groups = g_slist_append(groups, strdup("stuff"));
|
||||
roster_add("person@server.org", NULL, groups, NULL, FALSE);
|
||||
|
||||
GSList *groups_res = roster_get_groups();
|
||||
assert_int_equal(g_slist_length(groups_res), 3);
|
||||
|
||||
GSList *found = g_slist_find_custom(groups_res, "friends", g_strcmp0);
|
||||
assert_true(found != NULL);
|
||||
assert_string_equal(found->data, "friends");
|
||||
found = g_slist_find_custom(groups_res, "work", g_strcmp0);
|
||||
assert_true(found != NULL);
|
||||
assert_string_equal(found->data, "work");
|
||||
found = g_slist_find_custom(groups_res, "stuff", g_strcmp0);
|
||||
assert_true(found != NULL);
|
||||
assert_string_equal(found->data, "stuff");
|
||||
|
||||
g_slist_free_full(groups_res, g_free);
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
||||
void add_contact_with_three_groups_update_adding_two_returns_groups(void **state)
|
||||
{
|
||||
roster_init();
|
||||
|
||||
GSList *groups1 = NULL;
|
||||
groups1 = g_slist_append(groups1, strdup("friends"));
|
||||
groups1 = g_slist_append(groups1, strdup("work"));
|
||||
groups1 = g_slist_append(groups1, strdup("stuff"));
|
||||
roster_add("person@server.org", NULL, groups1, NULL, FALSE);
|
||||
|
||||
GSList *groups2 = NULL;
|
||||
groups2 = g_slist_append(groups2, strdup("friends"));
|
||||
groups2 = g_slist_append(groups2, strdup("work"));
|
||||
groups2 = g_slist_append(groups2, strdup("stuff"));
|
||||
groups2 = g_slist_append(groups2, strdup("things"));
|
||||
groups2 = g_slist_append(groups2, strdup("people"));
|
||||
roster_update("person@server.org", NULL, groups2, NULL, FALSE);
|
||||
|
||||
GSList *groups_res = roster_get_groups();
|
||||
assert_int_equal(g_slist_length(groups_res), 5);
|
||||
|
||||
GSList *found = g_slist_find_custom(groups_res, "friends", g_strcmp0);
|
||||
assert_true(found != NULL);
|
||||
assert_string_equal(found->data, "friends");
|
||||
found = g_slist_find_custom(groups_res, "work", g_strcmp0);
|
||||
assert_true(found != NULL);
|
||||
assert_string_equal(found->data, "work");
|
||||
found = g_slist_find_custom(groups_res, "stuff", g_strcmp0);
|
||||
assert_true(found != NULL);
|
||||
assert_string_equal(found->data, "stuff");
|
||||
found = g_slist_find_custom(groups_res, "things", g_strcmp0);
|
||||
assert_true(found != NULL);
|
||||
assert_string_equal(found->data, "things");
|
||||
found = g_slist_find_custom(groups_res, "people", g_strcmp0);
|
||||
assert_true(found != NULL);
|
||||
assert_string_equal(found->data, "people");
|
||||
|
||||
g_slist_free_full(groups_res, g_free);
|
||||
roster_clear();
|
||||
roster_free();
|
||||
}
|
||||
|
@ -16,3 +16,8 @@ void find_on_empty_returns_null(void **state);
|
||||
void find_twice_returns_second_when_two_match(void **state);
|
||||
void find_five_times_finds_fifth(void **state);
|
||||
void find_twice_returns_first_when_two_match_and_reset(void **state);
|
||||
void add_contact_with_no_group_returns_no_groups(void **state);
|
||||
void add_contact_with_group_returns_group(void **state);
|
||||
void add_contact_with_two_groups_returns_groups(void **state);
|
||||
void add_contact_with_three_groups_returns_groups(void **state);
|
||||
void add_contact_with_three_groups_update_adding_two_returns_groups(void **state);
|
||||
|
@ -210,6 +210,11 @@ int main(int argc, char* argv[]) {
|
||||
unit_test(find_twice_returns_second_when_two_match),
|
||||
unit_test(find_five_times_finds_fifth),
|
||||
unit_test(find_twice_returns_first_when_two_match_and_reset),
|
||||
unit_test(add_contact_with_no_group_returns_no_groups),
|
||||
unit_test(add_contact_with_group_returns_group),
|
||||
unit_test(add_contact_with_two_groups_returns_groups),
|
||||
unit_test(add_contact_with_three_groups_returns_groups),
|
||||
unit_test(add_contact_with_three_groups_update_adding_two_returns_groups),
|
||||
|
||||
unit_test_setup_teardown(returns_false_when_chat_session_does_not_exist,
|
||||
init_chat_sessions,
|
||||
|
Loading…
Reference in New Issue
Block a user