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