changeset 22617:f9cdfdae0594

Between an offline presence with offline-message capability, and a mobile online presence, choose the latter over the former when sending an instant message.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Thu, 03 Apr 2008 00:24:20 +0000
parents 0f8fe131008a
children 357809e70305
files libpurple/status.c
diffstat 1 files changed, 34 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/status.c	Wed Apr 02 16:45:07 2008 +0000
+++ b/libpurple/status.c	Thu Apr 03 00:24:20 2008 +0000
@@ -1547,14 +1547,37 @@
 	return purple_presence_is_online(presence) ? presence->login_time : 0;
 }
 
+static int
+purple_presence_compute_score(const PurplePresence *presence)
+{
+	GList *l;
+	int score = 0;
+
+	for (l = purple_presence_get_statuses(presence); l != NULL; l = l->next) {
+		PurpleStatus *status = (PurpleStatus *)l->data;
+		PurpleStatusType *type = purple_status_get_type(status);
+
+		if (purple_status_is_active(status)) {
+			score += primitive_scores[purple_status_type_get_primitive(type)];
+			if (!purple_status_is_online(status)) {
+				PurpleBuddy *b = purple_presence_get_buddy(presence);
+				if (b && purple_account_supports_offline_message(purple_buddy_get_account(b), b))
+					score += primitive_scores[SCORE_OFFLINE_MESSAGE];
+			}
+		}
+	}
+	score += purple_account_get_int(purple_presence_get_account(presence), "score", 0);
+	if (purple_presence_is_idle(presence))
+		score += primitive_scores[SCORE_IDLE];
+	return score;
+}
+
 gint
 purple_presence_compare(const PurplePresence *presence1,
 		const PurplePresence *presence2)
 {
-	gboolean idle1, idle2;
 	time_t idle_time_1, idle_time_2;
 	int score1 = 0, score2 = 0;
-	GList *l;
 
 	if (presence1 == presence2)
 		return 0;
@@ -1563,49 +1586,18 @@
 	else if (presence2 == NULL)
 		return -1;
 
-	/* Compute the score of the first set of statuses. */
-	for (l = purple_presence_get_statuses(presence1); l != NULL; l = l->next)
-	{
-		PurpleStatus *status = (PurpleStatus *)l->data;
-		PurpleStatusType *type = purple_status_get_type(status);
+	if (purple_presence_is_online(presence1) &&
+			!purple_presence_is_online(presence2))
+		return -1;
+	else if (purple_presence_is_online(presence2) &&
+			!purple_presence_is_online(presence1))
+		return 1;
 
-		if (purple_status_is_active(status)) {
-			score1 += primitive_scores[purple_status_type_get_primitive(type)];
-			if (!purple_status_is_online(status)) {
-				PurpleBuddy *b = purple_presence_get_buddy(presence1);
-				if (b && purple_account_supports_offline_message(purple_buddy_get_account(b),b))
-					score1 += primitive_scores[SCORE_OFFLINE_MESSAGE];
-			}
-		}
-	}
-	score1 += purple_account_get_int(purple_presence_get_account(presence1), "score", 0);
+	/* Compute the score of the first set of statuses. */
+	score1 = purple_presence_compute_score(presence1);
 
 	/* Compute the score of the second set of statuses. */
-	for (l = purple_presence_get_statuses(presence2); l != NULL; l = l->next)
-	{
-		PurpleStatus *status = (PurpleStatus *)l->data;
-		PurpleStatusType *type = purple_status_get_type(status);
-
-		if (purple_status_is_active(status)) {
-			score2 += primitive_scores[purple_status_type_get_primitive(type)];
-			if (!purple_status_is_online(status)) {
-				PurpleBuddy *b = purple_presence_get_buddy(presence2);
-				if (b && purple_account_supports_offline_message(purple_buddy_get_account(b),b))
-					score2 += primitive_scores[SCORE_OFFLINE_MESSAGE];
-			}
-
-		}
-	}
-	score2 += purple_account_get_int(purple_presence_get_account(presence2), "score", 0);
-
-	idle1 = purple_presence_is_idle(presence1);
-	idle2 = purple_presence_is_idle(presence2);
-
-	if (idle1)
-		score1 += primitive_scores[SCORE_IDLE];
-
-	if (idle2)
-		score2 += primitive_scores[SCORE_IDLE];
+	score2 = purple_presence_compute_score(presence2);
 
 	idle_time_1 = time(NULL) - purple_presence_get_idle_time(presence1);
 	idle_time_2 = time(NULL) - purple_presence_get_idle_time(presence2);