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