diff --git a/src/command/commands.c b/src/command/commands.c index 9447f648..cdbce0f8 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -887,7 +887,7 @@ cmd_export(ProfWin *window, const char *const command, gchar **args) if (-1 == write(fd, "jid,name\n", strlen("jid,name\n"))) goto write_error; - list = roster_get_contacts(ROSTER_ORD_NAME, TRUE); + list = roster_get_contacts(ROSTER_ORD_NAME); if (list) { GSList *curr = list; while (curr){ @@ -1623,13 +1623,13 @@ _who_roster(ProfWin *window, const char *const command, gchar **args) cons_show(""); GSList *list = NULL; if (group) { - list = roster_get_group(group, ROSTER_ORD_NAME, TRUE); + list = roster_get_group(group, ROSTER_ORD_NAME); if (list == NULL) { cons_show("No such group: %s.", group); return; } } else { - list = roster_get_contacts(ROSTER_ORD_NAME, TRUE); + list = roster_get_contacts(ROSTER_ORD_NAME); if (list == NULL) { cons_show("No contacts in roster."); return; @@ -1931,7 +1931,7 @@ cmd_group(ProfWin *window, const char *const command, gchar **args) return TRUE; } - GSList *list = roster_get_group(group, ROSTER_ORD_NAME, TRUE); + GSList *list = roster_get_group(group, ROSTER_ORD_NAME); cons_show_roster_group(group, list); return TRUE; } @@ -2014,7 +2014,7 @@ cmd_roster(ProfWin *window, const char *const command, gchar **args) return TRUE; } - GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE); + GSList *list = roster_get_contacts(ROSTER_ORD_NAME); cons_show_roster(list); g_slist_free(list); return TRUE; @@ -2591,7 +2591,7 @@ cmd_roster(ProfWin *window, const char *const command, gchar **args) return TRUE; } - GSList *all = roster_get_contacts(ROSTER_ORD_NAME, TRUE); + GSList *all = roster_get_contacts(ROSTER_ORD_NAME); GSList *curr = all; while (curr) { PContact contact = curr->data; diff --git a/src/roster_list.c b/src/roster_list.c index be99112e..7eb613ac 100644 --- a/src/roster_list.c +++ b/src/roster_list.c @@ -406,7 +406,7 @@ roster_get_contacts_by_presence(const char *const presence) } GSList* -roster_get_contacts(roster_ord_t order, gboolean include_offline) +roster_get_contacts(roster_ord_t order) { assert(roster != NULL); @@ -424,12 +424,6 @@ roster_get_contacts(roster_ord_t order, gboolean include_offline) g_hash_table_iter_init(&iter, roster->contacts); while (g_hash_table_iter_next(&iter, &key, &value)) { - PContact contact = value; - const char *presence = p_contact_presence(contact); - if (!include_offline && (g_strcmp0(presence, "offline") == 0)) { - continue; - } - result = g_slist_insert_sorted(result, value, cmp_func); } @@ -494,7 +488,7 @@ roster_fulljid_autocomplete(const char *const search_str) } GSList* -roster_get_group(const char *const group, roster_ord_t order, gboolean include_offline) +roster_get_group(const char *const group, roster_ord_t order) { assert(roster != NULL); @@ -512,12 +506,6 @@ roster_get_group(const char *const group, roster_ord_t order, gboolean include_o g_hash_table_iter_init(&iter, roster->contacts); while (g_hash_table_iter_next(&iter, &key, &value)) { - PContact contact = value; - const char *presence = p_contact_presence(contact); - if (!include_offline && (g_strcmp0(presence, "offline") == 0)) { - continue; - } - GSList *groups = p_contact_groups(value); if (group == NULL) { if (groups == NULL) { diff --git a/src/roster_list.h b/src/roster_list.h index 1b35febd..6ff9c391 100644 --- a/src/roster_list.h +++ b/src/roster_list.h @@ -59,12 +59,12 @@ void roster_update(const char *const barejid, const char *const name, GSList *gr gboolean roster_add(const char *const barejid, const char *const name, GSList *groups, const char *const subscription, gboolean pending_out); char* roster_barejid_from_name(const char *const name); -GSList* roster_get_contacts(roster_ord_t order, gboolean include_offline); +GSList* roster_get_contacts(roster_ord_t order); GSList* roster_get_contacts_online(void); gboolean roster_has_pending_subscriptions(void); char* roster_contact_autocomplete(const char *const search_str); char* roster_fulljid_autocomplete(const char *const search_str); -GSList* roster_get_group(const char *const group, roster_ord_t order, gboolean include_offline); +GSList* roster_get_group(const char *const group, roster_ord_t order); GSList* roster_get_groups(void); char* roster_group_autocomplete(const char *const search_str); char* roster_barejid_autocomplete(const char *const search_str); diff --git a/src/ui/console.c b/src/ui/console.c index f2c1f35b..7f80a227 100644 --- a/src/ui/console.c +++ b/src/ui/console.c @@ -621,7 +621,7 @@ void cons_show_sent_subs(void) { if (roster_has_pending_subscriptions()) { - GSList *contacts = roster_get_contacts(ROSTER_ORD_NAME, TRUE); + GSList *contacts = roster_get_contacts(ROSTER_ORD_NAME); PContact contact = NULL; cons_show("Awaiting subscription responses from:"); GSList *curr = contacts; diff --git a/src/ui/rosterwin.c b/src/ui/rosterwin.c index 4144b660..971c6fd3 100644 --- a/src/ui/rosterwin.c +++ b/src/ui/rosterwin.c @@ -68,6 +68,8 @@ static void _rosterwin_room(ProfLayoutSplit *layout, ProfMucWin *mucwin); static void _rosterwin_private_chats(ProfLayoutSplit *layout); static void _rosterwin_private_header(ProfLayoutSplit *layout, GList *privs); +static GSList* _filter_contacts(GSList *contacts); +static GSList* _filter_contacts_with_presence(GSList *contacts, const char *const presence); static theme_item_t _get_roster_theme(roster_contact_theme_t theme_type, const char *presence); static int _compare_rooms_name(ProfMucWin *a, ProfMucWin *b); static int _compare_rooms_unread(ProfMucWin *a, ProfMucWin *b); @@ -134,50 +136,50 @@ _rosterwin_contacts_all(ProfLayoutSplit *layout, gboolean newline) GSList *contacts = NULL; char *order = prefs_get_string(PREF_ROSTER_ORDER); - gboolean offline = prefs_get_boolean(PREF_ROSTER_OFFLINE); if (g_strcmp0(order, "presence") == 0) { - contacts = roster_get_contacts(ROSTER_ORD_PRESENCE, offline); + contacts = roster_get_contacts(ROSTER_ORD_PRESENCE); } else { - contacts = roster_get_contacts(ROSTER_ORD_NAME, offline); + contacts = roster_get_contacts(ROSTER_ORD_NAME); } prefs_free_string(order); - _rosterwin_contacts_header(layout, "Roster", newline, contacts); + GSList *filtered_contacts = _filter_contacts(contacts); + g_slist_free(contacts); - if (contacts) { - GSList *curr_contact = contacts; + _rosterwin_contacts_header(layout, "Roster", newline, filtered_contacts); + + if (filtered_contacts) { + GSList *curr_contact = filtered_contacts; while (curr_contact) { PContact contact = curr_contact->data; _rosterwin_contact(layout, contact); curr_contact = g_slist_next(curr_contact); } } - g_slist_free(contacts); + g_slist_free(filtered_contacts); } static void _rosterwin_contacts_by_presence(ProfLayoutSplit *layout, const char *const presence, char *title, gboolean newline) { - if ((g_strcmp0(presence, "offline") == 0) && !prefs_get_boolean(PREF_ROSTER_OFFLINE)) { - return; - } - GSList *contacts = roster_get_contacts_by_presence(presence); + GSList *filtered_contacts = _filter_contacts_with_presence(contacts, presence); + g_slist_free(contacts); // if this group has contacts, or if we want to show empty groups - if (contacts || prefs_get_boolean(PREF_ROSTER_EMPTY)) { - _rosterwin_contacts_header(layout, title, newline, contacts); + if (filtered_contacts || prefs_get_boolean(PREF_ROSTER_EMPTY)) { + _rosterwin_contacts_header(layout, title, newline, filtered_contacts); } - if (contacts) { - GSList *curr_contact = contacts; + if (filtered_contacts) { + GSList *curr_contact = filtered_contacts; while (curr_contact) { PContact contact = curr_contact->data; _rosterwin_contact(layout, contact); curr_contact = g_slist_next(curr_contact); } } - g_slist_free(contacts); + g_slist_free(filtered_contacts); } static void @@ -186,29 +188,31 @@ _rosterwin_contacts_by_group(ProfLayoutSplit *layout, char *group, gboolean newl GSList *contacts = NULL; char *order = prefs_get_string(PREF_ROSTER_ORDER); - gboolean offline = prefs_get_boolean(PREF_ROSTER_OFFLINE); if (g_strcmp0(order, "presence") == 0) { - contacts = roster_get_group(group, ROSTER_ORD_PRESENCE, offline); + contacts = roster_get_group(group, ROSTER_ORD_PRESENCE); } else { - contacts = roster_get_group(group, ROSTER_ORD_NAME, offline); + contacts = roster_get_group(group, ROSTER_ORD_NAME); } prefs_free_string(order); - if (contacts || prefs_get_boolean(PREF_ROSTER_EMPTY)) { + GSList *filtered_contacts = _filter_contacts(contacts); + g_slist_free(contacts); + + if (filtered_contacts || prefs_get_boolean(PREF_ROSTER_EMPTY)) { if (group) { - _rosterwin_contacts_header(layout, group, newline, contacts); + _rosterwin_contacts_header(layout, group, newline, filtered_contacts); } else { - _rosterwin_contacts_header(layout, "no group", newline, contacts); + _rosterwin_contacts_header(layout, "no group", newline, filtered_contacts); } - GSList *curr_contact = contacts; + GSList *curr_contact = filtered_contacts; while (curr_contact) { PContact contact = curr_contact->data; _rosterwin_contact(layout, contact); curr_contact = g_slist_next(curr_contact); } } - g_slist_free(contacts); + g_slist_free(filtered_contacts); } static void @@ -915,3 +919,82 @@ _rosterwin_private_header(ProfLayoutSplit *layout, GList *privs) g_string_free(title_str, TRUE); } + +static GSList* +_filter_contacts(GSList *contacts) +{ + GSList *filtered_contacts = NULL; + + // if show offline, include all contacts + if (prefs_get_boolean(PREF_ROSTER_OFFLINE)) { + GSList *curr = contacts; + while (curr) { + filtered_contacts = g_slist_append(filtered_contacts, curr->data); + curr = g_slist_next(curr); + } + // if dont show offline + } else { + GSList *curr = contacts; + while (curr) { + PContact contact = curr->data; + const char *presence = p_contact_presence(contact); + + // include if offline and unread messages + if (g_strcmp0(presence, "offline") == 0) { + ProfChatWin *chatwin = wins_get_chat(p_contact_barejid(contact)); + if (chatwin && chatwin->unread > 0) { + filtered_contacts = g_slist_append(filtered_contacts, contact); + } + + // include if not offline + } else { + filtered_contacts = g_slist_append(filtered_contacts, contact); + } + curr = g_slist_next(curr); + } + } + + return filtered_contacts; +} + +static GSList* +_filter_contacts_with_presence(GSList *contacts, const char *const presence) +{ + GSList *filtered_contacts = NULL; + + // handling offline contacts + if (g_strcmp0(presence, "offline") == 0) { + + // if show offline, include all contacts + if (prefs_get_boolean(PREF_ROSTER_OFFLINE)) { + GSList *curr = contacts; + while (curr) { + filtered_contacts = g_slist_append(filtered_contacts, curr->data); + curr = g_slist_next(curr); + } + + // otherwise show if unread messages + } else { + GSList *curr = contacts; + while (curr) { + PContact contact = curr->data; + ProfChatWin *chatwin = wins_get_chat(p_contact_barejid(contact)); + if (chatwin && chatwin->unread > 0) { + filtered_contacts = g_slist_append(filtered_contacts, contact); + } + curr = g_slist_next(curr); + } + } + + // any other presence, include all + } else { + GSList *curr = contacts; + while (curr) { + filtered_contacts = g_slist_append(filtered_contacts, curr->data); + curr = g_slist_next(curr); + } + } + + return filtered_contacts; +} + diff --git a/tests/unittests/test_cmd_roster.c b/tests/unittests/test_cmd_roster.c index 8874bdb2..054328be 100644 --- a/tests/unittests/test_cmd_roster.c +++ b/tests/unittests/test_cmd_roster.c @@ -55,7 +55,7 @@ void cmd_roster_shows_roster_when_no_args(void **state) roster_create(); roster_add("bob@server.org", "bob", NULL, "both", FALSE); - GSList *roster = roster_get_contacts(ROSTER_ORD_NAME, TRUE); + GSList *roster = roster_get_contacts(ROSTER_ORD_NAME); expect_memory(cons_show_roster, list, roster, sizeof(roster)); diff --git a/tests/unittests/test_roster_list.c b/tests/unittests/test_roster_list.c index 786f3b97..9df23495 100644 --- a/tests/unittests/test_roster_list.c +++ b/tests/unittests/test_roster_list.c @@ -12,7 +12,7 @@ void empty_list_when_none_added(void **state) { roster_create(); - GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE); + GSList *list = roster_get_contacts(ROSTER_ORD_NAME); assert_null(list); roster_destroy(); } @@ -21,7 +21,7 @@ void contains_one_element(void **state) { roster_create(); roster_add("James", NULL, NULL, NULL, FALSE); - GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE); + GSList *list = roster_get_contacts(ROSTER_ORD_NAME); assert_int_equal(1, g_slist_length(list)); roster_destroy(); } @@ -30,7 +30,7 @@ void first_element_correct(void **state) { roster_create(); roster_add("James", NULL, NULL, NULL, FALSE); - GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE); + GSList *list = roster_get_contacts(ROSTER_ORD_NAME); PContact james = list->data; assert_string_equal("James", p_contact_barejid(james)); @@ -42,7 +42,7 @@ void contains_two_elements(void **state) roster_create(); roster_add("James", NULL, NULL, NULL, FALSE); roster_add("Dave", NULL, NULL, NULL, FALSE); - GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE); + GSList *list = roster_get_contacts(ROSTER_ORD_NAME); assert_int_equal(2, g_slist_length(list)); roster_destroy(); @@ -53,7 +53,7 @@ void first_and_second_elements_correct(void **state) roster_create(); roster_add("James", NULL, NULL, NULL, FALSE); roster_add("Dave", NULL, NULL, NULL, FALSE); - GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE); + GSList *list = roster_get_contacts(ROSTER_ORD_NAME); PContact first = list->data; PContact second = (g_slist_next(list))->data; @@ -69,7 +69,7 @@ void contains_three_elements(void **state) roster_add("James", NULL, NULL, NULL, FALSE); roster_add("Bob", NULL, NULL, NULL, FALSE); roster_add("Dave", NULL, NULL, NULL, FALSE); - GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE); + GSList *list = roster_get_contacts(ROSTER_ORD_NAME); assert_int_equal(3, g_slist_length(list)); roster_destroy(); @@ -81,7 +81,7 @@ void first_three_elements_correct(void **state) roster_add("Bob", NULL, NULL, NULL, FALSE); roster_add("Dave", NULL, NULL, NULL, FALSE); roster_add("James", NULL, NULL, NULL, FALSE); - GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE); + GSList *list = roster_get_contacts(ROSTER_ORD_NAME); PContact bob = list->data; PContact dave = (g_slist_next(list))->data; PContact james = (g_slist_next(g_slist_next(list)))->data; @@ -99,7 +99,7 @@ void add_twice_at_beginning_adds_once(void **state) roster_add("James", NULL, NULL, NULL, FALSE); roster_add("Dave", NULL, NULL, NULL, FALSE); roster_add("Bob", NULL, NULL, NULL, FALSE); - GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE); + GSList *list = roster_get_contacts(ROSTER_ORD_NAME); PContact first = list->data; PContact second = (g_slist_next(list))->data; PContact third = (g_slist_next(g_slist_next(list)))->data; @@ -118,7 +118,7 @@ void add_twice_in_middle_adds_once(void **state) roster_add("Dave", NULL, NULL, NULL, FALSE); roster_add("James", NULL, NULL, NULL, FALSE); roster_add("Bob", NULL, NULL, NULL, FALSE); - GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE); + GSList *list = roster_get_contacts(ROSTER_ORD_NAME); PContact first = list->data; PContact second = (g_slist_next(list))->data; PContact third = (g_slist_next(g_slist_next(list)))->data; @@ -137,7 +137,7 @@ void add_twice_at_end_adds_once(void **state) roster_add("Dave", NULL, NULL, NULL, FALSE); roster_add("Bob", NULL, NULL, NULL, FALSE); roster_add("James", NULL, NULL, NULL, FALSE); - GSList *list = roster_get_contacts(ROSTER_ORD_NAME, TRUE); + GSList *list = roster_get_contacts(ROSTER_ORD_NAME); PContact first = list->data; PContact second = (g_slist_next(list))->data; PContact third = (g_slist_next(g_slist_next(list)))->data;