/soc/2013/ankitkv/gobjectification: af814aaf48bb: Added GObject ...
Ankit Vani
a at nevitus.org
Tue Jul 16 06:38:24 EDT 2013
Changeset: af814aaf48bb937c90e528f8d20d6b86204e0766
Author: Ankit Vani <a at nevitus.org>
Date: 2013-07-16 16:07 +0530
Branch: soc.2013.gobjectification
URL: https://hg.pidgin.im/soc/2013/ankitkv/gobjectification/rev/af814aaf48bb
Description:
Added GObject code to PurpleBuddyList
diffstat:
libpurple/buddylist.c | 114 +++++++++++++++++++++++++++++++++++--------------
1 files changed, 82 insertions(+), 32 deletions(-)
diffs (265 lines):
diff --git a/libpurple/buddylist.c b/libpurple/buddylist.c
--- a/libpurple/buddylist.c
+++ b/libpurple/buddylist.c
@@ -40,13 +40,15 @@
/** Private data for a buddy list. */
typedef struct {
- GHashTable *buddies; /**< Every buddy in this list */
+ GHashTable *buddies; /**< Every buddy in this list */
} PurpleBuddyListPrivate;
static PurpleBListUiOps *blist_ui_ops = NULL;
static PurpleBuddyList *purplebuddylist = NULL;
+static GObjectClass *parent_class;
+
/**
* A hash table used for efficient lookups of buddies by name.
* PurpleAccount* => GHashTable*, with the inner hash table being
@@ -683,15 +685,10 @@ purple_blist_boot(void)
{
PurpleBListUiOps *ui_ops;
GList *account;
- PurpleBuddyList *gbl = g_new0(PurpleBuddyList, 1);
- PURPLE_DBUS_REGISTER_POINTER(gbl, PurpleBuddyList);
+ PurpleBuddyList *gbl = g_object_new(PURPLE_TYPE_BUDDY_LIST, NULL);
ui_ops = purple_blist_get_ui_ops();
- gbl->buddies = g_hash_table_new_full((GHashFunc)_purple_blist_hbuddy_hash,
- (GEqualFunc)_purple_blist_hbuddy_equal,
- (GDestroyNotify)_purple_blist_hbuddy_free_key, NULL);
-
buddies_cache = g_hash_table_new_full(g_direct_hash, g_direct_equal,
NULL, (GDestroyNotify)g_hash_table_destroy);
@@ -739,7 +736,8 @@ purple_blist_get_buddies()
if (!purplebuddylist)
return NULL;
- g_hash_table_foreach(purplebuddylist->buddies, append_buddy, &buddies);
+ g_hash_table_foreach(PURPLE_BUDDY_LIST_GET_PRIVATE(purplebuddylist)->buddies,
+ append_buddy, &buddies);
return buddies;
}
@@ -777,6 +775,7 @@ void purple_blist_update_buddies_cache(P
GHashTable *account_buddies;
PurpleAccount *account;
gchar *name;
+ PurpleBuddyListPrivate *priv = PURPLE_BUDDY_LIST_GET_PRIVATE(purplebuddylist);
g_return_if_fail(buddy != NULL);
@@ -787,13 +786,13 @@ void purple_blist_update_buddies_cache(P
hb->name = (gchar *)purple_normalize(account, name);
hb->account = account;
hb->group = PURPLE_BLIST_NODE(buddy)->parent->parent;
- g_hash_table_remove(purplebuddylist->buddies, hb);
+ g_hash_table_remove(priv->buddies, hb);
account_buddies = g_hash_table_lookup(buddies_cache, account);
g_hash_table_remove(account_buddies, hb);
hb->name = g_strdup(purple_normalize(account, new_name));
- g_hash_table_replace(purplebuddylist->buddies, hb, buddy);
+ g_hash_table_replace(priv->buddies, hb, buddy);
hb2 = g_new(struct _purple_hbuddy, 1);
hb2->name = g_strdup(hb->name);
@@ -912,6 +911,7 @@ void purple_blist_add_buddy(PurpleBuddy
PurpleBListUiOps *ops = purple_blist_get_ui_ops();
struct _purple_hbuddy *hb, *hb2;
GHashTable *account_buddies;
+ PurpleBuddyListPrivate *priv = PURPLE_BUDDY_LIST_GET_PRIVATE(purplebuddylist);
g_return_if_fail(buddy != NULL);
g_return_if_fail(PURPLE_IS_BUDDY((PurpleBListNode*)buddy));
@@ -983,7 +983,7 @@ void purple_blist_add_buddy(PurpleBuddy
purple_buddy_get_name(buddy));
hb.account = account;
hb.group = bnode->parent->parent;
- g_hash_table_remove(purplebuddylist->buddies, &hb);
+ g_hash_table_remove(priv->buddies, &hb);
account_buddies = g_hash_table_lookup(buddies_cache, account);
g_hash_table_remove(account_buddies, &hb);
@@ -1035,7 +1035,7 @@ void purple_blist_add_buddy(PurpleBuddy
hb->account = account;
hb->group = PURPLE_BLIST_NODE(buddy)->parent->parent;
- g_hash_table_replace(purplebuddylist->buddies, hb, buddy);
+ g_hash_table_replace(priv->buddies, hb, buddy);
account_buddies = g_hash_table_lookup(buddies_cache, account);
@@ -1068,6 +1068,7 @@ void purple_blist_add_contact(PurpleCont
PurpleGroup *g;
PurpleBListNode *gnode, *cnode, *bnode;
PurpleCountingNode *contact_counter, *group_counter;
+ PurpleBuddyListPrivate *priv = PURPLE_BUDDY_LIST_GET_PRIVATE(purplebuddylist);
g_return_if_fail(contact != NULL);
@@ -1114,14 +1115,14 @@ void purple_blist_add_contact(PurpleCont
hb->account = account;
hb->group = cnode->parent;
- g_hash_table_remove(purplebuddylist->buddies, hb);
+ g_hash_table_remove(priv->buddies, hb);
account_buddies = g_hash_table_lookup(buddies_cache, account);
g_hash_table_remove(account_buddies, hb);
if (!purple_blist_find_buddy_in_group(account, purple_buddy_get_name(b), g)) {
hb->group = gnode;
- g_hash_table_replace(purplebuddylist->buddies, hb, b);
+ g_hash_table_replace(priv->buddies, hb, b);
hb2 = g_new(struct _purple_hbuddy, 1);
hb2->name = g_strdup(hb->name);
@@ -1392,7 +1393,7 @@ void purple_blist_remove_buddy(PurpleBud
hb.name = (gchar *)purple_normalize(account, purple_buddy_get_name(buddy));
hb.account = account;
hb.group = gnode;
- g_hash_table_remove(purplebuddylist->buddies, &hb);
+ g_hash_table_remove(PURPLE_BUDDY_LIST_GET_PRIVATE(purplebuddylist)->buddies, &hb);
account_buddies = g_hash_table_lookup(buddies_cache, account);
g_hash_table_remove(account_buddies, &hb);
@@ -1534,7 +1535,8 @@ PurpleBuddy *purple_blist_find_buddy(Pur
continue;
hb.group = group;
- if ((buddy = g_hash_table_lookup(purplebuddylist->buddies, &hb))) {
+ if ((buddy = g_hash_table_lookup(PURPLE_BUDDY_LIST_GET_PRIVATE(purplebuddylist)->buddies,
+ &hb))) {
return buddy;
}
}
@@ -1555,7 +1557,8 @@ PurpleBuddy *purple_blist_find_buddy_in_
hb.account = account;
hb.group = (PurpleBListNode*)group;
- return g_hash_table_lookup(purplebuddylist->buddies, &hb);
+ return g_hash_table_lookup(PURPLE_BUDDY_LIST_GET_PRIVATE(purplebuddylist)->buddies,
+ &hb);
}
static void find_acct_buddies(gpointer key, gpointer value, gpointer data)
@@ -1586,7 +1589,8 @@ GSList *purple_blist_find_buddies(Purple
continue;
hb.group = node;
- if ((buddy = g_hash_table_lookup(purplebuddylist->buddies, &hb)) != NULL)
+ if ((buddy = g_hash_table_lookup(PURPLE_BUDDY_LIST_GET_PRIVATE(purplebuddylist)->buddies,
+ &hb)) != NULL)
ret = g_slist_prepend(ret, buddy);
}
} else {
@@ -2041,32 +2045,66 @@ purple_blist_uninit(void)
}
purplebuddylist->root = NULL;
- g_hash_table_destroy(purplebuddylist->buddies);
g_hash_table_destroy(buddies_cache);
g_hash_table_destroy(groups_cache);
buddies_cache = NULL;
groups_cache = NULL;
- PURPLE_DBUS_UNREGISTER_POINTER(purplebuddylist);
- g_free(purplebuddylist);
+ g_object_unref(purplebuddylist);
purplebuddylist = NULL;
purple_signals_disconnect_by_handle(purple_blist_get_handle());
purple_signals_unregister_by_instance(purple_blist_get_handle());
}
-static PurpleBuddyList *
-purple_buddy_list_copy(PurpleBuddyList *blist)
+/**************************************************************************
+ * GObject code
+ **************************************************************************/
+
+/* GObject initialization function */
+static void
+purple_buddy_list_init(GTypeInstance *instance, gpointer klass)
{
- PurpleBuddyList *blist_copy;
+ PurpleBuddyList *blist = PURPLE_BUDDY_LIST(instance);
- g_return_val_if_fail(blist != NULL, NULL);
+ PURPLE_DBUS_REGISTER_POINTER(blist, PurpleBuddyList);
- blist_copy = g_new(PurpleBuddyList, 1);
- *blist_copy = *blist;
+ PURPLE_BUDDY_LIST_GET_PRIVATE(blist)->buddies = g_hash_table_new_full(
+ (GHashFunc)_purple_blist_hbuddy_hash,
+ (GEqualFunc)_purple_blist_hbuddy_equal,
+ (GDestroyNotify)_purple_blist_hbuddy_free_key, NULL);
+}
- return blist_copy;
+/* GObject dispose function */
+static void
+purple_buddy_list_dispose(GObject *object)
+{
+ PURPLE_DBUS_UNREGISTER_POINTER(object);
+
+ G_OBJECT_CLASS(parent_class)->dispose(object);
+}
+
+/* GObject finalize function */
+static void
+purple_buddy_list_finalize(GObject *object)
+{
+ g_hash_table_destroy(PURPLE_BUDDY_LIST_GET_PRIVATE(object)->buddies);
+
+ G_OBJECT_CLASS(parent_class)->finalize(object);
+}
+
+/* Class initializer function */
+static void purple_buddy_list_class_init(PurpleBuddyListClass *klass)
+{
+ GObjectClass *obj_class = G_OBJECT_CLASS(klass);
+
+ parent_class = g_type_class_peek_parent(klass);
+
+ obj_class->dispose = purple_buddy_list_dispose;
+ obj_class->finalize = purple_buddy_list_finalize;
+
+ g_type_class_add_private(klass, sizeof(PurpleBuddyListPrivate));
}
GType
@@ -2074,10 +2112,22 @@ purple_buddy_list_get_type(void)
{
static GType type = 0;
- if (type == 0) {
- type = g_boxed_type_register_static("PurpleBuddyList",
- (GBoxedCopyFunc)purple_buddy_list_copy,
- (GBoxedFreeFunc)g_free);
+ if(type == 0) {
+ static const GTypeInfo info = {
+ sizeof(PurpleBuddyListClass),
+ NULL,
+ NULL,
+ (GClassInitFunc)purple_buddy_list_class_init,
+ NULL,
+ NULL,
+ sizeof(PurpleBuddyList),
+ 0,
+ (GInstanceInitFunc)purple_buddy_list_init,
+ NULL,
+ };
+
+ type = g_type_register_static(G_TYPE_OBJECT,
+ "PurpleBuddyList", &info, 0);
}
return type;
More information about the Commits
mailing list