1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Only create contacts resource when online

This commit is contained in:
James Booth 2013-02-10 11:19:36 +00:00
parent 84a4ab9545
commit ec78914044
6 changed files with 98 additions and 118 deletions

View File

@ -64,8 +64,10 @@ p_contact_new(const char * const barejid, const char * const name,
contact->resources = g_hash_table_new_full(g_str_hash, g_str_equal, free, contact->resources = g_hash_table_new_full(g_str_hash, g_str_equal, free,
(GDestroyNotify)resource_destroy); (GDestroyNotify)resource_destroy);
// TODO, priority, last activity // TODO, priority, last activity
Resource *resource = resource_new("default", presence, status, 0, caps_str); if (g_strcmp0(presence, "offline") != 0) {
g_hash_table_insert(contact->resources, strdup(resource->name), resource); Resource *resource = resource_new("default", presence, status, 0, caps_str);
g_hash_table_insert(contact->resources, strdup(resource->name), resource);
}
return contact; return contact;
} }
@ -127,15 +129,23 @@ p_contact_name(const PContact contact)
const char * const char *
p_contact_presence(const PContact contact) p_contact_presence(const PContact contact)
{ {
Resource *resource = g_hash_table_lookup(contact->resources, "default"); if (g_hash_table_size(contact->resources) == 0) {
return resource->show; return "offline";
} else {
Resource *resource = g_hash_table_lookup(contact->resources, "default");
return resource->show;
}
} }
const char * const char *
p_contact_status(const PContact contact) p_contact_status(const PContact contact)
{ {
Resource *resource = g_hash_table_lookup(contact->resources, "default"); if (g_hash_table_size(contact->resources) == 0) {
return resource->status; return NULL;
} else {
Resource *resource = g_hash_table_lookup(contact->resources, "default");
return resource->status;
}
} }
const char * const char *
@ -159,17 +169,32 @@ p_contact_last_activity(const PContact contact)
const char * const char *
p_contact_caps_str(const PContact contact) p_contact_caps_str(const PContact contact)
{ {
Resource *resource = g_hash_table_lookup(contact->resources, "default"); if (g_hash_table_size(contact->resources) == 0) {
return resource->caps_str; return NULL;
} else {
Resource *resource = g_hash_table_lookup(contact->resources, "default");
return resource->caps_str;
}
} }
void void
p_contact_set_presence(const PContact contact, const char * const presence) p_contact_set_presence(const PContact contact, const char * const presence)
{ {
Resource *resource = g_hash_table_lookup(contact->resources, "default"); if (g_strcmp0(presence, "offline") == 0) {
FREE_SET_NULL(resource->show); g_hash_table_remove(contact->resources, "default");
if (presence != NULL) { } else {
resource->show = strdup(presence); if (g_hash_table_size(contact->resources) == 0) {
Resource *resource = resource_new("default", presence, NULL, 0, NULL);
g_hash_table_insert(contact->resources, strdup(resource->name), resource);
} else {
Resource *resource = g_hash_table_lookup(contact->resources, "default");
if (presence != NULL) {
FREE_SET_NULL(resource->show);
resource->show = strdup(presence);
} else {
resource->show = NULL;
}
}
} }
} }

View File

@ -64,14 +64,13 @@ contact_list_reset_search_attempts(void)
gboolean gboolean
contact_list_add(const char * const barejid, const char * const name, contact_list_add(const char * const barejid, const char * const name,
const char * const presence, const char * const status,
const char * const subscription, gboolean pending_out) const char * const subscription, gboolean pending_out)
{ {
gboolean added = FALSE; gboolean added = FALSE;
PContact contact = g_hash_table_lookup(contacts, barejid); PContact contact = g_hash_table_lookup(contacts, barejid);
if (contact == NULL) { if (contact == NULL) {
contact = p_contact_new(barejid, name, presence, status, subscription, contact = p_contact_new(barejid, name, "offline", NULL, subscription,
pending_out, NULL); pending_out, NULL);
g_hash_table_insert(contacts, strdup(barejid), contact); g_hash_table_insert(contacts, strdup(barejid), contact);
autocomplete_add(ac, strdup(barejid)); autocomplete_add(ac, strdup(barejid));

View File

@ -33,7 +33,6 @@ void contact_list_free(void);
void contact_list_reset_search_attempts(void); void contact_list_reset_search_attempts(void);
void contact_list_remove(const char * const barejid); void contact_list_remove(const char * const barejid);
gboolean contact_list_add(const char * const barejid, const char * const name, gboolean contact_list_add(const char * const barejid, const char * const name,
const char * const presence, const char * const status,
const char * const subscription, gboolean pending_out); const char * const subscription, gboolean pending_out);
gboolean contact_list_update_contact(const char * const barejid, const char * const presence, gboolean contact_list_update_contact(const char * const barejid, const char * const presence,
const char * const status, GDateTime *last_activity, const char * const caps_str); const char * const status, GDateTime *last_activity, const char * const caps_str);

View File

@ -30,6 +30,7 @@
Resource * resource_new(const char * const name, const char * const show, Resource * resource_new(const char * const name, const char * const show,
const char * const status, const int priority, const char * const caps_str) const char * const status, const int priority, const char * const caps_str)
{ {
assert(g_strcmp0(show, "offline") != 0);
assert(name != NULL); assert(name != NULL);
Resource *new_resource = malloc(sizeof(struct resource_t)); Resource *new_resource = malloc(sizeof(struct resource_t));
new_resource->name = strdup(name); new_resource->name = strdup(name);

View File

@ -144,8 +144,7 @@ _iq_handle_roster_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
pending_out = TRUE; pending_out = TRUE;
} }
gboolean added = contact_list_add(barejid, name, "offline", NULL, sub, gboolean added = contact_list_add(barejid, name, sub, pending_out);
pending_out);
if (!added) { if (!added) {
log_warning("Attempt to add contact twice: %s", barejid); log_warning("Attempt to add contact twice: %s", barejid);

View File

@ -31,14 +31,14 @@ static void empty_list_when_none_added(void)
static void contains_one_element(void) static void contains_one_element(void)
{ {
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
GSList *list = get_contact_list(); GSList *list = get_contact_list();
assert_int_equals(1, g_slist_length(list)); assert_int_equals(1, g_slist_length(list));
} }
static void first_element_correct(void) static void first_element_correct(void)
{ {
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
GSList *list = get_contact_list(); GSList *list = get_contact_list();
PContact james = list->data; PContact james = list->data;
@ -47,8 +47,8 @@ static void first_element_correct(void)
static void contains_two_elements(void) static void contains_two_elements(void)
{ {
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_add("Dave", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Dave", NULL, NULL, FALSE);
GSList *list = get_contact_list(); GSList *list = get_contact_list();
assert_int_equals(2, g_slist_length(list)); assert_int_equals(2, g_slist_length(list));
@ -56,8 +56,8 @@ static void contains_two_elements(void)
static void first_and_second_elements_correct(void) static void first_and_second_elements_correct(void)
{ {
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_add("Dave", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Dave", NULL, NULL, FALSE);
GSList *list = get_contact_list(); GSList *list = get_contact_list();
PContact first = list->data; PContact first = list->data;
@ -69,9 +69,9 @@ static void first_and_second_elements_correct(void)
static void contains_three_elements(void) static void contains_three_elements(void)
{ {
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_add("Bob", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Bob", NULL, NULL, FALSE);
contact_list_add("Dave", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Dave", NULL, NULL, FALSE);
GSList *list = get_contact_list(); GSList *list = get_contact_list();
assert_int_equals(3, g_slist_length(list)); assert_int_equals(3, g_slist_length(list));
@ -79,9 +79,9 @@ static void contains_three_elements(void)
static void first_three_elements_correct(void) static void first_three_elements_correct(void)
{ {
contact_list_add("Bob", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Bob", NULL, NULL, FALSE);
contact_list_add("Dave", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Dave", NULL, NULL, FALSE);
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
GSList *list = get_contact_list(); GSList *list = get_contact_list();
PContact bob = list->data; PContact bob = list->data;
PContact dave = (g_slist_next(list))->data; PContact dave = (g_slist_next(list))->data;
@ -94,10 +94,10 @@ static void first_three_elements_correct(void)
static void add_twice_at_beginning_adds_once(void) static void add_twice_at_beginning_adds_once(void)
{ {
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_add("Dave", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Dave", NULL, NULL, FALSE);
contact_list_add("Bob", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Bob", NULL, NULL, FALSE);
GSList *list = get_contact_list(); GSList *list = get_contact_list();
PContact first = list->data; PContact first = list->data;
PContact second = (g_slist_next(list))->data; PContact second = (g_slist_next(list))->data;
@ -111,10 +111,10 @@ static void add_twice_at_beginning_adds_once(void)
static void add_twice_in_middle_adds_once(void) static void add_twice_in_middle_adds_once(void)
{ {
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_add("Dave", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Dave", NULL, NULL, FALSE);
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_add("Bob", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Bob", NULL, NULL, FALSE);
GSList *list = get_contact_list(); GSList *list = get_contact_list();
PContact first = list->data; PContact first = list->data;
PContact second = (g_slist_next(list))->data; PContact second = (g_slist_next(list))->data;
@ -128,10 +128,10 @@ static void add_twice_in_middle_adds_once(void)
static void add_twice_at_end_adds_once(void) static void add_twice_at_end_adds_once(void)
{ {
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_add("Dave", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Dave", NULL, NULL, FALSE);
contact_list_add("Bob", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Bob", NULL, NULL, FALSE);
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
GSList *list = get_contact_list(); GSList *list = get_contact_list();
PContact first = list->data; PContact first = list->data;
PContact second = (g_slist_next(list))->data; PContact second = (g_slist_next(list))->data;
@ -143,45 +143,18 @@ static void add_twice_at_end_adds_once(void)
assert_string_equals("Bob", p_contact_barejid(third)); assert_string_equals("Bob", p_contact_barejid(third));
} }
static void test_show_when_value(void)
{
contact_list_add("James", NULL, "away", NULL, NULL, FALSE);
GSList *list = get_contact_list();
PContact james = list->data;
assert_string_equals("away", p_contact_presence(james));
}
static void test_show_online_when_no_value(void) static void test_show_online_when_no_value(void)
{ {
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
GSList *list = get_contact_list(); GSList *list = get_contact_list();
PContact james = list->data; PContact james = list->data;
assert_string_equals("online", p_contact_presence(james)); assert_string_equals("offline", p_contact_presence(james));
}
static void test_show_online_when_empty_string(void)
{
contact_list_add("James", NULL, "", NULL, NULL, FALSE);
GSList *list = get_contact_list();
PContact james = list->data;
assert_string_equals("online", p_contact_presence(james));
}
static void test_status_when_value(void)
{
contact_list_add("James", NULL, NULL, "I'm not here right now", NULL, FALSE);
GSList *list = get_contact_list();
PContact james = list->data;
assert_string_equals("I'm not here right now", p_contact_status(james));
} }
static void test_status_when_no_value(void) static void test_status_when_no_value(void)
{ {
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
GSList *list = get_contact_list(); GSList *list = get_contact_list();
PContact james = list->data; PContact james = list->data;
@ -190,7 +163,7 @@ static void test_status_when_no_value(void)
static void update_show(void) static void update_show(void)
{ {
contact_list_add("James", NULL, "away", NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_update_contact("James", "dnd", NULL, NULL, NULL); contact_list_update_contact("James", "dnd", NULL, NULL, NULL);
GSList *list = get_contact_list(); GSList *list = get_contact_list();
@ -200,21 +173,9 @@ static void update_show(void)
assert_string_equals("dnd", p_contact_presence(first)); assert_string_equals("dnd", p_contact_presence(first));
} }
static void set_show_to_null(void)
{
contact_list_add("James", NULL, "away", NULL, NULL, FALSE);
contact_list_update_contact("James", NULL, NULL, NULL, NULL);
GSList *list = get_contact_list();
assert_int_equals(1, g_slist_length(list));
PContact james = list->data;
assert_string_equals("James", p_contact_barejid(james));
assert_is_null(p_contact_presence(james));
}
static void update_status(void) static void update_status(void)
{ {
contact_list_add("James", NULL, NULL, "I'm not here right now", NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_update_contact("James", NULL, "Gone to lunch", NULL, NULL); contact_list_update_contact("James", NULL, "Gone to lunch", NULL, NULL);
GSList *list = get_contact_list(); GSList *list = get_contact_list();
@ -226,7 +187,7 @@ static void update_status(void)
static void set_status_to_null(void) static void set_status_to_null(void)
{ {
contact_list_add("James", NULL, NULL, "Gone to lunch", NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_update_contact("James", NULL, NULL, NULL, NULL); contact_list_update_contact("James", NULL, NULL, NULL, NULL);
GSList *list = get_contact_list(); GSList *list = get_contact_list();
@ -238,9 +199,9 @@ static void set_status_to_null(void)
static void find_first_exists(void) static void find_first_exists(void)
{ {
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_add("Dave", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Dave", NULL, NULL, FALSE);
contact_list_add("Bob", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Bob", NULL, NULL, FALSE);
char *search = (char *) malloc(2 * sizeof(char)); char *search = (char *) malloc(2 * sizeof(char));
strcpy(search, "B"); strcpy(search, "B");
@ -253,9 +214,9 @@ static void find_first_exists(void)
static void find_second_exists(void) static void find_second_exists(void)
{ {
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_add("Dave", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Dave", NULL, NULL, FALSE);
contact_list_add("Bob", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Bob", NULL, NULL, FALSE);
char *result = contact_list_find_contact("Dav"); char *result = contact_list_find_contact("Dav");
assert_string_equals("Dave", result); assert_string_equals("Dave", result);
@ -264,9 +225,9 @@ static void find_second_exists(void)
static void find_third_exists(void) static void find_third_exists(void)
{ {
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_add("Dave", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Dave", NULL, NULL, FALSE);
contact_list_add("Bob", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Bob", NULL, NULL, FALSE);
char *result = contact_list_find_contact("Ja"); char *result = contact_list_find_contact("Ja");
assert_string_equals("James", result); assert_string_equals("James", result);
@ -275,9 +236,9 @@ static void find_third_exists(void)
static void find_returns_null(void) static void find_returns_null(void)
{ {
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_add("Dave", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Dave", NULL, NULL, FALSE);
contact_list_add("Bob", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Bob", NULL, NULL, FALSE);
char *result = contact_list_find_contact("Mike"); char *result = contact_list_find_contact("Mike");
assert_is_null(result); assert_is_null(result);
@ -291,9 +252,9 @@ static void find_on_empty_returns_null(void)
static void find_twice_returns_second_when_two_match(void) static void find_twice_returns_second_when_two_match(void)
{ {
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_add("Jamie", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Jamie", NULL, NULL, FALSE);
contact_list_add("Bob", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Bob", NULL, NULL, FALSE);
char *result1 = contact_list_find_contact("Jam"); char *result1 = contact_list_find_contact("Jam");
char *result2 = contact_list_find_contact(result1); char *result2 = contact_list_find_contact(result1);
@ -304,16 +265,16 @@ static void find_twice_returns_second_when_two_match(void)
static void find_five_times_finds_fifth(void) static void find_five_times_finds_fifth(void)
{ {
contact_list_add("Jama", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Jama", NULL, NULL, FALSE);
contact_list_add("Jamb", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Jamb", NULL, NULL, FALSE);
contact_list_add("Mike", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Mike", NULL, NULL, FALSE);
contact_list_add("Dave", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Dave", NULL, NULL, FALSE);
contact_list_add("Jamm", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Jamm", NULL, NULL, FALSE);
contact_list_add("Jamn", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Jamn", NULL, NULL, FALSE);
contact_list_add("Matt", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Matt", NULL, NULL, FALSE);
contact_list_add("Jamo", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Jamo", NULL, NULL, FALSE);
contact_list_add("Jamy", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Jamy", NULL, NULL, FALSE);
contact_list_add("Jamz", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Jamz", NULL, NULL, FALSE);
char *result1 = contact_list_find_contact("Jam"); char *result1 = contact_list_find_contact("Jam");
char *result2 = contact_list_find_contact(result1); char *result2 = contact_list_find_contact(result1);
@ -330,9 +291,9 @@ static void find_five_times_finds_fifth(void)
static void find_twice_returns_first_when_two_match_and_reset(void) static void find_twice_returns_first_when_two_match_and_reset(void)
{ {
contact_list_add("James", NULL, NULL, NULL, NULL, FALSE); contact_list_add("James", NULL, NULL, FALSE);
contact_list_add("Jamie", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Jamie", NULL, NULL, FALSE);
contact_list_add("Bob", NULL, NULL, NULL, NULL, FALSE); contact_list_add("Bob", NULL, NULL, FALSE);
char *result1 = contact_list_find_contact("Jam"); char *result1 = contact_list_find_contact("Jam");
contact_list_reset_search_attempts(); contact_list_reset_search_attempts();
@ -358,13 +319,9 @@ void register_contact_list_tests(void)
TEST(add_twice_at_beginning_adds_once); TEST(add_twice_at_beginning_adds_once);
TEST(add_twice_in_middle_adds_once); TEST(add_twice_in_middle_adds_once);
TEST(add_twice_at_end_adds_once); TEST(add_twice_at_end_adds_once);
TEST(test_show_when_value);
TEST(test_show_online_when_no_value); TEST(test_show_online_when_no_value);
TEST(test_show_online_when_empty_string);
TEST(test_status_when_value);
TEST(test_status_when_no_value); TEST(test_status_when_no_value);
TEST(update_show); TEST(update_show);
TEST(set_show_to_null);
TEST(update_status); TEST(update_status);
TEST(set_status_to_null); TEST(set_status_to_null);
TEST(find_first_exists); TEST(find_first_exists);