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.
22 #include "cache-loader.h"
24 #include <sys/types.h>
30 #include <gdk-pixbuf/gdk-pixbuf.h>
31 #include <glib-object.h>
35 #include "image-load.h"
40 #include "ui-fileops.h"
43 static gboolean cache_loader_phase2_idle_cb(gpointer data);
45 static void cache_loader_phase1_done_cb(ImageLoader *, gpointer data)
47 auto cl = static_cast<CacheLoader *>(data);
49 cl->idle_id = g_idle_add(cache_loader_phase2_idle_cb, cl);
52 static void cache_loader_phase1_error_cb(ImageLoader *, gpointer data)
54 auto cl = static_cast<CacheLoader *>(data);
57 cl->idle_id = g_idle_add(cache_loader_phase2_idle_cb, cl);
60 static gboolean cache_loader_phase1_process(CacheLoader *cl)
62 if (cl->todo_mask & CACHE_LOADER_SIMILARITY && !cl->cd->similarity)
65 if (!cl->il && !cl->error)
67 cl->il = image_loader_new(cl->fd);
68 g_signal_connect(G_OBJECT(cl->il), "error", (GCallback)cache_loader_phase1_error_cb, cl);
69 g_signal_connect(G_OBJECT(cl->il), "done", (GCallback)cache_loader_phase1_done_cb, cl);
70 if (image_loader_start(cl->il))
72 return G_SOURCE_REMOVE;
79 cl->idle_id = g_idle_add(cache_loader_phase2_idle_cb, cl);
81 return G_SOURCE_REMOVE;
84 static gboolean cache_loader_phase2_process(CacheLoader *cl)
86 if (cl->todo_mask & CACHE_LOADER_SIMILARITY && !cl->cd->similarity && cl->il)
89 pixbuf = image_loader_get_pixbuf(cl->il);
94 ImageSimilarityData *sim;
96 sim = image_sim_new_from_pixbuf(pixbuf);
97 cache_sim_data_set_similarity(cl->cd, sim);
100 cl->todo_mask = static_cast<CacheDataType>(cl->todo_mask & ~CACHE_LOADER_SIMILARITY);
101 cl->done_mask = static_cast<CacheDataType>(cl->done_mask | CACHE_LOADER_SIMILARITY);
104 /* we have the dimensions via pixbuf */
105 if (!cl->cd->dimensions)
107 cache_sim_data_set_dimensions(cl->cd, gdk_pixbuf_get_width(pixbuf),
108 gdk_pixbuf_get_height(pixbuf));
109 if (cl->todo_mask & CACHE_LOADER_DIMENSIONS)
111 cl->todo_mask = static_cast<CacheDataType>(cl->todo_mask & ~CACHE_LOADER_DIMENSIONS);
112 cl->done_mask = static_cast<CacheDataType>(cl->done_mask | CACHE_LOADER_DIMENSIONS);
117 image_loader_free(cl->il);
120 cl->todo_mask = static_cast<CacheDataType>(cl->todo_mask & ~CACHE_LOADER_SIMILARITY);
122 else if (cl->todo_mask & CACHE_LOADER_DIMENSIONS &&
126 image_load_dimensions(cl->fd, &cl->cd->width, &cl->cd->height))
128 cl->cd->dimensions = TRUE;
129 cl->done_mask = static_cast<CacheDataType>(cl->done_mask | CACHE_LOADER_DIMENSIONS);
136 cl->todo_mask = static_cast<CacheDataType>(cl->todo_mask & ~CACHE_LOADER_DIMENSIONS);
138 else if (cl->todo_mask & CACHE_LOADER_MD5SUM &&
139 !cl->cd->have_md5sum)
141 if (md5_get_digest_from_file_utf8(cl->fd->path, cl->cd->md5sum))
143 cl->cd->have_md5sum = TRUE;
144 cl->done_mask = static_cast<CacheDataType>(cl->done_mask | CACHE_LOADER_MD5SUM);
151 cl->todo_mask = static_cast<CacheDataType>(cl->todo_mask & ~CACHE_LOADER_MD5SUM);
153 else if (cl->todo_mask & CACHE_LOADER_DATE &&
159 text = metadata_read_string(cl->fd, "Exif.Image.DateTime", METADATA_FORMATTED);
164 memset(&t, 0, sizeof(t));
166 if (sscanf(text, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon, &t.tm_mday,
167 &t.tm_hour, &t.tm_min, &t.tm_sec) == 6)
178 cl->cd->have_date = TRUE;
180 cl->done_mask = static_cast<CacheDataType>(cl->done_mask | CACHE_LOADER_DATE);
181 cl->todo_mask = static_cast<CacheDataType>(cl->todo_mask & ~CACHE_LOADER_DATE);
185 /* done, save then call done function */
186 if (options->thumbnails.enable_caching &&
187 cl->done_mask != CACHE_LOADER_NONE)
192 base = cache_get_location(CACHE_TYPE_SIM, cl->fd->path, FALSE, &mode);
193 if (recursive_mkdir_if_not_exists(base, mode))
195 g_free(cl->cd->path);
196 cl->cd->path = cache_get_location(CACHE_TYPE_SIM, cl->fd->path, TRUE, nullptr);
197 if (cache_sim_data_save(cl->cd))
199 filetime_set(cl->cd->path, filetime(cl->fd->path));
209 cl->done_func(cl, cl->error, cl->done_data);
212 return G_SOURCE_REMOVE;
215 return G_SOURCE_CONTINUE;
218 static gboolean cache_loader_phase1_idle_cb(gpointer data)
220 auto cl = static_cast<CacheLoader *>(data);
222 return cache_loader_phase1_process(cl);
225 static gboolean cache_loader_phase2_idle_cb(gpointer data)
227 auto cl = static_cast<CacheLoader *>(data);
229 return cache_loader_phase2_process(cl);
232 CacheLoader *cache_loader_new(FileData *fd, CacheDataType load_mask,
233 CacheLoader::DoneFunc done_func, gpointer done_data)
238 if (!fd || !isfile(fd->path)) return nullptr;
240 cl = g_new0(CacheLoader, 1);
241 cl->fd = file_data_ref(fd);
243 cl->done_func = done_func;
244 cl->done_data = done_data;
246 found = cache_find_location(CACHE_TYPE_SIM, cl->fd->path);
247 if (found && filetime(found) == filetime(cl->fd->path))
249 cl->cd = cache_sim_data_load(found);
253 if (!cl->cd) cl->cd = cache_sim_data_new();
255 cl->todo_mask = load_mask;
256 cl->done_mask = CACHE_LOADER_NONE;
259 cl->idle_id = g_idle_add(cache_loader_phase1_idle_cb, cl);
266 void cache_loader_free(CacheLoader *cl)
272 g_source_remove_by_user_data(cl);
276 image_loader_free(cl->il);
277 cache_sim_data_free(cl->cd);
279 file_data_unref(cl->fd);
282 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */