Fix #1240: Regression: Option to open new full-function window directly is missing
[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 <sys/types.h>
25
26 #include <cstdio>
27 #include <cstring>
28 #include <ctime>
29
30 #include <gdk-pixbuf/gdk-pixbuf.h>
31 #include <glib-object.h>
32
33 #include "cache.h"
34 #include "filedata.h"
35 #include "image-load.h"
36 #include "metadata.h"
37 #include "options.h"
38 #include "similar.h"
39 #include "typedefs.h"
40 #include "ui-fileops.h"
41
42
43 static gboolean cache_loader_phase2_idle_cb(gpointer data);
44
45 static void cache_loader_phase1_done_cb(ImageLoader *, gpointer data)
46 {
47         auto cl = static_cast<CacheLoader *>(data);
48
49         cl->idle_id = g_idle_add(cache_loader_phase2_idle_cb, cl);
50 }
51
52 static void cache_loader_phase1_error_cb(ImageLoader *, gpointer data)
53 {
54         auto cl = static_cast<CacheLoader *>(data);
55
56         cl->error = TRUE;
57         cl->idle_id = g_idle_add(cache_loader_phase2_idle_cb, cl);
58 }
59
60 static gboolean cache_loader_phase1_process(CacheLoader *cl)
61 {
62         if (cl->todo_mask & CACHE_LOADER_SIMILARITY && !cl->cd->similarity)
63                 {
64
65                 if (!cl->il && !cl->error)
66                         {
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))
71                                 {
72                                 return G_SOURCE_REMOVE;
73                                 }
74
75                         cl->error = TRUE;
76                         }
77                 }
78
79         cl->idle_id = g_idle_add(cache_loader_phase2_idle_cb, cl);
80
81         return G_SOURCE_REMOVE;
82 }
83
84 static gboolean cache_loader_phase2_process(CacheLoader *cl)
85 {
86         if (cl->todo_mask & CACHE_LOADER_SIMILARITY && !cl->cd->similarity && cl->il)
87                 {
88                 GdkPixbuf *pixbuf;
89                 pixbuf = image_loader_get_pixbuf(cl->il);
90                 if (pixbuf)
91                         {
92                         if (!cl->error)
93                                 {
94                                 ImageSimilarityData *sim;
95
96                                 sim = image_sim_new_from_pixbuf(pixbuf);
97                                 cache_sim_data_set_similarity(cl->cd, sim);
98                                 image_sim_free(sim);
99
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);
102                                 }
103
104                         /* we have the dimensions via pixbuf */
105                         if (!cl->cd->dimensions)
106                                 {
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)
110                                         {
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);
113                                         }
114                                 }
115                         }
116
117                 image_loader_free(cl->il);
118                 cl->il = nullptr;
119
120                 cl->todo_mask = static_cast<CacheDataType>(cl->todo_mask & ~CACHE_LOADER_SIMILARITY);
121                 }
122         else if (cl->todo_mask & CACHE_LOADER_DIMENSIONS &&
123                  !cl->cd->dimensions)
124                 {
125                 if (!cl->error &&
126                     image_load_dimensions(cl->fd, &cl->cd->width, &cl->cd->height))
127                         {
128                         cl->cd->dimensions = TRUE;
129                         cl->done_mask = static_cast<CacheDataType>(cl->done_mask | CACHE_LOADER_DIMENSIONS);
130                         }
131                 else
132                         {
133                         cl->error = TRUE;
134                         }
135
136                 cl->todo_mask = static_cast<CacheDataType>(cl->todo_mask & ~CACHE_LOADER_DIMENSIONS);
137                 }
138         else if (cl->todo_mask & CACHE_LOADER_MD5SUM &&
139                  !cl->cd->have_md5sum)
140                 {
141                 if (md5_get_digest_from_file_utf8(cl->fd->path, cl->cd->md5sum))
142                         {
143                         cl->cd->have_md5sum = TRUE;
144                         cl->done_mask = static_cast<CacheDataType>(cl->done_mask | CACHE_LOADER_MD5SUM);
145                         }
146                 else
147                         {
148                         cl->error = TRUE;
149                         }
150
151                 cl->todo_mask = static_cast<CacheDataType>(cl->todo_mask & ~CACHE_LOADER_MD5SUM);
152                 }
153         else if (cl->todo_mask & CACHE_LOADER_DATE &&
154                  !cl->cd->have_date)
155                 {
156                 time_t date = -1;
157                 gchar *text;
158
159                 text =  metadata_read_string(cl->fd, "Exif.Image.DateTime", METADATA_FORMATTED);
160                 if (text)
161                         {
162                         struct tm t;
163
164                         memset(&t, 0, sizeof(t));
165
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)
168                                 {
169                                 t.tm_year -= 1900;
170                                 t.tm_mon -= 1;
171                                 t.tm_isdst = -1;
172                                 date = mktime(&t);
173                                 }
174                         g_free(text);
175                         }
176
177                 cl->cd->date = date;
178                 cl->cd->have_date = TRUE;
179
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);
182                 }
183         else
184                 {
185                 /* done, save then call done function */
186                 if (options->thumbnails.enable_caching &&
187                     cl->done_mask != CACHE_LOADER_NONE)
188                         {
189                         gchar *base;
190                         mode_t mode = 0755;
191
192                         base = cache_get_location(CACHE_TYPE_SIM, cl->fd->path, FALSE, &mode);
193                         if (recursive_mkdir_if_not_exists(base, mode))
194                                 {
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))
198                                         {
199                                         filetime_set(cl->cd->path, filetime(cl->fd->path));
200                                         }
201                                 }
202                         g_free(base);
203                         }
204
205                 cl->idle_id = 0;
206
207                 if (cl->done_func)
208                         {
209                         cl->done_func(cl, cl->error, cl->done_data);
210                         }
211
212                 return G_SOURCE_REMOVE;
213                 }
214
215         return G_SOURCE_CONTINUE;
216 }
217
218 static gboolean cache_loader_phase1_idle_cb(gpointer data)
219 {
220         auto cl = static_cast<CacheLoader *>(data);
221
222         return cache_loader_phase1_process(cl);
223 }
224
225 static gboolean cache_loader_phase2_idle_cb(gpointer data)
226 {
227         auto cl = static_cast<CacheLoader *>(data);
228
229         return cache_loader_phase2_process(cl);
230 }
231
232 CacheLoader *cache_loader_new(FileData *fd, CacheDataType load_mask,
233                               CacheLoader::DoneFunc done_func, gpointer done_data)
234 {
235         CacheLoader *cl;
236         gchar *found;
237
238         if (!fd || !isfile(fd->path)) return nullptr;
239
240         cl = g_new0(CacheLoader, 1);
241         cl->fd = file_data_ref(fd);
242
243         cl->done_func = done_func;
244         cl->done_data = done_data;
245
246         found = cache_find_location(CACHE_TYPE_SIM, cl->fd->path);
247         if (found && filetime(found) == filetime(cl->fd->path))
248                 {
249                 cl->cd = cache_sim_data_load(found);
250                 }
251         g_free(found);
252
253         if (!cl->cd) cl->cd = cache_sim_data_new();
254
255         cl->todo_mask = load_mask;
256         cl->done_mask = CACHE_LOADER_NONE;
257
258         cl->il = nullptr;
259         cl->idle_id = g_idle_add(cache_loader_phase1_idle_cb, cl);
260
261         cl->error = FALSE;
262
263         return cl;
264 }
265
266 void cache_loader_free(CacheLoader *cl)
267 {
268         if (!cl) return;
269
270         if (cl->idle_id)
271                 {
272                 g_source_remove_by_user_data(cl);
273                 cl->idle_id = 0;
274                 }
275
276         image_loader_free(cl->il);
277         cache_sim_data_free(cl->cd);
278
279         file_data_unref(cl->fd);
280         g_free(cl);
281 }
282 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */