Cleanup main.h header
[geeqie.git] / src / cache-loader.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "cache-loader.h"
23
24 #include <config.h>
25
26 #include "filedata.h"
27 #include "metadata.h"
28 #include "options.h"
29 #include "ui-fileops.h"
30
31
32 static gboolean cache_loader_phase2_idle_cb(gpointer data);
33
34 static void cache_loader_phase1_done_cb(ImageLoader *, gpointer data)
35 {
36         auto cl = static_cast<CacheLoader *>(data);
37
38         cl->idle_id = g_idle_add(cache_loader_phase2_idle_cb, cl);
39 }
40
41 static void cache_loader_phase1_error_cb(ImageLoader *, gpointer data)
42 {
43         auto cl = static_cast<CacheLoader *>(data);
44
45         cl->error = TRUE;
46         cl->idle_id = g_idle_add(cache_loader_phase2_idle_cb, cl);
47 }
48
49 static gboolean cache_loader_phase1_process(CacheLoader *cl)
50 {
51         if (cl->todo_mask & CACHE_LOADER_SIMILARITY && !cl->cd->similarity)
52                 {
53
54                 if (!cl->il && !cl->error)
55                         {
56                         cl->il = image_loader_new(cl->fd);
57                         g_signal_connect(G_OBJECT(cl->il), "error", (GCallback)cache_loader_phase1_error_cb, cl);
58                         g_signal_connect(G_OBJECT(cl->il), "done", (GCallback)cache_loader_phase1_done_cb, cl);
59                         if (image_loader_start(cl->il))
60                                 {
61                                 return G_SOURCE_REMOVE;
62                                 }
63
64                         cl->error = TRUE;
65                         }
66                 }
67
68         cl->idle_id = g_idle_add(cache_loader_phase2_idle_cb, cl);
69
70         return G_SOURCE_REMOVE;
71 }
72
73 static gboolean cache_loader_phase2_process(CacheLoader *cl)
74 {
75         if (cl->todo_mask & CACHE_LOADER_SIMILARITY && !cl->cd->similarity && cl->il)
76                 {
77                 GdkPixbuf *pixbuf;
78                 pixbuf = image_loader_get_pixbuf(cl->il);
79                 if (pixbuf)
80                         {
81                         if (!cl->error)
82                                 {
83                                 ImageSimilarityData *sim;
84
85                                 sim = image_sim_new_from_pixbuf(pixbuf);
86                                 cache_sim_data_set_similarity(cl->cd, sim);
87                                 image_sim_free(sim);
88
89                                 cl->todo_mask = static_cast<CacheDataType>(cl->todo_mask & ~CACHE_LOADER_SIMILARITY);
90                                 cl->done_mask = static_cast<CacheDataType>(cl->done_mask | CACHE_LOADER_SIMILARITY);
91                                 }
92
93                         /* we have the dimensions via pixbuf */
94                         if (!cl->cd->dimensions)
95                                 {
96                                 cache_sim_data_set_dimensions(cl->cd, gdk_pixbuf_get_width(pixbuf),
97                                                                       gdk_pixbuf_get_height(pixbuf));
98                                 if (cl->todo_mask & CACHE_LOADER_DIMENSIONS)
99                                         {
100                                         cl->todo_mask = static_cast<CacheDataType>(cl->todo_mask & ~CACHE_LOADER_DIMENSIONS);
101                                         cl->done_mask = static_cast<CacheDataType>(cl->done_mask | CACHE_LOADER_DIMENSIONS);
102                                         }
103                                 }
104                         }
105
106                 image_loader_free(cl->il);
107                 cl->il = nullptr;
108
109                 cl->todo_mask = static_cast<CacheDataType>(cl->todo_mask & ~CACHE_LOADER_SIMILARITY);
110                 }
111         else if (cl->todo_mask & CACHE_LOADER_DIMENSIONS &&
112                  !cl->cd->dimensions)
113                 {
114                 if (!cl->error &&
115                     image_load_dimensions(cl->fd, &cl->cd->width, &cl->cd->height))
116                         {
117                         cl->cd->dimensions = TRUE;
118                         cl->done_mask = static_cast<CacheDataType>(cl->done_mask | CACHE_LOADER_DIMENSIONS);
119                         }
120                 else
121                         {
122                         cl->error = TRUE;
123                         }
124
125                 cl->todo_mask = static_cast<CacheDataType>(cl->todo_mask & ~CACHE_LOADER_DIMENSIONS);
126                 }
127         else if (cl->todo_mask & CACHE_LOADER_MD5SUM &&
128                  !cl->cd->have_md5sum)
129                 {
130                 if (md5_get_digest_from_file_utf8(cl->fd->path, cl->cd->md5sum))
131                         {
132                         cl->cd->have_md5sum = TRUE;
133                         cl->done_mask = static_cast<CacheDataType>(cl->done_mask | CACHE_LOADER_MD5SUM);
134                         }
135                 else
136                         {
137                         cl->error = TRUE;
138                         }
139
140                 cl->todo_mask = static_cast<CacheDataType>(cl->todo_mask & ~CACHE_LOADER_MD5SUM);
141                 }
142         else if (cl->todo_mask & CACHE_LOADER_DATE &&
143                  !cl->cd->have_date)
144                 {
145                 time_t date = -1;
146                 gchar *text;
147
148                 text =  metadata_read_string(cl->fd, "Exif.Image.DateTime", METADATA_FORMATTED);
149                 if (text)
150                         {
151                         struct tm t;
152
153                         memset(&t, 0, sizeof(t));
154
155                         if (sscanf(text, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon, &t.tm_mday,
156                                    &t.tm_hour, &t.tm_min, &t.tm_sec) == 6)
157                                 {
158                                 t.tm_year -= 1900;
159                                 t.tm_mon -= 1;
160                                 t.tm_isdst = -1;
161                                 date = mktime(&t);
162                                 }
163                         g_free(text);
164                         }
165
166                 cl->cd->date = date;
167                 cl->cd->have_date = TRUE;
168
169                 cl->done_mask = static_cast<CacheDataType>(cl->done_mask | CACHE_LOADER_DATE);
170                 cl->todo_mask = static_cast<CacheDataType>(cl->todo_mask & ~CACHE_LOADER_DATE);
171                 }
172         else
173                 {
174                 /* done, save then call done function */
175                 if (options->thumbnails.enable_caching &&
176                     cl->done_mask != CACHE_LOADER_NONE)
177                         {
178                         gchar *base;
179                         mode_t mode = 0755;
180
181                         base = cache_get_location(CACHE_TYPE_SIM, cl->fd->path, FALSE, &mode);
182                         if (recursive_mkdir_if_not_exists(base, mode))
183                                 {
184                                 g_free(cl->cd->path);
185                                 cl->cd->path = cache_get_location(CACHE_TYPE_SIM, cl->fd->path, TRUE, nullptr);
186                                 if (cache_sim_data_save(cl->cd))
187                                         {
188                                         filetime_set(cl->cd->path, filetime(cl->fd->path));
189                                         }
190                                 }
191                         g_free(base);
192                         }
193
194                 cl->idle_id = 0;
195
196                 if (cl->done_func)
197                         {
198                         cl->done_func(cl, cl->error, cl->done_data);
199                         }
200
201                 return G_SOURCE_REMOVE;
202                 }
203
204         return G_SOURCE_CONTINUE;
205 }
206
207 static gboolean cache_loader_phase1_idle_cb(gpointer data)
208 {
209         auto cl = static_cast<CacheLoader *>(data);
210
211         return cache_loader_phase1_process(cl);
212 }
213
214 static gboolean cache_loader_phase2_idle_cb(gpointer data)
215 {
216         auto cl = static_cast<CacheLoader *>(data);
217
218         return cache_loader_phase2_process(cl);
219 }
220
221 CacheLoader *cache_loader_new(FileData *fd, CacheDataType load_mask,
222                               CacheLoader::DoneFunc done_func, gpointer done_data)
223 {
224         CacheLoader *cl;
225         gchar *found;
226
227         if (!fd || !isfile(fd->path)) return nullptr;
228
229         cl = g_new0(CacheLoader, 1);
230         cl->fd = file_data_ref(fd);
231
232         cl->done_func = done_func;
233         cl->done_data = done_data;
234
235         found = cache_find_location(CACHE_TYPE_SIM, cl->fd->path);
236         if (found && filetime(found) == filetime(cl->fd->path))
237                 {
238                 cl->cd = cache_sim_data_load(found);
239                 }
240         g_free(found);
241
242         if (!cl->cd) cl->cd = cache_sim_data_new();
243
244         cl->todo_mask = load_mask;
245         cl->done_mask = CACHE_LOADER_NONE;
246
247         cl->il = nullptr;
248         cl->idle_id = g_idle_add(cache_loader_phase1_idle_cb, cl);
249
250         cl->error = FALSE;
251
252         return cl;
253 }
254
255 void cache_loader_free(CacheLoader *cl)
256 {
257         if (!cl) return;
258
259         if (cl->idle_id)
260                 {
261                 g_source_remove_by_user_data(cl);
262                 cl->idle_id = 0;
263                 }
264
265         image_loader_free(cl->il);
266         cache_sim_data_free(cl->cd);
267
268         file_data_unref(cl->fd);
269         g_free(cl);
270 }
271 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */