/pidgin/main: 9b4fccc8ceac: GG: reference images, not their ids

Tomasz Wasilczyk twasilczyk at pidgin.im
Thu Apr 10 19:01:18 EDT 2014


Changeset: 9b4fccc8ceac8e686edbd514531215e254efcec6
Author:	 Tomasz Wasilczyk <twasilczyk at pidgin.im>
Date:	 2014-04-11 01:01 +0200
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/9b4fccc8ceac

Description:

GG: reference images, not their ids

diffstat:

 libpurple/protocols/gg/image-prpl.c   |  90 ++++++++++------------------------
 libpurple/protocols/gg/image-prpl.h   |  31 ++++++++---
 libpurple/protocols/gg/message-prpl.c |  29 ++++++-----
 3 files changed, 65 insertions(+), 85 deletions(-)

diffs (truncated from 332 to 300 lines):

diff --git a/libpurple/protocols/gg/image-prpl.c b/libpurple/protocols/gg/image-prpl.c
--- a/libpurple/protocols/gg/image-prpl.c
+++ b/libpurple/protocols/gg/image-prpl.c
@@ -46,7 +46,7 @@ struct _ggp_image_session_data
 
 typedef struct
 {
-	guint id; /* TODO: store image ref, not id */
+	PurpleImage *image;
 	gchar *conv_name; /* TODO: callback */
 } ggp_image_sent;
 
@@ -61,24 +61,10 @@ typedef struct
 	gpointer user_data;
 } ggp_image_requested_listener;
 
-static void
-image_unref_by_id(guint id)
+static void ggp_image_sent_free(gpointer _sent_image)
 {
-	/* TODO: store image ref, not id */
-	PurpleImage *img = purple_image_store_get(id);
-	g_object_unref(img);
-}
-
-static void ggp_image_got_free(gpointer data)
-{
-	guint id = GPOINTER_TO_INT(data);
-	image_unref_by_id(id); /* TODO: store image ref, not id */
-}
-
-static void ggp_image_sent_free(gpointer data)
-{
-	ggp_image_sent *sent_image = (ggp_image_sent*)data;
-	image_unref_by_id(sent_image->id);
+	ggp_image_sent *sent_image = _sent_image;
+	g_object_unref(sent_image->image);
 	g_free(sent_image->conv_name);
 	g_free(sent_image);
 }
@@ -110,8 +96,7 @@ void ggp_image_setup(PurpleConnection *g
 	accdata->image_data = sdata;
 
 	sdata->got_images = g_hash_table_new_full(
-		g_int64_hash, g_int64_equal, g_free,
-		ggp_image_got_free);
+		g_int64_hash, g_int64_equal, g_free, g_object_unref);
 	sdata->incoming_images = g_hash_table_new_full(
 		g_int64_hash, g_int64_equal, g_free,
 		ggp_image_requested_free);
@@ -130,22 +115,17 @@ void ggp_image_cleanup(PurpleConnection 
 	g_free(sdata);
 }
 
-ggp_image_prepare_result ggp_image_prepare(PurpleConversation *conv,
-	const guint stored_id, uint64_t *id)
+ggp_image_prepare_result
+ggp_image_prepare(PurpleConversation *conv, PurpleImage *image, uint64_t *id)
 {
 	PurpleConnection *gc = purple_conversation_get_connection(conv);
 	ggp_image_session_data *sdata = ggp_image_get_sdata(gc);
-	PurpleImage *image = purple_image_store_get(stored_id);
 	size_t image_size;
 	gconstpointer image_data;
 	uint32_t image_crc;
 	ggp_image_sent *sent_image;
 
-	if (!image) {
-		purple_debug_error("gg", "ggp_image_prepare: image %d "
-			"not found in image store\n", stored_id);
-		return GGP_IMAGE_PREPARE_FAILURE;
-	}
+	g_return_val_if_fail(image, GGP_IMAGE_PREPARE_FAILURE);
 
 	image_size = purple_image_get_size(image);
 
@@ -160,13 +140,14 @@ ggp_image_prepare_result ggp_image_prepa
 	image_crc = gg_crc32(0, image_data, image_size);
 
 	purple_debug_info("gg", "ggp_image_prepare: image prepared "
-		"[id=%d, crc=%u, size=%" G_GSIZE_FORMAT "]\n",
-		stored_id, image_crc, image_size);
+		"[crc=%u, size=%" G_GSIZE_FORMAT "]",
+		image_crc, image_size);
 
 	*id = ggp_image_params_to_id(image_crc, image_size);
 
+	g_object_ref(image);
 	sent_image = g_new(ggp_image_sent, 1);
-	sent_image->id = stored_id;
+	sent_image->image = image;
 	sent_image->conv_name = g_strdup(purple_conversation_get_name(conv));
 	g_hash_table_insert(sdata->sent_images, ggp_uint64dup(*id),
 		sent_image);
@@ -179,7 +160,6 @@ void ggp_image_recv(PurpleConnection *gc
 {
 	ggp_image_session_data *sdata = ggp_image_get_sdata(gc);
 	PurpleImage *img;
-	guint stored_id;
 	ggp_image_requested *req;
 	GList *it;
 	uint64_t id;
@@ -188,28 +168,23 @@ void ggp_image_recv(PurpleConnection *gc
 		g_memdup(image_reply->image, image_reply->size),
 		image_reply->size);
 	purple_image_set_friendly_filename(img, image_reply->filename);
-	stored_id = purple_image_store_add(img);
-	g_object_unref(img);
 
 	id = ggp_image_params_to_id(image_reply->crc32, image_reply->size);
 
 	purple_debug_info("gg", "ggp_image_recv: got image "
-		"[stored_id=%d, crc=%u, size=%u, filename=%s, id="
-		GGP_IMAGE_ID_FORMAT "]\n",
-		stored_id,
-		image_reply->crc32,
-		image_reply->size,
-		image_reply->filename,
-		id);
+		"[crc=%u, size=%u, filename=%s, id=" GGP_IMAGE_ID_FORMAT "]",
+		image_reply->crc32, image_reply->size,
+		image_reply->filename, id);
 
-	g_hash_table_insert(sdata->got_images, ggp_uint64dup(id),
-		GINT_TO_POINTER(stored_id));
+	g_object_ref(img);
+	g_hash_table_insert(sdata->got_images, ggp_uint64dup(id), img);
 
 	req = g_hash_table_lookup(sdata->incoming_images, &id);
 	if (!req) {
 		purple_debug_warning("gg", "ggp_image_recv: "
 			"image " GGP_IMAGE_ID_FORMAT " wasn't requested\n",
 			id);
+		g_object_unref(img);
 		return;
 	}
 
@@ -218,8 +193,9 @@ void ggp_image_recv(PurpleConnection *gc
 		ggp_image_requested_listener *listener = it->data;
 		it = g_list_next(it);
 
-		listener->cb(gc, id, stored_id, listener->user_data);
+		listener->cb(gc, id, img, listener->user_data);
 	}
+	g_object_unref(img);
 	g_hash_table_remove(sdata->incoming_images, &id);
 }
 
@@ -229,7 +205,6 @@ void ggp_image_send(PurpleConnection *gc
 	GGPInfo *accdata = purple_connection_get_protocol_data(gc);
 	ggp_image_session_data *sdata = ggp_image_get_sdata(gc);
 	ggp_image_sent *sent_image;
-	PurpleImage *image;
 	PurpleConversation *conv;
 	uint64_t id;
 	gchar *gg_filename;
@@ -258,27 +233,17 @@ void ggp_image_send(PurpleConnection *gc
 	}
 
 	purple_debug_misc("gg", "ggp_image_send: requested image found "
-		"[id=" GGP_IMAGE_ID_FORMAT ", stored id=%d, conv=%s]\n",
-		id,
-		sent_image->id,
-		sent_image->conv_name);
+		"[id=" GGP_IMAGE_ID_FORMAT ", conv=%s]\n",
+		id, sent_image->conv_name);
 
-	image = purple_image_store_get(sent_image->id);
-
-	if (!image) {
-		purple_debug_error("gg", "ggp_image_send: requested image "
-			"found, but doesn't exists in image store\n");
-		g_hash_table_remove(sdata->sent_images,
-			GINT_TO_POINTER(image_request->crc32));
-		return;
-	}
+	g_return_if_fail(sent_image->image);
 
 	/* TODO: check allowed recipients */
 	gg_filename = g_strdup_printf(GGP_IMAGE_ID_FORMAT, id);
 	gg_image_reply(accdata->session, image_request->sender,
 		gg_filename,
-		purple_image_get_data(image),
-		purple_image_get_size(image));
+		purple_image_get_data(sent_image->image),
+		purple_image_get_size(sent_image->image));
 	g_free(gg_filename);
 
 	conv = purple_conversations_find_with_account(
@@ -334,9 +299,10 @@ void ggp_image_request(PurpleConnection 
 	req->listeners = g_list_append(req->listeners, listener);
 }
 
-int ggp_image_get_cached(PurpleConnection *gc, uint64_t id)
+PurpleImage *
+ggp_image_get_cached(PurpleConnection *gc, uint64_t id)
 {
 	ggp_image_session_data *sdata = ggp_image_get_sdata(gc);
 
-	return GPOINTER_TO_INT(g_hash_table_lookup(sdata->got_images, &id));
+	return g_hash_table_lookup(sdata->got_images, &id);
 }
diff --git a/libpurple/protocols/gg/image-prpl.h b/libpurple/protocols/gg/image-prpl.h
--- a/libpurple/protocols/gg/image-prpl.h
+++ b/libpurple/protocols/gg/image-prpl.h
@@ -45,21 +45,32 @@ typedef enum
 	GGP_IMAGE_PREPARE_TOO_BIG
 } ggp_image_prepare_result;
 
-typedef void (*ggp_image_request_cb)(PurpleConnection *gc, uint64_t id,
-	guint stored_id, gpointer user_data);
+typedef void
+(*ggp_image_request_cb)(PurpleConnection *gc, uint64_t id, PurpleImage *image,
+	gpointer user_data);
 
-void ggp_image_setup(PurpleConnection *gc);
-void ggp_image_cleanup(PurpleConnection *gc);
+void
+ggp_image_setup(PurpleConnection *gc);
 
-ggp_image_prepare_result ggp_image_prepare(PurpleConversation *conv,
-	const guint stored_id, uint64_t *id);
+void
+ggp_image_cleanup(PurpleConnection *gc);
 
-void ggp_image_recv(PurpleConnection *gc,
+ggp_image_prepare_result
+ggp_image_prepare(PurpleConversation *conv, PurpleImage *image, uint64_t *id);
+
+void
+ggp_image_recv(PurpleConnection *gc,
 	const struct gg_event_image_reply *image_reply);
-void ggp_image_send(PurpleConnection *gc,
+
+void
+ggp_image_send(PurpleConnection *gc,
 	const struct gg_event_image_request *image_request);
-void ggp_image_request(PurpleConnection *gc, uin_t uin, uint64_t id,
+
+void
+ggp_image_request(PurpleConnection *gc, uin_t uin, uint64_t id,
 	ggp_image_request_cb cb, gpointer user_data);
-int ggp_image_get_cached(PurpleConnection *gc, uint64_t id);
+
+PurpleImage *
+ggp_image_get_cached(PurpleConnection *gc, uint64_t id);
 
 #endif /* _GGP_IMAGE_H */
diff --git a/libpurple/protocols/gg/message-prpl.c b/libpurple/protocols/gg/message-prpl.c
--- a/libpurple/protocols/gg/message-prpl.c
+++ b/libpurple/protocols/gg/message-prpl.c
@@ -194,12 +194,13 @@ static void ggp_message_got_data_free(gg
 
 static void
 ggp_message_request_images_got(PurpleConnection *gc, uint64_t id,
-	guint stored_id, gpointer _msg)
+	PurpleImage *image, gpointer _msg)
 {
 	ggp_message_session_data *sdata = ggp_message_get_sdata(gc);
 	ggp_message_got_data *msg = _msg;
 	GList *m_it, *i_it;
 	gchar *tmp, *tag_search, *tag_replace;
+	guint image_id;
 
 	m_it = g_list_find(sdata->pending_messages, msg);
 	if (!m_it) {
@@ -216,8 +217,9 @@ ggp_message_request_images_got(PurpleCon
 		return;
 	}
 
+	image_id = purple_image_store_add(image);
 	tag_search = g_strdup_printf(GGP_IMAGE_REPLACEMENT, id);
-	tag_replace = g_strdup_printf(GGP_IMAGE_DESTINATION, stored_id);
+	tag_replace = g_strdup_printf(GGP_IMAGE_DESTINATION, image_id);
 
 	tmp = msg->text;
 	msg->text = purple_strreplace(msg->text, tag_search, tag_replace);
@@ -345,7 +347,7 @@ static gboolean ggp_message_format_from_
 	ggp_message_got_data *msg = data;
 	gchar *name, *replacement;
 	int64_t id;
-	int stored_id;
+	PurpleImage *image;
 
 	name = g_match_info_fetch(info, 1);
 	if (sscanf(name, "%" G_GINT64_MODIFIER "x", &id) != 1)
@@ -358,13 +360,13 @@ static gboolean ggp_message_format_from_
 		return FALSE;
 	}
 
-	stored_id = ggp_image_get_cached(msg->gc, id);
+	image = ggp_image_get_cached(msg->gc, id);
 
-	if (stored_id > 0) {
+	if (image) {
+		guint image_id = purple_image_store_add(image);
 		purple_debug_info("gg", "ggp_message_format_from_gg_found_img: "
-			"getting image " GGP_IMAGE_ID_FORMAT " from cache\n",
-			id);
-		replacement = g_strdup_printf(GGP_IMAGE_DESTINATION, stored_id);



More information about the Commits mailing list