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