/pidgin/main: 268931512478: imgstore: unref oscar

Tomasz Wasilczyk twasilczyk at pidgin.im
Fri Apr 11 16:20:18 EDT 2014


Changeset: 2689315124783482646f494d9d0a59feb721237b
Author:	 Tomasz Wasilczyk <twasilczyk at pidgin.im>
Date:	 2014-04-11 22:20 +0200
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/268931512478

Description:

imgstore: unref oscar

diffstat:

 libpurple/image.c                           |  47 +++++++++++++++++++-
 libpurple/image.h                           |   3 +
 libpurple/protocols/oscar/family_oservice.c |  13 +++--
 libpurple/protocols/oscar/odc.c             |  49 ++++++++-------------
 libpurple/protocols/oscar/oscar.c           |  66 +++++++++++++++-------------
 libpurple/protocols/oscar/oscar.h           |   2 -
 libpurple/protocols/oscar/oscarcommon.h     |   4 +-
 pidgin/gtkwebview.c                         |   8 +--
 8 files changed, 114 insertions(+), 78 deletions(-)

diffs (truncated from 478 to 300 lines):

diff --git a/libpurple/image.c b/libpurple/image.c
--- a/libpurple/image.c
+++ b/libpurple/image.c
@@ -35,6 +35,7 @@ typedef struct {
 	const gchar *extension;
 	const gchar *mime;
 	gchar *gen_filename;
+	gchar *friendly_filename;
 
 	gboolean is_ready;
 	gboolean has_failed;
@@ -375,8 +376,49 @@ purple_image_generate_filename(PurpleIma
 void
 purple_image_set_friendly_filename(PurpleImage *image, const gchar *filename)
 {
-	/* TODO */
-	/* filter with g_path_get_basename() and purple_escape_filename() */
+	PurpleImagePrivate *priv = PURPLE_IMAGE_GET_PRIVATE(image);
+	gchar *newname;
+	const gchar *escaped;
+
+	g_return_if_fail(priv != NULL);
+
+	newname = g_path_get_basename(filename);
+	escaped = purple_escape_filename(newname);
+	g_free(newname);
+	newname = NULL;
+
+	if (g_strcmp0(escaped, "") == 0 || g_strcmp0(escaped, ".") == 0 ||
+		g_strcmp0(escaped, G_DIR_SEPARATOR_S) == 0 ||
+		g_strcmp0(escaped, "/") == 0 || g_strcmp0(escaped, "\\") == 0)
+	{
+		escaped = NULL;
+	}
+
+	g_free(priv->friendly_filename);
+	priv->friendly_filename = g_strdup(escaped);
+}
+
+const gchar *
+purple_image_get_friendly_filename(PurpleImage *image)
+{
+	PurpleImagePrivate *priv = PURPLE_IMAGE_GET_PRIVATE(image);
+
+	g_return_val_if_fail(priv != NULL, NULL);
+
+	if (G_UNLIKELY(!priv->friendly_filename)) {
+		const gchar *newname = purple_image_generate_filename(image);
+		gsize newname_len = strlen(newname);
+
+		if (newname_len < 10)
+			return NULL;
+
+		/* let's use last 6 characters from checksum + 4 characters
+		 * from file ext */
+		newname += newname_len - 10;
+		priv->friendly_filename = g_strdup(newname);
+	}
+
+	return priv->friendly_filename;
 }
 
 PurpleImage *
@@ -473,6 +515,7 @@ purple_image_finalize(GObject *obj)
 		g_string_free(priv->contents, TRUE);
 	g_free(priv->path);
 	g_free(priv->gen_filename);
+	g_free(priv->friendly_filename);
 
 	G_OBJECT_CLASS(parent_class)->finalize(obj);
 }
diff --git a/libpurple/image.h b/libpurple/image.h
--- a/libpurple/image.h
+++ b/libpurple/image.h
@@ -116,6 +116,9 @@ purple_image_generate_filename(PurpleIma
 void
 purple_image_set_friendly_filename(PurpleImage *image, const gchar *filename);
 
+const gchar *
+purple_image_get_friendly_filename(PurpleImage *image);
+
 PurpleImage *
 purple_image_transfer_new(void);
 
diff --git a/libpurple/protocols/oscar/family_oservice.c b/libpurple/protocols/oscar/family_oservice.c
--- a/libpurple/protocols/oscar/family_oservice.c
+++ b/libpurple/protocols/oscar/family_oservice.c
@@ -912,26 +912,27 @@ aim_parse_extstatus(OscarData *od, FlapC
 				aim_srv_requestnew(od, SNAC_FAMILY_BART);
 			} else {
 				PurpleAccount *account = purple_connection_get_account(od->gc);
-				PurpleStoredImage *img = purple_buddy_icons_find_account_icon(account);
+				PurpleImage *img = purple_buddy_icons_find_account_icon(account);
 				if (img == NULL) {
 					aim_ssi_delicon(od);
 				} else {
 
 					purple_debug_info("oscar",
 									"Uploading icon to icon server\n");
-					aim_bart_upload(od, purple_imgstore_get_data(img),
-							purple_imgstore_get_size(img));
-					purple_imgstore_unref(img);
+					aim_bart_upload(od,
+						purple_image_get_data(img),
+						purple_image_get_size(img));
+					g_object_unref(img);
 				}
 			}
 		} else if (flags == 0x81) {
 			PurpleAccount *account = purple_connection_get_account(od->gc);
-			PurpleStoredImage *img = purple_buddy_icons_find_account_icon(account);
+			PurpleImage *img = purple_buddy_icons_find_account_icon(account);
 			if (img == NULL)
 				aim_ssi_delicon(od);
 			else {
 				aim_ssi_seticon(od, md5, length);
-				purple_imgstore_unref(img);
+				g_object_unref(img);
 			}
 		}
 
diff --git a/libpurple/protocols/oscar/odc.c b/libpurple/protocols/oscar/odc.c
--- a/libpurple/protocols/oscar/odc.c
+++ b/libpurple/protocols/oscar/odc.c
@@ -25,7 +25,7 @@
 
 /* From Purple */
 #include "conversation.h"
-#include "imgstore.h"
+#include "image-store.h"
 #include "util.h"
 
 #define DIRECTIM_MAX_FILESIZE 52428800
@@ -210,10 +210,10 @@ struct embedded_data
  * function is passed a long chunk of data which contains the IM with any
  * data chunks (images) appended to it.
  *
- * This function rips out all the data chunks and creates an imgstore for
+ * This function rips out all the data chunks and creates a PurpleImage (?) for
  * each one.  In order to do this, it first goes through the IM and takes
  * out all the IMG tags.  When doing so, it rewrites the original IMG tag
- * with one compatible with the imgstore Purple core code. For each one, we
+ * with one compatible with the PurpleImage (?) code. For each one, we
  * then read in chunks of data from the end of the message and actually
  * create the img store using the given data.
  *
@@ -244,7 +244,7 @@ peer_odc_handle_payload(PeerConnection *
 	GData *attributes;
 	GHashTable *embedded_datas;
 	struct embedded_data *embedded_data;
-	GSList *images;
+	gboolean any_images = FALSE;
 	gchar *utf8;
 	GString *newmsg;
 	PurpleMessageFlags imflags;
@@ -332,12 +332,11 @@ peer_odc_handle_payload(PeerConnection *
 	 * Loop through the message, replacing OSCAR img tags with the
 	 * equivalent Purple img tag.
 	 */
-	images = NULL;
 	newmsg = g_string_new("");
 	tmp = msg;
 	while (purple_markup_find_tag("img", tmp, &start, &end, &attributes))
 	{
-		int imgid = 0;
+		PurpleImage *image = NULL;
 
 		idstr   = g_datalist_get_data(&attributes, "id");
 		src     = g_datalist_get_data(&attributes, "src");
@@ -355,16 +354,10 @@ peer_odc_handle_payload(PeerConnection *
 
 			if ((embedded_data != NULL) && (embedded_data->size == size))
 			{
-				char *basename;
-				char *escaped;
-				basename = g_path_get_basename(src);
-				escaped = g_strdup(purple_escape_filename(basename));
-				g_free(basename);
-				imgid = purple_imgstore_new_with_id(g_memdup(embedded_data->data, size), size, escaped);
-				g_free(escaped);
-
-				/* Record the image number */
-				images = g_slist_append(images, GINT_TO_POINTER(imgid));
+				image = purple_image_new_from_data(
+					g_memdup(embedded_data->data, size),
+					size);
+				purple_image_set_friendly_filename(image, src);
 			}
 		}
 
@@ -378,12 +371,17 @@ peer_odc_handle_payload(PeerConnection *
 			g_free(utf8);
 		}
 
-		if (imgid != 0)
+		if (image)
 		{
+			guint img_id;
+
+			img_id = purple_image_store_add_temporary(image);
+			g_object_unref(image);
+			any_images = TRUE;
+
 			/* Write the new image tag */
-			g_string_append_printf(newmsg,
-			                       "<IMG SRC=\"" PURPLE_STORED_IMAGE_PROTOCOL "%d\">",
-			                       imgid);
+			g_string_append_printf(newmsg, "<img src=\""
+				PURPLE_IMAGE_STORE_PROTOCOL "%u\">", img_id);
 		}
 
 		/* Continue from the end of the tag */
@@ -402,22 +400,13 @@ peer_odc_handle_payload(PeerConnection *
 
 	/* Display the message we received */
 	imflags = 0;
-	if (images != NULL)
+	if (any_images)
 		imflags |= PURPLE_MESSAGE_IMAGES;
 	if (autoreply)
 		imflags |= PURPLE_MESSAGE_AUTO_RESP;
 	purple_serv_got_im(gc, conn->bn, newmsg->str, imflags, time(NULL));
 	g_string_free(newmsg, TRUE);
 
-	/* unref any images we allocated */
-	if (images)
-	{
-		GSList *l;
-		for (l = images; l != NULL; l = l->next)
-			purple_imgstore_unref_by_id(GPOINTER_TO_INT(l->data));
-		g_slist_free(images);
-	}
-
 	/* Delete our list of pointers to embedded images */
 	g_hash_table_destroy(embedded_datas);
 }
diff --git a/libpurple/protocols/oscar/oscar.c b/libpurple/protocols/oscar/oscar.c
--- a/libpurple/protocols/oscar/oscar.c
+++ b/libpurple/protocols/oscar/oscar.c
@@ -38,7 +38,7 @@
 #include "core.h"
 #include "debug.h"
 #include "encoding.h"
-#include "imgstore.h"
+#include "image-store.h"
 #include "network.h"
 #include "notify.h"
 #include "prpl.h"
@@ -1349,7 +1349,7 @@ static int incomingim_chan1(OscarData *o
 	PurpleAccount *account = purple_connection_get_account(gc);
 	PurpleMessageFlags flags = 0;
 	struct buddyinfo *bi;
-	PurpleStoredImage *img;
+	PurpleImage *img;
 	gchar *tmp;
 	const char *start, *end;
 	GData *attribs;
@@ -1383,8 +1383,8 @@ static int incomingim_chan1(OscarData *o
 	img = purple_buddy_icons_find_account_icon(account);
 	if ((img != NULL) &&
 	    (args->icbmflags & AIM_IMFLAGS_BUDDYREQ) && !bi->ico_sent && bi->ico_informed) {
-		gconstpointer data = purple_imgstore_get_data(img);
-		size_t len = purple_imgstore_get_size(img);
+		gconstpointer data = purple_image_get_data(img);
+		size_t len = purple_image_get_size(img);
 		purple_debug_info("oscar",
 				"Sending buddy icon to %s (%" G_GSIZE_FORMAT " bytes)\n",
 				userinfo->bn, len);
@@ -1392,7 +1392,7 @@ static int incomingim_chan1(OscarData *o
 			purple_buddy_icons_get_account_icon_timestamp(account),
 			aimutil_iconsum(data, len));
 	}
-	purple_imgstore_unref(img);
+	g_object_unref(img);
 
 	tmp = g_strdup(args->msg);
 
@@ -2500,15 +2500,15 @@ purple_icons_fetch(PurpleConnection *gc)
 
 	if (od->set_icon) {
 		PurpleAccount *account = purple_connection_get_account(gc);
-		PurpleStoredImage *img = purple_buddy_icons_find_account_icon(account);
+		PurpleImage *img = purple_buddy_icons_find_account_icon(account);
 		if (img == NULL) {
 			aim_ssi_delicon(od);
 		} else {
 			purple_debug_info("oscar",
-				   "Uploading icon to icon server\n");
-			aim_bart_upload(od, purple_imgstore_get_data(img),
-			                purple_imgstore_get_size(img));
-			purple_imgstore_unref(img);
+				"Uploading icon to icon server");
+			aim_bart_upload(od, purple_image_get_data(img),
+				purple_image_get_size(img));
+			g_object_unref(img);
 		}
 		od->set_icon = FALSE;
 	}
@@ -3003,23 +3003,29 @@ purple_odc_send_im(PeerConnection *conn,
 	/* for each valid IMG tag... */



More information about the Commits mailing list