Thunar 1.0.0 中是怎么进行图片预览的 (二)
上一章介绍的是图片预览的主要实现方式,这次来介绍下Thunar实现预览的流程。
?
./thunar/thunar-icon-factory.c获取文件的图标 thunar_icon_factory_load_file_icon()。
对于已经存在缩略图的查找缩略图thunar_vfs_thumb_factory_lookup_thumbnail(),
判断其修改时间和缩略图修改时间是否一致thunar_vfs_thumbnail_is_valid(),不一致则重新生成 。
如果找不到缩略图,或者需要重新生成缩略图的文件,调用thunar_thumbnail_generator_enqueue()。
最后,生成缩略图后当作文件的图标。
??
./thunar/thunar-thumbnail-generator.c中开辟一个新线程来进行缩略图的生成:
?/** * thunar_vfs_thumbnail_is_valid: * @thumbnail : the absolute path to a thumbnail file. * @uri : the URI of the original file. * @mtime : the modification time of the original file. * * Checks whether the file located at @thumbnail contains a * valid thumbnail for the file described by @uri and @mtime. * * The usage of this method is thread-safe. * * Return value: %TRUE if @thumbnail is a valid thumbnail for * the file referred to by @uri, else %FALSE. **/gbooleanthunar_vfs_thumbnail_is_valid (const gchar *thumbnail, const gchar *uri, ThunarVfsFileTime mtime){ png_structp png_ptr; png_infop info_ptr; png_textp text_ptr; gboolean is_valid = FALSE; gchar signature[4]; FILE *fp; gint n_checked; gint n_text; gint n; g_return_val_if_fail (g_path_is_absolute (thumbnail), FALSE); g_return_val_if_fail (uri != NULL && *uri != '\0', FALSE); /* try to open the thumbnail file */ fp = fopen (thumbnail, "r"); if (G_UNLIKELY (fp == NULL)) return FALSE; /* read the png signature */ if (G_UNLIKELY (fread (signature, 1, sizeof (signature), fp) != sizeof (signature))) goto done0; /* verify the png signature */ if (G_LIKELY (png_check_sig ((png_bytep) signature, sizeof (signature)))) rewind (fp); else goto done0; /* allocate the png read structure */ png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (G_UNLIKELY (png_ptr == NULL)) goto done0; /* allocate the png info structure */ info_ptr = png_create_info_struct (png_ptr); if (G_UNLIKELY (info_ptr == NULL)) { png_destroy_read_struct (&png_ptr, NULL, NULL); goto done0; } /* read the png info from the file */ png_init_io (png_ptr, fp); png_read_info (png_ptr, info_ptr); /* verify the tEXt attributes defined by the thumbnail spec */ n_text = png_get_text (png_ptr, info_ptr, &text_ptr, &n_text); for (n = 0, n_checked = 0; n_checked < 2 && n < n_text; ++n) { if (strcmp (text_ptr[n].key, "Thumb::MTime") == 0) { /* verify the modification time */ if (G_UNLIKELY (strtol (text_ptr[n].text, NULL, 10) != mtime)) goto done1; ++n_checked; } else if (strcmp (text_ptr[n].key, "Thumb::URI") == 0) { /* check if the URIs are equal */ if (strcmp (text_ptr[n].text, uri) != 0) goto done1; ++n_checked; } } /* check if all required attributes were checked successfully */ is_valid = (n_checked == 2);done1: png_destroy_read_struct (&png_ptr, &info_ptr, NULL);done0: fclose (fp); return is_valid;}??
?
?