comparison libpurple/smiley.c @ 23185:013ec6fabd3f

Always store custom smileys with hashed filenames. Closes #5913. committer: Sadrul Habib Chowdhury <imadil@gmail.com>
author Marcus Lundblad <ml@update.uu.se>
date Sun, 25 May 2008 16:16:22 +0000
parents f1df88964b3d
children 90c7142de9d0
comparison
equal deleted inserted replaced
23184:89713caa9ade 23185:013ec6fabd3f
117 static PurpleSmiley *purple_smiley_load_file(const char *shortcut, const char *checksum, 117 static PurpleSmiley *purple_smiley_load_file(const char *shortcut, const char *checksum,
118 const char *filename); 118 const char *filename);
119 119
120 static void 120 static void
121 purple_smiley_set_data_impl(PurpleSmiley *smiley, guchar *smiley_data, 121 purple_smiley_set_data_impl(PurpleSmiley *smiley, guchar *smiley_data,
122 size_t smiley_data_len, const char *filename); 122 size_t smiley_data_len);
123 123
124 static void 124 static void
125 purple_smiley_data_store(PurpleStoredImage *stored_img); 125 purple_smiley_data_store(PurpleStoredImage *stored_img);
126 126
127 static void 127 static void
497 497
498 smiley->checksum = g_strdup(checksum); 498 smiley->checksum = g_strdup(checksum);
499 499
500 if (read_smiley_file(fullpath, &smiley_data, &smiley_data_len)) 500 if (read_smiley_file(fullpath, &smiley_data, &smiley_data_len))
501 purple_smiley_set_data_impl(smiley, smiley_data, 501 purple_smiley_set_data_impl(smiley, smiley_data,
502 smiley_data_len, filename); 502 smiley_data_len);
503 else 503 else
504 purple_smiley_delete(smiley); 504 purple_smiley_delete(smiley);
505 505
506 g_free(fullpath); 506 g_free(fullpath);
507 507
615 return stored_img; 615 return stored_img;
616 } 616 }
617 617
618 static void 618 static void
619 purple_smiley_set_data_impl(PurpleSmiley *smiley, guchar *smiley_data, 619 purple_smiley_set_data_impl(PurpleSmiley *smiley, guchar *smiley_data,
620 size_t smiley_data_len, const char *filename) 620 size_t smiley_data_len)
621 { 621 {
622 PurpleStoredImage *old_img, *new_img; 622 PurpleStoredImage *old_img, *new_img;
623 const char *old_filename = NULL; 623 const char *old_filename = NULL;
624 const char *new_filename = NULL; 624 const char *new_filename = NULL;
625 625
627 g_return_if_fail(smiley_data != NULL); 627 g_return_if_fail(smiley_data != NULL);
628 g_return_if_fail(smiley_data_len > 0); 628 g_return_if_fail(smiley_data_len > 0);
629 629
630 old_img = smiley->img; 630 old_img = smiley->img;
631 631
632 if (filename) 632 new_img = purple_smiley_data_new(smiley_data, smiley_data_len);
633 new_img = purple_imgstore_add(smiley_data, smiley_data_len, filename);
634 else
635 new_img = purple_smiley_data_new(smiley_data, smiley_data_len);
636 633
637 g_object_set(G_OBJECT(smiley), PROP_IMGSTORE_S, new_img, NULL); 634 g_object_set(G_OBJECT(smiley), PROP_IMGSTORE_S, new_img, NULL);
638 635
639 /* If the old and new image files have different names we need 636 /* If the old and new image files have different names we need
640 * to unstore old image file. */ 637 * to unstore old image file. */
686 return smiley; 683 return smiley;
687 } 684 }
688 685
689 static PurpleSmiley * 686 static PurpleSmiley *
690 purple_smiley_new_from_stream(const char *shortcut, guchar *smiley_data, 687 purple_smiley_new_from_stream(const char *shortcut, guchar *smiley_data,
691 size_t smiley_data_len, const char *filename) 688 size_t smiley_data_len)
692 { 689 {
693 PurpleSmiley *smiley; 690 PurpleSmiley *smiley;
694 691
695 g_return_val_if_fail(shortcut != NULL, NULL); 692 g_return_val_if_fail(shortcut != NULL, NULL);
696 g_return_val_if_fail(smiley_data != NULL, NULL); 693 g_return_val_if_fail(smiley_data != NULL, NULL);
703 /* purple_smiley_create() sets shortcut */ 700 /* purple_smiley_create() sets shortcut */
704 smiley = purple_smiley_create(shortcut); 701 smiley = purple_smiley_create(shortcut);
705 if (!smiley) 702 if (!smiley)
706 return NULL; 703 return NULL;
707 704
708 purple_smiley_set_data_impl(smiley, smiley_data, smiley_data_len, filename); 705 purple_smiley_set_data_impl(smiley, smiley_data, smiley_data_len);
709 706
710 purple_smiley_data_store(smiley->img); 707 purple_smiley_data_store(smiley->img);
711 708
712 return smiley; 709 return smiley;
713 } 710 }
716 purple_smiley_new_from_file(const char *shortcut, const char *filepath) 713 purple_smiley_new_from_file(const char *shortcut, const char *filepath)
717 { 714 {
718 PurpleSmiley *smiley = NULL; 715 PurpleSmiley *smiley = NULL;
719 guchar *smiley_data; 716 guchar *smiley_data;
720 size_t smiley_data_len; 717 size_t smiley_data_len;
721 char *filename;
722 718
723 g_return_val_if_fail(shortcut != NULL, NULL); 719 g_return_val_if_fail(shortcut != NULL, NULL);
724 g_return_val_if_fail(filepath != NULL, NULL); 720 g_return_val_if_fail(filepath != NULL, NULL);
725 721
726 filename = g_path_get_basename(filepath); 722 if (read_smiley_file(filepath, &smiley_data, &smiley_data_len)) {
727 if (read_smiley_file(filepath, &smiley_data, &smiley_data_len))
728 smiley = purple_smiley_new_from_stream(shortcut, smiley_data, 723 smiley = purple_smiley_new_from_stream(shortcut, smiley_data,
729 smiley_data_len, filename); 724 smiley_data_len);
730 g_free(filename); 725 }
731 726
732 return smiley; 727 return smiley;
733 } 728 }
734 729
735 void 730 void
736 purple_smiley_delete(PurpleSmiley *smiley) 731 purple_smiley_delete(PurpleSmiley *smiley)
767 return TRUE; 762 return TRUE;
768 } 763 }
769 764
770 void 765 void
771 purple_smiley_set_data(PurpleSmiley *smiley, guchar *smiley_data, 766 purple_smiley_set_data(PurpleSmiley *smiley, guchar *smiley_data,
772 size_t smiley_data_len, gboolean keepfilename) 767 size_t smiley_data_len)
773 { 768 {
774 g_return_if_fail(smiley != NULL); 769 g_return_if_fail(smiley != NULL);
775 g_return_if_fail(smiley_data != NULL); 770 g_return_if_fail(smiley_data != NULL);
776 g_return_if_fail(smiley_data_len > 0); 771 g_return_if_fail(smiley_data_len > 0);
777 772
778 /* Remove the previous entry */ 773 /* Remove the previous entry */
779 g_hash_table_remove(smiley_checksum_index, smiley->checksum); 774 g_hash_table_remove(smiley_checksum_index, smiley->checksum);
780 775
781 /* Update the file data. This also updates the checksum. */ 776 /* Update the file data. This also updates the checksum. */
782 if ((keepfilename) && (smiley->img) && 777 purple_smiley_set_data_impl(smiley, smiley_data, smiley_data_len);
783 (purple_imgstore_get_filename(smiley->img)))
784 purple_smiley_set_data_impl(smiley, smiley_data,
785 smiley_data_len,
786 purple_imgstore_get_filename(smiley->img));
787 else
788 purple_smiley_set_data_impl(smiley, smiley_data,
789 smiley_data_len, NULL);
790 778
791 /* Reinsert the index item. */ 779 /* Reinsert the index item. */
792 g_hash_table_insert(smiley_checksum_index, g_strdup(smiley->checksum), smiley); 780 g_hash_table_insert(smiley_checksum_index, g_strdup(smiley->checksum), smiley);
793 781
794 purple_smileys_save(); 782 purple_smileys_save();