about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-11-18 00:06:00 +0000
committerJames Booth <boothj5@gmail.com>2015-11-18 00:06:00 +0000
commit2260e3bd6f7e99fd16f05372c7545b8e2379ff6e (patch)
treeac0a6aa5f378e0a26c13f5d8ff37f50dfe354143
parent8e8852fa9f0171bb2c0826303b97655a32e481ca (diff)
downloadprofani-tty-2260e3bd6f7e99fd16f05372c7545b8e2379ff6e.tar.gz
Pass ordering to roster list functions
-rw-r--r--src/command/commands.c10
-rw-r--r--src/roster_list.c82
-rw-r--r--src/roster_list.h14
-rw-r--r--src/ui/console.c2
-rw-r--r--src/ui/rosterwin.c12
-rw-r--r--tests/unittests/test_cmd_roster.c2
-rw-r--r--tests/unittests/test_roster_list.c20
7 files changed, 52 insertions, 90 deletions
diff --git a/src/command/commands.c b/src/command/commands.c
index 67f8ae98..c1edfdb2 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -1320,13 +1320,13 @@ _who_roster(ProfWin *window, const char *const command, gchar **args)
     cons_show("");
     GSList *list = NULL;
     if (group) {
-        list = roster_get_group(group);
+        list = roster_get_group(group, ROSTER_ORD_NAME);
         if (list == NULL) {
             cons_show("No such group: %s.", group);
             return;
         }
     } else {
-        list = roster_get_contacts();
+        list = roster_get_contacts(ROSTER_ORD_NAME);
         if (list == NULL) {
             cons_show("No contacts in roster.");
             return;
@@ -1628,7 +1628,7 @@ cmd_group(ProfWin *window, const char *const command, gchar **args)
             return TRUE;
         }
 
-        GSList *list = roster_get_group(group);
+        GSList *list = roster_get_group(group, ROSTER_ORD_NAME);
         cons_show_roster_group(group, list);
         return TRUE;
     }
@@ -1711,7 +1711,7 @@ cmd_roster(ProfWin *window, const char *const command, gchar **args)
             return TRUE;
         }
 
-        GSList *list = roster_get_contacts();
+        GSList *list = roster_get_contacts(ROSTER_ORD_NAME);
         cons_show_roster(list);
         g_slist_free(list);
         return TRUE;
@@ -1931,7 +1931,7 @@ cmd_roster(ProfWin *window, const char *const command, gchar **args)
             return TRUE;
         }
 
-        GSList *all = roster_get_contacts();
+        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 fe3063dd..2f1a0b55 100644
--- a/src/roster_list.c
+++ b/src/roster_list.c
@@ -383,33 +383,23 @@ roster_get_contacts_by_presence(const char *const presence)
 }
 
 GSList*
-roster_get_contacts_ord_presence(void)
+roster_get_contacts(roster_ord_t order)
 {
     GSList *result = NULL;
     GHashTableIter iter;
     gpointer key;
     gpointer value;
 
-    g_hash_table_iter_init(&iter, contacts);
-    while (g_hash_table_iter_next(&iter, &key, &value)) {
-        result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_presence);
+    GCompareFunc cmp_func;
+    if (order == ROSTER_ORD_PRESENCE) {
+        cmp_func = (GCompareFunc) _compare_presence;
+    } else {
+        cmp_func = (GCompareFunc) _compare_name;
     }
 
-    // return all contact structs
-    return result;
-}
-
-GSList*
-roster_get_contacts(void)
-{
-    GSList *result = NULL;
-    GHashTableIter iter;
-    gpointer key;
-    gpointer value;
-
     g_hash_table_iter_init(&iter, contacts);
     while (g_hash_table_iter_next(&iter, &key, &value)) {
-        result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_name);
+        result = g_slist_insert_sorted(result, value, cmp_func);
     }
 
     // return all contact structs
@@ -465,38 +455,25 @@ roster_fulljid_autocomplete(const char *const search_str)
 }
 
 GSList*
-roster_get_nogroup_ord_presence(void)
+roster_get_nogroup(roster_ord_t order)
 {
     GSList *result = NULL;
     GHashTableIter iter;
     gpointer key;
     gpointer value;
 
-    g_hash_table_iter_init(&iter, contacts);
-    while (g_hash_table_iter_next(&iter, &key, &value)) {
-        GSList *groups = p_contact_groups(value);
-        if (groups == NULL) {
-            result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_presence);
-        }
+    GCompareFunc cmp_func;
+    if (order == ROSTER_ORD_PRESENCE) {
+        cmp_func = (GCompareFunc) _compare_presence;
+    } else {
+        cmp_func = (GCompareFunc) _compare_name;
     }
 
-    // return all contact structs
-    return result;
-}
-
-GSList*
-roster_get_nogroup(void)
-{
-    GSList *result = NULL;
-    GHashTableIter iter;
-    gpointer key;
-    gpointer value;
-
     g_hash_table_iter_init(&iter, contacts);
     while (g_hash_table_iter_next(&iter, &key, &value)) {
         GSList *groups = p_contact_groups(value);
         if (groups == NULL) {
-            result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_name);
+            result = g_slist_insert_sorted(result, value, cmp_func);
         }
     }
 
@@ -505,43 +482,26 @@ roster_get_nogroup(void)
 }
 
 GSList*
-roster_get_group_ord_presence(const char *const group)
+roster_get_group(const char *const group, roster_ord_t order)
 {
     GSList *result = NULL;
     GHashTableIter iter;
     gpointer key;
     gpointer value;
 
-    g_hash_table_iter_init(&iter, contacts);
-    while (g_hash_table_iter_next(&iter, &key, &value)) {
-        GSList *groups = p_contact_groups(value);
-        while (groups) {
-            if (strcmp(groups->data, group) == 0) {
-                result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_presence);
-                break;
-            }
-            groups = g_slist_next(groups);
-        }
+    GCompareFunc cmp_func;
+    if (order == ROSTER_ORD_PRESENCE) {
+        cmp_func = (GCompareFunc) _compare_presence;
+    } else {
+        cmp_func = (GCompareFunc) _compare_name;
     }
 
-    // return all contact structs
-    return result;
-}
-
-GSList*
-roster_get_group(const char *const group)
-{
-    GSList *result = NULL;
-    GHashTableIter iter;
-    gpointer key;
-    gpointer value;
-
     g_hash_table_iter_init(&iter, contacts);
     while (g_hash_table_iter_next(&iter, &key, &value)) {
         GSList *groups = p_contact_groups(value);
         while (groups) {
             if (strcmp(groups->data, group) == 0) {
-                result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_name);
+                result = g_slist_insert_sorted(result, value, cmp_func);
                 break;
             }
             groups = g_slist_next(groups);
diff --git a/src/roster_list.h b/src/roster_list.h
index da4bbbcd..15cdacef 100644
--- a/src/roster_list.h
+++ b/src/roster_list.h
@@ -40,6 +40,11 @@
 #include "resource.h"
 #include "contact.h"
 
+typedef enum {
+    ROSTER_ORD_NAME,
+    ROSTER_ORD_PRESENCE
+} roster_ord_t;
+
 void roster_clear(void);
 gboolean roster_update_presence(const char *const barejid, Resource *resource, GDateTime *last_activity);
 PContact roster_get_contact(const char *const barejid);
@@ -54,20 +59,17 @@ 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(void);
-GSList* roster_get_contacts_ord_presence(void);
+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);
-GSList* roster_get_group_ord_presence(const char *const group);
+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);
 GSList* roster_get_contacts_by_presence(const char *const presence);
-GSList* roster_get_nogroup(void);
-GSList* roster_get_nogroup_ord_presence(void);
+GSList* roster_get_nogroup(roster_ord_t order);
 char* roster_get_msg_display_name(const char *const barejid, const char *const resource);
 
 #endif
diff --git a/src/ui/console.c b/src/ui/console.c
index 882a9b54..0c6733c4 100644
--- a/src/ui/console.c
+++ b/src/ui/console.c
@@ -527,7 +527,7 @@ void
 cons_show_sent_subs(void)
 {
    if (roster_has_pending_subscriptions()) {
-        GSList *contacts = roster_get_contacts();
+        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 95d374bd..a689f033 100644
--- a/src/ui/rosterwin.c
+++ b/src/ui/rosterwin.c
@@ -171,9 +171,9 @@ _rosterwin_contacts_by_group(ProfLayoutSplit *layout, char *group)
 
     char *order = prefs_get_string(PREF_ROSTER_ORDER);
     if (g_strcmp0(order, "presence") == 0) {
-        contacts = roster_get_group_ord_presence(group);
+        contacts = roster_get_group(group, ROSTER_ORD_PRESENCE);
     } else {
-        contacts = roster_get_group(group);
+        contacts = roster_get_group(group, ROSTER_ORD_NAME);
     }
 
     if (contacts) {
@@ -194,9 +194,9 @@ _rosterwin_contacts_by_no_group(ProfLayoutSplit *layout)
 
     char *order = prefs_get_string(PREF_ROSTER_ORDER);
     if (g_strcmp0(order, "presence") == 0) {
-        contacts = roster_get_nogroup_ord_presence();
+        contacts = roster_get_nogroup(ROSTER_ORD_PRESENCE);
     } else {
-        contacts = roster_get_nogroup();
+        contacts = roster_get_nogroup(ROSTER_ORD_NAME);
     }
 
     if (contacts) {
@@ -248,9 +248,9 @@ rosterwin_roster(void)
 
             char *order = prefs_get_string(PREF_ROSTER_ORDER);
             if (g_strcmp0(order, "presence") == 0) {
-                contacts = roster_get_contacts_ord_presence();
+                contacts = roster_get_contacts(ROSTER_ORD_PRESENCE);
             } else {
-                contacts = roster_get_contacts();
+                contacts = roster_get_contacts(ROSTER_ORD_NAME);
             }
 
             if (contacts) {
diff --git a/tests/unittests/test_cmd_roster.c b/tests/unittests/test_cmd_roster.c
index bcc2193a..0ba49237 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_init();
     roster_add("bob@server.org", "bob", NULL, "both", FALSE);
-    GSList *roster = roster_get_contacts();
+    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 f12361e4..e2644d41 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_init();
-    GSList *list = roster_get_contacts();
+    GSList *list = roster_get_contacts(ROSTER_ORD_NAME);
     assert_null(list);
     roster_clear();
     roster_free();
@@ -22,7 +22,7 @@ void contains_one_element(void **state)
 {
     roster_init();
     roster_add("James", NULL, NULL, NULL, FALSE);
-    GSList *list = roster_get_contacts();
+    GSList *list = roster_get_contacts(ROSTER_ORD_NAME);
     assert_int_equal(1, g_slist_length(list));
     roster_clear();
     roster_free();
@@ -32,7 +32,7 @@ void first_element_correct(void **state)
 {
     roster_init();
     roster_add("James", NULL, NULL, NULL, FALSE);
-    GSList *list = roster_get_contacts();
+    GSList *list = roster_get_contacts(ROSTER_ORD_NAME);
     PContact james = list->data;
 
     assert_string_equal("James", p_contact_barejid(james));
@@ -45,7 +45,7 @@ void contains_two_elements(void **state)
     roster_init();
     roster_add("James", NULL, NULL, NULL, FALSE);
     roster_add("Dave", NULL, NULL, NULL, FALSE);
-    GSList *list = roster_get_contacts();
+    GSList *list = roster_get_contacts(ROSTER_ORD_NAME);
 
     assert_int_equal(2, g_slist_length(list));
     roster_clear();
@@ -57,7 +57,7 @@ void first_and_second_elements_correct(void **state)
     roster_init();
     roster_add("James", NULL, NULL, NULL, FALSE);
     roster_add("Dave", NULL, NULL, NULL, FALSE);
-    GSList *list = roster_get_contacts();
+    GSList *list = roster_get_contacts(ROSTER_ORD_NAME);
 
     PContact first = list->data;
     PContact second = (g_slist_next(list))->data;
@@ -74,7 +74,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();
+    GSList *list = roster_get_contacts(ROSTER_ORD_NAME);
 
     assert_int_equal(3, g_slist_length(list));
     roster_clear();
@@ -87,7 +87,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();
+    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;
@@ -106,7 +106,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();
+    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;
@@ -126,7 +126,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();
+    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;
@@ -146,7 +146,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();
+    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;