comparison src/gtkaccount.c @ 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 d0380151c6d1
comparison
equal deleted inserted replaced
9333:120e0607e1cd 9334:b2f102b8abf1
428 } 428 }
429 gtk_drag_finish(dc, FALSE, FALSE, t); 429 gtk_drag_finish(dc, FALSE, FALSE, t);
430 } 430 }
431 431
432 432
433 #if GTK_CHECK_VERSION(2,4,0) 433 #if GTK_CHECK_VERSION(2,2,0)
434 gboolean str_array_match(char **a, char **b) 434 static gboolean
435 str_array_match(char **a, char **b)
435 { 436 {
436 int i, j; 437 int i, j;
438
439 if (!a || !b)
440 return FALSE;
437 for (i = 0; a[i] != NULL; i++) 441 for (i = 0; a[i] != NULL; i++)
438 for (j = 0; b[j] != NULL; j++) 442 for (j = 0; b[j] != NULL; j++)
439 if (!g_ascii_strcasecmp(a[i], b[j])) 443 if (!g_ascii_strcasecmp(a[i], b[j]))
440 return TRUE; 444 return TRUE;
441 return FALSE; 445 return FALSE;
443 #endif 447 #endif
444 448
445 static void 449 static void
446 convert_and_set_buddy_icon(GaimAccount *account, const char *path) 450 convert_and_set_buddy_icon(GaimAccount *account, const char *path)
447 { 451 {
448 #if GTK_CHECK_VERSION(2,4,0) 452 #if GTK_CHECK_VERSION(2,2,0)
449 int width, height; 453 int width, height;
450 char **pixbuf_formats; 454 char **pixbuf_formats = NULL;
451 GdkPixbufFormat *format; 455 GdkPixbufFormat *format;
456 GdkPixbuf *pixbuf;
452 GaimPluginProtocolInfo *prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gaim_find_prpl(account->protocol_id)); 457 GaimPluginProtocolInfo *prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gaim_find_prpl(account->protocol_id));
453 char **prpl_formats = g_strsplit (prpl_info->icon_spec.format,",",0); 458 char **prpl_formats = g_strsplit (prpl_info->icon_spec.format,",",0);
454 459 #if !GTK_CHECK_VERSION(2,4,0)
460 GdkPixbufLoader *loader;
461 FILE *file;
462 struct stat st;
463 void *data = NULL;
464 #endif
465
466 #if GTK_CHECK_VERSION(2,4,0)
455 format = gdk_pixbuf_get_file_info (path, &width, &height); 467 format = gdk_pixbuf_get_file_info (path, &width, &height);
468 #else
469 loader = gdk_pixbuf_loader_new();
470 if (!stat(path, &st) && (file = fopen(path, "rb")) != NULL) {
471 data = g_malloc(st.st_size);
472 fread(data, 1, st.st_size, file);
473 fclose(file);
474 gdk_pixbuf_loader_write(loader, data, st.st_size, NULL);
475 g_free(data);
476 }
477 pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
478 width = gdk_pixbuf_get_width(pixbuf);
479 height = gdk_pixbuf_get_height(pixbuf);
480 format = gdk_pixbuf_loader_get_format(loader);
481 gdk_pixbuf_loader_close(loader, NULL);
482 g_object_unref(G_OBJECT(pixbuf));
483 #endif
456 pixbuf_formats = gdk_pixbuf_format_get_extensions(format); 484 pixbuf_formats = gdk_pixbuf_format_get_extensions(format);
457 485
458 if (str_array_match(pixbuf_formats, prpl_formats) && /* This is an acceptable format AND */ 486 if (str_array_match(pixbuf_formats, prpl_formats) && /* This is an acceptable format AND */
459 (!(prpl_info->icon_spec.scale_rules & GAIM_ICON_SCALE_SEND) || /* The prpl doesn't scale before it sends OR */ 487 (!(prpl_info->icon_spec.scale_rules & GAIM_ICON_SCALE_SEND) || /* The prpl doesn't scale before it sends OR */
460 (prpl_info->icon_spec.min_width <= width && 488 (prpl_info->icon_spec.min_width <= width &&
461 prpl_info->icon_spec.max_width >= width && 489 prpl_info->icon_spec.max_width >= width &&
462 prpl_info->icon_spec.min_height <= height && 490 prpl_info->icon_spec.min_height <= height &&
463 prpl_info->icon_spec.max_height >= height))) { /* The icon is the correct size */ 491 prpl_info->icon_spec.max_height >= height))) { /* The icon is the correct size */
464 #endif 492 #endif
465 gaim_account_set_buddy_icon(account, path); 493 gaim_account_set_buddy_icon(account, path);
466 #if GTK_CHECK_VERSION(2,4,0) 494 #if GTK_CHECK_VERSION(2,2,0)
467 } else { 495 } else {
468 int i; 496 int i;
469 GError *error = NULL; 497 GError *error = NULL;
470 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, &error);
471 GdkPixbuf *scale; 498 GdkPixbuf *scale;
472 char *random = g_strdup_printf("%x", g_random_int()); 499 char *random = g_strdup_printf("%x", g_random_int());
473 const char *dirname = gaim_buddy_icons_get_cache_dir(); 500 const char *dirname = gaim_buddy_icons_get_cache_dir();
474 char *filename = g_build_filename(dirname, random, NULL); 501 char *filename = g_build_filename(dirname, random, NULL);
502 pixbuf = gdk_pixbuf_new_from_file(path, &error);
475 if (!error && prpl_info->icon_spec.scale_rules & GAIM_ICON_SCALE_SEND) { 503 if (!error && prpl_info->icon_spec.scale_rules & GAIM_ICON_SCALE_SEND) {
476 int new_width = gdk_pixbuf_get_width(pixbuf); 504 int new_width = gdk_pixbuf_get_width(pixbuf);
477 int new_height = gdk_pixbuf_get_height(pixbuf); 505 int new_height = gdk_pixbuf_get_height(pixbuf);
478 506
479 if(new_width > prpl_info->icon_spec.max_width) 507 if(new_width > prpl_info->icon_spec.max_width)
485 else if(new_height < prpl_info->icon_spec.min_height) 513 else if(new_height < prpl_info->icon_spec.min_height)
486 new_height = prpl_info->icon_spec.min_height; 514 new_height = prpl_info->icon_spec.min_height;
487 515
488 scale = gdk_pixbuf_scale_simple (pixbuf, new_width, new_height, 516 scale = gdk_pixbuf_scale_simple (pixbuf, new_width, new_height,
489 GDK_INTERP_HYPER); 517 GDK_INTERP_HYPER);
490 gdk_pixbuf_unref(pixbuf); 518 g_object_unref(G_OBJECT(pixbuf));
491 pixbuf = scale; 519 pixbuf = scale;
492 } 520 }
493 if (error) { 521 if (error) {
494 g_free(filename); 522 g_free(filename);
495 g_free(random); 523 g_free(random);
496 gaim_debug_error("buddyicon", "Could not open icon for conversion: %s\n", error->message); 524 gaim_debug_error("buddyicon", "Could not open icon for conversion: %s\n", error->message);
525 g_error_free(error);
497 return; 526 return;
498 } 527 }
499 for (i = 0; prpl_formats[i]; i++) { 528 for (i = 0; prpl_formats[i]; i++) {
500 gaim_debug_info("buddyicon", "Converting buddy icon to %s as %s\n", prpl_formats[i], filename); 529 gaim_debug_info("buddyicon", "Converting buddy icon to %s as %s\n", prpl_formats[i], filename);
501 if (gdk_pixbuf_save (pixbuf, filename, prpl_formats[i], &error, NULL) == FALSE) 530 if (gdk_pixbuf_save (pixbuf, filename, prpl_formats[i], &error, NULL) == FALSE)
503 } 532 }
504 if (!error) { 533 if (!error) {
505 gaim_account_set_buddy_icon(account, filename); 534 gaim_account_set_buddy_icon(account, filename);
506 } else { 535 } else {
507 gaim_debug_error("buddyicon", "Could not convert icon to usable format: %s\n", error->message); 536 gaim_debug_error("buddyicon", "Could not convert icon to usable format: %s\n", error->message);
537 g_error_free(error);
508 } 538 }
509 g_free(filename); 539 g_free(filename);
510 g_free(random); 540 g_free(random);
511 g_object_unref(pixbuf); 541 g_object_unref(G_OBJECT(pixbuf));
512 } 542 }
513 #endif 543 #endif
514 } 544 }
515 545
516 static void 546 static void