Exemplo n.º 1
0
/**
 * e_icon_factory_create_thumbnail
 * @filename: the file name to create the thumbnail for
 *
 * Creates system thumbnail for @filename.
 *
 * Returns: Path to system thumbnail of the file; %NULL if couldn't
 *          create it. Free it with g_free().
 **/
gchar *
e_icon_factory_create_thumbnail (const gchar *filename)
{
#ifdef HAVE_GNOME_DESKTOP
	static GnomeDesktopThumbnailFactory *thumbnail_factory = NULL;
	struct stat file_stat;
	gchar *thumbnail = NULL;

	g_return_val_if_fail (filename != NULL, NULL);

	if (thumbnail_factory == NULL) {
		thumbnail_factory = gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_NORMAL);
	}

	if (g_stat (filename, &file_stat) != -1 && S_ISREG (file_stat.st_mode)) {
		gchar *content_type, *mime = NULL;
		gboolean uncertain = FALSE;

		content_type = g_content_type_guess (filename, NULL, 0, &uncertain);
		if (content_type)
			mime = g_content_type_get_mime_type (content_type);

		if (mime) {
			gchar *uri = g_filename_to_uri (filename, NULL, NULL);

			g_return_val_if_fail (uri != NULL, NULL);

			thumbnail = gnome_desktop_thumbnail_factory_lookup (thumbnail_factory, uri, file_stat.st_mtime);
			if (!thumbnail && gnome_desktop_thumbnail_factory_can_thumbnail (thumbnail_factory, uri, mime, file_stat.st_mtime)) {
				GdkPixbuf *pixbuf;

				pixbuf = gnome_desktop_thumbnail_factory_generate_thumbnail (thumbnail_factory, uri, mime);

				if (pixbuf) {
					gnome_desktop_thumbnail_factory_save_thumbnail (thumbnail_factory, pixbuf, uri, file_stat.st_mtime);
					g_object_unref (pixbuf);

					thumbnail = gnome_desktop_thumbnail_factory_lookup (thumbnail_factory, uri, file_stat.st_mtime);
				}
			}

			g_free (uri);
		}

		g_free (content_type);
		g_free (mime);
	}

	return thumbnail;
#else
	return NULL;
#endif /* HAVE_GNOME_DESKTOP */
}
Exemplo n.º 2
0
static GdkPixbuf *
impl_load_thumbnail (StTextureCache    *cache,
                     const char        *uri,
                     const char        *mime_type,
                     guint              size,
                     GError           **error)
{
  GnomeDesktopThumbnailFactory *thumbnail_factory;
  GdkPixbuf *pixbuf = NULL;
  GFile *file;
  GFileInfo *file_info;
  GTimeVal mtime_g;
  time_t mtime = 0;
  char *existing_thumbnail;

  file = g_file_new_for_uri (uri);
  file_info = g_file_query_info (file, G_FILE_ATTRIBUTE_TIME_MODIFIED, G_FILE_QUERY_INFO_NONE, NULL, NULL);
  g_object_unref (file);
  if (file_info)
    {
      g_file_info_get_modification_time (file_info, &mtime_g);
      g_object_unref (file_info);
      mtime = (time_t) mtime_g.tv_sec;
    }

  thumbnail_factory = cache->priv->thumbnails;

  existing_thumbnail = gnome_desktop_thumbnail_factory_lookup (thumbnail_factory, uri, mtime);

  if (existing_thumbnail != NULL)
    {
      pixbuf = gdk_pixbuf_new_from_file_at_size (existing_thumbnail, size, size, error);
      g_free (existing_thumbnail);
    }
  else if (gnome_desktop_thumbnail_factory_has_valid_failed_thumbnail (thumbnail_factory, uri, mtime))
    g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Has failed thumbnail");
  else if (gnome_desktop_thumbnail_factory_can_thumbnail (thumbnail_factory, uri, mime_type, mtime))
    {
      pixbuf = gnome_desktop_thumbnail_factory_generate_thumbnail (thumbnail_factory, uri, mime_type);
      if (pixbuf)
        {
          // we need to save the thumbnail so that we don't need to generate it again in the future
          gnome_desktop_thumbnail_factory_save_thumbnail (thumbnail_factory, pixbuf, uri, mtime);
        }
      else
        {
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Failed to generate thumbnail");
          gnome_desktop_thumbnail_factory_create_failed_thumbnail (thumbnail_factory, uri, mtime);
        }
     }
   return pixbuf;
}
Exemplo n.º 3
0
gboolean
nautilus_can_thumbnail (NautilusFile *file)
{
	GnomeDesktopThumbnailFactory *factory;
	gboolean res;
	char *uri;
	time_t mtime;
	char *mime_type;
		
	uri = nautilus_file_get_uri (file);
	mime_type = nautilus_file_get_mime_type (file);
	mtime = nautilus_file_get_mtime (file);
	
	factory = get_thumbnail_factory ();
	res = gnome_desktop_thumbnail_factory_can_thumbnail (factory,
							     uri,
							     mime_type,
							     mtime);
	g_free (mime_type);
	g_free (uri);

	return res;
}
Exemplo n.º 4
0
/**
 * eog_thumbnail_load:
 * @image: a #EogImage
 * @error: location to store the error ocurring or %NULL to ignore
 *
 * Loads the thumbnail for @image. In case of error, %NULL is returned
 * and @error is set.
 *
 * Returns: (transfer full): a new #GdkPixbuf with the thumbnail for
 * @image or %NULL in case of error.
 **/
GdkPixbuf*
eog_thumbnail_load (EogImage *image, GError **error)
{
	GdkPixbuf *thumb = NULL;
	GFile *file;
	EogThumbData *data;
	GdkPixbuf *pixbuf = NULL;

	g_return_val_if_fail (image != NULL, NULL);
	g_return_val_if_fail (error != NULL && *error == NULL, NULL);

	file = eog_image_get_file (image);
	data = eog_thumb_data_new (file, error);
	g_object_unref (file);

	if (data == NULL)
		return NULL;

	if (!data->can_read ||
	    (data->failed_thumb_exists && gnome_desktop_thumbnail_factory_has_valid_failed_thumbnail (factory, data->uri_str, data->mtime))) {
		eog_debug_message (DEBUG_THUMBNAIL, "%s: bad permissions or valid failed thumbnail present",data->uri_str);
		set_thumb_error (error, EOG_THUMB_ERROR_GENERIC, "Thumbnail creation failed");
		return NULL;
	}

	/* check if there is already a valid cached thumbnail */
	thumb = get_valid_thumbnail (data, error);

	if (thumb != NULL) {
		eog_debug_message (DEBUG_THUMBNAIL, "%s: loaded from cache",data->uri_str);
	} else if (gnome_desktop_thumbnail_factory_can_thumbnail (factory, data->uri_str, data->mime_type, data->mtime)) {
		/* Only use the image pixbuf when it is up to date. */
		if (!eog_image_is_file_changed (image))
			pixbuf = eog_image_get_pixbuf (image);

		if (pixbuf != NULL) {
			/* generate a thumbnail from the in-memory image,
			   if we have already loaded the image */
			eog_debug_message (DEBUG_THUMBNAIL, "%s: creating from pixbuf",data->uri_str);
			thumb = create_thumbnail_from_pixbuf (data, pixbuf, error);
			g_object_unref (pixbuf);
		} else {
			/* generate a thumbnail from the file */
			eog_debug_message (DEBUG_THUMBNAIL, "%s: creating from file",data->uri_str);
			thumb = gnome_desktop_thumbnail_factory_generate_thumbnail (factory, data->uri_str, data->mime_type);
		}

		if (thumb != NULL) {
			/* Save the new thumbnail */
			gnome_desktop_thumbnail_factory_save_thumbnail (factory, thumb, data->uri_str, data->mtime);
			eog_debug_message (DEBUG_THUMBNAIL, "%s: normal thumbnail saved",data->uri_str);
		} else {
			/* Save a failed thumbnail, to stop further thumbnail attempts */
			gnome_desktop_thumbnail_factory_create_failed_thumbnail (factory, data->uri_str, data->mtime);
			eog_debug_message (DEBUG_THUMBNAIL, "%s: failed thumbnail saved",data->uri_str);
			set_thumb_error (error, EOG_THUMB_ERROR_GENERIC, "Thumbnail creation failed");
		}
	}

	eog_thumb_data_free (data);

	return thumb;
}