From e6b076ce5f24805af94de25918a7a860474186cd Mon Sep 17 00:00:00 2001 From: James Booth Date: Fri, 9 Mar 2012 23:07:53 +0000 Subject: [PATCH] Show status with online contact list --- contact_list.c | 68 +++++++++++++++----- contact_list.h | 13 ++-- jabber.c | 2 +- test_contact_list.c | 151 +++++++++++++++++++++++++------------------- windows.c | 8 ++- 5 files changed, 153 insertions(+), 89 deletions(-) diff --git a/contact_list.c b/contact_list.c index 7c0ca490..cd5b91b0 100644 --- a/contact_list.c +++ b/contact_list.c @@ -27,14 +27,17 @@ // contact list node struct _contact_node_t { - char *contact; + contact_t *contact; struct _contact_node_t *next; }; // the contact list static struct _contact_node_t *_contact_list = NULL; -static struct _contact_node_t * _make_contact_node(const char * const contact); +static struct _contact_node_t * _make_contact_node(const char * const name, + const char * const show); +static contact_t * _new_contact(const char * const name, const char * const show); +static void _destroy_contact(contact_t *contact); void contact_list_clear(void) { @@ -42,7 +45,8 @@ void contact_list_clear(void) if (curr) { while(curr) { - free(curr->contact); + contact_t *contact = curr->contact; + _destroy_contact(contact); curr = curr->next; } @@ -51,7 +55,7 @@ void contact_list_clear(void) } } -int contact_list_remove(const char * const contact) +int contact_list_remove(const char * const name) { if (!_contact_list) { return 0; @@ -60,13 +64,14 @@ int contact_list_remove(const char * const contact) struct _contact_node_t *prev = NULL; while(curr) { - if (strcmp(curr->contact, contact) == 0) { + contact_t *contact = curr->contact; + if (strcmp(contact->name, name) == 0) { if (prev) prev->next = curr->next; else _contact_list = curr->next; - - free(curr->contact); + + _destroy_contact(contact); free(curr); return 1; @@ -80,10 +85,11 @@ int contact_list_remove(const char * const contact) } } -int contact_list_add(const char * const contact) +int contact_list_add(const char * const name, const char * const show) { + if (!_contact_list) { - _contact_list = _make_contact_node(contact); + _contact_list = _make_contact_node(name, show); return 1; } else { @@ -91,14 +97,15 @@ int contact_list_add(const char * const contact) struct _contact_node_t *prev = NULL; while(curr) { - if (strcmp(curr->contact, contact) == 0) + contact_t *curr_contact = curr->contact; + if (strcmp(curr_contact->name, name) == 0) return 0; prev = curr; curr = curr->next; } - curr = _make_contact_node(contact); + curr = _make_contact_node(name, show); if (prev) prev->next = curr; @@ -119,12 +126,12 @@ contact_list_t *get_contact_list(void) if (!curr) { list->contacts = NULL; } else { - list->contacts = (char **) malloc(sizeof(char **)); + list->contacts = (contact_t **) malloc(sizeof(contact_t *)); while(curr) { + contact_t *curr_contact = curr->contact; list->contacts[count] = - (char *) malloc((strlen(curr->contact) + 1) * sizeof(char)); - strcpy(list->contacts[count], curr->contact); + _new_contact(curr_contact->name, curr_contact->show); count++; curr = curr->next; } @@ -135,14 +142,41 @@ contact_list_t *get_contact_list(void) return list; } -struct _contact_node_t * _make_contact_node(const char * const contact) +struct _contact_node_t * _make_contact_node(const char * const name, + const char * const show) { struct _contact_node_t *new = (struct _contact_node_t *) malloc(sizeof(struct _contact_node_t)); - new->contact = (char *) malloc((strlen(contact) + 1) * sizeof(char)); - strcpy(new->contact, contact); + new->contact = _new_contact(name, show); new->next = NULL; return new; } +static contact_t * _new_contact(const char * const name, const char * const show) +{ + contact_t *new = (contact_t *) malloc(sizeof(contact_t)); + new->name = (char *) malloc((strlen(name) + 1) * sizeof(char)); + strcpy(new->name, name); + + if (show) { + new->show = (char *) malloc((strlen(show) + 1) * sizeof(char)); + strcpy(new->show, show); + } else { + new->show = NULL; + } + + return new; +} + +static void _destroy_contact(contact_t *contact) +{ + free(contact->name); + + if (contact->show) { + free(contact->show); + } + + free(contact); +} + diff --git a/contact_list.h b/contact_list.h index 01fbd46f..ce8aa5cf 100644 --- a/contact_list.h +++ b/contact_list.h @@ -23,14 +23,19 @@ #ifndef CONTACT_LIST_H #define CONTACT_LIST_H +typedef struct _contact { + char *name; + char *show; +} contact_t; + typedef struct _contact_list_t { - char **contacts; + contact_t **contacts; int size; } contact_list_t; void contact_list_clear(void); -int contact_list_add(const char * const contact); -int contact_list_remove(const char * const contact); -contact_list_t *get_contact_list(void); +int contact_list_add(const char * const name, const char * const show); +int contact_list_remove(const char * const name); +contact_list_t * get_contact_list(void); #endif diff --git a/jabber.c b/jabber.c index b6d54fbb..2fad6b8c 100644 --- a/jabber.c +++ b/jabber.c @@ -313,7 +313,7 @@ static int _jabber_presence_handler(xmpp_conn_t * const conn, if (type == NULL) {// online win_contact_online(short_from, show_str, status_str); - contact_list_add(short_from); + contact_list_add(short_from, show_str); } else {// offline win_contact_offline(short_from, show_str, status_str); contact_list_remove(short_from); diff --git a/test_contact_list.c b/test_contact_list.c index 794e436b..f570268a 100644 --- a/test_contact_list.c +++ b/test_contact_list.c @@ -15,23 +15,24 @@ static void empty_list_when_none_added(void) static void contains_one_element(void) { - contact_list_add("James"); + contact_list_add("James", NULL); contact_list_t *list = get_contact_list(); assert_int_equals(1, list->size); } static void first_element_correct(void) { - contact_list_add("James"); + contact_list_add("James", NULL); contact_list_t *list = get_contact_list(); + contact_t *james = list->contacts[0]; - assert_string_equals("James", list->contacts[0]); + assert_string_equals("James", james->name); } static void contains_two_elements(void) { - contact_list_add("James"); - contact_list_add("Dave"); + contact_list_add("James", NULL); + contact_list_add("Dave", NULL); contact_list_t *list = get_contact_list(); assert_int_equals(2, list->size); @@ -39,19 +40,21 @@ static void contains_two_elements(void) static void first_and_second_elements_correct(void) { - contact_list_add("James"); - contact_list_add("Dave"); + contact_list_add("James", NULL); + contact_list_add("Dave", NULL); contact_list_t *list = get_contact_list(); + contact_t *james = list->contacts[0]; + contact_t *dave = list->contacts[1]; - assert_string_equals("James", list->contacts[0]); - assert_string_equals("Dave", list->contacts[1]); + assert_string_equals("James", james->name); + assert_string_equals("Dave", dave->name); } static void contains_three_elements(void) { - contact_list_add("James"); - contact_list_add("Dave"); - contact_list_add("Bob"); + contact_list_add("James", NULL); + contact_list_add("Dave", NULL); + contact_list_add("Bob", NULL); contact_list_t *list = get_contact_list(); assert_int_equals(3, list->size); @@ -59,59 +62,68 @@ static void contains_three_elements(void) static void first_three_elements_correct(void) { - contact_list_add("James"); - contact_list_add("Dave"); - contact_list_add("Bob"); + contact_list_add("James", NULL); + contact_list_add("Dave", NULL); + contact_list_add("Bob", NULL); contact_list_t *list = get_contact_list(); + contact_t *james = list->contacts[0]; + contact_t *dave = list->contacts[1]; + contact_t *bob = list->contacts[2]; - assert_string_equals("James", list->contacts[0]); - assert_string_equals("Dave", list->contacts[1]); - assert_string_equals("Bob", list->contacts[2]); + assert_string_equals("James", james->name); + assert_string_equals("Dave", dave->name); + assert_string_equals("Bob", bob->name); } static void add_twice_at_beginning_adds_once(void) { - contact_list_add("James"); - contact_list_add("James"); - contact_list_add("Dave"); - contact_list_add("Bob"); + contact_list_add("James", NULL); + contact_list_add("James", NULL); + contact_list_add("Dave", NULL); + contact_list_add("Bob", NULL); contact_list_t *list = get_contact_list(); + contact_t *james = list->contacts[0]; + contact_t *dave = list->contacts[1]; + contact_t *bob = list->contacts[2]; assert_int_equals(3, list->size); - assert_string_equals("James", list->contacts[0]); - assert_string_equals("Dave", list->contacts[1]); - assert_string_equals("Bob", list->contacts[2]); - + assert_string_equals("James", james->name); + assert_string_equals("Dave", dave->name); + assert_string_equals("Bob", bob->name); } static void add_twice_in_middle_adds_once(void) { - contact_list_add("James"); - contact_list_add("Dave"); - contact_list_add("James"); - contact_list_add("Bob"); + contact_list_add("James", NULL); + contact_list_add("Dave", NULL); + contact_list_add("James", NULL); + contact_list_add("Bob", NULL); contact_list_t *list = get_contact_list(); + contact_t *james = list->contacts[0]; + contact_t *dave = list->contacts[1]; + contact_t *bob = list->contacts[2]; assert_int_equals(3, list->size); - assert_string_equals("James", list->contacts[0]); - assert_string_equals("Dave", list->contacts[1]); - assert_string_equals("Bob", list->contacts[2]); - + assert_string_equals("James", james->name); + assert_string_equals("Dave", dave->name); + assert_string_equals("Bob", bob->name); } static void add_twice_at_end_adds_once(void) { - contact_list_add("James"); - contact_list_add("Dave"); - contact_list_add("Bob"); - contact_list_add("James"); + contact_list_add("James", NULL); + contact_list_add("Dave", NULL); + contact_list_add("Bob", NULL); + contact_list_add("James", NULL); contact_list_t *list = get_contact_list(); + contact_t *james = list->contacts[0]; + contact_t *dave = list->contacts[1]; + contact_t *bob = list->contacts[2]; assert_int_equals(3, list->size); - assert_string_equals("James", list->contacts[0]); - assert_string_equals("Dave", list->contacts[1]); - assert_string_equals("Bob", list->contacts[2]); - + assert_string_equals("James", james->name); + assert_string_equals("Dave", dave->name); + assert_string_equals("Bob", bob->name); } static void remove_when_none_does_nothing(void) @@ -124,7 +136,7 @@ static void remove_when_none_does_nothing(void) static void remove_when_one_removes(void) { - contact_list_add("James"); + contact_list_add("James", NULL); contact_list_remove("James"); contact_list_t *list = get_contact_list(); @@ -133,68 +145,79 @@ static void remove_when_one_removes(void) static void remove_first_when_two(void) { - contact_list_add("James"); - contact_list_add("Dave"); + contact_list_add("James", NULL); + contact_list_add("Dave", NULL); contact_list_remove("James"); contact_list_t *list = get_contact_list(); assert_int_equals(1, list->size); - assert_string_equals("Dave", list->contacts[0]); + contact_t *dave = list->contacts[0]; + assert_string_equals("Dave", dave->name); } static void remove_second_when_two(void) { - contact_list_add("James"); - contact_list_add("Dave"); + contact_list_add("James", NULL); + contact_list_add("Dave", NULL); contact_list_remove("Dave"); contact_list_t *list = get_contact_list(); assert_int_equals(1, list->size); - assert_string_equals("James", list->contacts[0]); + contact_t *james = list->contacts[0]; + assert_string_equals("James", james->name); } static void remove_first_when_three(void) { - contact_list_add("James"); - contact_list_add("Dave"); - contact_list_add("Bob"); + contact_list_add("James", NULL); + contact_list_add("Dave", NULL); + contact_list_add("Bob", NULL); contact_list_remove("James"); contact_list_t *list = get_contact_list(); assert_int_equals(2, list->size); - assert_string_equals("Dave", list->contacts[0]); - assert_string_equals("Bob", list->contacts[1]); + contact_t *dave = list->contacts[0]; + contact_t *bob = list->contacts[1]; + + assert_string_equals("Dave", dave->name); + assert_string_equals("Bob", bob->name); } static void remove_second_when_three(void) { - contact_list_add("James"); - contact_list_add("Dave"); - contact_list_add("Bob"); + contact_list_add("James", NULL); + contact_list_add("Dave", NULL); + contact_list_add("Bob", NULL); contact_list_remove("Dave"); contact_list_t *list = get_contact_list(); assert_int_equals(2, list->size); - assert_string_equals("James", list->contacts[0]); - assert_string_equals("Bob", list->contacts[1]); + contact_t *james = list->contacts[0]; + contact_t *bob = list->contacts[1]; + + assert_string_equals("James", james->name); + assert_string_equals("Bob", bob->name); } static void remove_third_when_three(void) { - contact_list_add("James"); - contact_list_add("Dave"); - contact_list_add("Bob"); + contact_list_add("James", NULL); + contact_list_add("Dave", NULL); + contact_list_add("Bob", NULL); contact_list_remove("Bob"); contact_list_t *list = get_contact_list(); assert_int_equals(2, list->size); - assert_string_equals("James", list->contacts[0]); - assert_string_equals("Dave", list->contacts[1]); + contact_t *james = list->contacts[0]; + contact_t *dave = list->contacts[1]; + + assert_string_equals("James", james->name); + assert_string_equals("Dave", dave->name); } void register_contact_list_tests(void) diff --git a/windows.c b/windows.c index 5d5161c2..8c4e973b 100644 --- a/windows.c +++ b/windows.c @@ -240,13 +240,15 @@ void cons_show_online_contacts(const contact_list_t * const list) int i; for (i = 0; i < list->size; i++) { - char *contact = list->contacts[i]; + contact_t *contact = list->contacts[i]; _win_show_time(_cons_win); wattron(_cons_win, COLOR_PAIR(2)); - wprintw(_cons_win, "%s\n", contact); + wprintw(_cons_win, "%s", contact->name); + if (contact->show) + wprintw(_cons_win, ", %s", contact->show); + wprintw(_cons_win, "\n"); wattroff(_cons_win, COLOR_PAIR(2)); } - } void cons_bad_show(const char * const msg)