/soc/2015/jgeboski/facebook: bc1889935d07: facebok: implemented ...

James Geboski jgeboski at gmail.com
Fri Jun 5 21:26:43 EDT 2015


Changeset: bc1889935d07a538a20abda9cdf00768a2140dc3
Author:	 James Geboski <jgeboski at gmail.com>
Date:	 2015-06-05 21:26 -0400
Branch:	 facebook
URL: https://hg.pidgin.im/soc/2015/jgeboski/facebook/rev/bc1889935d07

Description:

facebok: implemented user icons

diffstat:

 libpurple/protocols/facebook/api.c      |  16 +++++---
 libpurple/protocols/facebook/api.h      |   1 +
 libpurple/protocols/facebook/facebook.c |  63 +++++++++++++++++++++++++++++---
 3 files changed, 67 insertions(+), 13 deletions(-)

diffs (141 lines):

diff --git a/libpurple/protocols/facebook/api.c b/libpurple/protocols/facebook/api.c
--- a/libpurple/protocols/facebook/api.c
+++ b/libpurple/protocols/facebook/api.c
@@ -968,8 +968,7 @@ static void
 fb_api_cb_contacts(PurpleHttpConnection *con, PurpleHttpResponse *res,
                    gpointer data)
 {
-	const gchar *name;
-	const gchar *uid;
+	const gchar *str;
 	FbApi *api = data;
 	FbApiUser user;
 	GError *err = NULL;
@@ -994,15 +993,20 @@ fb_api_cb_contacts(PurpleHttpConnection 
 
 	for (l = elms; l != NULL; l = l->next) {
 		node = l->data;
-		uid = fb_json_node_get_str(node, "$.represented_profile.id",
+		str = fb_json_node_get_str(node, "$.represented_profile.id",
 		                           &err);
 		FB_API_ERROR_CHK(api, err, goto finish);
-		user.uid = FB_ID_FROM_STR(uid);
+		user.uid = FB_ID_FROM_STR(str);
 
-		name = fb_json_node_get_str(node, "$.structured_name.text",
+		str = fb_json_node_get_str(node, "$.structured_name.text",
+		                           &err);
+		FB_API_ERROR_CHK(api, err, goto finish);
+		user.name = str;
+
+		str = fb_json_node_get_str(node, "$.huge_picture_url.uri",
 		                            &err);
 		FB_API_ERROR_CHK(api, err, goto finish);
-		user.name = name;
+		user.icon = str;
 
 		mptr = g_memdup(&user, sizeof user);
 		users = g_slist_prepend(users, mptr);
diff --git a/libpurple/protocols/facebook/api.h b/libpurple/protocols/facebook/api.h
--- a/libpurple/protocols/facebook/api.h
+++ b/libpurple/protocols/facebook/api.h
@@ -128,6 +128,7 @@ struct _FbApiUser
 {
 	FbId uid;
 	const gchar *name;
+	const gchar *icon;
 };
 
 struct _FbApiHttpInfo
diff --git a/libpurple/protocols/facebook/facebook.c b/libpurple/protocols/facebook/facebook.c
--- a/libpurple/protocols/facebook/facebook.c
+++ b/libpurple/protocols/facebook/facebook.c
@@ -41,6 +41,9 @@ static const gchar *fb_props_strs[] = {
 
 static PurpleProtocol *my_protocol = NULL;
 
+static void
+fb_cb_api_error(FbApi *api, GError *error, gpointer data);
+
 static gboolean
 fb_props_load(PurpleConnection *gc)
 {
@@ -160,9 +163,44 @@ fb_cb_api_connect(FbApi *api, gpointer d
 }
 
 static void
+fb_cb_icon_fetch(PurpleHttpConnection *con, PurpleHttpResponse *res,
+                 gpointer data)
+{
+	const gchar *str;
+	const gchar *name = data;
+	FbApi *api;
+	GError *err;
+	gsize size;
+	guchar *idata;
+	PurpleAccount *acct;
+	PurpleConnection *gc;
+
+	gc = purple_http_conn_get_purple_connection(con);
+	acct = purple_connection_get_account(gc);
+	api = purple_connection_get_protocol_data(gc);
+
+	if (!fb_http_error_chk(res, &err)) {
+		fb_cb_api_error(api, err, gc);
+		g_error_free(err);
+		return;
+	}
+
+	str = purple_http_response_get_data(res, &size);
+	idata = g_memdup(str, size);
+
+	if (G_UNLIKELY(name == NULL)) {
+		purple_buddy_icons_set_account_icon(acct, idata, size);
+		return;
+	}
+
+	purple_buddy_icons_set_for_user(acct, name, idata, size, NULL);
+}
+
+static void
 fb_cb_api_contacts(FbApi *api, GSList *users, gpointer data)
 {
 	const gchar *alias;
+	const gchar *name;
 	FbApiUser *user;
 	FbId muid;
 	gchar uid[FB_ID_STRMAX];
@@ -186,15 +224,26 @@ fb_cb_api_contacts(FbApi *api, GSList *u
 		user = l->data;
 		FB_ID_TO_STR(user->uid, uid);
 
-		if (G_UNLIKELY((user->uid == muid) && (alias == NULL))) {
-			purple_account_set_private_alias(acct, user->name);
-			continue;
+		if (G_UNLIKELY(user->uid == muid)) {
+			if (G_UNLIKELY(alias == NULL)) {
+				purple_account_set_private_alias(acct,
+				                                 user->name);
+			}
+
+			name = NULL;
+		} else {
+			bdy = purple_blist_find_buddy(acct, uid);
+
+			if (bdy == NULL) {
+				bdy = purple_buddy_new(acct, uid, user->name);
+				purple_blist_add_buddy(bdy, NULL, grp, NULL);
+			}
+
+			name = purple_buddy_get_name(bdy);
 		}
 
-		if (purple_blist_find_buddy(acct, uid) == NULL) {
-			bdy = purple_buddy_new(acct, uid, user->name);
-			purple_blist_add_buddy(bdy, NULL, grp, NULL);
-		}
+		purple_http_get(gc, fb_cb_icon_fetch, (gchar *) name,
+		                user->icon);
 	}
 
 	purple_connection_update_progress(gc, _("Connecting"), 3, 4);



More information about the Commits mailing list