changeset 9334:b2f102b8abf1

[gaim-migrate @ 10142] nosnilmot made Sean's automatic buddy icon conversation stuff work on GTK 2.2. Thanks nosnilmot! committer: Tailor Script <tailor@pidgin.im>
author Tim Ringenbach <marv@pidgin.im>
date Mon, 21 Jun 2004 23:42:05 +0000
parents 120e0607e1cd
children b726aba28229
files ChangeLog src/gtkaccount.c
diffstat 2 files changed, 40 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun Jun 20 23:02:36 2004 +0000
+++ b/ChangeLog	Mon Jun 21 23:42:05 2004 +0000
@@ -23,7 +23,7 @@
 	  defaults for the next pounce
 	* Yahoo buddy icon support
 	* Selected buddy icons will automatically convert to the appropriate
-	  format for the protocol. (GTK 2.4 only)
+	  format for the protocol. (GTK 2.2 and higher only)
 	* Dragging an image file into the Modify Account dialog will set that
 	  as a buddy icon.
 
--- a/src/gtkaccount.c	Sun Jun 20 23:02:36 2004 +0000
+++ b/src/gtkaccount.c	Mon Jun 21 23:42:05 2004 +0000
@@ -430,10 +430,14 @@
 }
 
 
-#if GTK_CHECK_VERSION(2,4,0)
-gboolean str_array_match(char **a, char **b)
+#if GTK_CHECK_VERSION(2,2,0)
+static gboolean
+str_array_match(char **a, char **b)
 {
 	int i, j;
+
+	if (!a || !b)
+		return FALSE;
 	for (i = 0; a[i] != NULL; i++)
 		for (j = 0; b[j] != NULL; j++)
 			if (!g_ascii_strcasecmp(a[i], b[j]))
@@ -445,14 +449,38 @@
 static void
 convert_and_set_buddy_icon(GaimAccount *account, const char *path)
 {
-#if GTK_CHECK_VERSION(2,4,0)
+#if GTK_CHECK_VERSION(2,2,0)
 	int width, height;
-	char **pixbuf_formats;
+	char **pixbuf_formats = NULL;
 	GdkPixbufFormat *format;
+	GdkPixbuf *pixbuf;
 	GaimPluginProtocolInfo *prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gaim_find_prpl(account->protocol_id));
 	char **prpl_formats =  g_strsplit (prpl_info->icon_spec.format,",",0);
-
+#if !GTK_CHECK_VERSION(2,4,0)
+	GdkPixbufLoader *loader;
+	FILE *file;
+	struct stat st;
+	void *data = NULL;
+#endif
+
+#if GTK_CHECK_VERSION(2,4,0)
 	format = gdk_pixbuf_get_file_info (path, &width, &height);
+#else
+	loader = gdk_pixbuf_loader_new();
+	if (!stat(path, &st) && (file = fopen(path, "rb")) != NULL) {
+		data = g_malloc(st.st_size);
+		fread(data, 1, st.st_size, file);
+		fclose(file);
+		gdk_pixbuf_loader_write(loader, data, st.st_size, NULL);
+		g_free(data);
+	}
+	pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
+	width = gdk_pixbuf_get_width(pixbuf);
+	height = gdk_pixbuf_get_height(pixbuf);
+	format = gdk_pixbuf_loader_get_format(loader);
+	gdk_pixbuf_loader_close(loader, NULL);
+	g_object_unref(G_OBJECT(pixbuf));
+#endif
 	pixbuf_formats =  gdk_pixbuf_format_get_extensions(format);
 
 	if (str_array_match(pixbuf_formats, prpl_formats) &&                                 /* This is an acceptable format AND */
@@ -463,15 +491,15 @@
 		   prpl_info->icon_spec.max_height >= height))) {                                  /* The icon is the correct size */
 #endif
 		gaim_account_set_buddy_icon(account, path);
-#if GTK_CHECK_VERSION(2,4,0)
+#if GTK_CHECK_VERSION(2,2,0)
 	} else {
 		int i;
 		GError *error = NULL;
-		GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &error);
 		GdkPixbuf *scale;
 		char *random   = g_strdup_printf("%x", g_random_int());
 		const char *dirname = gaim_buddy_icons_get_cache_dir();
 		char *filename = g_build_filename(dirname, random, NULL);
+		pixbuf = gdk_pixbuf_new_from_file(path, &error);
 		if (!error && prpl_info->icon_spec.scale_rules & GAIM_ICON_SCALE_SEND) {
 			int new_width = gdk_pixbuf_get_width(pixbuf);
 			int new_height = gdk_pixbuf_get_height(pixbuf);
@@ -487,13 +515,14 @@
 
 			scale = gdk_pixbuf_scale_simple (pixbuf, new_width, new_height,
 					GDK_INTERP_HYPER);
-			gdk_pixbuf_unref(pixbuf);
+			g_object_unref(G_OBJECT(pixbuf));
 			pixbuf = scale;
 		}
 		if (error) {
 			g_free(filename);
 			g_free(random);
 			gaim_debug_error("buddyicon", "Could not open icon for conversion: %s\n", error->message);
+			g_error_free(error);
 			return;
 		}
 		for (i = 0; prpl_formats[i]; i++) {
@@ -505,10 +534,11 @@
 			gaim_account_set_buddy_icon(account, filename);
 		} else {
 			gaim_debug_error("buddyicon", "Could not convert icon to usable format: %s\n", error->message);
+			g_error_free(error);
 		}
 		g_free(filename);
 		g_free(random);
-		g_object_unref(pixbuf);
+		g_object_unref(G_OBJECT(pixbuf));
 	}
 #endif
 }