7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
13 #include "cache-loader.h"
17 #include "ui_fileops.h"
20 static gboolean cache_loader_process(CacheLoader *cl);
23 static void cache_loader_done_cb(ImageLoader *il, gpointer data)
25 CacheLoader *cl = data;
27 cache_loader_process(cl);
30 static void cache_loader_error_cb(ImageLoader *il, gpointer data)
32 CacheLoader *cl = data;
35 cache_loader_done_cb(il, data);
38 static gboolean cache_loader_process(CacheLoader *cl)
40 if (cl->todo_mask & CACHE_LOADER_SIMILARITY &&
45 if (!cl->il && !cl->error)
47 cl->il = image_loader_new(cl->path);
48 image_loader_set_error_func(cl->il, cache_loader_error_cb, cl);
49 if (image_loader_start(cl->il, cache_loader_done_cb, cl))
57 pixbuf = image_loader_get_pixbuf(cl->il);
62 ImageSimilarityData *sim;
64 sim = image_sim_new_from_pixbuf(pixbuf);
65 cache_sim_data_set_similarity(cl->cd, sim);
68 cl->done_mask |= CACHE_LOADER_SIMILARITY;
71 /* we have the dimensions via pixbuf */
72 if (!cl->cd->dimensions)
74 cache_sim_data_set_dimensions(cl->cd, gdk_pixbuf_get_width(pixbuf),
75 gdk_pixbuf_get_height(pixbuf));
76 if (cl->todo_mask & CACHE_LOADER_DIMENSIONS)
78 cl->todo_mask &= ~CACHE_LOADER_DIMENSIONS;
79 cl->done_mask |= CACHE_LOADER_DIMENSIONS;
84 image_loader_free(cl->il);
87 cl->todo_mask &= ~CACHE_LOADER_SIMILARITY;
89 else if (cl->todo_mask & CACHE_LOADER_DIMENSIONS &&
93 image_load_dimensions(cl->path, &cl->cd->width, &cl->cd->height))
95 cl->cd->dimensions = TRUE;
96 cl->done_mask |= CACHE_LOADER_DIMENSIONS;
103 cl->todo_mask &= ~CACHE_LOADER_DIMENSIONS;
105 else if (cl->todo_mask & CACHE_LOADER_MD5SUM &&
106 !cl->cd->have_md5sum)
108 if (md5_get_digest_from_file_utf8(cl->path, cl->cd->md5sum))
110 cl->cd->have_md5sum = TRUE;
111 cl->done_mask |= CACHE_LOADER_MD5SUM;
118 cl->todo_mask &= ~CACHE_LOADER_MD5SUM;
120 else if (cl->todo_mask & CACHE_LOADER_DATE &&
126 exif = exif_read(cl->path);
131 text = exif_get_data_as_text(exif, "fDateTime");
136 if (sscanf(text, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon, &t.tm_mday,
137 &t.tm_hour, &t.tm_min, &t.tm_sec) == 6)
150 cl->cd->have_date = TRUE;
152 cl->done_mask |= CACHE_LOADER_DATE;
153 cl->todo_mask &= ~CACHE_LOADER_DATE;
157 /* done, save then call done function */
158 if (enable_thumb_caching &&
159 cl->done_mask != CACHE_LOADER_NONE)
164 base = cache_get_location(CACHE_TYPE_SIM, cl->path, FALSE, &mode);
165 if (cache_ensure_dir_exists(base, mode))
167 g_free(cl->cd->path);
168 cl->cd->path = cache_get_location(CACHE_TYPE_SIM, cl->path, TRUE, NULL);
169 if (cache_sim_data_save(cl->cd))
171 filetime_set(cl->cd->path, filetime(cl->path));
181 cl->done_func(cl, cl->error, cl->done_data);
190 static gboolean cache_loader_idle_cb(gpointer data)
192 CacheLoader *cl = data;
194 return cache_loader_process(cl);
197 CacheLoader *cache_loader_new(const gchar *path, CacheDataType load_mask,
198 CacheLoaderDoneFunc done_func, gpointer done_data)
203 if (!path || !isfile(path)) return NULL;
205 cl = g_new0(CacheLoader, 1);
206 cl->path = g_strdup(path);
208 cl->done_func = done_func;
209 cl->done_data = done_data;
211 found = cache_find_location(CACHE_TYPE_SIM, path);
212 if (found && filetime(found) == filetime(path))
214 cl->cd = cache_sim_data_load(found);
218 if (!cl->cd) cl->cd = cache_sim_data_new();
220 cl->todo_mask = load_mask;
221 cl->done_mask = CACHE_LOADER_NONE;
224 cl->idle_id = g_idle_add(cache_loader_idle_cb, cl);
231 void cache_loader_free(CacheLoader *cl)
235 if (cl->idle_id != -1)
237 g_source_remove(cl->idle_id);
241 image_loader_free(cl->il);
242 cache_sim_data_free(cl->cd);