2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "cache-loader.h"
30 #include "ui_fileops.h"
33 static gboolean cache_loader_process(CacheLoader *cl);
36 static void cache_loader_done_cb(ImageLoader *il, gpointer data)
38 CacheLoader *cl = data;
40 cache_loader_process(cl);
43 static void cache_loader_error_cb(ImageLoader *il, gpointer data)
45 CacheLoader *cl = data;
48 cache_loader_done_cb(il, data);
51 static gboolean cache_loader_process(CacheLoader *cl)
53 if (cl->todo_mask & CACHE_LOADER_SIMILARITY &&
58 if (!cl->il && !cl->error)
60 cl->il = image_loader_new(cl->fd);
61 g_signal_connect(G_OBJECT(cl->il), "error", (GCallback)cache_loader_error_cb, cl);
62 g_signal_connect(G_OBJECT(cl->il), "done", (GCallback)cache_loader_done_cb, cl);
63 if (image_loader_start(cl->il))
71 pixbuf = image_loader_get_pixbuf(cl->il);
76 ImageSimilarityData *sim;
78 sim = image_sim_new_from_pixbuf(pixbuf);
79 cache_sim_data_set_similarity(cl->cd, sim);
82 cl->done_mask |= CACHE_LOADER_SIMILARITY;
85 /* we have the dimensions via pixbuf */
86 if (!cl->cd->dimensions)
88 cache_sim_data_set_dimensions(cl->cd, gdk_pixbuf_get_width(pixbuf),
89 gdk_pixbuf_get_height(pixbuf));
90 if (cl->todo_mask & CACHE_LOADER_DIMENSIONS)
92 cl->todo_mask &= ~CACHE_LOADER_DIMENSIONS;
93 cl->done_mask |= CACHE_LOADER_DIMENSIONS;
98 image_loader_free(cl->il);
101 cl->todo_mask &= ~CACHE_LOADER_SIMILARITY;
103 else if (cl->todo_mask & CACHE_LOADER_DIMENSIONS &&
107 image_load_dimensions(cl->fd, &cl->cd->width, &cl->cd->height))
109 cl->cd->dimensions = TRUE;
110 cl->done_mask |= CACHE_LOADER_DIMENSIONS;
117 cl->todo_mask &= ~CACHE_LOADER_DIMENSIONS;
119 else if (cl->todo_mask & CACHE_LOADER_MD5SUM &&
120 !cl->cd->have_md5sum)
122 if (md5_get_digest_from_file_utf8(cl->fd->path, cl->cd->md5sum))
124 cl->cd->have_md5sum = TRUE;
125 cl->done_mask |= CACHE_LOADER_MD5SUM;
132 cl->todo_mask &= ~CACHE_LOADER_MD5SUM;
134 else if (cl->todo_mask & CACHE_LOADER_DATE &&
140 text = metadata_read_string(cl->fd, "formatted.DateTime", METADATA_FORMATTED);
145 memset(&t, 0, sizeof(t));
147 if (sscanf(text, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon, &t.tm_mday,
148 &t.tm_hour, &t.tm_min, &t.tm_sec) == 6)
159 cl->cd->have_date = TRUE;
161 cl->done_mask |= CACHE_LOADER_DATE;
162 cl->todo_mask &= ~CACHE_LOADER_DATE;
166 /* done, save then call done function */
167 if (options->thumbnails.enable_caching &&
168 cl->done_mask != CACHE_LOADER_NONE)
173 base = cache_get_location(CACHE_TYPE_SIM, cl->fd->path, FALSE, &mode);
174 if (recursive_mkdir_if_not_exists(base, mode))
176 g_free(cl->cd->path);
177 cl->cd->path = cache_get_location(CACHE_TYPE_SIM, cl->fd->path, TRUE, NULL);
178 if (cache_sim_data_save(cl->cd))
180 filetime_set(cl->cd->path, filetime(cl->fd->path));
190 cl->done_func(cl, cl->error, cl->done_data);
199 static gboolean cache_loader_idle_cb(gpointer data)
201 CacheLoader *cl = data;
203 return cache_loader_process(cl);
206 CacheLoader *cache_loader_new(FileData *fd, CacheDataType load_mask,
207 CacheLoaderDoneFunc done_func, gpointer done_data)
212 if (!fd || !isfile(fd->path)) return NULL;
214 cl = g_new0(CacheLoader, 1);
215 cl->fd = file_data_ref(fd);
217 cl->done_func = done_func;
218 cl->done_data = done_data;
220 found = cache_find_location(CACHE_TYPE_SIM, cl->fd->path);
221 if (found && filetime(found) == filetime(cl->fd->path))
223 cl->cd = cache_sim_data_load(found);
227 if (!cl->cd) cl->cd = cache_sim_data_new();
229 cl->todo_mask = load_mask;
230 cl->done_mask = CACHE_LOADER_NONE;
233 cl->idle_id = g_idle_add(cache_loader_idle_cb, cl);
240 void cache_loader_free(CacheLoader *cl)
246 g_source_remove(cl->idle_id);
250 image_loader_free(cl->il);
251 cache_sim_data_free(cl->cd);
253 file_data_unref(cl->fd);
256 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */