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
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
nautilus-thumbnails.h: Thumbnail code for icon factory.
Copyright (C) 2000, 2001 Eazel, Inc.
Copyright (C) 2002, 2003 Red Hat, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Andy Hertzfeld <andy@eazel.com>
*/
#include <config.h>
#include "nautilus-thumbnails.h"
#include "nautilus-directory-notify.h"
#include "nautilus-global-preferences.h"
#include "nautilus-icon-factory-private.h"
#include "nautilus-icon-factory.h"
#include <math.h>
#include <eel/eel-gdk-pixbuf-extensions.h>
#include <eel/eel-graphic-effects.h>
#include <eel/eel-string.h>
#include <eel/eel-vfs-extensions.h>
#include <gtk/gtkmain.h>
#include <libgnomevfs/gnome-vfs.h>
#include <librsvg/rsvg.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include "nautilus-file-private.h"
/* turn this on to see messages about thumbnail creation */
#if 0
#define DEBUG_THUMBNAILS
#endif
/* Should never be a reasonable actual mtime */
#define INVALID_MTIME 0
/* Cool-off period between last file modification time and thumbnail creation */
#define THUMBNAIL_CREATION_DELAY_SECS 3
static gpointer thumbnail_thread_start (gpointer data);
/* structure used for making thumbnails, associating a uri with where the thumbnail is to be stored */
typedef struct {
char *image_uri;
char *mime_type;
time_t original_file_mtime;
} NautilusThumbnailInfo;
struct NautilusThumbnailAsyncLoadHandle {
EelReadFileHandle *eel_read_handle;
char *file_path;
guint base_size;
guint nominal_size;
gboolean force_nominal;
NautilusThumbnailAsyncLoadFunc load_func;
gpointer load_func_user_data;
};
/*
* Thumbnail thread state.
*/
/* The id of the idle handler used to start the thumbnail thread, or 0 if no
idle handler is currently registered. */
static guint thumbnail_thread_starter_id = 0;
/* Our mutex used when accessing data shared between the main thread and the
thumbnail thread, i.e. the thumbnail_thread_is_running flag and the
thumbnails_to_make list. */
static pthread_mutex_t thumbnails_mutex = PTHREAD_MUTEX_INITIALIZER;
/* A flag to indicate whether a thumbnail thread is running, so we don't
start more than one. Lock thumbnails_mutex when accessing this. */
static volatile gboolean thumbnail_thread_is_running = FALSE;
/* Added in glib 2.14 */
#ifndef G_QUEUE_INIT
#define G_QUEUE_INIT { NULL, NULL, 0 }
#endif
/* The list of NautilusThumbnailInfo structs containing information about the
thumbnails we are making. Lock thumbnails_mutex when accessing this. */
static volatile GQueue thumbnails_to_make = G_QUEUE_INIT;
/* Quickly check if uri is in thumbnails_to_make list */
static GHashTable *thumbnails_to_make_hash = NULL;
/* The currently thumbnailed icon. it also exists in the thumbnails_to_make list
* to avoid adding it again. Lock thumbnails_mutex when accessing this. */
static NautilusThumbnailInfo *currently_thumbnailing = NULL;
static GnomeThumbnailFactory *thumbnail_factory = NULL;
static int thumbnail_icon_size = 0;
static gboolean
get_file_mtime (const char *file_uri, time_t* mtime)
{
GnomeVFSFileInfo *file_info;
/* gather the info and then compare modification times */
file_info = gnome_vfs_file_info_new ();
gnome_vfs_get_file_info (file_uri, file_info, GNOME_VFS_FILE_INFO_FOLLOW_LINKS);
if (file_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_MTIME)
*mtime = file_info->mtime;
else
*mtime = INVALID_MTIME;
gnome_vfs_file_info_unref (file_info);
return TRUE;
}
static void
free_thumbnail_info (NautilusThumbnailInfo *info)
{
g_free (info->image_uri);
g_free (info->mime_type);
g_free (info);
}
/* This function is added as a very low priority idle function to start the
thread to create any needed thumbnails. It is added with a very low priority
so that it doesn't delay showing the directory in the icon/list views.
We want to show the files in the directory as quickly as possible. */
static gboolean
thumbnail_thread_starter_cb (gpointer data)
{
pthread_attr_t thread_attributes;
pthread_t thumbnail_thread;
/* Don't do this in thread, since g_object_ref is not threadsafe */
if (thumbnail_factory == NULL) {
thumbnail_factory = nautilus_icon_factory_get_thumbnail_factory ();
}
/* We create the thread in the detached state, as we don't need/want
to join with it at any point. */
pthread_attr_init (&thread_attributes);
pthread_attr_setdetachstate (&thread_attributes,
PTHREAD_CREATE_DETACHED);
#ifdef _POSIX_THREAD_ATTR_STACKSIZE
pthread_attr_setstacksize (&thread_attributes, 128*1024);
#endif
#ifdef DEBUG_THUMBNAILS
g_message ("(Main Thread) Creating thumbnails thread\n");
#endif
/* We set a flag to indicate the thread is running, so we don't create
a new one. We don't need to lock a mutex here, as the thumbnail
thread isn't running yet. And we know we won't create the thread
twice, as we also check thumbnail_thread_starter_id before
scheduling this idle function. */
thumbnail_thread_is_running = TRUE;
pthread_create (&thumbnail_thread, &thread_attributes,
thumbnail_thread_start, NULL);
thumbnail_thread_starter_id = 0;
return FALSE;
}
void
nautilus_update_thumbnail_file_copied (const char *source_file_uri,
const char *destination_file_uri)
{
char *old_thumbnail_path;
GdkPixbuf *pixbuf;
GnomeVFSFileInfo *file_info;
GnomeThumbnailFactory *factory;
old_thumbnail_path = gnome_thumbnail_path_for_uri (source_file_uri, GNOME_THUMBNAIL_SIZE_NORMAL);
if (old_thumbnail_path != NULL &&
g_file_test (old_thumbnail_path, G_FILE_TEST_EXISTS)) {
file_info = gnome_vfs_file_info_new ();
if (gnome_vfs_get_file_info (destination_file_uri,
file_info,
GNOME_VFS_FILE_INFO_DEFAULT) == GNOME_VFS_OK) {
pixbuf = gdk_pixbuf_new_from_file (old_thumbnail_path, NULL);
if (pixbuf && gnome_thumbnail_has_uri (pixbuf, source_file_uri)) {
factory = nautilus_icon_factory_get_thumbnail_factory ();
gnome_thumbnail_factory_save_thumbnail (factory,
pixbuf,
destination_file_uri,
file_info->mtime);
g_object_unref (factory);
}
if (pixbuf) {
g_object_unref (pixbuf);
}
}
gnome_vfs_file_info_unref (file_info);
}
g_free (old_thumbnail_path);
}
void
nautilus_update_thumbnail_file_renamed (const char *source_file_uri,
const char *destination_file_uri)
{
nautilus_update_thumbnail_file_copied (source_file_uri, destination_file_uri);
nautilus_remove_thumbnail_for_file (source_file_uri);
}
void
nautilus_remove_thumbnail_for_file (const char *file_uri)
{
char *thumbnail_path;
thumbnail_path = gnome_thumbnail_path_for_uri (file_uri, GNOME_THUMBNAIL_SIZE_NORMAL);
if (thumbnail_path != NULL) {
unlink (thumbnail_path);
}
g_free (thumbnail_path);
}
void
nautilus_thumbnail_frame_image (GdkPixbuf **pixbuf)
{
GdkPixbuf *pixbuf_with_frame, *frame;
int left_offset, top_offset, right_offset, bottom_offset;
/* The pixbuf isn't already framed (i.e., it was not made by
* an old Nautilus), so we must embed it in a frame.
*/
frame = nautilus_icon_factory_get_thumbnail_frame ();
if (frame == NULL) {
return;
}
left_offset = 3;
top_offset = 3;
right_offset = 6;
bottom_offset = 6;
pixbuf_with_frame = eel_embed_image_in_frame
(*pixbuf, frame,
left_offset, top_offset, right_offset, bottom_offset);
g_object_unref (*pixbuf);
*pixbuf=pixbuf_with_frame;
}
typedef struct {
gboolean is_thumbnail;
guint base_size;
guint nominal_size;
gboolean force_nominal;
int original_height;
int original_width;
double *scale_x_out;
double *scale_y_out;
} ThumbnailLoadArgs;
static void
thumbnail_loader_size_prepared (GdkPixbufLoader *loader,
int width,
int height,
ThumbnailLoadArgs *args)
{
int size = MAX (width, height);
args->original_width = width;
args->original_height = height;
if (args->force_nominal) {
args->base_size = size;
} else if (args->base_size == 0) {
if (args->is_thumbnail) {
args->base_size = 128 * NAUTILUS_ICON_SIZE_STANDARD / thumbnail_icon_size;
} else {
if (size > args->nominal_size * thumbnail_icon_size / NAUTILUS_ICON_SIZE_STANDARD) {
args->base_size = size * NAUTILUS_ICON_SIZE_STANDARD / thumbnail_icon_size;
} else if (size > NAUTILUS_ICON_SIZE_STANDARD) {
args->base_size = args->nominal_size;
} else {
/* Don't scale up small icons */
args->base_size = NAUTILUS_ICON_SIZE_STANDARD;
}
}
}
if (args->base_size != args->nominal_size) {
double scale;
scale = (double) args->nominal_size / args->base_size;
if ((int) (width * scale) > NAUTILUS_ICON_MAXIMUM_SIZE ||
(int) (height * scale) > NAUTILUS_ICON_MAXIMUM_SIZE) {
scale = MIN ((double) NAUTILUS_ICON_MAXIMUM_SIZE / width,
(double) NAUTILUS_ICON_MAXIMUM_SIZE / height);
}
width = MAX (1, floor (width * scale + 0.5));
height = MAX (1, floor (height * scale + 0.5));
gdk_pixbuf_loader_set_size (loader, width, height);
}
}
static void
thumbnail_loader_area_prepared (GdkPixbufLoader *loader,
ThumbnailLoadArgs *args)
{
GdkPixbuf *pixbuf;
pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
*args->scale_x_out = (double) gdk_pixbuf_get_width (pixbuf) / args->original_width;
*args->scale_y_out = (double) gdk_pixbuf_get_height (pixbuf) / args->original_height;
}
static GdkPixbuf *
get_pixbuf_from_data (const unsigned char *buffer,
gsize buflen,
const char *path,
guint base_size,
guint nominal_size,
gboolean force_nominal,
double *scale_x_out,
double *scale_y_out)
{
GdkPixbufLoader *loader;
GdkPixbuf *pixbuf;
ThumbnailLoadArgs args;
GError *error;
if (thumbnail_icon_size == 0) {
eel_preferences_add_auto_integer (NAUTILUS_PREFERENCES_ICON_VIEW_THUMBNAIL_SIZE,
&thumbnail_icon_size);
}
loader = gdk_pixbuf_loader_new ();
g_signal_connect (loader, "size-prepared",
G_CALLBACK (thumbnail_loader_size_prepared),
&args);
g_signal_connect (loader, "area-prepared",
G_CALLBACK (thumbnail_loader_area_prepared),
&args);
args.is_thumbnail = strstr (path, "/.thumbnails/") != NULL;
args.base_size = base_size;
args.nominal_size = nominal_size;
args.force_nominal = force_nominal;
args.scale_x_out = scale_x_out;
args.scale_y_out = scale_y_out;
error = NULL;
if (!gdk_pixbuf_loader_write (loader, buffer, buflen, &error)) {
g_message ("Failed to write %s to thumbnail pixbuf loader: %s", path, error->message);
gdk_pixbuf_loader_close (loader, NULL);
g_object_unref (G_OBJECT (loader));
g_error_free (error);
return NULL;
}
error = NULL;
if (!gdk_pixbuf_loader_close (loader, &error) ||
/* Seems we have to check this even if it returned TRUE (#403255) */
error != NULL) {
g_message ("Failed to close thumbnail pixbuf loader for %s: %s", path, error->message);
g_object_unref (G_OBJECT (loader));
g_error_free (error);
return NULL;
}
pixbuf = g_object_ref (gdk_pixbuf_loader_get_pixbuf (loader));
g_object_unref (G_OBJECT (loader));
return pixbuf;
}
/* routine to load an image from the passed-in path
*/
GdkPixbuf *
nautilus_thumbnail_load_image (const char *path,
guint base_size,
guint nominal_size,
gboolean force_nominal,
double *scale_x_out,
double *scale_y_out)
{
GdkPixbuf *pixbuf;
guchar *buffer;
gsize buflen;
GError *error;
error = NULL;
if (!g_file_get_contents (path, (gchar **) &buffer, &buflen, &error)) {
g_message ("Failed to load %s into memory: %s", path, error->message);
g_error_free (error);
return NULL;
}
pixbuf = get_pixbuf_from_data (buffer, buflen, path,
base_size, nominal_size, force_nominal,
scale_x_out, scale_y_out);
g_free (buffer);
return pixbuf;
}
static void
async_thumbnail_read_image (GnomeVFSResult result,
GnomeVFSFileSize file_size,
char *file_contents,
gpointer callback_data)
{
GdkPixbuf *pixbuf;
double scale_x, scale_y;
NautilusThumbnailAsyncLoadHandle *handle = callback_data;
pixbuf = NULL;
scale_x = scale_y = 1.0;
if (result == GNOME_VFS_OK) {
pixbuf = get_pixbuf_from_data (file_contents, file_size,
handle->file_path,
handle->base_size,
handle->nominal_size,
handle->force_nominal,
&scale_x, &scale_y);
}
handle->load_func (handle,
handle->file_path,
pixbuf, scale_x, scale_y,
handle->load_func_user_data);
gdk_pixbuf_unref (pixbuf);
g_free (handle->file_path);
g_free (handle);
}
NautilusThumbnailAsyncLoadHandle *
nautilus_thumbnail_load_image_async (const char *path,
guint base_size,
guint nominal_size,
gboolean force_nominal,
NautilusThumbnailAsyncLoadFunc load_func,
gpointer load_func_user_data)
{
NautilusThumbnailAsyncLoadHandle *handle;
char *uri;
uri = gnome_vfs_get_uri_from_local_path (path);
if (uri == NULL) {
return NULL;
}
handle = g_new (NautilusThumbnailAsyncLoadHandle, 1);
handle->eel_read_handle =
eel_read_entire_file_async (uri, GNOME_VFS_PRIORITY_DEFAULT,
(EelReadFileCallback) async_thumbnail_read_image,
handle);
handle->file_path = g_strdup (path);
handle->base_size = base_size;
handle->nominal_size = nominal_size;
handle->force_nominal = force_nominal;
handle->load_func = load_func;
handle->load_func_user_data = load_func_user_data;
g_free (uri);
return handle;
}
void
nautilus_thumbnail_load_image_cancel (NautilusThumbnailAsyncLoadHandle *handle)
{
g_assert (handle != NULL);
eel_read_file_cancel (handle->eel_read_handle);
g_free (handle->file_path);
g_free (handle);
}
void
nautilus_thumbnail_remove_from_queue (const char *file_uri)
{
GList *node;
#ifdef DEBUG_THUMBNAILS
g_message ("(Remove from queue) Locking mutex\n");
#endif
pthread_mutex_lock (&thumbnails_mutex);
/*********************************
* MUTEX LOCKED
*********************************/
if (thumbnails_to_make_hash) {
node = g_hash_table_lookup (thumbnails_to_make_hash, file_uri);
if (node && node->data != currently_thumbnailing) {
g_hash_table_remove (thumbnails_to_make_hash, file_uri);
free_thumbnail_info (node->data);
g_queue_delete_link ((GQueue *)&thumbnails_to_make, node);
}
}
/*********************************
* MUTEX UNLOCKED
*********************************/
#ifdef DEBUG_THUMBNAILS
g_message ("(Remove from queue) Unlocking mutex\n");
#endif
pthread_mutex_unlock (&thumbnails_mutex);
}
void
nautilus_thumbnail_remove_all_from_queue (void)
{
NautilusThumbnailInfo *info;
GList *l, *next;
#ifdef DEBUG_THUMBNAILS
g_message ("(Remove all from queue) Locking mutex\n");
#endif
pthread_mutex_lock (&thumbnails_mutex);
/*********************************
* MUTEX LOCKED
*********************************/
l = thumbnails_to_make.head;
while (l != NULL) {
info = l->data;
next = l->next;
if (info != currently_thumbnailing) {
g_hash_table_remove (thumbnails_to_make_hash,
info->image_uri);
free_thumbnail_info (info);
g_queue_delete_link ((GQueue *)&thumbnails_to_make, l);
}
l = next;
}
/*********************************
* MUTEX UNLOCKED
*********************************/
#ifdef DEBUG_THUMBNAILS
g_message ("(Remove all from queue) Unlocking mutex\n");
#endif
pthread_mutex_unlock (&thumbnails_mutex);
}
void
nautilus_thumbnail_prioritize (const char *file_uri)
{
GList *node;
#ifdef DEBUG_THUMBNAILS
g_message ("(Prioritize) Locking mutex\n");
#endif
pthread_mutex_lock (&thumbnails_mutex);
/*********************************
* MUTEX LOCKED
*********************************/
if (thumbnails_to_make_hash) {
node = g_hash_table_lookup (thumbnails_to_make_hash, file_uri);
if (node && node->data != currently_thumbnailing) {
g_queue_unlink ((GQueue *)&thumbnails_to_make, node);
g_queue_push_head_link ((GQueue *)&thumbnails_to_make, node);
}
}
/*********************************
* MUTEX UNLOCKED
*********************************/
#ifdef DEBUG_THUMBNAILS
g_message ("(Prioritize) Unlocking mutex\n");
#endif
pthread_mutex_unlock (&thumbnails_mutex);
}
/***************************************************************************
* Thumbnail Thread Functions.
***************************************************************************/
/* This is a one-shot idle callback called from the main loop to call
notify_file_changed() for a thumbnail. It frees the uri afterwards.
We do this in an idle callback as I don't think nautilus_file_changed() is
thread-safe. */
static gboolean
thumbnail_thread_notify_file_changed (gpointer image_uri)
{
NautilusFile *file;
GDK_THREADS_ENTER ();
file = nautilus_file_get ((char *) image_uri);
#ifdef DEBUG_THUMBNAILS
g_message ("(Thumbnail Thread) Notifying file changed file:%p uri: %s\n", file, (char*) image_uri);
#endif
if (file != NULL) {
nautilus_file_set_is_thumbnailing (file, FALSE);
nautilus_file_changed (file);
nautilus_file_unref (file);
}
g_free (image_uri);
GDK_THREADS_LEAVE ();
return FALSE;
}
void
nautilus_create_thumbnail (NautilusFile *file)
{
time_t file_mtime = 0;
NautilusThumbnailInfo *info;
NautilusThumbnailInfo *existing_info;
GList *existing, *node;
nautilus_file_set_is_thumbnailing (file, TRUE);
info = g_new0 (NautilusThumbnailInfo, 1);
info->image_uri = nautilus_file_get_uri (file);
info->mime_type = nautilus_file_get_mime_type (file);
/* Hopefully the NautilusFile will already have the image file mtime,
so we can just use that. Otherwise we have to get it ourselves. */
if (file->details->info
&& file->details->file_info_is_up_to_date
&& file->details->info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_MTIME) {
file_mtime = file->details->info->mtime;
} else {
get_file_mtime (info->image_uri, &file_mtime);
}
info->original_file_mtime = file_mtime;
#ifdef DEBUG_THUMBNAILS
g_message ("(Main Thread) Locking mutex\n");
#endif
pthread_mutex_lock (&thumbnails_mutex);
/*********************************
* MUTEX LOCKED
*********************************/
if (thumbnails_to_make_hash == NULL) {
thumbnails_to_make_hash = g_hash_table_new (g_str_hash,
g_str_equal);
}
/* Check if it is already in the list of thumbnails to make. */
existing = g_hash_table_lookup (thumbnails_to_make_hash, info->image_uri);
if (existing == NULL) {
/* Add the thumbnail to the list. */
#ifdef DEBUG_THUMBNAILS
g_message ("(Main Thread) Adding thumbnail: %s\n",
info->image_uri);
#endif
g_queue_push_tail ((GQueue *)&thumbnails_to_make, info);
node = g_queue_peek_tail_link ((GQueue *)&thumbnails_to_make);
g_hash_table_insert (thumbnails_to_make_hash,
info->image_uri,
node);
/* If the thumbnail thread isn't running, and we haven't
scheduled an idle function to start it up, do that now.
We don't want to start it until all the other work is done,
so the GUI will be updated as quickly as possible.*/
if (thumbnail_thread_is_running == FALSE &&
thumbnail_thread_starter_id == 0) {
thumbnail_thread_starter_id = g_idle_add_full (G_PRIORITY_LOW, thumbnail_thread_starter_cb, NULL, NULL);
}
} else {
#ifdef DEBUG_THUMBNAILS
g_message ("(Main Thread) Updating non-current mtime: %s\n",
info->image_uri);
#endif
/* The file in the queue might need a new original mtime */
existing_info = existing->data;
existing_info->original_file_mtime = info->original_file_mtime;
free_thumbnail_info (info);
}
/*********************************
* MUTEX UNLOCKED
*********************************/
#ifdef DEBUG_THUMBNAILS
g_message ("(Main Thread) Unlocking mutex\n");
#endif
pthread_mutex_unlock (&thumbnails_mutex);
}
/* thumbnail_thread is invoked as a separate thread to to make thumbnails. */
static gpointer
thumbnail_thread_start (gpointer data)
{
NautilusThumbnailInfo *info = NULL;
GdkPixbuf *pixbuf;
time_t current_orig_mtime = 0;
time_t current_time;
GList *node;
/* We loop until there are no more thumbails to make, at which point
we exit the thread. */
for (;;) {
#ifdef DEBUG_THUMBNAILS
g_message ("(Thumbnail Thread) Locking mutex\n");
#endif
pthread_mutex_lock (&thumbnails_mutex);
/*********************************
* MUTEX LOCKED
*********************************/
/* Pop the last thumbnail we just made off the head of the
list and free it. I did this here so we only have to lock
the mutex once per thumbnail, rather than once before
creating it and once after.
Don't pop the thumbnail off the queue if the original file
mtime of the request changed. Then we need to redo the thumbnail.
*/
if (currently_thumbnailing &&
currently_thumbnailing->original_file_mtime == current_orig_mtime) {
g_assert (info == currently_thumbnailing);
node = g_hash_table_lookup (thumbnails_to_make_hash, info->image_uri);
g_assert (node != NULL);
g_hash_table_remove (thumbnails_to_make_hash, info->image_uri);
free_thumbnail_info (info);
g_queue_delete_link ((GQueue *)&thumbnails_to_make, node);
}
currently_thumbnailing = NULL;
/* If there are no more thumbnails to make, reset the
thumbnail_thread_is_running flag, unlock the mutex, and
exit the thread. */
if (g_queue_is_empty ((GQueue *)&thumbnails_to_make)) {
#ifdef DEBUG_THUMBNAILS
g_message ("(Thumbnail Thread) Exiting\n");
#endif
thumbnail_thread_is_running = FALSE;
pthread_mutex_unlock (&thumbnails_mutex);
pthread_exit (NULL);
}
/* Get the next one to make. We leave it on the list until it
is created so the main thread doesn't add it again while we
are creating it. */
info = g_queue_peek_head ((GQueue *)&thumbnails_to_make);
currently_thumbnailing = info;
current_orig_mtime = info->original_file_mtime;
/*********************************
* MUTEX UNLOCKED
*********************************/
#ifdef DEBUG_THUMBNAILS
g_message ("(Thumbnail Thread) Unlocking mutex\n");
#endif
pthread_mutex_unlock (&thumbnails_mutex);
time (¤t_time);
/* Don't try to create a thumbnail if the file was modified recently.
This prevents constant re-thumbnailing of changing files. */
if (current_time < current_orig_mtime + THUMBNAIL_CREATION_DELAY_SECS &&
current_time >= current_orig_mtime) {
#ifdef DEBUG_THUMBNAILS
g_message ("(Thumbnail Thread) Skipping: %s\n",
info->image_uri);
#endif
/* Reschedule thumbnailing via a change notification */
g_timeout_add (1000, thumbnail_thread_notify_file_changed,
g_strdup (info->image_uri));
continue;
}
/* Create the thumbnail. */
#ifdef DEBUG_THUMBNAILS
g_message ("(Thumbnail Thread) Creating thumbnail: %s\n",
info->image_uri);
#endif
pixbuf = gnome_thumbnail_factory_generate_thumbnail (thumbnail_factory,
info->image_uri,
info->mime_type);
if (pixbuf) {
gnome_thumbnail_factory_save_thumbnail (thumbnail_factory,
pixbuf,
info->image_uri,
current_orig_mtime);
g_object_unref (pixbuf);
} else {
gnome_thumbnail_factory_create_failed_thumbnail (thumbnail_factory,
info->image_uri,
current_orig_mtime);
}
/* We need to call nautilus_file_changed(), but I don't think that is
thread safe. So add an idle handler and do it from the main loop. */
g_idle_add_full (G_PRIORITY_HIGH_IDLE,
thumbnail_thread_notify_file_changed,
g_strdup (info->image_uri), NULL);
}
}
|