Mon Nov 27 01:23:23 2006 John Ellis <johne@verizon.net>
[geeqie.git] / src / cache-loader.c
1 /*
2  * GQview
3  * (C) 2005 John Ellis
4  *
5  * Author: John Ellis
6  *
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!
10  */
11
12 #include "gqview.h"
13 #include "cache-loader.h"
14
15 #include "exif.h"
16 #include "md5-util.h"
17 #include "ui_fileops.h"
18
19
20 static gboolean cache_loader_process(CacheLoader *cl);
21
22
23 static void cache_loader_done_cb(ImageLoader *il, gpointer data)
24 {
25         CacheLoader *cl = data;
26
27         cache_loader_process(cl);
28 }
29
30 static void cache_loader_error_cb(ImageLoader *il, gpointer data)
31 {
32         CacheLoader *cl = data;
33
34         cl->error = TRUE;
35         cache_loader_done_cb(il, data);
36 }
37
38 static gboolean cache_loader_process(CacheLoader *cl)
39 {
40         if (cl->todo_mask & CACHE_LOADER_SIMILARITY &&
41             !cl->cd->similarity)
42                 {
43                 GdkPixbuf *pixbuf;
44
45                 if (!cl->il && !cl->error)
46                         {
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))
50                                 {
51                                 return FALSE;
52                                 }
53
54                         cl->error = TRUE;
55                         }
56
57                 pixbuf = image_loader_get_pixbuf(cl->il);
58                 if (pixbuf)
59                         {
60                         if (!cl->error)
61                                 {
62                                 ImageSimilarityData *sim;
63
64                                 sim = image_sim_new_from_pixbuf(pixbuf);
65                                 cache_sim_data_set_similarity(cl->cd, sim);
66                                 image_sim_free(sim);
67
68                                 cl->done_mask |= CACHE_LOADER_SIMILARITY;
69                                 }
70
71                         /* we have the dimensions via pixbuf */
72                         if (!cl->cd->dimensions)
73                                 {
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)
77                                         {
78                                         cl->todo_mask &= ~CACHE_LOADER_DIMENSIONS;
79                                         cl->done_mask |= CACHE_LOADER_DIMENSIONS;
80                                         }
81                                 }
82                         }
83
84                 image_loader_free(cl->il);
85                 cl->il = NULL;
86
87                 cl->todo_mask &= ~CACHE_LOADER_SIMILARITY;
88                 }
89         else if (cl->todo_mask & CACHE_LOADER_DIMENSIONS &&
90                  !cl->cd->dimensions)
91                 {
92                 if (!cl->error &&
93                     image_load_dimensions(cl->path, &cl->cd->width, &cl->cd->height))
94                         {
95                         cl->cd->dimensions = TRUE;
96                         cl->done_mask |= CACHE_LOADER_DIMENSIONS;
97                         }
98                 else
99                         {
100                         cl->error = TRUE;
101                         }
102
103                 cl->todo_mask &= ~CACHE_LOADER_DIMENSIONS;
104                 }
105         else if (cl->todo_mask & CACHE_LOADER_MD5SUM &&
106                  !cl->cd->have_md5sum)
107                 {
108                 if (md5_get_digest_from_file_utf8(cl->path, cl->cd->md5sum))
109                         {
110                         cl->cd->have_md5sum = TRUE;
111                         cl->done_mask |= CACHE_LOADER_MD5SUM;
112                         }
113                 else
114                         {
115                         cl->error = TRUE;
116                         }
117
118                 cl->todo_mask &= ~CACHE_LOADER_MD5SUM;
119                 }
120         else if (cl->todo_mask & CACHE_LOADER_DATE &&
121                  !cl->cd->have_date)
122                 {
123                 time_t date = -1;
124                 ExifData *exif;
125
126                 exif = exif_read(cl->path, FALSE);
127                 if (exif)
128                         {
129                         gchar *text;
130
131                         text = exif_get_data_as_text(exif, "fDateTime");
132                         if (text)
133                                 {
134                                 struct tm t = { 0 };
135
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)
138                                         {
139                                         t.tm_year -= 1900;
140                                         t.tm_mon -= 1;
141                                         t.tm_isdst = -1;
142                                         date = mktime(&t);
143                                         }
144                                 g_free(text);
145                                 }
146                         exif_free(exif);
147                         }
148
149                 cl->cd->date = date;
150                 cl->cd->have_date = TRUE;
151
152                 cl->done_mask |= CACHE_LOADER_DATE;
153                 cl->todo_mask &= ~CACHE_LOADER_DATE;
154                 }
155         else
156                 {
157                 /* done, save then call done function */
158                 if (enable_thumb_caching &&
159                     cl->done_mask != CACHE_LOADER_NONE)
160                         {
161                         gchar *base;
162                         mode_t mode = 0755;
163
164                         base = cache_get_location(CACHE_TYPE_SIM, cl->path, FALSE, &mode);
165                         if (cache_ensure_dir_exists(base, mode))
166                                 {
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))
170                                         {
171                                         filetime_set(cl->cd->path, filetime(cl->path));
172                                         }
173                                 }
174                         g_free(base);
175                         }
176
177                 cl->idle_id = -1;
178
179                 if (cl->done_func)
180                         {
181                         cl->done_func(cl, cl->error, cl->done_data);
182                         }
183
184                 return FALSE;
185                 }
186
187         return TRUE;
188 }
189
190 static gboolean cache_loader_idle_cb(gpointer data)
191 {
192         CacheLoader *cl = data;
193
194         return cache_loader_process(cl);
195 }
196
197 CacheLoader *cache_loader_new(const gchar *path, CacheDataType load_mask,
198                               CacheLoaderDoneFunc done_func, gpointer done_data)
199 {
200         CacheLoader *cl;
201         gchar *found;
202
203         if (!path || !isfile(path)) return NULL;
204
205         cl = g_new0(CacheLoader, 1);
206         cl->path = g_strdup(path);
207
208         cl->done_func = done_func;
209         cl->done_data = done_data;
210
211         found = cache_find_location(CACHE_TYPE_SIM, path);
212         if (found && filetime(found) == filetime(path))
213                 {
214                 cl->cd = cache_sim_data_load(found);
215                 }
216         g_free(found);
217
218         if (!cl->cd) cl->cd = cache_sim_data_new();
219
220         cl->todo_mask = load_mask;
221         cl->done_mask = CACHE_LOADER_NONE;
222
223         cl->il = NULL;
224         cl->idle_id = g_idle_add(cache_loader_idle_cb, cl);
225
226         cl->error = FALSE;
227
228         return cl;
229 }
230
231 void cache_loader_free(CacheLoader *cl)
232 {
233         if (!cl) return;
234
235         if (cl->idle_id != -1)
236                 {
237                 g_source_remove(cl->idle_id);
238                 cl->idle_id = -1;
239                 }
240
241         image_loader_free(cl->il);
242         cache_sim_data_free(cl->cd);
243
244         g_free(cl->path);
245         g_free(cl);
246 }
247