comparison gtk/gtkutils.c @ 15222:26357b7f117e

[gaim-migrate @ 18012] * Get rid of an assertion failure that I think was happening when you added a new account without setting a custom icon for it, and you still had the Accounts window open * A little code-reuse in some buddy icon scaling code * An minor memleak that could happen when unable open a new file in $HOME/.gaim/icons/ committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Sun, 17 Dec 2006 05:04:23 +0000
parents d380e165cd27
children a5ef8fc9fbeb
comparison
equal deleted inserted replaced
15221:3043806ad900 15222:26357b7f117e
2453 return TRUE; 2453 return TRUE;
2454 return FALSE; 2454 return FALSE;
2455 } 2455 }
2456 #endif 2456 #endif
2457 2457
2458 /* TODO: Use icon_spec.filesize */
2458 char * 2459 char *
2459 gaim_gtk_convert_buddy_icon(GaimPlugin *plugin, const char *path) 2460 gaim_gtk_convert_buddy_icon(GaimPlugin *plugin, const char *path)
2460 { 2461 {
2461 GaimPluginProtocolInfo *prpl_info; 2462 GaimPluginProtocolInfo *prpl_info;
2462 char **prpl_formats; 2463 char **prpl_formats;
2465 char **pixbuf_formats = NULL; 2466 char **pixbuf_formats = NULL;
2466 GdkPixbufFormat *format; 2467 GdkPixbufFormat *format;
2467 GdkPixbuf *pixbuf; 2468 GdkPixbuf *pixbuf;
2468 #if !GTK_CHECK_VERSION(2,4,0) 2469 #if !GTK_CHECK_VERSION(2,4,0)
2469 GdkPixbufLoader *loader; 2470 GdkPixbufLoader *loader;
2470 FILE *file;
2471 struct stat st;
2472 void *data = NULL;
2473 #endif 2471 #endif
2474 #endif 2472 #endif
2473 gchar *contents;
2474 gsize length;
2475 const char *dirname; 2475 const char *dirname;
2476 char *random; 2476 char *random;
2477 char *filename; 2477 char *filename;
2478 2478
2479 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(plugin); 2479 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(plugin);
2502 #if GTK_CHECK_VERSION(2,2,0) 2502 #if GTK_CHECK_VERSION(2,2,0)
2503 #if GTK_CHECK_VERSION(2,4,0) 2503 #if GTK_CHECK_VERSION(2,4,0)
2504 format = gdk_pixbuf_get_file_info (path, &width, &height); 2504 format = gdk_pixbuf_get_file_info (path, &width, &height);
2505 #else 2505 #else
2506 loader = gdk_pixbuf_loader_new(); 2506 loader = gdk_pixbuf_loader_new();
2507 if (!g_stat(path, &st) && (file = g_fopen(path, "rb")) != NULL) { 2507 if (g_file_get_contents(path, &contents, &length, NULL)) {
2508 data = g_malloc(st.st_size); 2508 gdk_pixbuf_loader_write(loader, contents, length, NULL);
2509 fread(data, 1, st.st_size, file); 2509 g_free(contents);
2510 fclose(file);
2511 gdk_pixbuf_loader_write(loader, data, st.st_size, NULL);
2512 g_free(data);
2513 } 2510 }
2514 gdk_pixbuf_loader_close(loader, NULL); 2511 gdk_pixbuf_loader_close(loader, NULL);
2515 pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); 2512 pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
2516 width = gdk_pixbuf_get_width(pixbuf); 2513 width = gdk_pixbuf_get_width(pixbuf);
2517 height = gdk_pixbuf_get_height(pixbuf); 2514 height = gdk_pixbuf_get_height(pixbuf);
2528 prpl_info->icon_spec.max_width >= width && 2525 prpl_info->icon_spec.max_width >= width &&
2529 prpl_info->icon_spec.min_height <= height && 2526 prpl_info->icon_spec.min_height <= height &&
2530 prpl_info->icon_spec.max_height >= height))) /* The icon is the correct size */ 2527 prpl_info->icon_spec.max_height >= height))) /* The icon is the correct size */
2531 #endif 2528 #endif
2532 { 2529 {
2533 gchar *contents;
2534 gsize length;
2535 FILE *image; 2530 FILE *image;
2536 2531
2537 #if GTK_CHECK_VERSION(2,2,0) 2532 #if GTK_CHECK_VERSION(2,2,0)
2538 g_strfreev(prpl_formats); 2533 g_strfreev(prpl_formats);
2539 g_strfreev(pixbuf_formats); 2534 g_strfreev(pixbuf_formats);
2540 #endif 2535 #endif
2541 2536
2542 /* Copy the image to the cache folder as "filename". */ 2537 /* We don't need to scale the image, so copy it to the cache folder verbatim */
2543 2538
2539 contents = NULL;
2544 if (!g_file_get_contents(path, &contents, &length, NULL) || 2540 if (!g_file_get_contents(path, &contents, &length, NULL) ||
2545 (image = g_fopen(filename, "wb")) == NULL) 2541 (image = g_fopen(filename, "wb")) == NULL)
2546 { 2542 {
2547 g_free(random); 2543 g_free(random);
2548 g_free(filename); 2544 g_free(filename);
2545 g_free(contents);
2549 #if GTK_CHECK_VERSION(2,2,0) && !GTK_CHECK_VERSION(2,4,0) 2546 #if GTK_CHECK_VERSION(2,2,0) && !GTK_CHECK_VERSION(2,4,0)
2550 g_object_unref(G_OBJECT(pixbuf)); 2547 g_object_unref(G_OBJECT(pixbuf));
2551 #endif 2548 #endif
2552 return NULL; 2549 return NULL;
2553 } 2550 }
2590 height > prpl_info->icon_spec.max_height)) 2587 height > prpl_info->icon_spec.max_height))
2591 { 2588 {
2592 int new_width = width; 2589 int new_width = width;
2593 int new_height = height; 2590 int new_height = height;
2594 2591
2595 if(new_width > prpl_info->icon_spec.max_width) 2592 gaim_buddy_icon_get_scale_size(&prpl_info->icon_spec, &new_width, &new_height);
2596 new_width = prpl_info->icon_spec.max_width;
2597 else if(new_width < prpl_info->icon_spec.min_width)
2598 new_width = prpl_info->icon_spec.min_width;
2599 if(new_height > prpl_info->icon_spec.max_height)
2600 new_height = prpl_info->icon_spec.max_height;
2601 else if(new_height < prpl_info->icon_spec.min_height)
2602 new_height = prpl_info->icon_spec.min_height;
2603
2604 /* preserve aspect ratio */
2605 if ((double)height * (double)new_width >
2606 (double)width * (double)new_height) {
2607 new_width = 0.5 + (double)width * (double)new_height / (double)height;
2608 } else {
2609 new_height = 0.5 + (double)height * (double)new_width / (double)width;
2610 }
2611 2593
2612 scale = gdk_pixbuf_scale_simple (pixbuf, new_width, new_height, 2594 scale = gdk_pixbuf_scale_simple (pixbuf, new_width, new_height,
2613 GDK_INTERP_HYPER); 2595 GDK_INTERP_HYPER);
2614 g_object_unref(G_OBJECT(pixbuf)); 2596 g_object_unref(G_OBJECT(pixbuf));
2615 pixbuf = scale; 2597 pixbuf = scale;
2623 return NULL; 2605 return NULL;
2624 } 2606 }
2625 2607
2626 for (i = 0; prpl_formats[i]; i++) { 2608 for (i = 0; prpl_formats[i]; i++) {
2627 gaim_debug_info("buddyicon", "Converting buddy icon to %s as %s\n", prpl_formats[i], filename); 2609 gaim_debug_info("buddyicon", "Converting buddy icon to %s as %s\n", prpl_formats[i], filename);
2628 /* The gdk-pixbuf documentation is wrong. gdk_pixbuf_save returns TRUE if it was successful, 2610 if (gdk_pixbuf_save(pixbuf, filename, prpl_formats[i], &error, NULL))
2629 * FALSE if an error was set. */ 2611 /* Success! */
2630 if (gdk_pixbuf_save (pixbuf, filename, prpl_formats[i], &error, NULL) == TRUE) 2612 break;
2631 break;
2632 gaim_debug_warning("buddyicon", "Could not convert to %s: %s\n", prpl_formats[i], error->message); 2613 gaim_debug_warning("buddyicon", "Could not convert to %s: %s\n", prpl_formats[i], error->message);
2633 g_error_free(error); 2614 g_error_free(error);
2634 error = NULL; 2615 error = NULL;
2635 } 2616 }
2636 g_strfreev(prpl_formats); 2617 g_strfreev(prpl_formats);