gobjectification: 2ef380ad: Refactored remove_buddy, a real mess

aluink at soc.pidgin.im aluink at soc.pidgin.im
Mon Jul 6 00:25:34 EDT 2009


-----------------------------------------------------------------
Revision: 2ef380ad3a75f214245ca5e1a34e7fc3d4e23914
Ancestor: ccec64d5bc8b9689b1e222692ca3245e280527eb
Author: aluink at soc.pidgin.im
Date: 2009-07-06T04:22:45
Branch: im.pidgin.gobjectification
URL: http://d.pidgin.im/viewmtn/revision/info/2ef380ad3a75f214245ca5e1a34e7fc3d4e23914

Modified files:
        libpurple/blist-node.c libpurple/blist-node.h
        libpurple/blist.c libpurple/buddy.c libpurple/buddy.h
        libpurple/contact.c libpurple/contact.h libpurple/group.c

ChangeLog: 

Refactored remove_buddy, a real mess

-------------- next part --------------
============================================================
--- libpurple/blist-node.c	235981fa31a0347b6426691dbc02e3d8ae4655d2
+++ libpurple/blist-node.c	284f162c202c47dce41901211e5a7148ea6382bc
@@ -430,6 +430,31 @@ purple_blist_node_get_extended_menu(Purp
 	return menu;
 }
 
+static void
+purple_blist_node_add_node(PurpleBlistNode *parent, PurpleBlistNode *child)
+{
+}
+
+static void
+purple_blist_node_remove_node(PurpleBlistNode *group, PurpleBlistNode *child)
+{
+	PurpleBlistNodeClass *gklass = PURPLE_GET_BLIST_NODE_CLASS(group);
+	PurpleBlistNode *contact = purple_blist_node_get_parent(child);
+	PurpleBlistNodeClass *cklass = PURPLE_GET_BLIST_NODE_CLASS(contact);
+
+	/* Remove the node from its parent */
+	if (child->prev)
+		child->prev->next = child->next;
+	if (child->next)
+		child->next->prev = child->prev;
+	/* Is the child the first in the list? */
+	if ((child->parent != NULL) && (child->parent->child == child))
+		child->parent->child = child->next;
+
+	cklass->remove_node(contact, child);
+	gklass->remove_node(group, contact);
+}
+
 /******************/
 /*  GObject Code  */
 /******************/
@@ -484,6 +509,9 @@ purple_blist_node_class_init(PurpleBlist
 	obj_class->finalize = purple_blist_node_finalize;
 
 	g_type_class_add_private(klass, sizeof(PurpleBlistNodePrivate));
+
+	klass->add_node = purple_blist_node_add_node;
+	klass->remove_node = purple_blist_node_remove_node;
 }
 
 static void
============================================================
--- libpurple/blist-node.h	933fa4444f3647002eb3de7375f051d97646c5cf
+++ libpurple/blist-node.h	328c1b48a8d24bc2b1bc00afad46944896b6f4d6
@@ -75,6 +75,14 @@ struct _PurpleBlistNodeClass {
 
 struct _PurpleBlistNodeClass {
 	PurpleObjectClass parent;
+
+	void (*add_node)(PurpleBlistNode *parent, PurpleBlistNode *child);
+	void (*remove_node)(PurpleBlistNode *parent, PurpleBlistNode *child);
+
+	void (*purple_reserved1)(void);
+	void (*purple_reserved2)(void);
+	void (*purple_reserved3)(void);
+	void (*purple_reserved4)(void);
 };
 
 /**
============================================================
--- libpurple/blist.c	b469b39f9cc9a908c834379fa43e4b9bc5dcca85
+++ libpurple/blist.c	35079d994842b0aad4a1e6a418ab888ffd3f6dd1
@@ -548,9 +548,9 @@ void purple_blist_add_contact(PurpleCont
 			}
 		}
 
-		if (contact->online > 0)
+		if (purple_contact_get_online(contact) > 0)
 			((PurpleGroup*)cnode->parent)->online--;
-		if (contact->currentsize > 0)
+		if (purple_contact_get_currentsize(contact) > 0)
 			((PurpleGroup*)cnode->parent)->currentsize--;
 		((PurpleGroup*)cnode->parent)->totalsize--;
 
@@ -577,9 +577,9 @@ void purple_blist_add_contact(PurpleCont
 		cnode->parent = gnode;
 	}
 
-	if (contact->online > 0)
+	if (purple_contact_get_online(contact) > 0)
 		g->online++;
-	if (contact->currentsize > 0)
+	if (purple_contact_get_currentsize(contact) > 0)
 		g->currentsize++;
 	g->totalsize++;
 
@@ -706,49 +706,16 @@ void purple_blist_remove_buddy(PurpleBud
 {
 	PurpleBlistUiOps *ops = purple_blist_get_ui_ops();
 	PurpleBlistNode *node, *cnode, *gnode;
-	PurpleContact *contact;
-	PurpleGroup *group;
 	struct _purple_hbuddy hb;
 	GHashTable *account_buddies;
 
 	g_return_if_fail(buddy != NULL);
 
-	node = (PurpleBlistNode *)buddy;
-	cnode = node->parent;
-	gnode = (cnode != NULL) ? cnode->parent : NULL;
-	contact = (PurpleContact *)cnode;
-	group = (PurpleGroup *)gnode;
+	node = PURPLE_BLIST_NODE(buddy);
+	cnode = purple_blist_node_get_parent(node);
+	gnode = purple_blist_node_get_parent(cnode);
 
-	/* Remove the node from its parent */
-	if (node->prev)
-		node->prev->next = node->next;
-	if (node->next)
-		node->next->prev = node->prev;
-	if ((cnode != NULL) && (cnode->child == node))
-		cnode->child = node->next;
-
-	/* Adjust size counts */
-	if (contact != NULL) {
-		if (PURPLE_BUDDY_IS_ONLINE(buddy)) {
-			contact->online--;
-			if (contact->online == 0)
-				group->online--;
-		}
-		if (purple_account_is_connected(purple_buddy_get_account(buddy))) {
-			contact->currentsize--;
-			if (contact->currentsize == 0)
-				group->currentsize--;
-		}
-		contact->totalsize--;
-
-		/* Re-sort the contact */
-		if (cnode->child && contact->priority == buddy) {
-			purple_contact_invalidate_priority_buddy(contact);
-			if (ops && ops->update)
-				ops->update(purplebuddylist, cnode);
-		}
-	}
-
+	PURPLE_GET_BLIST_NODE_CLASS(node)->remove_node(gnode, node);
 	purple_blist_schedule_save();
 
 	/* Remove this buddy from the buddies hash table */
@@ -773,10 +740,6 @@ void purple_blist_remove_buddy(PurpleBud
 			PURPLE_BLIST_NODE(buddy));
 
 	purple_buddy_destroy(buddy);
-
-	/* If the contact is empty then remove it */
-	if ((contact != NULL) && !cnode->child)
-		purple_blist_remove_contact(contact);
 }
 
 void purple_blist_remove_chat(PurpleChat *chat)
============================================================
--- libpurple/buddy.c	3078a1a6e83141ef494ea0b732ea814784a4e86a
+++ libpurple/buddy.c	8b4b8b61784bf6345933021c8479511e334abeeb
@@ -44,9 +44,6 @@ struct _PurpleBuddyPrivate {
 	PurplePresence *presence;
 };
 
-struct _PurpleBuddyClass {
-	PurpleBlistNodeClass parent;
-};
 void
 parse_buddy(PurpleGroup *group, PurpleContact *contact, xmlnode *bnode)
 {
@@ -97,10 +94,10 @@ purple_blist_update_buddy_status(PurpleB
 	PurpleBlistUiOps *ops = purple_blist_get_ui_ops();
 	PurplePresence *presence;
 	PurpleStatus *status;
-	PurpleBlistNode *cnode;
-	PurpleBuddyPrivate *priv = PURPLE_BUDDY_GET_PRIVATE(buddy);
+	PurpleBuddyPrivate *priv;
 
 	g_return_if_fail(buddy != NULL);
+	priv = PURPLE_BUDDY_GET_PRIVATE(buddy);
 
 	presence = purple_buddy_get_presence(buddy);
 	status = purple_presence_get_active_status(presence);
@@ -112,24 +109,17 @@ purple_blist_update_buddy_status(PurpleB
 		!purple_status_is_online(old_status)) {
 
 		purple_signal_emit(purple_blist_get_handle(), "buddy-signed-on", buddy);
-
-		cnode = buddy->node.parent;
-		if (++(PURPLE_CONTACT(cnode)->online) == 1)
-			PURPLE_GROUP(cnode->parent)->online++;
 	} else if (!purple_status_is_online(status) &&
 				purple_status_is_online(old_status)) {
 
 		purple_blist_node_set_int(&buddy->node, "last_seen", time(NULL));
 		purple_signal_emit(purple_blist_get_handle(), "buddy-signed-off", buddy);
-
-		cnode = buddy->node.parent;
-		if (--(PURPLE_CONTACT(cnode)->online) == 0)
-			PURPLE_GROUP(cnode->parent)->online--;
 	} else {
 		purple_signal_emit(purple_blist_get_handle(),
 		                 "buddy-status-changed", buddy, old_status,
 		                 status);
 	}
+	purple_contact_buddy_status_update(purple_buddy_get_contact(buddy), status, old_status);
 
 	/*
 	 * This function used to only call the following two functions if one of
@@ -356,8 +346,8 @@ const char *purple_buddy_get_contact_ali
 
 	/* The contact alias */
 	c = purple_buddy_get_contact(buddy);
-	if ((c != NULL) && (c->alias != NULL))
-		return c->alias;
+	if ((c != NULL) && (purple_contact_get_alias(c)	!= NULL))
+		return purple_contact_get_alias(c);
 
 	/* The server alias */
 	if ((purple_buddy_get_server_alias(buddy)) && (*purple_buddy_get_server_alias(buddy)))
@@ -421,8 +411,8 @@ const char *purple_buddy_get_local_alias
 
 	/* The contact alias */
 	c = purple_buddy_get_contact(buddy);
-	if ((c != NULL) && (c->alias != NULL))
-		return c->alias;
+	if ((c != NULL) && (purple_contact_get_alias(c) != NULL))
+		return purple_contact_get_alias(c);
 
 	/* The buddy's user name (i.e. no alias) */
 	return priv->name;
============================================================
--- libpurple/buddy.h	3007df93bd44c25d4b5da6ad32c570f23742d689
+++ libpurple/buddy.h	e84e6d4d75d85d6466cd6f0c29ad42ae86a209a2
@@ -53,6 +53,10 @@ struct _PurpleBuddy {
 	PurpleBlistNode node;                     /**< The node that this buddy inherits from */
 	PurpleBuddyPrivate *priv;               /**< The private members */
 };
+
+struct _PurpleBuddyClass {
+	PurpleBlistNodeClass parent;
+};
 #endif
 
 /**
============================================================
--- libpurple/contact.c	b9a9d3f699f370964407cc3300d045020052370d
+++ libpurple/contact.c	f225b58c37533bfdee32ee10d4ce4b8f00dd6a66
@@ -31,6 +31,8 @@
 #include "signals.h"
 #include "xmlnode.h"
 
+static PurpleBlistNodeClass *parent_class = NULL;
+
 xmlnode *
 contact_to_xmlnode(PurpleBlistNode *cnode)
 {
@@ -200,7 +202,47 @@ PurpleContact *purple_buddy_get_contact(
 	return PURPLE_CONTACT(PURPLE_BLIST_NODE(buddy)->parent);
 }
 
+int
+purple_contact_get_online(PurpleContact *contact)
+{
+	g_return_val_if_fail(contact, 0);
+	return contact->online;
+}
+
 void
+purple_contact_set_online(PurpleContact *contact, int count)
+{
+	g_return_if_fail(contact);
+	contact->online = count;
+}
+
+int
+purple_contact_get_currentsize(PurpleContact *contact)
+{
+	g_return_val_if_fail(contact, 0);
+	return contact->currentsize;
+}
+
+void
+purple_contact_buddy_status_update(PurpleContact *contact, PurpleStatus *status, PurpleStatus *old_status)
+{
+
+	g_return_if_fail(contact);
+
+	if (purple_status_is_online(status) &&
+		!purple_status_is_online(old_status)) {
+
+		if (++(contact->online) == 1)
+			PURPLE_GROUP(contact->node.parent)->online++;
+	} else if (!purple_status_is_online(status) &&
+				purple_status_is_online(old_status)) {
+
+		if (--(contact->online) == 0)
+			PURPLE_GROUP(contact->node.parent)->online--;
+	}
+}
+
+void
 purple_contact_destroy(PurpleContact *contact)
 {
 	/* This function is only a hack for api breakage */
@@ -208,11 +250,6 @@ purple_contact_destroy(PurpleContact *co
 	g_object_unref(G_OBJECT(contact));
 }
 
-void purple_contact_set_alias(PurpleContact *contact, const char *alias)
-{
-	purple_blist_alias_contact(contact,alias);
-}
-
 const char *purple_contact_get_alias(PurpleContact* contact)
 {
 	g_return_val_if_fail(contact != NULL, NULL);
@@ -260,11 +297,40 @@ PurpleBuddy *purple_contact_get_priority
 	return contact->priority;
 }
 
+static void
+purple_contact_add_buddy(PurpleBlistNode *parent, PurpleBlistNode *child)
+{
+
+}
+
+static void
+purple_contact_remove_buddy(PurpleBlistNode *parent, PurpleBlistNode *child)
+{
+	PurpleContact *contact = PURPLE_CONTACT(parent);
+	PurpleBuddy *buddy = PURPLE_BUDDY(child);
+	PurpleBlistUiOps *ops = purple_blist_get_ui_ops();
+	g_return_if_fail(contact);
+
+	if (PURPLE_BUDDY_IS_ONLINE(buddy))
+		contact->online--;
+	if (purple_account_is_connected(purple_buddy_get_account(buddy)))
+		contact->currentsize--;
+	contact->totalsize--;
+
+#warning The ui calls haven't been thought out yet, as to where they ought to go.
+	/* Re-sort the contact */
+	if (purple_blist_node_get_first_child(PURPLE_BLIST_NODE(contact)) && contact->priority == buddy) {
+		purple_contact_invalidate_priority_buddy(contact);
+		if (ops && ops->update)
+			ops->update(purplebuddylist, PURPLE_BLIST_NODE(contact));
+	}
+
+}
+
 /****************/
 /* GObject Code */
 /****************/
 
-static GObjectClass *parent_class = NULL;
 
 static void
 purple_contact_finalize(GObject *object)
@@ -272,7 +338,7 @@ purple_contact_finalize(GObject *object)
 	PurpleContact *contact = PURPLE_CONTACT(object);
 	g_free(contact->alias);
 	PURPLE_DBUS_UNREGISTER_POINTER(contact);
-	parent_class->finalize(object);
+	G_OBJECT_CLASS(parent_class)->finalize(object);
 }
 
 static void
@@ -281,6 +347,9 @@ purple_contact_class_init(PurpleContactC
 	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
 
 	parent_class = g_type_class_peek_parent(klass);
+	parent_class->add_node = purple_contact_add_buddy;
+	parent_class->remove_node = purple_contact_remove_buddy;
+
 	obj_class->finalize = purple_contact_finalize;
 }
 
============================================================
--- libpurple/contact.h	5b1f6d24e0c1be2b58b3c61f9530d028ef648e5a
+++ libpurple/contact.h	72b439d3cc6745409bbf88509d8bae1a028ca382
@@ -31,6 +31,7 @@ typedef struct _PurpleContact PurpleCont
 
 /** @copydoc _PurpleContact */
 typedef struct _PurpleContact PurpleContact;
+typedef struct _PurpleContactPrivate PurpleContactPrivate;
 typedef struct _PurpleContactClass PurpleContactClass;
 
 #include "buddy.h"
@@ -136,6 +137,38 @@ PurpleContact *purple_buddy_get_contact(
 PurpleContact *purple_buddy_get_contact(PurpleBuddy *buddy);
 
 /**
+ * Returns the number of online buddies for this contact.
+ *
+ * @param contact The contact
+ * @return The # of online buddies
+ */
+int purple_contact_get_online(PurpleContact *contact);
+
+/**
+ * Sets the number of online contacts for this contact
+ *
+ * @param contact The contact
+ * @param value The number to set to
+ */
+void purple_contact_set_online(PurpleContact *contact, int value);
+
+/**
+ * Update the contact based on a buddy status change
+ *
+ * @param contact The contact
+ * @param status The current status of the buddy
+ * @param old_status The old status of the buddy
+ */
+void purple_contact_buddy_status_update(PurpleContact *contact, PurpleStatus *status, PurpleStatus *old_status);
+
+/**
+ * Returns the number of buddies that correspond to online buddies
+ *
+ * @param contact The contact
+ */
+int purple_contact_get_currentsize(PurpleContact *contact);
+
+/**
  * Merges two contacts
  *
  * All of the buddies from source will be moved to target
============================================================
--- libpurple/group.c	17aa6f286fad45ba138f6cbffcfa3913e02e7a58
+++ libpurple/group.c	1d155a7e3af1302b0948d34bc83e5727a7baf620
@@ -226,6 +226,32 @@ GSList *purple_group_get_accounts(Purple
 	return l;
 }
 
+static void
+purple_group_remove_node(PurpleBlistNode *parent, PurpleBlistNode *child)
+{
+	PurpleGroup *group = PURPLE_GROUP(parent);
+	PurpleContact *contact = PURPLE_CONTACT(child);
+	PurpleBlistNode *itr;
+
+#warning Consider optimizing this.
+	group->totalsize = 0;
+	group->currentsize = 0;
+	group->online = 0;
+
+	/* Update size counts */
+  for(itr	= group->node.child;itr;itr = itr->next){
+		if(PURPLE_CONTACT(itr)->online > 0)
+			group->online++;
+		if(PURPLE_CONTACT(itr)->currentsize > 0)
+			group->currentsize++;
+		group->totalsize++;
+	}
+
+	/* If the contact is empty then remove it */
+	if ((contact != NULL) && !PURPLE_BLIST_NODE(contact)->child)
+		purple_blist_remove_contact(contact);
+}
+
 PurpleGroup *purple_buddy_get_group(PurpleBuddy *buddy)
 {
 	g_return_val_if_fail(buddy != NULL, NULL);
@@ -302,7 +328,7 @@ PurpleGroup *purple_group_new(const char
 												NULL);
 }
 
-static GObjectClass *parent_class = NULL;
+static PurpleBlistNodeClass *parent_class = NULL;
 
 static void
 purple_group_finalize(GObject *object)
@@ -310,7 +336,7 @@ purple_group_finalize(GObject *object)
 	PurpleGroup *group = PURPLE_GROUP(object);
 	g_free(group->name);
 	PURPLE_DBUS_UNREGISTER_POINTER(group);
-	parent_class->finalize(object);
+	G_OBJECT_CLASS(parent_class)->finalize(object);
 }
 
 static void
@@ -347,6 +373,7 @@ purple_group_class_init(PurpleGroupClass
 	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
 
 	parent_class = g_type_class_peek_parent(klass);
+	parent_class->remove_node = purple_group_remove_node;
 	obj_class->finalize = purple_group_finalize;
 
 	obj_class->set_property = purple_group_set_property;


More information about the Commits mailing list