/pidgin/main: 6dd40b244ce3: PurpleImage: convert buddyicon.c

Tomasz Wasilczyk twasilczyk at pidgin.im
Thu Apr 10 15:51:29 EDT 2014


Changeset: 6dd40b244ce35f1bc6f89fd0cff29a548d038118
Author:	 Tomasz Wasilczyk <twasilczyk at pidgin.im>
Date:	 2014-04-10 21:51 +0200
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/6dd40b244ce3

Description:

PurpleImage: convert buddyicon.c

diffstat:

 libpurple/buddyicon.c |  188 ++++++++++++++++++++++++++-----------------------
 libpurple/buddyicon.h |   30 +++---
 libpurple/image.c     |   75 +++++++++++++++++--
 libpurple/image.h     |    8 +-
 libpurple/imgstore.c  |    7 +-
 valgrind-suppressions |    8 ++
 6 files changed, 201 insertions(+), 115 deletions(-)

diffs (truncated from 765 to 300 lines):

diff --git a/libpurple/buddyicon.c b/libpurple/buddyicon.c
--- a/libpurple/buddyicon.c
+++ b/libpurple/buddyicon.c
@@ -25,7 +25,7 @@
 #include "conversation.h"
 #include "dbus-maybe.h"
 #include "debug.h"
-#include "imgstore.h"
+#include "image.h"
 #include "util.h"
 
 /* NOTE: Instances of this struct are allocated without zeroing the memory, so
@@ -33,7 +33,7 @@
 struct _PurpleBuddyIcon
 {
 	PurpleAccount *account;    /* The account the user is on.          */
-	PurpleStoredImage *img;    /* The stored image containing
+	PurpleImage *img;          /* The image containing
 	                              the icon data.                       */
 	char *username;            /* The username the icon belongs to.    */
 	char *checksum;            /* The protocol checksum.               */
@@ -53,7 +53,7 @@ struct _PurpleBuddyIcon
 static GHashTable *account_cache = NULL;
 
 /*
- * This hash table contains a bunch of PurpleStoredImages that are
+ * This hash table contains a bunch of PurpleImages that are
  * shared across all accounts.
  *
  * Key is the filename for this image as constructed by
@@ -61,11 +61,10 @@ static GHashTable *account_cache = NULL;
  * sha-1 hash plus an appropriate file extension.  For example:
  *   "0f4972d17d1e70e751c43c90c948e72efbff9796.gif"
  *
- * The value is a PurpleStoredImage containing the icon data.  These
- * images are reference counted, and when the count reaches 0
- * imgstore.c emits the image-deleting signal and we remove the image
- * from the hash table (but it might still be saved on disk, if the
- * icon is being used by offline accounts or some such).
+ * The value is a PurpleImage containing the icon data.  These images are
+ * reference counted, and when the count reaches 0 we remove the image from
+ * the hash table (but it might still be saved on disk, if the icon is being
+ * used by offline accounts or some such).
  */
 static GHashTable *icon_data_cache = NULL;
 
@@ -77,7 +76,7 @@ static GHashTable *icon_data_cache = NUL
  * time Pidgin starts.
  *
  * Key is the filename for this image as constructed by
- * purple_util_get_image_filename().  So it is the base16 encoded
+ * purple_image_generate_filename().  So it is the base16 encoded
  * sha-1 hash plus an appropriate file extension.  For example:
  *   "0f4972d17d1e70e751c43c90c948e72efbff9796.gif"
  *
@@ -140,34 +139,43 @@ unref_filename(const char *filename)
 	}
 }
 
+static const gchar *
+image_get_filename(PurpleImage *img)
+{
+	return g_object_get_data(G_OBJECT(img), "purple-buddyicon-filename");
+}
+
 static void
-purple_buddy_icon_data_cache(PurpleStoredImage *img)
+purple_buddy_icon_data_cache(PurpleImage *img)
 {
-	const char *dirname;
-	char *path;
+	const gchar *dirname, *filename;
+	gchar *path;
 
-	g_return_if_fail(img != NULL);
+	g_return_if_fail(PURPLE_IS_IMAGE(img));
 
 	if (!purple_buddy_icons_is_caching())
 		return;
 
 	dirname = purple_buddy_icons_get_cache_dir();
-	path = g_build_filename(dirname, purple_imgstore_get_filename(img), NULL);
+	filename = image_get_filename(img);
+	g_return_if_fail(filename != NULL);
+	path = g_build_filename(dirname, filename, NULL);
 
 	if (!g_file_test(dirname, G_FILE_TEST_IS_DIR))
 	{
-		purple_debug_info("buddyicon", "Creating icon cache directory.\n");
+		purple_debug_info("buddyicon", "creating icon cache directory");
 
 		if (g_mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR) < 0)
 		{
 			purple_debug_error("buddyicon",
-			                   "Unable to create directory %s: %s\n",
-			                   dirname, g_strerror(errno));
+				"unable to create directory %s: %s",
+				dirname, g_strerror(errno));
+			return;
 		}
 	}
 
-	purple_util_write_data_to_file_absolute(path, purple_imgstore_get_data(img),
-											purple_imgstore_get_size(img));
+	if (!purple_image_save(img, path))
+		purple_debug_error("buddyicon", "failed to save icon %s", path);
 	g_free(path);
 }
 
@@ -218,51 +226,50 @@ value_equals(gpointer key, gpointer valu
 }
 
 static void
-image_deleting_cb(const PurpleStoredImage *img, gpointer data)
+image_deleting_cb(gpointer _filename)
 {
-	const char *filename = purple_imgstore_get_filename(img);
+	PurpleImage *img;
+	gchar *filename = _filename;
 
-	/* If there's no filename, it can't be one of our images. */
-	if (filename == NULL)
-		return;
+	img = g_hash_table_lookup(icon_data_cache, filename);
+	purple_buddy_icon_data_uncache_file(filename);
+	g_hash_table_remove(icon_data_cache, filename);
 
-	if (img == g_hash_table_lookup(icon_data_cache, filename))
-	{
-		purple_buddy_icon_data_uncache_file(filename);
-		g_hash_table_remove(icon_data_cache, filename);
+	/* We could make this O(1) by using another hash table, but
+	 * this is probably good enough. */
+	g_hash_table_foreach_remove(pointer_icon_cache, value_equals, (gpointer)img);
 
-		/* We could make this O(1) by using another hash table, but
-		 * this is probably good enough. */
-		g_hash_table_foreach_remove(pointer_icon_cache, value_equals, (gpointer)img);
-	}
+	g_free(filename);
 }
 
-static PurpleStoredImage *
+static PurpleImage *
 purple_buddy_icon_data_new(guchar *icon_data, size_t icon_len)
 {
-	char *file;
-	PurpleStoredImage *img;
+	PurpleImage *newimg, *oldimg;
+	const gchar *filename;
 
 	g_return_val_if_fail(icon_data != NULL, NULL);
 	g_return_val_if_fail(icon_len > 0, NULL);
 
-	file = purple_util_get_image_filename(icon_data, icon_len);
+	newimg = purple_image_new_from_data(icon_data, icon_len);
+	filename = purple_image_generate_filename(newimg);
 
-	img = g_hash_table_lookup(icon_data_cache, file);
-	if (img) {
-		g_free(file);
-		g_free(icon_data);
-		return purple_imgstore_ref(img);
+	oldimg = g_hash_table_lookup(icon_data_cache, filename);
+	if (oldimg) {
+		g_warn_if_fail(PURPLE_IS_IMAGE(oldimg));
+		g_object_unref(newimg);
+		g_object_ref(oldimg);
+		return oldimg;
 	}
 
-	img = purple_imgstore_new(icon_data, icon_len, file);
+	/* This will take ownership of file and free it as needed */
+	g_hash_table_insert(icon_data_cache, g_strdup(filename), newimg);
+	g_object_set_data_full(G_OBJECT(newimg), "purple-buddyicon-filename",
+		g_strdup(filename), image_deleting_cb);
 
-	/* This will take ownership of file and free it as needed */
-	g_hash_table_insert(icon_data_cache, file, img);
+	purple_buddy_icon_data_cache(newimg);
 
-	purple_buddy_icon_data_cache(img);
-
-	return img;
+	return newimg;
 }
 
 /*
@@ -356,7 +363,7 @@ purple_buddy_icon_unref(PurpleBuddyIcon 
 
 		g_free(icon->username);
 		g_free(icon->checksum);
-		purple_imgstore_unref(icon->img);
+		g_object_unref(icon->img);
 
 		PURPLE_DBUS_UNREGISTER_POINTER(icon);
 		g_slice_free(PurpleBuddyIcon, icon);
@@ -398,7 +405,8 @@ purple_buddy_icon_update(PurpleBuddyIcon
 		                                                 "buddy_icon"));
 		if (icon->img && purple_buddy_icons_is_caching())
 		{
-			const char *filename = purple_imgstore_get_filename(icon->img);
+			const char *filename = image_get_filename(icon->img);
+			g_warn_if_fail(filename != NULL);
 			purple_blist_node_set_string((PurpleBlistNode *)buddy,
 			                             "buddy_icon",
 			                             filename);
@@ -440,7 +448,7 @@ void
 purple_buddy_icon_set_data(PurpleBuddyIcon *icon, guchar *data,
                            size_t len, const char *checksum)
 {
-	PurpleStoredImage *old_img;
+	PurpleImage *old_img;
 
 	g_return_if_fail(icon != NULL);
 
@@ -460,7 +468,8 @@ purple_buddy_icon_set_data(PurpleBuddyIc
 
 	purple_buddy_icon_update(icon);
 
-	purple_imgstore_unref(old_img);
+	if (old_img)
+		g_object_unref(old_img);
 }
 
 PurpleAccount *
@@ -495,9 +504,9 @@ purple_buddy_icon_get_data(const PurpleB
 	if (icon->img)
 	{
 		if (len != NULL)
-			*len = purple_imgstore_get_size(icon->img);
+			*len = purple_image_get_size(icon->img);
 
-		return purple_imgstore_get_data(icon->img);
+		return purple_image_get_data(icon->img);
 	}
 
 	return NULL;
@@ -507,7 +516,7 @@ const char *
 purple_buddy_icon_get_extension(const PurpleBuddyIcon *icon)
 {
 	if (icon->img != NULL)
-		return purple_imgstore_get_extension(icon->img);
+		return purple_image_get_extension(icon->img);
 
 	return NULL;
 }
@@ -563,20 +572,19 @@ purple_buddy_icons_set_for_user(PurpleAc
 	}
 }
 
-char *purple_buddy_icon_get_full_path(PurpleBuddyIcon *icon)
+const gchar *
+purple_buddy_icon_get_full_path(PurpleBuddyIcon *icon)
 {
-	char *path;
+	const gchar *path;
 
 	g_return_val_if_fail(icon != NULL, NULL);
 
 	if (icon->img == NULL)
 		return NULL;
 
-	path = g_build_filename(purple_buddy_icons_get_cache_dir(),
-	                        purple_imgstore_get_filename(icon->img), NULL);
+	path = purple_image_get_path(icon->img);
 	if (!g_file_test(path, G_FILE_TEST_EXISTS))
 	{
-		g_free(path);
 		return NULL;
 	}
 	return path;
@@ -667,10 +675,10 @@ purple_buddy_icons_find(PurpleAccount *a
 	return (icon ? purple_buddy_icon_ref(icon) : NULL);
 }
 
-PurpleStoredImage *
+PurpleImage *
 purple_buddy_icons_find_account_icon(PurpleAccount *account)
 {
-	PurpleStoredImage *img;
+	PurpleImage *img;
 	const char *account_icon_file;
 	const char *dirname;
 	char *path;
@@ -679,9 +687,10 @@ purple_buddy_icons_find_account_icon(Pur
 
 	g_return_val_if_fail(account != NULL, NULL);
 
-	if ((img = g_hash_table_lookup(pointer_icon_cache, account)))
-	{
-		return purple_imgstore_ref(img);
+	img = g_hash_table_lookup(pointer_icon_cache, account);
+	if (img) {
+		g_object_ref(img);
+		return img;
 	}
 
 	account_icon_file = purple_account_get_string(account, "buddy_icon", NULL);
@@ -692,23 +701,23 @@ purple_buddy_icons_find_account_icon(Pur



More information about the Commits mailing list