[empathy: 3/5] Clean up sorting function to take top/bottom lists of fake groups



commit 1a665bc714aa3c9789c3f0c46307f3e93679f15e
Author: Danielle Madeley <danielle madeley collabora co uk>
Date:   Mon Mar 22 17:02:21 2010 +1100

    Clean up sorting function to take top/bottom lists of fake groups
    
    Even if we choose not to sort People Nearby, this patch is useful because it
    makes how the sorting of groups is done much clearer and allows us to add
    other fake groups in the future.

 libempathy-gtk/empathy-contact-list-store.c |   61 +++++++++++++++++++++++----
 1 files changed, 52 insertions(+), 9 deletions(-)
---
diff --git a/libempathy-gtk/empathy-contact-list-store.c b/libempathy-gtk/empathy-contact-list-store.c
index 25a2e7a..55cddd4 100644
--- a/libempathy-gtk/empathy-contact-list-store.c
+++ b/libempathy-gtk/empathy-contact-list-store.c
@@ -1541,6 +1541,20 @@ contact_list_store_get_group (EmpathyContactListStore *store,
 }
 
 static gint
+get_position (const char **strv,
+	      const char *str)
+{
+	int i;
+
+	for (i = 0; strv[i] != NULL; i++) {
+		if (!tp_strdiff (strv[i], str))
+			return i;
+	}
+
+	return -1;
+}
+
+static gint
 compare_separator_and_groups (gboolean is_separator_a,
 			      gboolean is_separator_b,
 			      const gchar *name_a,
@@ -1550,6 +1564,19 @@ compare_separator_and_groups (gboolean is_separator_a,
 			      gboolean fake_group_a,
 			      gboolean fake_group_b)
 {
+	/* these two lists are the sorted list of fake groups to include at the
+	 * top and bottom of the roster */
+	const char *top_groups[] = {
+		EMPATHY_CONTACT_LIST_STORE_FAVORITE,
+		EMPATHY_CONTACT_LIST_STORE_PEOPLE_NEARBY,
+		NULL
+	};
+
+	const char *bottom_groups[] = {
+		EMPATHY_CONTACT_LIST_STORE_UNGROUPED,
+		NULL
+	};
+
 	if (is_separator_a || is_separator_b) {
 		/* We have at least one separator */
 		if (is_separator_a) {
@@ -1565,18 +1592,34 @@ compare_separator_and_groups (gboolean is_separator_a,
 	} else if (contact_a && !contact_b) {
 		return -1;
 	} else if (!contact_a && !contact_b) {
-		/* Two groups. The 'Ungrouped' fake group is display at the bottom of the
-		 * contact list and the 'Favorites' at the top. */
-		if (fake_group_a && !tp_strdiff (name_a, EMPATHY_CONTACT_LIST_STORE_UNGROUPED))
-			return 1;
-		else if (fake_group_b && !tp_strdiff (name_b, EMPATHY_CONTACT_LIST_STORE_UNGROUPED))
+		gboolean a_in_top, b_in_top, a_in_bottom, b_in_bottom;
+
+		a_in_top = fake_group_a &&
+			tp_strv_contains (top_groups, name_a);
+		b_in_top = fake_group_b &&
+			tp_strv_contains (top_groups, name_b);
+		a_in_bottom = fake_group_b &&
+			tp_strv_contains (bottom_groups, name_a);
+		b_in_bottom = fake_group_b &&
+			tp_strv_contains (bottom_groups, name_b);
+
+		if (a_in_top && b_in_top) {
+			/* compare positions */
+			return CLAMP (get_position (top_groups, name_a) -
+				      get_position (top_groups, name_b),
+				      -1, 1);
+		} else if (a_in_bottom && b_in_bottom) {
+			/* compare positions */
+			return CLAMP (get_position (bottom_groups, name_a) -
+				      get_position (bottom_groups, name_b),
+				      -1, 1);
+		} else if (a_in_top || b_in_bottom) {
 			return -1;
-		else if (fake_group_a && !tp_strdiff (name_a, EMPATHY_CONTACT_LIST_STORE_FAVORITE))
-			return -1;
-		else if (fake_group_b && !tp_strdiff (name_b, EMPATHY_CONTACT_LIST_STORE_FAVORITE))
+		} else if (b_in_top || a_in_bottom) {
 			return 1;
-		else
+		} else {
 			return g_utf8_collate (name_a, name_b);
+		}
 	}
 
 	/* Two contacts, ordering depends of the sorting policy */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]