summaryrefslogtreecommitdiff
path: root/src/eom-list-store.c
blob: d60df916fffe5788d15af9d764453f0e3de7b754 (plain)
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
/* Eye Of Mate - Image Store
 *
 * Copyright (C) 2006-2008 The Free Software Foundation
 *
 * Author: Claudio Saavedra <csaavedra@gnome.org>
 *
 * Based on code by: Jens Finke <jens@triq.net>
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "eom-list-store.h"
#include "eom-thumbnail.h"
#include "eom-image.h"
#include "eom-job-queue.h"
#include "eom-jobs.h"

#include <string.h>

#define EOM_LIST_STORE_GET_PRIVATE(object) \
	(G_TYPE_INSTANCE_GET_PRIVATE ((object), EOM_TYPE_LIST_STORE, EomListStorePrivate))

G_DEFINE_TYPE (EomListStore, eom_list_store, GTK_TYPE_LIST_STORE);

struct _EomListStorePrivate {
	GList *monitors;          /* Monitors for the directories */
	gint initial_image;       /* The image that should be selected firstly by the view. */
	GdkPixbuf *busy_image;    /* Loading image icon */
	GdkPixbuf *missing_image; /* Missing image icon */
	GMutex mutex;            /* Mutex for saving the jobs in the model */
};

static void
eom_list_store_finalize (GObject *object)
{
	EomListStore *store = EOM_LIST_STORE (object);

	if (store->priv != NULL) {
		g_free (store->priv);
		store->priv = NULL;
	}

	G_OBJECT_CLASS (eom_list_store_parent_class)->finalize (object);
}

static void
foreach_monitors_free (gpointer data, gpointer user_data)
{
	g_file_monitor_cancel (G_FILE_MONITOR (data));
}

static void
eom_list_store_dispose (GObject *object)
{
	EomListStore *store = EOM_LIST_STORE (object);

	g_list_foreach (store->priv->monitors,
			foreach_monitors_free, NULL);

	g_list_free (store->priv->monitors);

	store->priv->monitors = NULL;

	if(store->priv->busy_image != NULL) {
		g_object_unref (store->priv->busy_image);
		store->priv->busy_image = NULL;
	}

	if(store->priv->missing_image != NULL) {
		g_object_unref (store->priv->missing_image);
		store->priv->missing_image = NULL;
	}

	g_mutex_clear (&store->priv->mutex);

	G_OBJECT_CLASS (eom_list_store_parent_class)->dispose (object);
}

static void
eom_list_store_class_init (EomListStoreClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = eom_list_store_finalize;
	object_class->dispose = eom_list_store_dispose;

	g_type_class_add_private (object_class, sizeof (EomListStorePrivate));
}

/*
   Sorting functions
*/

static gint
eom_list_store_compare_func (GtkTreeModel *model,
			     GtkTreeIter *a,
			     GtkTreeIter *b,
			     gpointer user_data)
{
	gint r_value;

	EomImage *image_a, *image_b;

	gtk_tree_model_get (model, a,
			    EOM_LIST_STORE_EOM_IMAGE, &image_a,
			    -1);

	gtk_tree_model_get (model, b,
			    EOM_LIST_STORE_EOM_IMAGE, &image_b,
			    -1);

	r_value = strcmp (eom_image_get_collate_key (image_a),
			  eom_image_get_collate_key (image_b));

	g_object_unref (G_OBJECT (image_a));
	g_object_unref (G_OBJECT (image_b));

	return r_value;
}

static GdkPixbuf *
eom_list_store_get_icon (const gchar *icon_name)
{
	GError *error = NULL;
	GtkIconTheme *icon_theme;
	GdkPixbuf *pixbuf;

	icon_theme = gtk_icon_theme_get_default ();

	pixbuf = gtk_icon_theme_load_icon (icon_theme,
					   icon_name,
					   EOM_LIST_STORE_THUMB_SIZE,
					   0,
					   &error);

	if (!pixbuf) {
		g_warning ("Couldn't load icon: %s", error->message);
		g_error_free (error);
	}

	return pixbuf;
}

static void
eom_list_store_init (EomListStore *self)
{
	GType types[EOM_LIST_STORE_NUM_COLUMNS];

	types[EOM_LIST_STORE_THUMBNAIL] = GDK_TYPE_PIXBUF;
	types[EOM_LIST_STORE_EOM_IMAGE] = G_TYPE_OBJECT;
	types[EOM_LIST_STORE_THUMB_SET] = G_TYPE_BOOLEAN;
	types[EOM_LIST_STORE_EOM_JOB]   = G_TYPE_POINTER;

	gtk_list_store_set_column_types (GTK_LIST_STORE (self),
					 EOM_LIST_STORE_NUM_COLUMNS, types);

	self->priv = EOM_LIST_STORE_GET_PRIVATE (self);

	self->priv->monitors = NULL;
	self->priv->initial_image = -1;

	self->priv->busy_image = eom_list_store_get_icon ("image-loading");
	self->priv->missing_image = eom_list_store_get_icon ("image-missing");

	g_mutex_init (&self->priv->mutex);

	gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (self),
						 eom_list_store_compare_func,
						 NULL, NULL);

	gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self),
					      GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
					      GTK_SORT_ASCENDING);
}

/**
 * eom_list_store_new:
 *
 * Creates a new and empty #EomListStore.
 *
 * Returns: a newly created #EomListStore.
 **/
GtkListStore*
eom_list_store_new (void)
{
        return g_object_new (EOM_TYPE_LIST_STORE, NULL);
}

/*
   Searchs for a file in the store. If found and @iter_found is not NULL,
   then sets @iter_found to a #GtkTreeIter pointing to the file.
 */
static gboolean
is_file_in_list_store (EomListStore *store,
		       const gchar *info_uri,
		       GtkTreeIter *iter_found)
{
	gboolean found = FALSE;
	EomImage *image;
	GFile *file;
	gchar *str;
	GtkTreeIter iter;

	if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter)) {
		return FALSE;
	}

	do {
		gtk_tree_model_get (GTK_TREE_MODEL (store), &iter,
				    EOM_LIST_STORE_EOM_IMAGE, &image,
				    -1);
		if (!image)
			continue;

		file = eom_image_get_file (image);
		str = g_file_get_uri (file);

		found = (strcmp (str, info_uri) == 0)? TRUE : FALSE;

		g_object_unref (file);
		g_free (str);
		g_object_unref (G_OBJECT (image));

	} while (!found &&
		 gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter));

	if (found && iter_found != NULL) {
		*iter_found = iter;
	}

	return found;
}

static gboolean
is_file_in_list_store_file (EomListStore *store,
			   GFile *file,
			   GtkTreeIter *iter_found)
{
	gchar *uri_str;
	gboolean result;

	uri_str = g_file_get_uri (file);

	result = is_file_in_list_store (store, uri_str, iter_found);

	g_free (uri_str);

	return result;
}

static void
eom_job_thumbnail_cb (EomJobThumbnail *job, gpointer data)
{
	EomListStore *store;
	GtkTreeIter iter;
	EomImage *image;
	GdkPixbuf *thumbnail;
	GFile *file;

	g_return_if_fail (EOM_IS_LIST_STORE (data));

	store = EOM_LIST_STORE (data);

	file = eom_image_get_file (job->image);

	if (is_file_in_list_store_file (store, file, &iter)) {
		gtk_tree_model_get (GTK_TREE_MODEL (store), &iter,
				    EOM_LIST_STORE_EOM_IMAGE, &image,
				    -1);

		if (job->thumbnail) {
			eom_image_set_thumbnail (image, job->thumbnail);

			/* Getting the thumbnail, in case it needed
 			 * transformations */
			thumbnail = eom_image_get_thumbnail (image);
		} else {
			thumbnail = g_object_ref (store->priv->missing_image);
		}

		gtk_list_store_set (GTK_LIST_STORE (store), &iter,
				    EOM_LIST_STORE_THUMBNAIL, thumbnail,
				    EOM_LIST_STORE_THUMB_SET, TRUE,
				    EOM_LIST_STORE_EOM_JOB, NULL,
				    -1);

		g_object_unref (thumbnail);
	}

	g_object_unref (file);
}

static void
on_image_changed (EomImage *image, EomListStore *store)
{
	GtkTreePath *path;
	GtkTreeIter iter;
	gint pos;

	pos = eom_list_store_get_pos_by_image (store, image);
	path = gtk_tree_path_new_from_indices (pos, -1);

	gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &iter, path);
	eom_list_store_thumbnail_refresh (store, &iter);
	gtk_tree_path_free (path);
}

/**
 * eom_list_store_remove:
 * @store: An #EomListStore.
 * @iter: A #GtkTreeIter.
 *
 * Removes the image pointed by @iter from @store.
 **/
static void
eom_list_store_remove (EomListStore *store, GtkTreeIter *iter)
{
	EomImage *image;

	gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
			    EOM_LIST_STORE_EOM_IMAGE, &image,
			    -1);

	g_signal_handlers_disconnect_by_func (image, on_image_changed, store);
	g_object_unref (image);

	gtk_list_store_remove (GTK_LIST_STORE (store), iter);
}

/**
 * eom_list_store_append_image:
 * @store: An #EomListStore.
 * @image: An #EomImage.
 *
 * Adds an #EomImage to @store. The thumbnail of the image is not
 * loaded and will only be loaded if the thumbnail is made visible
 * or eom_list_store_set_thumbnail() is called.
 *
 **/
void
eom_list_store_append_image (EomListStore *store, EomImage *image)
{
	GtkTreeIter iter;

	g_signal_connect (image, "changed",
 			  G_CALLBACK (on_image_changed),
 			  store);

	gtk_list_store_append (GTK_LIST_STORE (store), &iter);
	gtk_list_store_set (GTK_LIST_STORE (store), &iter,
			    EOM_LIST_STORE_EOM_IMAGE, image,
			    EOM_LIST_STORE_THUMBNAIL, store->priv->busy_image,
			    EOM_LIST_STORE_THUMB_SET, FALSE,
			    -1);
}

static void
eom_list_store_append_image_from_file (EomListStore *store,
				       GFile *file)
{
	EomImage *image;

	g_return_if_fail (EOM_IS_LIST_STORE (store));

	image = eom_image_new_file (file);

	eom_list_store_append_image (store, image);
}

static void
file_monitor_changed_cb (GFileMonitor *monitor,
			 GFile *file,
			 GFile *other_file,
			 GFileMonitorEvent event,
			 EomListStore *store)
{
	const char *mimetype;
	GFileInfo *file_info;
	GtkTreeIter iter;
	EomImage *image;

	switch (event) {
	case G_FILE_MONITOR_EVENT_CHANGED:
		file_info = g_file_query_info (file,
					       G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
					       0, NULL, NULL);
		if (file_info == NULL) {
			break;
		}
		mimetype = g_file_info_get_content_type (file_info);

		if (is_file_in_list_store_file (store, file, &iter)) {
			if (eom_image_is_supported_mime_type (mimetype)) {
				gtk_tree_model_get (GTK_TREE_MODEL (store), &iter,
						    EOM_LIST_STORE_EOM_IMAGE, &image,
						    -1);
				eom_image_file_changed (image);
				g_object_unref (image);
				eom_list_store_thumbnail_refresh (store, &iter);
			} else {
				eom_list_store_remove (store, &iter);
			}
		} else {
			if (eom_image_is_supported_mime_type (mimetype)) {
				eom_list_store_append_image_from_file (store, file);
			}
		}
		g_object_unref (file_info);
		break;
	case G_FILE_MONITOR_EVENT_DELETED:
		if (is_file_in_list_store_file (store, file, &iter)) {
			EomImage *image;

			gtk_tree_model_get (GTK_TREE_MODEL (store), &iter,
					    EOM_LIST_STORE_EOM_IMAGE, &image,
					    -1);

			eom_list_store_remove (store, &iter);
		}
		break;
	case G_FILE_MONITOR_EVENT_CREATED:
		if (!is_file_in_list_store_file (store, file, NULL)) {
			file_info = g_file_query_info (file,
						       G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
						       0, NULL, NULL);
			if (file_info == NULL) {
				break;
			}
			mimetype = g_file_info_get_content_type (file_info);

			if (eom_image_is_supported_mime_type (mimetype)) {
				eom_list_store_append_image_from_file (store, file);
			}
			g_object_unref (file_info);
		}
		break;
	case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
		file_info = g_file_query_info (file,
					       G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
					       0, NULL, NULL);
		if (file_info == NULL) {
			break;
		}
		mimetype = g_file_info_get_content_type (file_info);
		if (is_file_in_list_store_file (store, file, &iter) &&
		    eom_image_is_supported_mime_type (mimetype)) {
			eom_list_store_thumbnail_refresh (store, &iter);
		}
		g_object_unref (file_info);
		break;
	default:
		break;
	}
}

/*
 * Called for each file in a directory. Checks if the file is some
 * sort of image. If so, it creates an image object and adds it to the
 * list.
 */
static void
directory_visit (GFile *directory,
		 GFileInfo *children_info,
		 EomListStore *store)
{
	GFile *child;
	gboolean load_uri = FALSE;
	const char *mime_type, *name;

	mime_type = g_file_info_get_content_type (children_info);
	name = g_file_info_get_name (children_info);

        if (!g_str_has_prefix (name, ".")) {
		if (eom_image_is_supported_mime_type (mime_type)) {
			load_uri = TRUE;
		}
	}

	if (load_uri) {
		child = g_file_get_child (directory, name);
		eom_list_store_append_image_from_file (store, child);
	}
}

static void
eom_list_store_append_directory (EomListStore *store,
				 GFile *file,
				 GFileType file_type)
{
	GFileMonitor *file_monitor;
	GFileEnumerator *file_enumerator;
	GFileInfo *file_info;

	g_return_if_fail (file_type == G_FILE_TYPE_DIRECTORY);

	file_monitor = g_file_monitor_directory (file,
						 0, NULL, NULL);

	if (file_monitor != NULL) {
		g_signal_connect (file_monitor, "changed",
				  G_CALLBACK (file_monitor_changed_cb), store);

		/* prepend seems more efficient to me, we don't need this list
		   to be sorted */
		store->priv->monitors = g_list_prepend (store->priv->monitors, file_monitor);
	}

	file_enumerator = g_file_enumerate_children (file,
						     G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
						     G_FILE_ATTRIBUTE_STANDARD_NAME,
						     0, NULL, NULL);
	file_info = g_file_enumerator_next_file (file_enumerator, NULL, NULL);

	while (file_info != NULL)
	{
		directory_visit (file, file_info, store);
		g_object_unref (file_info);
		file_info = g_file_enumerator_next_file (file_enumerator, NULL, NULL);
	}
	g_object_unref (file_enumerator);
}

/**
 * eom_list_store_add_files:
 * @store: An #EomListStore.
 * @file_list: (element-type GFile): A %NULL-terminated list of #GFile's.
 *
 * Adds a list of #GFile's to @store. The given list
 * must be %NULL-terminated.
 *
 * If any of the #GFile's in @file_list is a directory, all the images
 * in that directory will be added to @store. If the list of files contains
 * only one file and this is a regular file, then all the images in the same
 * directory will be added as well to @store.
 *
 **/
void
eom_list_store_add_files (EomListStore *store, GList *file_list)
{
	GList *it;
	GFileInfo *file_info;
	GFileType file_type;
	GFile *initial_file = NULL;
	GtkTreeIter iter;

	if (file_list == NULL) {
		return;
	}

	gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
					      GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID,
					      GTK_SORT_ASCENDING);

	for (it = file_list; it != NULL; it = it->next) {
		GFile *file = (GFile *) it->data;

		file_info = g_file_query_info (file,
					       G_FILE_ATTRIBUTE_STANDARD_TYPE","
					       G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
					       0, NULL, NULL);
		if (file_info == NULL) {
			continue;
		}
		file_type = g_file_info_get_file_type (file_info);

		/* Workaround for gvfs backends that don't set the GFileType. */
		if (G_UNLIKELY (file_type == G_FILE_TYPE_UNKNOWN)) {
			const gchar *ctype;

			ctype = g_file_info_get_content_type (file_info);

			/* If the content type is supported adjust file_type */
			if (eom_image_is_supported_mime_type (ctype))
				file_type = G_FILE_TYPE_REGULAR;
		}

		g_object_unref (file_info);

		if (file_type == G_FILE_TYPE_DIRECTORY) {
			eom_list_store_append_directory (store, file, file_type);
		} else if (file_type == G_FILE_TYPE_REGULAR &&
			   g_list_length (file_list) == 1) {

			initial_file = g_file_dup (file);

			file = g_file_get_parent (file);
			file_info = g_file_query_info (file,
						       G_FILE_ATTRIBUTE_STANDARD_TYPE,
						       0, NULL, NULL);

			/* If we can't get a file_info,
			   file_type will stay as G_FILE_TYPE_REGULAR */
			if (file_info != NULL) {
				file_type = g_file_info_get_file_type (file_info);
				g_object_unref (file_info);
			}

			if (file_type == G_FILE_TYPE_DIRECTORY) {
				eom_list_store_append_directory (store, file, file_type);

				if (!is_file_in_list_store_file (store,
								 initial_file,
								 &iter)) {
					eom_list_store_append_image_from_file (store, initial_file);
				}
			} else {
				eom_list_store_append_image_from_file (store, initial_file);
			}
			g_object_unref (file);
		} else if (file_type == G_FILE_TYPE_REGULAR &&
			   g_list_length (file_list) > 1) {
			eom_list_store_append_image_from_file (store, file);
		}
	}

	gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
					      GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
					      GTK_SORT_ASCENDING);

	if (initial_file &&
	    is_file_in_list_store_file (store, initial_file, &iter)) {
		store->priv->initial_image = eom_list_store_get_pos_by_iter (store, &iter);
		g_object_unref (initial_file);
	} else {
		store->priv->initial_image = 0;
	}
}

/**
 * eom_list_store_remove_image:
 * @store: An #EomListStore.
 * @image: An #EomImage.
 *
 * Removes @image from @store.
 **/
void
eom_list_store_remove_image (EomListStore *store, EomImage *image)
{
	GtkTreeIter iter;
	GFile *file;

	g_return_if_fail (EOM_IS_LIST_STORE (store));
	g_return_if_fail (EOM_IS_IMAGE (image));

	file = eom_image_get_file (image);

	if (is_file_in_list_store_file (store, file, &iter)) {
		eom_list_store_remove (store, &iter);
	}
	g_object_unref (file);
}

/**
 * eom_list_store_new_from_glist:
 * @list: (element-type EomImage): a %NULL-terminated list of #EomImage's.
 *
 * Creates a new #EomListStore from a list of #EomImage's.
 * The given list must be %NULL-terminated.
 *
 * Returns: a new #EomListStore.
 **/
GtkListStore *
eom_list_store_new_from_glist (GList *list)
{
	GList *it;

	GtkListStore *store = eom_list_store_new ();

	for (it = list; it != NULL; it = it->next) {
		eom_list_store_append_image (EOM_LIST_STORE (store),
					     EOM_IMAGE (it->data));
	}

	return store;
}

/**
 * eom_list_store_get_pos_by_image:
 * @store: An #EomListStore.
 * @image: An #EomImage.
 *
 * Gets the position where @image is stored in @store. If @image
 * is not stored in @store, -1 is returned.
 *
 * Returns: the position of @image in @store or -1 if not found.
 **/
gint
eom_list_store_get_pos_by_image (EomListStore *store, EomImage *image)
{
	GtkTreeIter iter;
	gint pos = -1;
	GFile *file;

	g_return_val_if_fail (EOM_IS_LIST_STORE (store), -1);
	g_return_val_if_fail (EOM_IS_IMAGE (image), -1);

	file = eom_image_get_file (image);

	if (is_file_in_list_store_file (store, file, &iter)) {
		pos = eom_list_store_get_pos_by_iter (store, &iter);
	}

	g_object_unref (file);
	return pos;
}

/**
 * eom_list_store_get_image_by_pos:
 * @store: An #EomListStore.
 * @pos: the position of the required #EomImage.
 *
 * Gets the #EomImage in the position @pos of @store. If there is
 * no image at position @pos, %NULL is returned.
 *
 * Returns: (transfer full): the #EomImage in position @pos or %NULL.
 *
 **/
EomImage *
eom_list_store_get_image_by_pos (EomListStore *store, gint pos)
{
	EomImage *image = NULL;
	GtkTreeIter iter;

	g_return_val_if_fail (EOM_IS_LIST_STORE (store), NULL);

	if (gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (store), &iter, NULL, pos)) {
		gtk_tree_model_get (GTK_TREE_MODEL (store), &iter,
				    EOM_LIST_STORE_EOM_IMAGE, &image,
				    -1);
	}

	return image;
}

/**
 * eom_list_store_get_pos_by_iter:
 * @store: An #EomListStore.
 * @iter: A #GtkTreeIter pointing to an image in @store.
 *
 * Gets the position of the image pointed by @iter.
 *
 * Returns: The position of the image pointed by @iter.
 **/
gint
eom_list_store_get_pos_by_iter (EomListStore *store,
				GtkTreeIter *iter)
{
	gint *indices;
	GtkTreePath *path;
	gint pos;

	path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), iter);
	indices = gtk_tree_path_get_indices (path);
	pos = indices [0];
	gtk_tree_path_free (path);

	return pos;
}

/**
 * eom_list_store_length:
 * @store: An #EomListStore.
 *
 * Returns the number of images in the store.
 *
 * Returns: The number of images in @store.
 **/
gint
eom_list_store_length (EomListStore *store)
{
	g_return_val_if_fail (EOM_IS_LIST_STORE (store), -1);

	return gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);
}

/**
 * eom_list_store_get_initial_pos:
 * @store: An #EomListStore.
 *
 * Gets the position of the #EomImage that should be loaded first.
 * If not set, it returns -1.
 *
 * Returns: the position of the image to be loaded first or -1.
 *
 **/
gint
eom_list_store_get_initial_pos (EomListStore *store)
{
	g_return_val_if_fail (EOM_IS_LIST_STORE (store), -1);

	return store->priv->initial_image;
}

static void
eom_list_store_remove_thumbnail_job (EomListStore *store,
				     GtkTreeIter *iter)
{
	EomJob *job;

	gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
			    EOM_LIST_STORE_EOM_JOB, &job,
			    -1);

	if (job != NULL) {
		g_mutex_lock (&store->priv->mutex);
		eom_job_queue_remove_job (job);
		gtk_list_store_set (GTK_LIST_STORE (store), iter,
				    EOM_LIST_STORE_EOM_JOB, NULL,
				    -1);
		g_mutex_unlock (&store->priv->mutex);
	}


}

static void
eom_list_store_add_thumbnail_job (EomListStore *store, GtkTreeIter *iter)
{
	EomImage *image;
	EomJob *job;

	gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
			    EOM_LIST_STORE_EOM_IMAGE, &image,
			    EOM_LIST_STORE_EOM_JOB, &job,
			    -1);

	if (job != NULL) {
		g_object_unref (image);
		return;
	}

	job = eom_job_thumbnail_new (image);

	g_signal_connect (job,
			  "finished",
			  G_CALLBACK (eom_job_thumbnail_cb),
			  store);

	g_mutex_lock (&store->priv->mutex);
	gtk_list_store_set (GTK_LIST_STORE (store), iter,
			    EOM_LIST_STORE_EOM_JOB, job,
			    -1);
	eom_job_queue_add_job (job);
	g_mutex_unlock (&store->priv->mutex);
	g_object_unref (job);
	g_object_unref (image);
}

/**
 * eom_list_store_thumbnail_set:
 * @store: An #EomListStore.
 * @iter: A #GtkTreeIter pointing to an image in @store.
 *
 * Sets the thumbnail for the image pointed by @iter.
 *
 **/
void
eom_list_store_thumbnail_set (EomListStore *store,
			      GtkTreeIter *iter)
{
	gboolean thumb_set = FALSE;

	gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
			    EOM_LIST_STORE_THUMB_SET, &thumb_set,
			    -1);

	if (thumb_set) {
		return;
	}

	eom_list_store_add_thumbnail_job (store, iter);
}

/**
 * eom_list_store_thumbnail_unset:
 * @store: An #EomListStore.
 * @iter: A #GtkTreeIter pointing to an image in @store.
 *
 * Unsets the thumbnail for the image pointed by @iter, changing
 * it to a "busy" icon.
 *
 **/
void
eom_list_store_thumbnail_unset (EomListStore *store,
				GtkTreeIter *iter)
{
	EomImage *image;

	eom_list_store_remove_thumbnail_job (store, iter);

	gtk_tree_model_get (GTK_TREE_MODEL (store), iter,
			    EOM_LIST_STORE_EOM_IMAGE, &image,
			    -1);
	eom_image_set_thumbnail (image, NULL);
	g_object_unref (image);

	gtk_list_store_set (GTK_LIST_STORE (store), iter,
			    EOM_LIST_STORE_THUMBNAIL, store->priv->busy_image,
			    EOM_LIST_STORE_THUMB_SET, FALSE,
			    -1);
}

/**
 * eom_list_store_thumbnail_refresh:
 * @store: An #EomListStore.
 * @iter: A #GtkTreeIter pointing to an image in @store.
 *
 * Refreshes the thumbnail for the image pointed by @iter.
 *
 **/
void
eom_list_store_thumbnail_refresh (EomListStore *store,
				  GtkTreeIter *iter)
{
	eom_list_store_remove_thumbnail_job (store, iter);
	eom_list_store_add_thumbnail_job (store, iter);
}