Mercurial > geeqie.yaz
annotate src/thumb_standard.c @ 1105:215e102c1819
Drop most old css classes.
author | zas_ |
---|---|
date | Sun, 09 Nov 2008 12:48:00 +0000 |
parents | 1646720364cf |
children | 95860439070b |
rev | line source |
---|---|
9 | 1 /* |
196 | 2 * Geeqie |
79
528e3432e0c0
Thu Oct 19 07:23:37 2006 John Ellis <johne@verizon.net>
gqview
parents:
66
diff
changeset
|
3 * (C) 2006 John Ellis |
475 | 4 * Copyright (C) 2008 The Geeqie Team |
9 | 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 | |
281 | 14 #include "main.h" |
9 | 15 #include "thumb_standard.h" |
16 | |
17 #include "cache.h" /* for cache_ensure_dir_exists */ | |
18 #include "image-load.h" | |
19 #include "md5-util.h" | |
20 #include "pixbuf_util.h" | |
21 #include "ui_fileops.h" | |
838 | 22 #include "filedata.h" |
839 | 23 #include "exif.h" |
9 | 24 |
25 | |
26 /* | |
27 * This thumbnail caching implementation attempts to conform | |
28 * to the Thumbnail Managing Standard proposed on freedesktop.org | |
29 * The standard is documented here: | |
30 * http://triq.net/~jens/thumbnail-spec/index.html | |
31 * (why isn't it actually hosted on freedesktop.org?) | |
32 * | |
33 * This code attempts to conform to version 0.7.0 of the standard. | |
34 * | |
35 * Notes: | |
36 * > Validation of the thumb's embedded uri is a simple strcmp between our | |
37 * version of the escaped uri and the thumb's escaped uri. But not all uri | |
38 * escape functions escape the same set of chars, comparing the unescaped | |
39 * versions may be more accurate. | |
40 * > Only Thumb::URI and Thumb::MTime are stored in a thumb at this time. | |
41 * Storing the Size, Width, Height should probably be implemented. | |
42 */ | |
43 | |
44 | |
45 #define THUMB_SIZE_NORMAL 128 | |
46 #define THUMB_SIZE_LARGE 256 | |
47 | |
48 #define THUMB_MARKER_URI "tEXt::Thumb::URI" | |
49 #define THUMB_MARKER_MTIME "tEXt::Thumb::MTime" | |
50 #define THUMB_MARKER_SIZE "tEXt::Thumb::Size" | |
51 #define THUMB_MARKER_WIDTH "tEXt::Thumb::Image::Width" | |
52 #define THUMB_MARKER_HEIGHT "tEXt::Thumb::Image::Height" | |
53 #define THUMB_MARKER_APP "tEXt::Software" | |
54 | |
55 #define THUMB_PERMS_FOLDER 0700 | |
56 #define THUMB_PERMS_THUMB 0600 | |
57 | |
58 | |
59 | |
60 /* | |
61 *----------------------------------------------------------------------------- | |
62 * thumbnail loader | |
63 *----------------------------------------------------------------------------- | |
64 */ | |
65 | |
66 | |
67 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data); | |
1009
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
68 static gint thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd); |
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
69 static gint thumb_loader_std_setup_path(ThumbLoaderStd *tl, const gchar *path); |
9 | 70 |
71 | |
72 ThumbLoaderStd *thumb_loader_std_new(gint width, gint height) | |
73 { | |
74 ThumbLoaderStd *tl; | |
75 | |
76 tl = g_new0(ThumbLoaderStd, 1); | |
77 | |
78 tl->standard_loader = TRUE; | |
79 | |
80 tl->requested_width = width; | |
81 tl->requested_height = height; | |
82 | |
83 tl->il = NULL; | |
838 | 84 tl->fd = NULL; |
9 | 85 |
333 | 86 tl->cache_enable = options->thumbnails.enable_caching; |
9 | 87 tl->cache_local = FALSE; |
88 tl->cache_retry = FALSE; | |
89 | |
90 return tl; | |
91 } | |
92 | |
93 void thumb_loader_std_set_callbacks(ThumbLoaderStd *tl, | |
94 ThumbLoaderStdFunc func_done, | |
95 ThumbLoaderStdFunc func_error, | |
96 ThumbLoaderStdFunc func_progress, | |
97 gpointer data) | |
98 { | |
99 if (!tl) return; | |
100 | |
101 tl->func_done = func_done; | |
102 tl->func_error = func_error; | |
103 tl->func_progress = func_progress; | |
104 tl->data = data; | |
105 } | |
106 | |
107 static void thumb_loader_std_reset(ThumbLoaderStd *tl) | |
108 { | |
109 image_loader_free(tl->il); | |
110 tl->il = NULL; | |
111 | |
838 | 112 file_data_unref(tl->fd); |
113 tl->fd = NULL; | |
9 | 114 |
115 g_free(tl->thumb_path); | |
116 tl->thumb_path = NULL; | |
117 | |
118 g_free(tl->thumb_uri); | |
119 tl->thumb_uri = NULL; | |
120 tl->local_uri = NULL; | |
121 | |
122 tl->thumb_path_local = FALSE; | |
123 | |
124 tl->cache_hit = FALSE; | |
125 | |
126 tl->source_mtime = 0; | |
127 tl->source_size = 0; | |
128 tl->source_mode = 0; | |
129 | |
130 tl->progress = 0.0; | |
131 } | |
132 | |
133 static gchar *thumb_std_cache_path(const gchar *path, const gchar *uri, gint local, | |
134 const gchar *cache_subfolder) | |
135 { | |
136 gchar *result = NULL; | |
137 gchar *md5_text; | |
138 guchar digest[16]; | |
716
c534b8cecc1f
thumb_std_cache_path(): use g_build_filename() and simplify.
zas_
parents:
671
diff
changeset
|
139 gchar *name; |
9 | 140 |
141 if (!path || !uri || !cache_subfolder) return NULL; | |
142 | |
716
c534b8cecc1f
thumb_std_cache_path(): use g_build_filename() and simplify.
zas_
parents:
671
diff
changeset
|
143 md5_get_digest((guchar *)uri, strlen(uri), digest); |
c534b8cecc1f
thumb_std_cache_path(): use g_build_filename() and simplify.
zas_
parents:
671
diff
changeset
|
144 md5_text = md5_digest_to_text(digest); |
c534b8cecc1f
thumb_std_cache_path(): use g_build_filename() and simplify.
zas_
parents:
671
diff
changeset
|
145 |
c534b8cecc1f
thumb_std_cache_path(): use g_build_filename() and simplify.
zas_
parents:
671
diff
changeset
|
146 if (!md5_text) return NULL; |
c534b8cecc1f
thumb_std_cache_path(): use g_build_filename() and simplify.
zas_
parents:
671
diff
changeset
|
147 |
c534b8cecc1f
thumb_std_cache_path(): use g_build_filename() and simplify.
zas_
parents:
671
diff
changeset
|
148 name = g_strconcat(md5_text, THUMB_NAME_EXTENSION, NULL); |
c534b8cecc1f
thumb_std_cache_path(): use g_build_filename() and simplify.
zas_
parents:
671
diff
changeset
|
149 |
9 | 150 if (local) |
151 { | |
716
c534b8cecc1f
thumb_std_cache_path(): use g_build_filename() and simplify.
zas_
parents:
671
diff
changeset
|
152 gchar *base = remove_level_from_path(path); |
9 | 153 |
716
c534b8cecc1f
thumb_std_cache_path(): use g_build_filename() and simplify.
zas_
parents:
671
diff
changeset
|
154 result = g_build_filename(base, THUMB_FOLDER_LOCAL, cache_subfolder, name, NULL); |
9 | 155 g_free(base); |
156 } | |
157 else | |
158 { | |
716
c534b8cecc1f
thumb_std_cache_path(): use g_build_filename() and simplify.
zas_
parents:
671
diff
changeset
|
159 result = g_build_filename(homedir(), THUMB_FOLDER_GLOBAL, cache_subfolder, name, NULL); |
9 | 160 } |
161 | |
716
c534b8cecc1f
thumb_std_cache_path(): use g_build_filename() and simplify.
zas_
parents:
671
diff
changeset
|
162 g_free(name); |
9 | 163 g_free(md5_text); |
164 | |
165 return result; | |
166 } | |
167 | |
168 static gchar *thumb_loader_std_cache_path(ThumbLoaderStd *tl, gint local, GdkPixbuf *pixbuf, gint fail) | |
169 { | |
436 | 170 const gchar *folder; |
9 | 171 gint w, h; |
172 | |
838 | 173 if (!tl->fd || !tl->thumb_uri) return NULL; |
9 | 174 |
175 if (pixbuf) | |
176 { | |
177 w = gdk_pixbuf_get_width(pixbuf); | |
178 h = gdk_pixbuf_get_height(pixbuf); | |
179 } | |
180 else | |
181 { | |
182 w = tl->requested_width; | |
183 h = tl->requested_height; | |
184 } | |
185 | |
186 if (fail) | |
187 { | |
436 | 188 folder = THUMB_FOLDER_FAIL; |
9 | 189 } |
190 else if (w > THUMB_SIZE_NORMAL || h > THUMB_SIZE_NORMAL) | |
191 { | |
436 | 192 folder = THUMB_FOLDER_LARGE; |
9 | 193 } |
194 else | |
195 { | |
436 | 196 folder = THUMB_FOLDER_NORMAL; |
9 | 197 } |
198 | |
838 | 199 return thumb_std_cache_path(tl->fd->path, |
9 | 200 (local) ? tl->local_uri : tl->thumb_uri, |
436 | 201 local, folder); |
9 | 202 } |
203 | |
204 static gint thumb_loader_std_fail_check(ThumbLoaderStd *tl) | |
205 { | |
206 gchar *fail_path; | |
207 gint result = FALSE; | |
208 | |
209 fail_path = thumb_loader_std_cache_path(tl, FALSE, NULL, TRUE); | |
210 if (isfile(fail_path)) | |
211 { | |
212 GdkPixbuf *pixbuf; | |
213 | |
214 if (tl->cache_retry) | |
215 { | |
216 pixbuf = NULL; | |
217 } | |
218 else | |
219 { | |
220 gchar *pathl; | |
221 | |
222 pathl = path_from_utf8(fail_path); | |
223 pixbuf = gdk_pixbuf_new_from_file(pathl, NULL); | |
224 g_free(pathl); | |
225 } | |
226 | |
227 if (pixbuf) | |
228 { | |
229 const gchar *mtime_str; | |
230 | |
231 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME); | |
232 if (mtime_str && strtol(mtime_str, NULL, 10) == tl->source_mtime) | |
233 { | |
234 result = TRUE; | |
838 | 235 DEBUG_1("thumb fail valid: %s", tl->fd->path); |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
236 DEBUG_1(" thumb: %s", fail_path); |
9 | 237 } |
238 | |
239 g_object_unref(G_OBJECT(pixbuf)); | |
240 } | |
241 | |
242 if (!result) unlink_file(fail_path); | |
243 } | |
244 g_free(fail_path); | |
245 | |
246 return result; | |
247 } | |
248 | |
249 static gint thumb_loader_std_validate(ThumbLoaderStd *tl, GdkPixbuf *pixbuf) | |
250 { | |
251 const gchar *valid_uri; | |
252 const gchar *uri; | |
253 const gchar *mtime_str; | |
254 time_t mtime; | |
255 gint w, h; | |
256 | |
257 if (!pixbuf) return FALSE; | |
258 | |
259 w = gdk_pixbuf_get_width(pixbuf); | |
260 h = gdk_pixbuf_get_height(pixbuf); | |
261 | |
262 if (w != THUMB_SIZE_NORMAL && w != THUMB_SIZE_LARGE && | |
263 h != THUMB_SIZE_NORMAL && h != THUMB_SIZE_LARGE) return FALSE; | |
264 | |
265 valid_uri = (tl->thumb_path_local) ? tl->local_uri : tl->thumb_uri; | |
266 | |
267 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI); | |
268 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME); | |
269 | |
270 if (!mtime_str || !uri || !valid_uri) return FALSE; | |
271 if (strcmp(uri, valid_uri) != 0) return FALSE; | |
272 | |
273 mtime = strtol(mtime_str, NULL, 10); | |
274 if (tl->source_mtime != mtime) return FALSE; | |
275 | |
276 return TRUE; | |
277 } | |
278 | |
279 static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf) | |
280 { | |
281 gchar *base_path; | |
282 gchar *tmp_path; | |
283 gint fail; | |
284 | |
285 if (!tl->cache_enable || tl->cache_hit) return; | |
286 if (tl->thumb_path) return; | |
287 | |
288 if (!pixbuf) | |
289 { | |
290 /* local failures are not stored */ | |
291 if (tl->cache_local) return; | |
292 | |
436 | 293 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 1, 1); |
9 | 294 fail = TRUE; |
295 } | |
296 else | |
297 { | |
298 g_object_ref(G_OBJECT(pixbuf)); | |
299 fail = FALSE; | |
300 } | |
301 | |
302 tl->thumb_path = thumb_loader_std_cache_path(tl, tl->cache_local, pixbuf, fail); | |
303 if (!tl->thumb_path) | |
304 { | |
305 g_object_unref(G_OBJECT(pixbuf)); | |
306 return; | |
307 } | |
308 tl->thumb_path_local = tl->cache_local; | |
309 | |
310 /* create thumbnail dir if needed */ | |
311 base_path = remove_level_from_path(tl->thumb_path); | |
312 if (tl->cache_local) | |
313 { | |
314 if (!isdir(base_path)) | |
315 { | |
316 struct stat st; | |
317 gchar *source_base; | |
318 | |
838 | 319 source_base = remove_level_from_path(tl->fd->path); |
9 | 320 if (stat_utf8(source_base, &st)) |
321 { | |
322 cache_ensure_dir_exists(base_path, st.st_mode); | |
323 } | |
324 g_free(source_base); | |
325 } | |
326 } | |
327 else | |
328 { | |
329 cache_ensure_dir_exists(base_path, THUMB_PERMS_FOLDER); | |
330 } | |
331 g_free(base_path); | |
332 | |
838 | 333 DEBUG_1("thumb saving: %s", tl->fd->path); |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
334 DEBUG_1(" saved: %s", tl->thumb_path); |
9 | 335 |
336 /* save thumb, using a temp file then renaming into place */ | |
337 tmp_path = unique_filename(tl->thumb_path, ".tmp", "_", 2); | |
338 if (tmp_path) | |
339 { | |
340 const gchar *mark_uri; | |
341 gchar *mark_app; | |
342 gchar *mark_mtime; | |
343 gchar *pathl; | |
344 gint success; | |
345 | |
346 mark_uri = (tl->cache_local) ? tl->local_uri :tl->thumb_uri; | |
347 | |
288
d1f74154463e
Replace occurences of Geeqie / geeqie by constants defined in main.h.
zas_
parents:
281
diff
changeset
|
348 mark_app = g_strdup_printf("%s %s", GQ_APPNAME, VERSION); |
9 | 349 mark_mtime = g_strdup_printf("%lu", tl->source_mtime); |
350 | |
351 pathl = path_from_utf8(tmp_path); | |
352 success = gdk_pixbuf_save(pixbuf, pathl, "png", NULL, | |
353 THUMB_MARKER_URI, mark_uri, | |
354 THUMB_MARKER_MTIME, mark_mtime, | |
355 THUMB_MARKER_APP, mark_app, | |
356 NULL); | |
357 if (success) | |
358 { | |
359 chmod(pathl, (tl->cache_local) ? tl->source_mode : THUMB_PERMS_THUMB); | |
360 success = rename_file(tmp_path, tl->thumb_path); | |
361 } | |
362 | |
363 g_free(pathl); | |
364 | |
365 g_free(mark_mtime); | |
366 g_free(mark_app); | |
367 | |
368 g_free(tmp_path); | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
369 if (!success) |
9 | 370 { |
838 | 371 DEBUG_1("thumb save failed: %s", tl->fd->path); |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
372 DEBUG_1(" thumb: %s", tl->thumb_path); |
9 | 373 } |
374 | |
375 } | |
376 | |
377 g_object_unref(G_OBJECT(pixbuf)); | |
378 } | |
379 | |
876
2d8705f33da5
set fallback thumbnail pixbuf only if the loading really fails
nadvornik
parents:
864
diff
changeset
|
380 static void thumb_loader_std_set_fallback(ThumbLoaderStd *tl) |
2d8705f33da5
set fallback thumbnail pixbuf only if the loading really fails
nadvornik
parents:
864
diff
changeset
|
381 { |
2d8705f33da5
set fallback thumbnail pixbuf only if the loading really fails
nadvornik
parents:
864
diff
changeset
|
382 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf); |
2d8705f33da5
set fallback thumbnail pixbuf only if the loading really fails
nadvornik
parents:
864
diff
changeset
|
383 tl->fd->thumb_pixbuf = pixbuf_fallback(tl->fd, tl->requested_width, tl->requested_height); |
2d8705f33da5
set fallback thumbnail pixbuf only if the loading really fails
nadvornik
parents:
864
diff
changeset
|
384 } |
2d8705f33da5
set fallback thumbnail pixbuf only if the loading really fails
nadvornik
parents:
864
diff
changeset
|
385 |
14
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
386 static GdkPixbuf *thumb_loader_std_finish(ThumbLoaderStd *tl, GdkPixbuf *pixbuf, gint shrunk) |
9 | 387 { |
388 GdkPixbuf *pixbuf_thumb = NULL; | |
389 GdkPixbuf *result; | |
839 | 390 GdkPixbuf *rotated = NULL; |
9 | 391 gint sw, sh; |
392 | |
839 | 393 |
394 if (!tl->cache_hit && options->image.exif_rotate_enable) | |
395 { | |
396 if (!tl->fd->exif_orientation) | |
397 { | |
398 ExifData *exif = exif_read_fd(tl->fd); | |
399 gint orientation; | |
400 | |
401 if (exif && exif_get_integer(exif, "Exif.Image.Orientation", &orientation)) | |
402 tl->fd->exif_orientation = orientation; | |
403 else | |
842
94048d7843ba
do not allocate new buffer for thumbnails with correct orientation
nadvornik
parents:
841
diff
changeset
|
404 tl->fd->exif_orientation = EXIF_ORIENTATION_TOP_LEFT; |
844 | 405 exif_free_fd(tl->fd, exif); |
839 | 406 } |
407 | |
842
94048d7843ba
do not allocate new buffer for thumbnails with correct orientation
nadvornik
parents:
841
diff
changeset
|
408 if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT) |
94048d7843ba
do not allocate new buffer for thumbnails with correct orientation
nadvornik
parents:
841
diff
changeset
|
409 { |
94048d7843ba
do not allocate new buffer for thumbnails with correct orientation
nadvornik
parents:
841
diff
changeset
|
410 rotated = pixbuf_apply_orientation(pixbuf, tl->fd->exif_orientation); |
94048d7843ba
do not allocate new buffer for thumbnails with correct orientation
nadvornik
parents:
841
diff
changeset
|
411 pixbuf = rotated; |
94048d7843ba
do not allocate new buffer for thumbnails with correct orientation
nadvornik
parents:
841
diff
changeset
|
412 } |
839 | 413 } |
414 | |
9 | 415 sw = gdk_pixbuf_get_width(pixbuf); |
416 sh = gdk_pixbuf_get_height(pixbuf); | |
417 | |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
418 if (tl->cache_enable) |
9 | 419 { |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
420 if (!tl->cache_hit) |
9 | 421 { |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
422 gint cache_w, cache_h; |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
423 |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
424 if (tl->requested_width > THUMB_SIZE_NORMAL || tl->requested_height > THUMB_SIZE_NORMAL) |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
425 { |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
426 cache_w = cache_h = THUMB_SIZE_LARGE; |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
427 } |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
428 else |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
429 { |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
430 cache_w = cache_h = THUMB_SIZE_NORMAL; |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
431 } |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
432 |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
433 if (sw > cache_w || sh > cache_h || shrunk) |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
434 { |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
435 gint thumb_w, thumb_h; |
9 | 436 |
864 | 437 if (pixbuf_scale_aspect(cache_w, cache_h, sw, sh, |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
438 &thumb_w, &thumb_h)) |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
439 { |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
440 pixbuf_thumb = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h, |
333 | 441 (GdkInterpType)options->thumbnails.quality); |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
442 } |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
443 else |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
444 { |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
445 pixbuf_thumb = pixbuf; |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
446 g_object_ref(G_OBJECT(pixbuf_thumb)); |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
447 } |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
448 |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
449 thumb_loader_std_save(tl, pixbuf_thumb); |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
450 } |
9 | 451 } |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
452 else if (tl->cache_hit && |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
453 tl->cache_local && !tl->thumb_path_local) |
9 | 454 { |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
455 /* A local cache save was requested, but a valid thumb is in $HOME, |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
456 * so specifically save as a local thumbnail. |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
457 */ |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
458 g_free(tl->thumb_path); |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
459 tl->thumb_path = NULL; |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
460 |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
461 tl->cache_hit = FALSE; |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
462 |
838 | 463 DEBUG_1("thumb copied: %s", tl->fd->path); |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
464 |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
14
diff
changeset
|
465 thumb_loader_std_save(tl, pixbuf); |
9 | 466 } |
467 } | |
468 | |
469 if (sw <= tl->requested_width && sh <= tl->requested_height) | |
470 { | |
471 result = pixbuf; | |
472 g_object_ref(result); | |
473 } | |
474 else | |
475 { | |
476 gint thumb_w, thumb_h; | |
477 | |
478 if (pixbuf_thumb) | |
479 { | |
480 pixbuf = pixbuf_thumb; | |
481 sw = gdk_pixbuf_get_width(pixbuf); | |
482 sh = gdk_pixbuf_get_height(pixbuf); | |
483 } | |
484 | |
864 | 485 if (pixbuf_scale_aspect(tl->requested_width, tl->requested_height, sw, sh, |
9 | 486 &thumb_w, &thumb_h)) |
487 { | |
488 result = gdk_pixbuf_scale_simple(pixbuf, thumb_w, thumb_h, | |
333 | 489 (GdkInterpType)options->thumbnails.quality); |
9 | 490 } |
491 else | |
492 { | |
493 result = pixbuf; | |
494 g_object_ref(result); | |
495 } | |
496 } | |
497 | |
498 if (pixbuf_thumb) g_object_unref(pixbuf_thumb); | |
839 | 499 if (rotated) g_object_unref(rotated); |
9 | 500 |
501 return result; | |
502 } | |
503 | |
504 static gint thumb_loader_std_next_source(ThumbLoaderStd *tl, gint remove_broken) | |
505 { | |
506 image_loader_free(tl->il); | |
507 tl->il = NULL; | |
508 | |
509 if (tl->thumb_path) | |
510 { | |
511 if (!tl->thumb_path_local && remove_broken) | |
512 { | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
513 DEBUG_1("thumb broken, unlinking: %s", tl->thumb_path); |
9 | 514 unlink_file(tl->thumb_path); |
515 } | |
516 | |
517 g_free(tl->thumb_path); | |
518 tl->thumb_path = NULL; | |
519 | |
520 if (!tl->thumb_path_local) | |
521 { | |
522 tl->thumb_path = thumb_loader_std_cache_path(tl, TRUE, NULL, FALSE); | |
1009
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
523 if (isfile(tl->thumb_path) && thumb_loader_std_setup_path(tl, tl->thumb_path)) |
9 | 524 { |
525 tl->thumb_path_local = TRUE; | |
526 return TRUE; | |
527 } | |
528 | |
529 g_free(tl->thumb_path); | |
530 tl->thumb_path = NULL; | |
531 } | |
532 | |
1009
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
533 if (thumb_loader_std_setup(tl, tl->fd)) return TRUE; |
9 | 534 } |
535 | |
536 thumb_loader_std_save(tl, NULL); | |
537 return FALSE; | |
538 } | |
539 | |
540 static void thumb_loader_std_done_cb(ImageLoader *il, gpointer data) | |
541 { | |
542 ThumbLoaderStd *tl = data; | |
543 GdkPixbuf *pixbuf; | |
544 | |
881
10d0bf784323
Prevent segfault in certain conditions when displaying debug message.
zas_
parents:
876
diff
changeset
|
545 DEBUG_1("thumb image done: %s", tl->fd ? tl->fd->path : "???"); |
1011 | 546 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path); |
9 | 547 |
548 pixbuf = image_loader_get_pixbuf(tl->il); | |
549 if (!pixbuf) | |
550 { | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
551 DEBUG_1("...but no pixbuf"); |
9 | 552 thumb_loader_std_error_cb(il, data); |
553 return; | |
554 } | |
555 | |
556 if (tl->thumb_path && !thumb_loader_std_validate(tl, pixbuf)) | |
557 { | |
558 if (thumb_loader_std_next_source(tl, TRUE)) return; | |
559 | |
560 if (tl->func_error) tl->func_error(tl, tl->data); | |
561 return; | |
562 } | |
563 | |
564 tl->cache_hit = (tl->thumb_path != NULL); | |
565 | |
838 | 566 if (tl->fd) |
567 { | |
845 | 568 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf); |
1011 | 569 tl->fd->thumb_pixbuf = thumb_loader_std_finish(tl, pixbuf, image_loader_get_shrunk(il)); |
838 | 570 } |
9 | 571 |
572 if (tl->func_done) tl->func_done(tl, tl->data); | |
573 } | |
574 | |
575 static void thumb_loader_std_error_cb(ImageLoader *il, gpointer data) | |
576 { | |
577 ThumbLoaderStd *tl = data; | |
578 | |
579 /* if at least some of the image is available, go to done */ | |
580 if (image_loader_get_pixbuf(tl->il) != NULL) | |
581 { | |
582 thumb_loader_std_done_cb(il, data); | |
583 return; | |
584 } | |
495 | 585 |
838 | 586 DEBUG_1("thumb image error: %s", tl->fd->path); |
1011 | 587 DEBUG_1(" from: %s", image_loader_get_fd(tl->il)->path); |
9 | 588 |
589 if (thumb_loader_std_next_source(tl, TRUE)) return; | |
590 | |
876
2d8705f33da5
set fallback thumbnail pixbuf only if the loading really fails
nadvornik
parents:
864
diff
changeset
|
591 thumb_loader_std_set_fallback(tl); |
2d8705f33da5
set fallback thumbnail pixbuf only if the loading really fails
nadvornik
parents:
864
diff
changeset
|
592 |
9 | 593 if (tl->func_error) tl->func_error(tl, tl->data); |
594 } | |
595 | |
596 static void thumb_loader_std_progress_cb(ImageLoader *il, gdouble percent, gpointer data) | |
597 { | |
598 ThumbLoaderStd *tl = data; | |
599 | |
600 tl->progress = (gdouble)percent; | |
601 | |
602 if (tl->func_progress) tl->func_progress(tl, tl->data); | |
603 } | |
604 | |
1009
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
605 static gint thumb_loader_std_setup(ThumbLoaderStd *tl, FileData *fd) |
9 | 606 { |
1009
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
607 tl->il = image_loader_new(fd); |
1036 | 608 image_loader_set_priority(tl->il, G_PRIORITY_LOW); |
9 | 609 |
333 | 610 if (options->thumbnails.fast) |
9 | 611 { |
14
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
612 /* this will speed up jpegs by up to 3x in some cases */ |
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
613 if (tl->requested_width <= THUMB_SIZE_NORMAL && |
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
614 tl->requested_height <= THUMB_SIZE_NORMAL) |
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
615 { |
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
616 image_loader_set_requested_size(tl->il, THUMB_SIZE_NORMAL, THUMB_SIZE_NORMAL); |
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
617 } |
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
618 else |
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
619 { |
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
620 image_loader_set_requested_size(tl->il, THUMB_SIZE_LARGE, THUMB_SIZE_LARGE); |
25335c62cd9b
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
621 } |
9 | 622 } |
623 | |
1012
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
624 g_signal_connect (G_OBJECT(tl->il), "error", (GCallback)thumb_loader_std_error_cb, tl); |
9 | 625 if (tl->func_progress) |
626 { | |
1012
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
627 g_signal_connect (G_OBJECT(tl->il), "percent", (GCallback)thumb_loader_std_progress_cb, tl); |
9 | 628 } |
1012
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
629 g_signal_connect (G_OBJECT(tl->il), "done", (GCallback)thumb_loader_std_done_cb, tl); |
9 | 630 |
1012
fe82830ab8fd
converted image loader to a GObject and use signals for notification
nadvornik
parents:
1011
diff
changeset
|
631 if (image_loader_start(tl->il)) |
9 | 632 { |
633 return TRUE; | |
634 } | |
635 | |
636 image_loader_free(tl->il); | |
637 tl->il = NULL; | |
638 return FALSE; | |
639 } | |
640 | |
1009
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
641 static gint thumb_loader_std_setup_path(ThumbLoaderStd *tl, const gchar *path) |
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
642 { |
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
643 FileData *fd = file_data_new_simple(path); |
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
644 gint ret = thumb_loader_std_setup(tl, fd); |
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
645 file_data_unref(fd); |
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
646 return ret; |
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
647 } |
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
648 |
9 | 649 /* |
650 * Note: Currently local_cache only specifies where to save a _new_ thumb, if | |
651 * a valid existing thumb is found anywhere the local thumb will not be created. | |
652 */ | |
653 void thumb_loader_std_set_cache(ThumbLoaderStd *tl, gint enable_cache, gint local, gint retry_failed) | |
654 { | |
655 if (!tl) return; | |
656 | |
657 tl->cache_enable = enable_cache; | |
658 tl->cache_local = local; | |
659 tl->cache_retry = retry_failed; | |
660 } | |
661 | |
838 | 662 gint thumb_loader_std_start(ThumbLoaderStd *tl, FileData *fd) |
9 | 663 { |
664 static gchar *thumb_cache = NULL; | |
665 struct stat st; | |
666 | |
838 | 667 if (!tl || !fd) return FALSE; |
9 | 668 |
669 thumb_loader_std_reset(tl); | |
670 | |
671 | |
838 | 672 tl->fd = file_data_ref(fd); |
972 | 673 if (!stat_utf8(fd->path, &st)) |
674 { | |
675 thumb_loader_std_set_fallback(tl); | |
676 return FALSE; | |
677 } | |
9 | 678 tl->source_mtime = st.st_mtime; |
679 tl->source_size = st.st_size; | |
680 tl->source_mode = st.st_mode; | |
681 | |
717 | 682 if (!thumb_cache) thumb_cache = g_build_filename(homedir(), THUMB_FOLDER_GLOBAL, NULL); |
838 | 683 if (strncmp(tl->fd->path, thumb_cache, strlen(thumb_cache)) != 0) |
9 | 684 { |
685 gchar *pathl; | |
686 | |
838 | 687 pathl = path_from_utf8(fd->path); |
9 | 688 tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL); |
689 tl->local_uri = filename_from_path(tl->thumb_uri); | |
690 g_free(pathl); | |
691 } | |
692 | |
693 if (tl->cache_enable) | |
694 { | |
695 gint found; | |
696 | |
697 tl->thumb_path = thumb_loader_std_cache_path(tl, FALSE, NULL, FALSE); | |
698 tl->thumb_path_local = FALSE; | |
699 | |
700 found = isfile(tl->thumb_path); | |
1009
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
701 if (found && thumb_loader_std_setup_path(tl, tl->thumb_path)) return TRUE; |
9 | 702 |
876
2d8705f33da5
set fallback thumbnail pixbuf only if the loading really fails
nadvornik
parents:
864
diff
changeset
|
703 if (thumb_loader_std_fail_check(tl) || |
2d8705f33da5
set fallback thumbnail pixbuf only if the loading really fails
nadvornik
parents:
864
diff
changeset
|
704 !thumb_loader_std_next_source(tl, found)) |
2d8705f33da5
set fallback thumbnail pixbuf only if the loading really fails
nadvornik
parents:
864
diff
changeset
|
705 { |
2d8705f33da5
set fallback thumbnail pixbuf only if the loading really fails
nadvornik
parents:
864
diff
changeset
|
706 thumb_loader_std_set_fallback(tl); |
2d8705f33da5
set fallback thumbnail pixbuf only if the loading really fails
nadvornik
parents:
864
diff
changeset
|
707 return FALSE; |
2d8705f33da5
set fallback thumbnail pixbuf only if the loading really fails
nadvornik
parents:
864
diff
changeset
|
708 } |
2d8705f33da5
set fallback thumbnail pixbuf only if the loading really fails
nadvornik
parents:
864
diff
changeset
|
709 return TRUE; |
9 | 710 } |
711 | |
1009
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
712 if (!thumb_loader_std_setup(tl, tl->fd)) |
9 | 713 { |
714 thumb_loader_std_save(tl, NULL); | |
876
2d8705f33da5
set fallback thumbnail pixbuf only if the loading really fails
nadvornik
parents:
864
diff
changeset
|
715 thumb_loader_std_set_fallback(tl); |
9 | 716 return FALSE; |
717 } | |
718 | |
719 return TRUE; | |
720 } | |
721 | |
722 void thumb_loader_std_free(ThumbLoaderStd *tl) | |
723 { | |
724 if (!tl) return; | |
725 | |
726 thumb_loader_std_reset(tl); | |
727 g_free(tl); | |
728 } | |
729 | |
864 | 730 GdkPixbuf *thumb_loader_std_get_pixbuf(ThumbLoaderStd *tl) |
9 | 731 { |
732 GdkPixbuf *pixbuf; | |
733 | |
845 | 734 if (tl && tl->fd && tl->fd->thumb_pixbuf) |
9 | 735 { |
845 | 736 pixbuf = tl->fd->thumb_pixbuf; |
9 | 737 g_object_ref(pixbuf); |
738 } | |
739 else | |
740 { | |
864 | 741 pixbuf = pixbuf_fallback(NULL, tl->requested_width, tl->requested_height); |
9 | 742 } |
743 | |
744 return pixbuf; | |
745 } | |
746 | |
747 | |
748 typedef struct _ThumbValidate ThumbValidate; | |
749 struct _ThumbValidate | |
750 { | |
751 ThumbLoaderStd *tl; | |
752 gchar *path; | |
753 gint days; | |
754 | |
755 void (*func_valid)(const gchar *path, gint valid, gpointer data); | |
756 gpointer data; | |
757 | |
758 gint idle_id; | |
759 }; | |
760 | |
761 static void thumb_loader_std_thumb_file_validate_free(ThumbValidate *tv) | |
762 { | |
763 thumb_loader_std_free(tv->tl); | |
764 g_free(tv->path); | |
765 g_free(tv); | |
766 } | |
767 | |
768 void thumb_loader_std_thumb_file_validate_cancel(ThumbLoaderStd *tl) | |
769 { | |
770 ThumbValidate *tv; | |
771 | |
772 if (!tl) return; | |
773 | |
774 tv = tl->data; | |
775 | |
776 if (tv->idle_id != -1) g_source_remove(tv->idle_id); | |
777 tv->idle_id = -1; | |
778 | |
779 thumb_loader_std_thumb_file_validate_free(tv); | |
780 } | |
781 | |
782 static void thumb_loader_std_thumb_file_validate_finish(ThumbValidate *tv, gint valid) | |
783 { | |
784 if (tv->func_valid) tv->func_valid(tv->path, valid, tv->data); | |
785 | |
786 thumb_loader_std_thumb_file_validate_free(tv); | |
787 } | |
788 | |
789 static void thumb_loader_std_thumb_file_validate_done_cb(ThumbLoaderStd *tl, gpointer data) | |
790 { | |
791 ThumbValidate *tv = data; | |
792 GdkPixbuf *pixbuf; | |
793 gint valid = FALSE; | |
794 | |
864 | 795 /* this function is called on success, so the pixbuf should not be a fallback*/ |
796 pixbuf = thumb_loader_std_get_pixbuf(tv->tl); | |
9 | 797 if (pixbuf) |
798 { | |
799 const gchar *uri; | |
800 const gchar *mtime_str; | |
801 | |
802 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI); | |
803 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME); | |
804 if (uri && mtime_str) | |
805 { | |
806 if (strncmp(uri, "file:", strlen("file:")) == 0) | |
807 { | |
808 struct stat st; | |
809 gchar *target; | |
810 | |
811 target = g_filename_from_uri(uri, NULL, NULL); | |
812 if (stat(target, &st) == 0 && | |
813 st.st_mtime == strtol(mtime_str, NULL, 10)) | |
814 { | |
815 valid = TRUE; | |
816 } | |
817 g_free(target); | |
818 } | |
819 else | |
820 { | |
821 struct stat st; | |
822 | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
823 DEBUG_1("thumb uri foreign, doing day check: %s", uri); |
9 | 824 |
825 if (stat_utf8(tv->path, &st)) | |
826 { | |
827 time_t now; | |
828 | |
829 now = time(NULL); | |
830 if (st.st_atime >= now - (time_t)tv->days * 24 * 60 * 60) | |
831 { | |
832 valid = TRUE; | |
833 } | |
834 } | |
835 } | |
836 } | |
837 | |
838 g_object_unref(pixbuf); | |
839 } | |
840 | |
841 thumb_loader_std_thumb_file_validate_finish(tv, valid); | |
842 } | |
843 | |
844 static void thumb_loader_std_thumb_file_validate_error_cb(ThumbLoaderStd *tl, gpointer data) | |
845 { | |
846 ThumbValidate *tv = data; | |
847 | |
848 thumb_loader_std_thumb_file_validate_finish(tv, FALSE); | |
849 } | |
850 | |
851 static gint thumb_loader_std_thumb_file_validate_idle_cb(gpointer data) | |
852 { | |
853 ThumbValidate *tv = data; | |
854 | |
855 tv->idle_id = -1; | |
856 thumb_loader_std_thumb_file_validate_finish(tv, FALSE); | |
857 | |
858 return FALSE; | |
859 } | |
860 | |
861 ThumbLoaderStd *thumb_loader_std_thumb_file_validate(const gchar *thumb_path, gint allowed_days, | |
862 void (*func_valid)(const gchar *path, gint valid, gpointer data), | |
863 gpointer data) | |
864 { | |
865 ThumbValidate *tv; | |
866 | |
867 tv = g_new0(ThumbValidate, 1); | |
868 | |
869 tv->tl = thumb_loader_std_new(THUMB_SIZE_LARGE, THUMB_SIZE_LARGE); | |
870 thumb_loader_std_set_callbacks(tv->tl, | |
871 thumb_loader_std_thumb_file_validate_done_cb, | |
872 thumb_loader_std_thumb_file_validate_error_cb, | |
873 NULL, | |
874 tv); | |
875 thumb_loader_std_reset(tv->tl); | |
876 | |
877 tv->path = g_strdup(thumb_path); | |
878 tv->days = allowed_days; | |
879 tv->func_valid = func_valid; | |
880 tv->data = data; | |
881 | |
1009
dd311dae857a
fixed thumbnail loader for the new raw preview interface
nadvornik
parents:
972
diff
changeset
|
882 if (!thumb_loader_std_setup_path(tv->tl, thumb_path)) |
9 | 883 { |
884 tv->idle_id = g_idle_add(thumb_loader_std_thumb_file_validate_idle_cb, tv); | |
885 } | |
886 else | |
887 { | |
888 tv->idle_id = -1; | |
889 } | |
890 | |
891 return tv->tl; | |
892 } | |
893 | |
894 static void thumb_std_maint_remove_one(const gchar *source, const gchar *uri, gint local, | |
895 const gchar *subfolder) | |
896 { | |
897 gchar *thumb_path; | |
898 | |
899 thumb_path = thumb_std_cache_path(source, | |
900 (local) ? filename_from_path(uri) : uri, | |
901 local, subfolder); | |
902 if (isfile(thumb_path)) | |
903 { | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
904 DEBUG_1("thumb removing: %s", thumb_path); |
9 | 905 unlink_file(thumb_path); |
906 } | |
907 g_free(thumb_path); | |
908 } | |
909 | |
910 /* this also removes local thumbnails (the source is gone so it makes sense) */ | |
911 void thumb_std_maint_removed(const gchar *source) | |
912 { | |
913 gchar *uri; | |
914 gchar *sourcel; | |
915 | |
916 sourcel = path_from_utf8(source); | |
917 uri = g_filename_to_uri(sourcel, NULL, NULL); | |
918 g_free(sourcel); | |
919 | |
920 /* all this to remove a thumbnail? */ | |
921 | |
922 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_NORMAL); | |
923 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_LARGE); | |
924 thumb_std_maint_remove_one(source, uri, FALSE, THUMB_FOLDER_FAIL); | |
925 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_NORMAL); | |
926 thumb_std_maint_remove_one(source, uri, TRUE, THUMB_FOLDER_LARGE); | |
927 | |
928 g_free(uri); | |
929 } | |
930 | |
931 typedef struct _TMaintMove TMaintMove; | |
932 struct _TMaintMove | |
933 { | |
934 gchar *source; | |
935 gchar *dest; | |
936 | |
937 ThumbLoaderStd *tl; | |
938 gchar *source_uri; | |
939 gchar *thumb_path; | |
940 | |
941 gint pass; | |
942 }; | |
943 | |
944 static GList *thumb_std_maint_move_list = NULL; | |
945 static GList *thumb_std_maint_move_tail = NULL; | |
946 | |
947 | |
948 static void thumb_std_maint_move_step(TMaintMove *tm); | |
949 static gint thumb_std_maint_move_idle(gpointer data); | |
950 | |
951 | |
952 static void thumb_std_maint_move_validate_cb(const gchar *path, gint valid, gpointer data) | |
953 { | |
954 TMaintMove *tm = data; | |
955 GdkPixbuf *pixbuf; | |
956 | |
864 | 957 /* this function is called on success, so the pixbuf should not be a fallback*/ |
958 pixbuf = thumb_loader_std_get_pixbuf(tm->tl); | |
9 | 959 if (pixbuf) |
960 { | |
961 const gchar *uri; | |
962 const gchar *mtime_str; | |
963 | |
964 uri = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_URI); | |
965 mtime_str = gdk_pixbuf_get_option(pixbuf, THUMB_MARKER_MTIME); | |
966 | |
967 if (uri && mtime_str && strcmp(uri, tm->source_uri) == 0) | |
968 { | |
969 gchar *pathl; | |
970 | |
971 /* The validation utility abuses ThumbLoader, and we | |
972 * abuse the utility just to load the thumbnail, | |
973 * but the loader needs to look sane for the save to complete. | |
974 */ | |
975 | |
976 tm->tl->cache_enable = TRUE; | |
977 tm->tl->cache_hit = FALSE; | |
978 tm->tl->cache_local = FALSE; | |
979 | |
838 | 980 file_data_unref(tm->tl->fd); |
981 tm->tl->fd = file_data_new_simple(tm->dest); | |
9 | 982 tm->tl->source_mtime = strtol(mtime_str, NULL, 10); |
983 | |
838 | 984 pathl = path_from_utf8(tm->tl->fd->path); |
9 | 985 g_free(tm->tl->thumb_uri); |
986 tm->tl->thumb_uri = g_filename_to_uri(pathl, NULL, NULL); | |
987 tm->tl->local_uri = filename_from_path(tm->tl->thumb_uri); | |
988 g_free(pathl); | |
989 | |
990 g_free(tm->tl->thumb_path); | |
991 tm->tl->thumb_path = NULL; | |
992 tm->tl->thumb_path_local = FALSE; | |
993 | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
994 DEBUG_1("thumb move attempting save:"); |
9 | 995 |
996 thumb_loader_std_save(tm->tl, pixbuf); | |
997 } | |
998 | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
999 DEBUG_1("thumb move unlink: %s", tm->thumb_path); |
9 | 1000 unlink_file(tm->thumb_path); |
864 | 1001 g_object_unref(pixbuf); |
9 | 1002 } |
1003 | |
1004 thumb_std_maint_move_step(tm); | |
1005 } | |
1006 | |
1007 static void thumb_std_maint_move_step(TMaintMove *tm) | |
1008 { | |
1009 const gchar *folder; | |
1010 | |
1011 tm->pass++; | |
1012 if (tm->pass > 2) | |
1013 { | |
1014 g_free(tm->source); | |
1015 g_free(tm->dest); | |
1016 g_free(tm->source_uri); | |
1017 g_free(tm->thumb_path); | |
1018 g_free(tm); | |
1019 | |
1020 if (thumb_std_maint_move_list) | |
1021 { | |
1022 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL); | |
1023 } | |
1024 | |
1025 return; | |
1026 } | |
1027 | |
1028 folder = (tm->pass == 1) ? THUMB_FOLDER_NORMAL : THUMB_FOLDER_LARGE; | |
442 | 1029 |
9 | 1030 g_free(tm->thumb_path); |
1031 tm->thumb_path = thumb_std_cache_path(tm->source, tm->source_uri, FALSE, folder); | |
1032 tm->tl = thumb_loader_std_thumb_file_validate(tm->thumb_path, 0, | |
1033 thumb_std_maint_move_validate_cb, tm); | |
1034 } | |
1035 | |
1036 static gint thumb_std_maint_move_idle(gpointer data) | |
1037 { | |
1038 TMaintMove *tm; | |
1039 gchar *pathl; | |
1040 | |
1041 if (!thumb_std_maint_move_list) return FALSE; | |
1042 | |
1043 tm = thumb_std_maint_move_list->data; | |
1044 | |
1045 thumb_std_maint_move_list = g_list_remove(thumb_std_maint_move_list, tm); | |
1046 if (!thumb_std_maint_move_list) thumb_std_maint_move_tail = NULL; | |
1047 | |
1048 pathl = path_from_utf8(tm->source); | |
1049 tm->source_uri = g_filename_to_uri(pathl, NULL, NULL); | |
1050 g_free(pathl); | |
1051 | |
1052 tm->pass = 0; | |
1053 | |
1054 thumb_std_maint_move_step(tm); | |
1055 | |
1056 return FALSE; | |
1057 } | |
1058 | |
1059 /* This will schedule a move of the thumbnail for source image to dest when idle. | |
1060 * We do this so that file renaming or moving speed is not sacrificed by | |
1061 * moving the thumbnails at the same time because: | |
1062 * | |
1063 * This cache design requires the tedious task of loading the png thumbnails and saving them. | |
1064 * | |
1065 * The thumbnails are processed when the app is idle. If the app | |
1066 * exits early well too bad - they can simply be regenerated from scratch. | |
1067 * | |
1068 * This does not manage local thumbnails (fixme ?) | |
1069 */ | |
1070 void thumb_std_maint_moved(const gchar *source, const gchar *dest) | |
1071 { | |
1072 TMaintMove *tm; | |
1073 | |
1074 tm = g_new0(TMaintMove, 1); | |
1075 tm->source = g_strdup(source); | |
1076 tm->dest = g_strdup(dest); | |
1077 | |
1078 if (!thumb_std_maint_move_list) | |
1079 { | |
1080 g_idle_add_full(G_PRIORITY_LOW, thumb_std_maint_move_idle, NULL, NULL); | |
1081 } | |
1082 | |
827 | 1083 if (thumb_std_maint_move_tail) |
1084 { | |
1085 thumb_std_maint_move_tail = g_list_append(thumb_std_maint_move_tail, tm); | |
1086 thumb_std_maint_move_tail = thumb_std_maint_move_tail->next; | |
1087 } | |
1088 else | |
1089 { | |
1090 thumb_std_maint_move_list = g_list_append(thumb_std_maint_move_list, tm); | |
1091 thumb_std_maint_move_tail = thumb_std_maint_move_list; | |
1092 } | |
9 | 1093 } |
1055
1646720364cf
Adding a vim modeline to all files - patch by Klaus Ethgen
nadvornik
parents:
1036
diff
changeset
|
1094 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ |