Skip to content

Commit

Permalink
Merge thumb_loader_save_to_cache() and thumb_loader_mark_failure()
Browse files Browse the repository at this point in the history
into thumb_loader_save_thumbnail().
Most of the code was redundant.
  • Loading branch information
Laurent Monin committed Jun 29, 2008
1 parent 29242ec commit e2bd6f7
Showing 1 changed file with 24 additions and 53 deletions.
77 changes: 24 additions & 53 deletions src/thumb.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h
*-----------------------------------------------------------------------------
*/

static gint thumb_loader_save_to_cache(ThumbLoader *tl)
/* Save thumbnail to disk
* or just mark failed thumbnail with 0 byte file (mark_failure = TRUE) */
static gboolean thumb_loader_save_thumbnail(ThumbLoader *tl, gboolean mark_failure)
{
gchar *cache_dir;
gint success = FALSE;
gboolean success = FALSE;
mode_t mode = 0755;

if (!tl || !tl->fd || !tl->fd->thumb_pixbuf) return FALSE;
if (!tl || !tl->fd) return FALSE;
if (!mark_failure && !tl->fd->thumb_pixbuf) return FALSE;

cache_dir = cache_get_location(CACHE_TYPE_THUMB, tl->fd->path, FALSE, &mode);

Expand All @@ -56,79 +59,47 @@ static gint thumb_loader_save_to_cache(ThumbLoader *tl)
cache_path = g_build_filename(cache_dir, name, NULL);
g_free(name);

DEBUG_1("Saving thumb: %s", cache_path);

pathl = path_from_utf8(cache_path);
success = pixbuf_to_file_as_png(tl->fd->thumb_pixbuf, pathl);
if (success)

if (mark_failure)
{
struct utimbuf ut;
/* set thumb time to that of source file */
FILE *f = fopen(pathl, "w"); ;

ut.actime = ut.modtime = filetime(tl->fd->path);
if (ut.modtime > 0)
DEBUG_1("Marking thumb failure: %s", cache_path);
if (f)
{
utime(pathl, &ut);
fclose(f);
success = TRUE;
}
}
else
{
DEBUG_1("Saving failed: %s", pathl);
DEBUG_1("Saving thumb: %s", cache_path);
success = pixbuf_to_file_as_png(tl->fd->thumb_pixbuf, pathl);
}

g_free(pathl);
g_free(cache_path);
}

g_free(cache_dir);

return success;
}

static gint thumb_loader_mark_failure(ThumbLoader *tl)
{
gchar *cache_dir;
gint success = FALSE;
mode_t mode = 0755;

if (!tl) return FALSE;

cache_dir = cache_get_location(CACHE_TYPE_THUMB, tl->fd->path, FALSE, &mode);

if (cache_ensure_dir_exists(cache_dir, mode))
{
gchar *cache_path;
gchar *pathl;
FILE *f;
gchar *name = g_strconcat(filename_from_path(tl->fd->path), GQ_CACHE_EXT_THUMB, NULL);

cache_path = g_build_filename(cache_dir, name, NULL);
g_free(name);

DEBUG_1("marking thumb failure: %s", cache_path);

pathl = path_from_utf8(cache_path);
f = fopen(pathl, "w");
if (f)
if (success)
{
struct utimbuf ut;

fclose(f);
/* set thumb time to that of source file */

ut.actime = ut.modtime = filetime(tl->fd->path);
if (ut.modtime > 0)
{
utime(pathl, &ut);
}

success = TRUE;
}
else
{
DEBUG_1("Saving failed: %s", pathl);
}

g_free(pathl);
g_free(cache_path);
}

g_free(cache_dir);

return success;
}

Expand Down Expand Up @@ -245,7 +216,7 @@ static void thumb_loader_done_cb(ImageLoader *il, gpointer data)
/* save it ? */
if (tl->cache_enable && save)
{
thumb_loader_save_to_cache(tl);
thumb_loader_save_thumbnail(tl, FALSE);
}

if (tl->func_done) tl->func_done(tl, tl->data);
Expand Down Expand Up @@ -427,7 +398,7 @@ gint thumb_loader_start(ThumbLoader *tl, FileData *fd)
/* mark failed thumbnail in cache with 0 byte file */
if (tl->cache_enable)
{
thumb_loader_mark_failure(tl);
thumb_loader_save_thumbnail(tl, TRUE);
}

image_loader_free(tl->il);
Expand Down

0 comments on commit e2bd6f7

Please sign in to comment.