diff options
author | Jasper St. Pierre <jstpierre@mecheye.net> | 2012-09-05 18:49:21 -0300 |
---|---|---|
committer | Bastien Nocera <hadess@hadess.net> | 2014-11-18 19:36:33 +0100 |
commit | 97f6f7762d3ec80719087bca31151cd99b0e2ef9 (patch) | |
tree | ed6632d096a23a52dcee752cf19bc832b68afb12 /libgnome-desktop | |
parent | 57c18b88f2f4e0c80579ca6088af982b5a40fb06 (diff) | |
download | gnome-desktop-97f6f7762d3ec80719087bca31151cd99b0e2ef9.tar.gz |
thumbnail: Move thumbnail generation/saving to a few helper functions
Another cleanup in preparation for a new "simple" thumbnail API.
https://bugzilla.gnome.org/show_bug.cgi?id=684026
Diffstat (limited to 'libgnome-desktop')
-rw-r--r-- | libgnome-desktop/gnome-desktop-thumbnail.c | 314 |
1 files changed, 116 insertions, 198 deletions
diff --git a/libgnome-desktop/gnome-desktop-thumbnail.c b/libgnome-desktop/gnome-desktop-thumbnail.c index 39123b0b..0d251d5e 100644 --- a/libgnome-desktop/gnome-desktop-thumbnail.c +++ b/libgnome-desktop/gnome-desktop-thumbnail.c @@ -1071,6 +1071,44 @@ thumbnail_failed_path (const char *uri) return path; } +static char * +validate_thumbnail_path (char *path, + const char *uri, + time_t mtime, + GnomeDesktopThumbnailSize size) +{ + GdkPixbuf *pixbuf; + + pixbuf = gdk_pixbuf_new_from_file (path, NULL); + if (pixbuf == NULL || + !gnome_desktop_thumbnail_is_valid (pixbuf, uri, mtime)) { + g_free (path); + return NULL; + } + + g_clear_object (&pixbuf); + + return path; +} + +static char * +lookup_thumbnail_path (const char *uri, + time_t mtime, + GnomeDesktopThumbnailSize size) +{ + char *path = thumbnail_path (uri, size); + return validate_thumbnail_path (path, uri, mtime, size); +} + +static char * +lookup_failed_thumbnail_path (const char *uri, + time_t mtime, + GnomeDesktopThumbnailSize size) +{ + char *path = thumbnail_failed_path (uri); + return validate_thumbnail_path (path, uri, mtime, size); +} + /** * gnome_desktop_thumbnail_factory_lookup: * @factory: a #GnomeDesktopThumbnailFactory @@ -1091,28 +1129,10 @@ gnome_desktop_thumbnail_factory_lookup (GnomeDesktopThumbnailFactory *factory, time_t mtime) { GnomeDesktopThumbnailFactoryPrivate *priv = factory->priv; - GdkPixbuf *pixbuf; - gboolean res; - char *path; g_return_val_if_fail (uri != NULL, NULL); - res = FALSE; - - path = thumbnail_path (uri, priv->size); - - pixbuf = gdk_pixbuf_new_from_file (path, NULL); - if (pixbuf != NULL) - { - res = gnome_desktop_thumbnail_is_valid (pixbuf, uri, mtime); - g_object_unref (pixbuf); - } - - if (res) - return path; - - g_free (path); - return NULL; + return lookup_thumbnail_path (uri, mtime, priv->size); } /** @@ -1137,23 +1157,16 @@ gnome_desktop_thumbnail_factory_has_valid_failed_thumbnail (GnomeDesktopThumbnai time_t mtime) { char *path; - GdkPixbuf *pixbuf; - gboolean res; - res = FALSE; + g_return_val_if_fail (uri != NULL, FALSE); - path = thumbnail_failed_path (uri); + path = lookup_failed_thumbnail_path (uri, mtime, factory->priv->size); + if (path == NULL) + return FALSE; - pixbuf = gdk_pixbuf_new_from_file (path, NULL); g_free (path); - if (pixbuf) - { - res = gnome_desktop_thumbnail_is_valid (pixbuf, uri, mtime); - g_object_unref (pixbuf); - } - - return res; + return TRUE; } static gboolean @@ -1469,89 +1482,88 @@ gnome_desktop_thumbnail_factory_generate_thumbnail (GnomeDesktopThumbnailFactory } static gboolean -make_thumbnail_dirs (GnomeDesktopThumbnailFactory *factory) +save_thumbnail (GdkPixbuf *pixbuf, + char *path, + const char *uri, + time_t mtime) { - char *thumbnail_dir; - char *image_dir; - gboolean res; + char *dirname; + char *tmp_path = NULL; + int tmp_fd; + char mtime_str[21]; + gboolean ret = FALSE; + GError *error = NULL; + const char *width, *height; - res = FALSE; + if (pixbuf == NULL) + return FALSE; - thumbnail_dir = g_build_filename (g_get_user_cache_dir (), - "thumbnails", - NULL); - if (!g_file_test (thumbnail_dir, G_FILE_TEST_IS_DIR)) - { - g_mkdir (thumbnail_dir, 0700); - res = TRUE; - } + dirname = g_path_get_dirname (path); - image_dir = g_build_filename (thumbnail_dir, - (factory->priv->size == GNOME_DESKTOP_THUMBNAIL_SIZE_NORMAL)?"normal":"large", - NULL); - if (!g_file_test (image_dir, G_FILE_TEST_IS_DIR)) - { - g_mkdir (image_dir, 0700); - res = TRUE; - } + if (!g_mkdir_with_parents (dirname, 0700)) + goto out; - g_free (thumbnail_dir); - g_free (image_dir); - - return res; -} + tmp_path = g_strconcat (path, ".XXXXXX", NULL); + tmp_fd = g_mkstemp (tmp_path); -static gboolean -make_thumbnail_fail_dirs (GnomeDesktopThumbnailFactory *factory) -{ - char *thumbnail_dir; - char *fail_dir; - char *app_dir; - gboolean res; + if (tmp_fd == -1) + goto out; + close (tmp_fd); - res = FALSE; + g_snprintf (mtime_str, 21, "%ld", mtime); + width = gdk_pixbuf_get_option (pixbuf, "tEXt::Thumb::Image::Width"); + height = gdk_pixbuf_get_option (pixbuf, "tEXt::Thumb::Image::Height"); - thumbnail_dir = g_build_filename (g_get_user_cache_dir (), - "thumbnails", - NULL); - if (!g_file_test (thumbnail_dir, G_FILE_TEST_IS_DIR)) - { - g_mkdir (thumbnail_dir, 0700); - res = TRUE; - } + error = NULL; + if (width != NULL && height != NULL) + ret = gdk_pixbuf_save (pixbuf, + tmp_path, + "png", &error, + "tEXt::Thumb::Image::Width", width, + "tEXt::Thumb::Image::Height", height, + "tEXt::Thumb::URI", uri, + "tEXt::Thumb::MTime", mtime_str, + "tEXt::Software", "GNOME::ThumbnailFactory", + NULL); + else + ret = gdk_pixbuf_save (pixbuf, + tmp_path, + "png", &error, + "tEXt::Thumb::URI", uri, + "tEXt::Thumb::MTime", mtime_str, + "tEXt::Software", "GNOME::ThumbnailFactory", + NULL); - fail_dir = g_build_filename (thumbnail_dir, - "fail", - NULL); - if (!g_file_test (fail_dir, G_FILE_TEST_IS_DIR)) - { - g_mkdir (fail_dir, 0700); - res = TRUE; - } + if (!ret) + goto out; + + g_chmod (tmp_path, 0600); + g_rename (tmp_path, path); - app_dir = g_build_filename (fail_dir, - appname, - NULL); - if (!g_file_test (app_dir, G_FILE_TEST_IS_DIR)) + out: + if (error != NULL) { - g_mkdir (app_dir, 0700); - res = TRUE; + g_warning ("Failed to create thumbnail %s: %s", tmp_path, error->message); + g_error_free (error); } - - g_free (thumbnail_dir); - g_free (fail_dir); - g_free (app_dir); - - return res; + g_unlink (tmp_path); + g_free (tmp_path); + g_free (dirname); + return ret; } +static GdkPixbuf * +make_failed_thumbnail (void) +{ + return gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, 1, 1); +} /** * gnome_desktop_thumbnail_factory_save_thumbnail: * @factory: a #GnomeDesktopThumbnailFactory - * @thumbnail: the thumbnail as a pixbuf + * @thumbnail: the thumbnail as a pixbuf * @uri: the uri of a file - * @original_mtime: the modification time of the original file + * @original_mtime: the modification time of the original file * * Saves @thumbnail at the right place. If the save fails a * failed thumbnail is written. @@ -1566,77 +1578,18 @@ gnome_desktop_thumbnail_factory_save_thumbnail (GnomeDesktopThumbnailFactory *fa const char *uri, time_t original_mtime) { - GnomeDesktopThumbnailFactoryPrivate *priv = factory->priv; char *path; - char *tmp_path; - const char *width, *height; - int tmp_fd; - char mtime_str[21]; - gboolean saved_ok; - GError *error; - - path = thumbnail_path (uri, priv->size); - - tmp_path = g_strconcat (path, ".XXXXXX", NULL); - - tmp_fd = g_mkstemp (tmp_path); - if (tmp_fd == -1 && - make_thumbnail_dirs (factory)) - { - g_free (tmp_path); - tmp_path = g_strconcat (path, ".XXXXXX", NULL); - tmp_fd = g_mkstemp (tmp_path); - } - if (tmp_fd == -1) + path = thumbnail_path (uri, factory->priv->size); + if (!save_thumbnail (thumbnail, path, uri, original_mtime)) { - gnome_desktop_thumbnail_factory_create_failed_thumbnail (factory, uri, original_mtime); - g_free (tmp_path); + thumbnail = make_failed_thumbnail (); g_free (path); - return; + path = thumbnail_failed_path (uri); + save_thumbnail (thumbnail, path, uri, original_mtime); + g_object_unref (thumbnail); } - close (tmp_fd); - - g_snprintf (mtime_str, 21, "%" G_GUINT64_FORMAT, (guint64) original_mtime); - width = gdk_pixbuf_get_option (thumbnail, "tEXt::Thumb::Image::Width"); - height = gdk_pixbuf_get_option (thumbnail, "tEXt::Thumb::Image::Height"); - - error = NULL; - if (width != NULL && height != NULL) - saved_ok = gdk_pixbuf_save (thumbnail, - tmp_path, - "png", &error, - "tEXt::Thumb::Image::Width", width, - "tEXt::Thumb::Image::Height", height, - "tEXt::Thumb::URI", uri, - "tEXt::Thumb::MTime", mtime_str, - "tEXt::Software", "GNOME::ThumbnailFactory", - NULL); - else - saved_ok = gdk_pixbuf_save (thumbnail, - tmp_path, - "png", &error, - "tEXt::Thumb::URI", uri, - "tEXt::Thumb::MTime", mtime_str, - "tEXt::Software", "GNOME::ThumbnailFactory", - NULL); - - - if (saved_ok) - { - g_chmod (tmp_path, 0600); - g_rename (tmp_path, path); - } - else - { - g_warning ("Failed to create thumbnail %s: %s", tmp_path, error->message); - gnome_desktop_thumbnail_factory_create_failed_thumbnail (factory, uri, original_mtime); - g_unlink (tmp_path); - g_clear_error (&error); - } - g_free (path); - g_free (tmp_path); } /** @@ -1658,48 +1611,13 @@ gnome_desktop_thumbnail_factory_create_failed_thumbnail (GnomeDesktopThumbnailFa time_t mtime) { char *path; - char *tmp_path; - int tmp_fd; - char mtime_str[21]; GdkPixbuf *pixbuf; path = thumbnail_failed_path (uri); - - tmp_path = g_strconcat (path, ".XXXXXX", NULL); - - tmp_fd = g_mkstemp (tmp_path); - if (tmp_fd == -1 && - make_thumbnail_fail_dirs (factory)) - { - g_free (tmp_path); - tmp_path = g_strconcat (path, ".XXXXXX", NULL); - tmp_fd = g_mkstemp (tmp_path); - } - - if (tmp_fd == -1) - { - g_free (tmp_path); - g_free (path); - return; - } - close (tmp_fd); - - g_snprintf (mtime_str, 21, "%" G_GUINT64_FORMAT, (guint64) mtime); - pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, 1, 1); - gdk_pixbuf_save (pixbuf, - tmp_path, - "png", NULL, - "tEXt::Thumb::URI", uri, - "tEXt::Thumb::MTime", mtime_str, - "tEXt::Software", "GNOME::ThumbnailFactory", - NULL); - g_object_unref (pixbuf); - - g_chmod (tmp_path, 0600); - g_rename (tmp_path, path); - + pixbuf = make_failed_thumbnail (); + save_thumbnail (pixbuf, path, uri, mtime); g_free (path); - g_free (tmp_path); + g_object_unref (pixbuf); } /** |