comparison src/gtkaccount.c @ 11286:0c2d1b4b1351

[gaim-migrate @ 13483] Always save the user's buddy icon to the cache folder. That way, they don't have to keep the original image around. Then, delete the file from the cache when the user sets a new icon. committer: Tailor Script <tailor@pidgin.im>
author Richard Laager <rlaager@wiktel.com>
date Wed, 17 Aug 2005 03:22:23 +0000
parents bb0d7b719af2
children ee1b890760be
comparison
equal deleted inserted replaced
11285:bcd7bd6a42dd 11286:0c2d1b4b1351
194 194
195 return hbox; 195 return hbox;
196 } 196 }
197 197
198 static void 198 static void
199 delete_buddy_icon(const char *filename)
200 {
201 const char *dirname;
202
203 if (filename == NULL)
204 return;
205
206 /* XXX: This is a hack so we only delete the file if it's
207 * in the cache dir. Otherwise, people who upgrade (who
208 * may have buddy icon filenames set outside of the cache
209 * dir) could lose files. */
210 dirname = gaim_buddy_icons_get_cache_dir();
211 if (!strncmp(dirname, filename, strlen(dirname)))
212 g_unlink(filename);
213 }
214
215 static void
199 set_account_protocol_cb(GtkWidget *item, const char *id, 216 set_account_protocol_cb(GtkWidget *item, const char *id,
200 AccountPrefsDialog *dialog) 217 AccountPrefsDialog *dialog)
201 { 218 {
202 GaimPlugin *new_plugin; 219 GaimPlugin *new_plugin;
203 220
526 GdkPixbufLoader *loader; 543 GdkPixbufLoader *loader;
527 FILE *file; 544 FILE *file;
528 struct stat st; 545 struct stat st;
529 void *data = NULL; 546 void *data = NULL;
530 #endif 547 #endif
531 548 #endif
549 const char *dirname = gaim_buddy_icons_get_cache_dir();
550 char *random = g_strdup_printf("%x", g_random_int());
551 char *filename = g_build_filename(dirname, random, NULL);
552 g_free(random);
553
554 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
555 gaim_debug_info("buddyicon", "Creating icon cache directory.\n");
556
557 if (g_mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR) < 0) {
558 gaim_debug_error("buddyicon",
559 "Unable to create directory %s: %s\n",
560 dirname, strerror(errno));
561 #if GTK_CHECK_VERSION(2,2,0)
562 g_strfreev(prpl_formats);
563 #endif
564 g_free(filename);
565 return NULL;
566 }
567 }
568
569 #if GTK_CHECK_VERSION(2,2,0)
532 #if GTK_CHECK_VERSION(2,4,0) 570 #if GTK_CHECK_VERSION(2,4,0)
533 format = gdk_pixbuf_get_file_info (path, &width, &height); 571 format = gdk_pixbuf_get_file_info (path, &width, &height);
534 #else 572 #else
535 loader = gdk_pixbuf_loader_new(); 573 loader = gdk_pixbuf_loader_new();
536 if (!g_stat(path, &st) && (file = g_fopen(path, "rb")) != NULL) { 574 if (!g_stat(path, &st) && (file = g_fopen(path, "rb")) != NULL) {
547 gdk_pixbuf_loader_close(loader, NULL); 585 gdk_pixbuf_loader_close(loader, NULL);
548 g_object_unref(G_OBJECT(loader)); 586 g_object_unref(G_OBJECT(loader));
549 #endif 587 #endif
550 pixbuf_formats = gdk_pixbuf_format_get_extensions(format); 588 pixbuf_formats = gdk_pixbuf_format_get_extensions(format);
551 589
552 if (str_array_match(pixbuf_formats, prpl_formats) && /* This is an acceptable format AND */ 590 if (str_array_match(pixbuf_formats, prpl_formats) && /* This is an acceptable format AND */
553 (!(prpl_info->icon_spec.scale_rules & GAIM_ICON_SCALE_SEND) || /* The prpl doesn't scale before it sends OR */ 591 (!(prpl_info->icon_spec.scale_rules & GAIM_ICON_SCALE_SEND) || /* The prpl doesn't scale before it sends OR */
554 (prpl_info->icon_spec.min_width <= width && 592 (prpl_info->icon_spec.min_width <= width &&
555 prpl_info->icon_spec.max_width >= width && 593 prpl_info->icon_spec.max_width >= width &&
556 prpl_info->icon_spec.min_height <= height && 594 prpl_info->icon_spec.min_height <= height &&
557 prpl_info->icon_spec.max_height >= height))) { /* The icon is the correct size */ 595 prpl_info->icon_spec.max_height >= height))) /* The icon is the correct size */
596 #endif
597 {
598 gchar *contents;
599 gsize length;
600 FILE *image;
601
602 #if GTK_CHECK_VERSION(2,2,0)
558 g_strfreev(prpl_formats); 603 g_strfreev(prpl_formats);
559 g_strfreev(pixbuf_formats); 604 g_strfreev(pixbuf_formats);
560 #endif 605 #endif
561 return g_strdup(path); 606
607 /* Copy the image to the cache folder as "filename". */
608
609 if (!g_file_get_contents(path, &contents, &length, NULL) ||
610 (image = g_fopen(filename, "wb")) == NULL)
611 {
612 g_free(filename);
613 #if GTK_CHECK_VERSION(2,2,0) && !GTK_CHECK_VERSION(2,4,0)
614 g_object_unref(G_OBJECT(pixbuf));
615 #endif
616 return NULL;
617 }
618
619 if (fwrite(contents, 1, length, image) != length)
620 {
621 fclose(image);
622 g_unlink(filename);
623
624 g_free(filename);
625 #if GTK_CHECK_VERSION(2,2,0) && !GTK_CHECK_VERSION(2,4,0)
626 g_object_unref(G_OBJECT(pixbuf));
627 #endif
628 return NULL;
629 }
630 fclose(image);
631
632 #if GTK_CHECK_VERSION(2,2,0) && !GTK_CHECK_VERSION(2,4,0)
633 g_object_unref(G_OBJECT(pixbuf));
634 #endif
635
636 return filename;
637 }
562 #if GTK_CHECK_VERSION(2,2,0) 638 #if GTK_CHECK_VERSION(2,2,0)
563 } else { 639 else
640 {
564 int i; 641 int i;
565 GError *error = NULL; 642 GError *error = NULL;
566 GdkPixbuf *scale; 643 GdkPixbuf *scale;
567 char *random = g_strdup_printf("%x", g_random_int());
568 const char *dirname = gaim_buddy_icons_get_cache_dir();
569 char *filename = g_build_filename(dirname, random, NULL);
570 pixbuf = gdk_pixbuf_new_from_file(path, &error); 644 pixbuf = gdk_pixbuf_new_from_file(path, &error);
571 g_strfreev(pixbuf_formats); 645 g_strfreev(pixbuf_formats);
572 if (!error && (prpl_info->icon_spec.scale_rules & GAIM_ICON_SCALE_SEND) && 646 if (!error && (prpl_info->icon_spec.scale_rules & GAIM_ICON_SCALE_SEND) &&
573 (width < prpl_info->icon_spec.min_width || 647 (width < prpl_info->icon_spec.min_width ||
574 width > prpl_info->icon_spec.max_width || 648 width > prpl_info->icon_spec.max_width ||
600 g_object_unref(G_OBJECT(pixbuf)); 674 g_object_unref(G_OBJECT(pixbuf));
601 pixbuf = scale; 675 pixbuf = scale;
602 } 676 }
603 if (error) { 677 if (error) {
604 g_free(filename); 678 g_free(filename);
605 g_free(random);
606 gaim_debug_error("buddyicon", "Could not open icon for conversion: %s\n", error->message); 679 gaim_debug_error("buddyicon", "Could not open icon for conversion: %s\n", error->message);
607 g_error_free(error); 680 g_error_free(error);
608 g_strfreev(prpl_formats); 681 g_strfreev(prpl_formats);
609 return NULL; 682 return NULL;
610 }
611
612 if (!g_file_test(dirname, G_FILE_TEST_IS_DIR)) {
613 gaim_debug_info("buddyicon", "Creating icon cache directory.\n");
614
615 if (g_mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR) < 0) {
616 gaim_debug_error("buddyicon",
617 "Unable to create directory %s: %s\n",
618 dirname, strerror(errno));
619 g_strfreev(prpl_formats);
620 return NULL;
621 }
622 } 683 }
623 684
624 for (i = 0; prpl_formats[i]; i++) { 685 for (i = 0; prpl_formats[i]; i++) {
625 gaim_debug_info("buddyicon", "Converting buddy icon to %s as %s\n", prpl_formats[i], filename); 686 gaim_debug_info("buddyicon", "Converting buddy icon to %s as %s\n", prpl_formats[i], filename);
626 /* The gdk-pixbuf documentation is wrong. gdk_pixbuf_save returns TRUE if it was successful, 687 /* The gdk-pixbuf documentation is wrong. gdk_pixbuf_save returns TRUE if it was successful,
631 g_error_free(error); 692 g_error_free(error);
632 error = NULL; 693 error = NULL;
633 } 694 }
634 g_strfreev(prpl_formats); 695 g_strfreev(prpl_formats);
635 if (!error) { 696 if (!error) {
636 g_free(random);
637 g_object_unref(G_OBJECT(pixbuf)); 697 g_object_unref(G_OBJECT(pixbuf));
638 return filename; 698 return filename;
639 } else { 699 } else {
640 gaim_debug_error("buddyicon", "Could not convert icon to usable format: %s\n", error->message); 700 gaim_debug_error("buddyicon", "Could not convert icon to usable format: %s\n", error->message);
641 g_error_free(error); 701 g_error_free(error);
642 } 702 }
643 g_free(filename); 703 g_free(filename);
644 g_free(random);
645 g_object_unref(G_OBJECT(pixbuf)); 704 g_object_unref(G_OBJECT(pixbuf));
646 } 705 }
647 return NULL; 706 return NULL;
648 #endif 707 #endif
649 } 708 }
1375 gaim_account_set_alias(dialog->account, value); 1434 gaim_account_set_alias(dialog->account, value);
1376 else 1435 else
1377 gaim_account_set_alias(dialog->account, NULL); 1436 gaim_account_set_alias(dialog->account, NULL);
1378 1437
1379 /* Buddy Icon */ 1438 /* Buddy Icon */
1380 value = dialog->icon_path; 1439 value = gaim_account_get_buddy_icon(dialog->account);
1381 gaim_account_set_buddy_icon(dialog->account, value); 1440 if (value == NULL || dialog->icon_path == NULL || strcmp(value, dialog->icon_path)) {
1441 delete_buddy_icon(value);
1442 gaim_account_set_buddy_icon(dialog->account, dialog->icon_path);
1443 }
1382 1444
1383 /* Remember Password */ 1445 /* Remember Password */
1384 gaim_account_set_remember_password(dialog->account, 1446 gaim_account_set_remember_password(dialog->account,
1385 gtk_toggle_button_get_active( 1447 gtk_toggle_button_get_active(
1386 GTK_TOGGLE_BUTTON(dialog->remember_pass_check))); 1448 GTK_TOGGLE_BUTTON(dialog->remember_pass_check)));