split filelist.c to filefilter.c and filedata.c
[geeqie.git] / src / cache-loader.c
1 /*
2  * Geeqie
3  * (C) 2005 John Ellis
4  * Copyright (C) 2008 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13 #include "main.h"
14 #include "cache-loader.h"
15 #include "cache.h"
16
17 #include "filedata.h"
18 #include "exif.h"
19 #include "md5-util.h"
20 #include "ui_fileops.h"
21
22
23 static gboolean cache_loader_process(CacheLoader *cl);
24
25
26 static void cache_loader_done_cb(ImageLoader *il, gpointer data)
27 {
28         CacheLoader *cl = data;
29
30         cache_loader_process(cl);
31 }
32
33 static void cache_loader_error_cb(ImageLoader *il, gpointer data)
34 {
35         CacheLoader *cl = data;
36
37         cl->error = TRUE;
38         cache_loader_done_cb(il, data);
39 }
40
41 static gboolean cache_loader_process(CacheLoader *cl)
42 {
43         if (cl->todo_mask & CACHE_LOADER_SIMILARITY &&
44             !cl->cd->similarity)
45                 {
46                 GdkPixbuf *pixbuf;
47
48                 if (!cl->il && !cl->error)
49                         {
50                         cl->il = image_loader_new(cl->fd);
51                         image_loader_set_error_func(cl->il, cache_loader_error_cb, cl);
52                         if (image_loader_start(cl->il, cache_loader_done_cb, cl))
53                                 {
54                                 return FALSE;
55                                 }
56
57                         cl->error = TRUE;
58                         }
59
60                 pixbuf = image_loader_get_pixbuf(cl->il);
61                 if (pixbuf)
62                         {
63                         if (!cl->error)
64                                 {
65                                 ImageSimilarityData *sim;
66
67                                 sim = image_sim_new_from_pixbuf(pixbuf);
68                                 cache_sim_data_set_similarity(cl->cd, sim);
69                                 image_sim_free(sim);
70
71                                 cl->done_mask |= CACHE_LOADER_SIMILARITY;
72                                 }
73
74                         /* we have the dimensions via pixbuf */
75                         if (!cl->cd->dimensions)
76                                 {
77                                 cache_sim_data_set_dimensions(cl->cd, gdk_pixbuf_get_width(pixbuf),
78                                                                       gdk_pixbuf_get_height(pixbuf));
79                                 if (cl->todo_mask & CACHE_LOADER_DIMENSIONS)
80                                         {
81                                         cl->todo_mask &= ~CACHE_LOADER_DIMENSIONS;
82                                         cl->done_mask |= CACHE_LOADER_DIMENSIONS;
83                                         }
84                                 }
85                         }
86
87                 image_loader_free(cl->il);
88                 cl->il = NULL;
89
90                 cl->todo_mask &= ~CACHE_LOADER_SIMILARITY;
91                 }
92         else if (cl->todo_mask & CACHE_LOADER_DIMENSIONS &&
93                  !cl->cd->dimensions)
94                 {
95                 if (!cl->error &&
96                     image_load_dimensions(cl->fd, &cl->cd->width, &cl->cd->height))
97                         {
98                         cl->cd->dimensions = TRUE;
99                         cl->done_mask |= CACHE_LOADER_DIMENSIONS;
100                         }
101                 else
102                         {
103                         cl->error = TRUE;
104                         }
105
106                 cl->todo_mask &= ~CACHE_LOADER_DIMENSIONS;
107                 }
108         else if (cl->todo_mask & CACHE_LOADER_MD5SUM &&
109                  !cl->cd->have_md5sum)
110                 {
111                 if (md5_get_digest_from_file_utf8(cl->fd->path, cl->cd->md5sum))
112                         {
113                         cl->cd->have_md5sum = TRUE;
114                         cl->done_mask |= CACHE_LOADER_MD5SUM;
115                         }
116                 else
117                         {
118                         cl->error = TRUE;
119                         }
120
121                 cl->todo_mask &= ~CACHE_LOADER_MD5SUM;
122                 }
123         else if (cl->todo_mask & CACHE_LOADER_DATE &&
124                  !cl->cd->have_date)
125                 {
126                 time_t date = -1;
127                 ExifData *exif;
128
129                 exif = exif_read_fd(cl->fd);
130                 if (exif)
131                         {
132                         gchar *text;
133
134                         text = exif_get_data_as_text(exif, "formatted.DateTime");
135                         if (text)
136                                 {
137                                 struct tm t = { 0 };
138
139                                 if (sscanf(text, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon, &t.tm_mday,
140                                            &t.tm_hour, &t.tm_min, &t.tm_sec) == 6)
141                                         {
142                                         t.tm_year -= 1900;
143                                         t.tm_mon -= 1;
144                                         t.tm_isdst = -1;
145                                         date = mktime(&t);
146                                         }
147                                 g_free(text);
148                                 }
149                         exif_free(exif);
150                         }
151
152                 cl->cd->date = date;
153                 cl->cd->have_date = TRUE;
154
155                 cl->done_mask |= CACHE_LOADER_DATE;
156                 cl->todo_mask &= ~CACHE_LOADER_DATE;
157                 }
158         else
159                 {
160                 /* done, save then call done function */
161                 if (options->thumbnails.enable_caching &&
162                     cl->done_mask != CACHE_LOADER_NONE)
163                         {
164                         gchar *base;
165                         mode_t mode = 0755;
166
167                         base = cache_get_location(CACHE_TYPE_SIM, cl->fd->path, FALSE, &mode);
168                         if (cache_ensure_dir_exists(base, mode))
169                                 {
170                                 g_free(cl->cd->path);
171                                 cl->cd->path = cache_get_location(CACHE_TYPE_SIM, cl->fd->path, TRUE, NULL);
172                                 if (cache_sim_data_save(cl->cd))
173                                         {
174                                         filetime_set(cl->cd->path, filetime(cl->fd->path));
175                                         }
176                                 }
177                         g_free(base);
178                         }
179
180                 cl->idle_id = -1;
181
182                 if (cl->done_func)
183                         {
184                         cl->done_func(cl, cl->error, cl->done_data);
185                         }
186
187                 return FALSE;
188                 }
189
190         return TRUE;
191 }
192
193 static gboolean cache_loader_idle_cb(gpointer data)
194 {
195         CacheLoader *cl = data;
196
197         return cache_loader_process(cl);
198 }
199
200 CacheLoader *cache_loader_new(FileData *fd, CacheDataType load_mask,
201                               CacheLoaderDoneFunc done_func, gpointer done_data)
202 {
203         CacheLoader *cl;
204         gchar *found;
205
206         if (!fd || !isfile(fd->path)) return NULL;
207
208         cl = g_new0(CacheLoader, 1);
209         cl->fd = file_data_ref(fd);
210
211         cl->done_func = done_func;
212         cl->done_data = done_data;
213
214         found = cache_find_location(CACHE_TYPE_SIM, cl->fd->path);
215         if (found && filetime(found) == filetime(cl->fd->path))
216                 {
217                 cl->cd = cache_sim_data_load(found);
218                 }
219         g_free(found);
220
221         if (!cl->cd) cl->cd = cache_sim_data_new();
222
223         cl->todo_mask = load_mask;
224         cl->done_mask = CACHE_LOADER_NONE;
225
226         cl->il = NULL;
227         cl->idle_id = g_idle_add(cache_loader_idle_cb, cl);
228
229         cl->error = FALSE;
230
231         return cl;
232 }
233
234 void cache_loader_free(CacheLoader *cl)
235 {
236         if (!cl) return;
237
238         if (cl->idle_id != -1)
239                 {
240                 g_source_remove(cl->idle_id);
241                 cl->idle_id = -1;
242                 }
243
244         image_loader_free(cl->il);
245         cache_sim_data_free(cl->cd);
246
247         file_data_unref(cl->fd);
248         g_free(cl);
249 }