pidgin: 06b54841: jabber: Treat JabberBuddy->resources as ...

darkrain42 at pidgin.im darkrain42 at pidgin.im
Tue Feb 16 10:23:05 EST 2010


-----------------------------------------------------------------
Revision: 06b54841abdd2f3fb9f13ce08946429e4ea5543b
Ancestor: 8ac4cde63cff22e9546dec112b7bdef365a04322
Author: darkrain42 at pidgin.im
Date: 2010-02-15T06:07:53
Branch: im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/06b54841abdd2f3fb9f13ce08946429e4ea5543b

Modified files:
        libpurple/protocols/jabber/buddy.c
        libpurple/protocols/jabber/buddy.h

ChangeLog: 

jabber: Treat JabberBuddy->resources as a sorted list.

Instead of having jabber_buddy_find_resource() do a bunch of
calculations, just do all those when presence changes.  The first entry
in the list is the highest-priority resource.

-------------- next part --------------
============================================================
--- libpurple/protocols/jabber/buddy.c	d88210ef5f22ba33b392bc31e79e435eedfda294
+++ libpurple/protocols/jabber/buddy.c	849ebc9d6ffdb829029ef7670f511456e86aec8e
@@ -177,48 +177,45 @@ JabberBuddyResource *jabber_buddy_find_r
 JabberBuddyResource *jabber_buddy_find_resource(JabberBuddy *jb,
 		const char *resource)
 {
-	JabberBuddyResource *jbr = NULL;
 	GList *l;
 
-	if(!jb)
+	if (!jb)
 		return NULL;
 
-	for(l = jb->resources; l; l = l->next)
+	if (resource == NULL)
+		return jb->resources ? jb->resources->data : NULL;
+
+	for (l = jb->resources; l; l = l->next)
 	{
-		JabberBuddyResource *tmp = (JabberBuddyResource *) l->data;
-		if (!jbr && !resource) {
-			jbr = tmp;
-		} else if (!resource) {
-			if (resource_compare_cb(tmp, jbr) < 0)
-				jbr = tmp;
-		} else if(tmp->name) {
-			if(!strcmp(tmp->name, resource)) {
-				jbr = tmp;
-				break;
-			}
-		}
+		JabberBuddyResource *jbr = l->data;
+		if (g_str_equal(resource, jbr->name))
+			return jbr;
 	}
 
-	return jbr;
+	return NULL;
 }
 
 JabberBuddyResource *jabber_buddy_track_resource(JabberBuddy *jb, const char *resource,
 		int priority, JabberBuddyState state, const char *status)
 {
+	/* TODO: Optimization: Only reinsert if priority+state changed */
 	JabberBuddyResource *jbr = jabber_buddy_find_resource(jb, resource);
-	if(!jbr) {
+	if (jbr) {
+		jb->resources = g_list_remove(jb->resources, jbr);
+	} else {
 		jbr = g_new0(JabberBuddyResource, 1);
 		jbr->jb = jb;
 		jbr->name = g_strdup(resource);
 		jbr->capabilities = JABBER_CAP_NONE;
 		jbr->tz_off = PURPLE_NO_TZ_OFF;
-		jb->resources = g_list_append(jb->resources, jbr);
 	}
 	jbr->priority = priority;
 	jbr->state = state;
 	g_free(jbr->status);
 	jbr->status = g_strdup(status);
 
+	jb->resources = g_list_insert_sorted(jb->resources, jbr,
+	                                     resource_compare_cb);
 	return jbr;
 }
 
@@ -802,6 +799,8 @@ static void jabber_buddy_info_show_if_re
 		jbr = jabber_buddy_find_resource(jbi->jb, resource_name);
 		add_jbr_info(jbi, resource_name, jbr);
 	} else {
+		/* TODO: This is in priority-ascending order (lowest prio first), because
+		 * everything is prepended.  Is that ok? */
 		for (resources = jbi->jb->resources; resources; resources = resources->next) {
 			jbr = resources->data;
 
============================================================
--- libpurple/protocols/jabber/buddy.h	d719dd25eabaa7c09429c0c3a73403768742ebd2
+++ libpurple/protocols/jabber/buddy.h	533af9d5419405050575642e18da8085b5c92fb2
@@ -31,6 +31,13 @@ struct _JabberBuddy {
 #include "jutil.h"
 
 struct _JabberBuddy {
+	/**
+	 * A sorted list of resources in priority descending order.
+	 * This means that the first resource in the list is the
+	 * "most available" (see resource_compare_cb in buddy.c for
+	 * details).  Don't play with this yourself, let
+	 * jabber_buddy_track_resource and jabber_buddy_remove_resource do it.
+	 */
 	GList *resources;
 	char *error_msg;
 	enum {


More information about the Commits mailing list