Updates references from using underscore filenames to hyphen filenames for files...
[geeqie.git] / src / collect.c
1 /*
2  * Copyright (C) 2006 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "main.h"
23 #include "collect.h"
24
25 #include "collect-dlg.h"
26 #include "collect-io.h"
27 #include "collect-table.h"
28 #include "editors.h"
29 #include "filedata.h"
30 #include "img-view.h"
31 #include "layout.h"
32 #include "layout-image.h"
33 #include "layout-util.h"
34 #include "misc.h"
35 #include "pixbuf-util.h"
36 #include "print.h"
37 #include "ui-fileops.h"
38 #include "ui-tree-edit.h"
39 #include "utilops.h"
40 #include "window.h"
41
42 #include <gdk/gdkkeysyms.h> /* for keyboard values */
43
44
45 #define COLLECT_DEF_WIDTH 440
46 #define COLLECT_DEF_HEIGHT 450
47
48 /**
49  *  list of paths to collections */
50
51 /**
52  * @brief  List of currently open Collections.
53  * 
54  * Type ::_CollectionData 
55  */
56 static GList *collection_list = NULL;
57
58 /**
59  * @brief  List of currently open Collection windows.
60  * 
61  * Type ::_CollectWindow
62  */
63 static GList *collection_window_list = NULL;
64
65 static void collection_window_get_geometry(CollectWindow *cw);
66 static void collection_window_refresh(CollectWindow *cw);
67 static void collection_window_update_title(CollectWindow *cw);
68 static void collection_window_add(CollectWindow *cw, CollectInfo *ci);
69 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci);
70 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci);
71 static void collection_window_update(CollectWindow *cw, CollectInfo *ci);
72
73 static void collection_window_close(CollectWindow *cw);
74
75 static void collection_notify_cb(FileData *fd, NotifyType type, gpointer data);
76
77 /*
78  *-------------------------------------------------------------------
79  * data, list handling
80  *-------------------------------------------------------------------
81  */
82
83 CollectInfo *collection_info_new(FileData *fd, struct stat *UNUSED(st), GdkPixbuf *pixbuf)
84 {
85         CollectInfo *ci;
86
87         if (!fd) return NULL;
88
89         ci = g_new0(CollectInfo, 1);
90         ci->fd = file_data_ref(fd);
91
92         ci->pixbuf = pixbuf;
93         if (ci->pixbuf) g_object_ref(ci->pixbuf);
94
95         return ci;
96 }
97
98 void collection_info_free_thumb(CollectInfo *ci)
99 {
100         if (ci->pixbuf) g_object_unref(ci->pixbuf);
101         ci->pixbuf = NULL;
102 }
103
104 void collection_info_free(CollectInfo *ci)
105 {
106         if (!ci) return;
107
108         file_data_unref(ci->fd);
109         collection_info_free_thumb(ci);
110         g_free(ci);
111 }
112
113 void collection_info_set_thumb(CollectInfo *ci, GdkPixbuf *pixbuf)
114 {
115         if (pixbuf) g_object_ref(pixbuf);
116         collection_info_free_thumb(ci);
117         ci->pixbuf = pixbuf;
118 }
119
120 gboolean collection_info_load_thumb(CollectInfo *ci)
121 {
122         if (!ci) return FALSE;
123
124         collection_info_free_thumb(ci);
125
126         log_printf("collection_info_load_thumb not implemented!\n(because an instant thumb loader not implemented)");
127         return FALSE;
128 }
129
130 void collection_list_free(GList *list)
131 {
132         GList *work;
133         work = list;
134         while (work)
135                 {
136                 collection_info_free((CollectInfo *)work->data);
137                 work = work->next;
138                 }
139         g_list_free(list);
140 }
141
142 /* an ugly static var, well what ya gonna do ? */
143 static SortType collection_list_sort_method = SORT_NAME;
144
145 static gint collection_list_sort_cb(gconstpointer a, gconstpointer b)
146 {
147         const CollectInfo *cia = a;
148         const CollectInfo *cib = b;
149
150         switch (collection_list_sort_method)
151                 {
152                 case SORT_NAME:
153                         break;
154                 case SORT_NONE:
155                         return 0;
156                         break;
157                 case SORT_SIZE:
158                         if (cia->fd->size < cib->fd->size) return -1;
159                         if (cia->fd->size > cib->fd->size) return 1;
160                         return 0;
161                         break;
162                 case SORT_TIME:
163                         if (cia->fd->date < cib->fd->date) return -1;
164                         if (cia->fd->date > cib->fd->date) return 1;
165                         return 0;
166                         break;
167                 case SORT_CTIME:
168                         if (cia->fd->cdate < cib->fd->cdate) return -1;
169                         if (cia->fd->cdate > cib->fd->cdate) return 1;
170                         return 0;
171                         break;
172                 case SORT_EXIFTIME:
173                         if (cia->fd->exifdate < cib->fd->exifdate) return -1;
174                         if (cia->fd->exifdate > cib->fd->exifdate) return 1;
175                         break;
176                 case SORT_EXIFTIMEDIGITIZED:
177                         if (cia->fd->exifdate_digitized < cib->fd->exifdate_digitized) return -1;
178                         if (cia->fd->exifdate_digitized > cib->fd->exifdate_digitized) return 1;
179                         break;
180                 case SORT_RATING:
181                         if (cia->fd->rating < cib->fd->rating) return -1;
182                         if (cia->fd->rating > cib->fd->rating) return 1;
183                         break;
184                 case SORT_PATH:
185                         return utf8_compare(cia->fd->path, cib->fd->path, options->file_sort.case_sensitive);
186                         break;
187                 case SORT_CLASS:
188                         if (cia->fd->format_class < cib->fd->format_class) return -1;
189                         if (cia->fd->format_class > cib->fd->format_class) return 1;
190                         break;
191 #ifdef HAVE_STRVERSCMP
192                 case SORT_NUMBER:
193                         return strverscmp(cia->fd->name, cib->fd->name);
194                         break;
195 #endif
196                 default:
197                         break;
198                 }
199
200         if (options->file_sort.case_sensitive)
201                 return strcmp(cia->fd->collate_key_name, cib->fd->collate_key_name);
202         else
203                 return strcmp(cia->fd->collate_key_name_nocase, cib->fd->collate_key_name_nocase);
204 }
205
206 GList *collection_list_sort(GList *list, SortType method)
207 {
208         if (method == SORT_NONE) return list;
209
210         collection_list_sort_method = method;
211
212         return g_list_sort(list, collection_list_sort_cb);
213 }
214
215 GList *collection_list_randomize(GList *list)
216 {
217         guint random, length, i;
218         gpointer tmp;
219         GList *nlist, *olist;
220
221         length = g_list_length(list);
222         if (!length) return NULL;
223
224         srand((unsigned int)time(NULL)); // Initialize random generator (hasn't to be that much strong)
225
226         for (i = 0; i < length; i++)
227                 {
228                 random = (guint) (1.0 * length * rand()/(RAND_MAX + 1.0));
229                 olist = g_list_nth(list, i);
230                 nlist = g_list_nth(list, random);
231                 tmp = olist->data;
232                 olist->data = nlist->data;
233                 nlist->data = tmp;
234                 }
235
236         return list;
237 }
238
239 GList *collection_list_add(GList *list, CollectInfo *ci, SortType method)
240 {
241         if (method != SORT_NONE)
242                 {
243                 collection_list_sort_method = method;
244                 list = g_list_insert_sorted(list, ci, collection_list_sort_cb);
245                 }
246         else
247                 {
248                 list = g_list_append(list, ci);
249                 }
250
251         return list;
252 }
253
254 GList *collection_list_insert(GList *list, CollectInfo *ci, CollectInfo *insert_ci, SortType method)
255 {
256         if (method != SORT_NONE)
257                 {
258                 collection_list_sort_method = method;
259                 list = g_list_insert_sorted(list, ci, collection_list_sort_cb);
260                 }
261         else
262                 {
263                 GList *point;
264
265                 point = g_list_find(list, insert_ci);
266                 list = uig_list_insert_link(list, point, ci);
267                 }
268
269         return list;
270 }
271
272 GList *collection_list_remove(GList *list, CollectInfo *ci)
273 {
274         list = g_list_remove(list, ci);
275         collection_info_free(ci);
276         return list;
277 }
278
279 CollectInfo *collection_list_find_fd(GList *list, FileData *fd)
280 {
281         GList *work = list;
282
283         while (work)
284                 {
285                 CollectInfo *ci = work->data;
286                 if (ci->fd == fd) return ci;
287                 work = work->next;
288                 }
289
290         return NULL;
291 }
292
293 GList *collection_list_to_filelist(GList *list)
294 {
295         GList *filelist = NULL;
296         GList *work = list;
297
298         while (work)
299                 {
300                 CollectInfo *info = work->data;
301                 filelist = g_list_prepend(filelist, file_data_ref(info->fd));
302                 work = work->next;
303                 }
304
305         filelist = g_list_reverse(filelist);
306         return filelist;
307 }
308
309 CollectWindow *collection_window_find(CollectionData *cd)
310 {
311         GList *work;
312
313         work = collection_window_list;
314         while (work)
315                 {
316                 CollectWindow *cw = work->data;
317                 if (cw->cd == cd) return cw;
318                 work = work->next;
319                 }
320
321         return NULL;
322 }
323
324 CollectWindow *collection_window_find_by_path(const gchar *path)
325 {
326         GList *work;
327
328         if (!path) return NULL;
329
330         work = collection_window_list;
331         while (work)
332                 {
333                 CollectWindow *cw = work->data;
334                 if (cw->cd->path && strcmp(cw->cd->path, path) == 0) return cw;
335                 work = work->next;
336                 }
337
338         return NULL;
339 }
340
341 /**
342  * @brief Checks string for existence of Collection.
343  * @param[in] param Filename, with or without extension of any collection
344  * @returns full pathname if found or NULL
345  * 
346  * Return value must be freed with g_free()
347  */
348 gchar *collection_path(const gchar *param)
349 {
350         gchar *path = NULL;
351         gchar *full_name = NULL;
352
353         if (file_extension_match(param, GQ_COLLECTION_EXT))
354                 {
355                 path = g_build_filename(get_collections_dir(), param, NULL);
356                 }
357         else if (file_extension_match(param, NULL))
358                 {
359                 full_name = g_strconcat(param, GQ_COLLECTION_EXT, NULL);
360                 path = g_build_filename(get_collections_dir(), full_name, NULL);
361                 }
362
363         if (!isfile(path))
364                 {
365                 g_free(path);
366                 path = NULL;
367                 }
368
369         g_free(full_name);
370         return path;
371 }
372
373 /**
374  * @brief Checks input string for existence of Collection.
375  * @param[in] param Filename with or without extension of any collection
376  * @returns TRUE if found
377  * 
378  * 
379  */
380 gboolean is_collection(const gchar *param)
381 {
382         gchar *name = NULL;
383
384         name = collection_path(param);
385         if (name)
386                 {
387                 g_free(name);
388                 return TRUE;
389                 }
390         return FALSE;
391 }
392
393 /**
394  * @brief Creates a text list of the image paths of the contents of a Collection
395  * @param[in] name The name of the collection, with or without extension
396  * @param[inout] contents A GString to which the image paths are appended
397  * 
398  * 
399  */
400 void collection_contents(const gchar *name, GString **contents)
401 {
402         gchar *path;
403         CollectionData *cd;
404         CollectInfo *ci;
405         GList *work;
406         FileData *fd;
407
408         if (is_collection(name))
409                 {
410                 path = collection_path(name);
411                 cd = collection_new("");
412                 collection_load(cd, path, COLLECTION_LOAD_APPEND);
413                 work = cd->list;
414                 while (work)
415                         {
416                         ci = work->data;
417                         fd = ci->fd;
418                         *contents = g_string_append(*contents, g_strdup(fd->path));
419                         *contents = g_string_append(*contents, "\n");
420
421                         work = work->next;
422                         }
423                 g_free(path);
424                 collection_free(cd);
425                 }
426 }
427
428 /**
429  * @brief Returns a list of filedatas of the contents of a Collection
430  * @param[in] name The name of the collection, with or without extension
431  * 
432  * 
433  */
434 GList *collection_contents_fd(const gchar *name)
435 {
436         gchar *path;
437         CollectionData *cd;
438         CollectInfo *ci;
439         GList *work;
440         GList *list = NULL;
441
442         if (is_collection(name))
443                 {
444                 path = collection_path(name);
445                 cd = collection_new("");
446                 collection_load(cd, path, COLLECTION_LOAD_APPEND);
447                 work = cd->list;
448                 while (work)
449                         {
450                         ci = work->data;
451                         list = g_list_append(list, ci->fd);
452
453                         work = work->next;
454                         }
455                 g_free(path);
456                 collection_free(cd);
457                 }
458
459         return list;
460 }
461
462 /*
463  *-------------------------------------------------------------------
464  * please use these to actually add/remove stuff
465  *-------------------------------------------------------------------
466  */
467
468 CollectionData *collection_new(const gchar *path)
469 {
470         CollectionData *cd;
471         static gint untitled_counter = 0;
472
473         cd = g_new0(CollectionData, 1);
474
475         cd->ref = 1;    /* starts with a ref of 1 */
476         cd->sort_method = SORT_NONE;
477         cd->window_w = COLLECT_DEF_WIDTH;
478         cd->window_h = COLLECT_DEF_HEIGHT;
479         cd->existence = g_hash_table_new(NULL, NULL);
480
481         if (path)
482                 {
483                 cd->path = g_strdup(path);
484                 cd->name = g_strdup(filename_from_path(cd->path));
485                 /* load it */
486                 }
487         else
488                 {
489                 if (untitled_counter == 0)
490                         {
491                         cd->name = g_strdup(_("Untitled"));
492                         }
493                 else
494                         {
495                         cd->name = g_strdup_printf(_("Untitled (%d)"), untitled_counter + 1);
496                         }
497
498                 untitled_counter++;
499                 }
500
501         file_data_register_notify_func(collection_notify_cb, cd, NOTIFY_PRIORITY_MEDIUM);
502
503
504         collection_list = g_list_append(collection_list, cd);
505
506         return cd;
507 }
508
509 void collection_free(CollectionData *cd)
510 {
511         if (!cd) return;
512
513         DEBUG_1("collection \"%s\" freed", cd->name);
514
515         collection_load_stop(cd);
516         collection_list_free(cd->list);
517
518         file_data_unregister_notify_func(collection_notify_cb, cd);
519
520         collection_list = g_list_remove(collection_list, cd);
521
522         g_hash_table_destroy(cd->existence);
523
524         g_free(cd->path);
525         g_free(cd->name);
526
527         g_free(cd);
528 }
529
530 void collection_ref(CollectionData *cd)
531 {
532         cd->ref++;
533
534         DEBUG_1("collection \"%s\" ref count = %d", cd->name, cd->ref);
535 }
536
537 void collection_unref(CollectionData *cd)
538 {
539         cd->ref--;
540
541         DEBUG_1("collection \"%s\" ref count = %d", cd->name, cd->ref);
542
543         if (cd->ref < 1)
544                 {
545                 collection_free(cd);
546                 }
547 }
548
549 void collection_path_changed(CollectionData *cd)
550 {
551         collection_window_update_title(collection_window_find(cd));
552 }
553
554 gint collection_to_number(CollectionData *cd)
555 {
556         return g_list_index(collection_list, cd);
557 }
558
559 CollectionData *collection_from_number(gint n)
560 {
561         return g_list_nth_data(collection_list, n);
562 }
563
564 CollectionData *collection_from_dnd_data(const gchar *data, GList **list, GList **info_list)
565 {
566         CollectionData *cd;
567         gint collection_number;
568         const gchar *ptr;
569
570         if (list) *list = NULL;
571         if (info_list) *info_list = NULL;
572
573         if (strncmp(data, "COLLECTION:", 11) != 0) return NULL;
574
575         ptr = data + 11;
576
577         collection_number = atoi(ptr);
578         cd = collection_from_number(collection_number);
579         if (!cd) return NULL;
580
581         if (!list && !info_list) return cd;
582
583         while (*ptr != '\0' && *ptr != '\n' ) ptr++;
584         if (*ptr == '\0') return cd;
585         ptr++;
586
587         while (*ptr != '\0')
588                 {
589                 guint item_number;
590                 CollectInfo *info;
591
592                 item_number = (guint) atoi(ptr);
593                 while (*ptr != '\n' && *ptr != '\0') ptr++;
594                 if (*ptr == '\0')
595                         break;
596                 else
597                         while (*ptr == '\n') ptr++;
598
599                 info = g_list_nth_data(cd->list, item_number);
600                 if (!info) continue;
601
602                 if (list) *list = g_list_append(*list, file_data_ref(info->fd));
603                 if (info_list) *info_list = g_list_append(*info_list, info);
604                 }
605
606         return cd;
607 }
608
609 gchar *collection_info_list_to_dnd_data(CollectionData *cd, GList *list, gint *length)
610 {
611         GList *work;
612         GList *temp = NULL;
613         gchar *ptr;
614         gchar *text;
615         gchar *uri_text;
616         gint collection_number;
617
618         *length = 0;
619         if (!list) return NULL;
620
621         collection_number = collection_to_number(cd);
622         if (collection_number < 0) return NULL;
623
624         text = g_strdup_printf("COLLECTION:%d\n", collection_number);
625         *length += strlen(text);
626         temp = g_list_prepend(temp, text);
627
628         work = list;
629         while (work)
630                 {
631                 gint item_number = g_list_index(cd->list, work->data);
632
633                 work = work->next;
634
635                 if (item_number < 0) continue;
636
637                 text = g_strdup_printf("%d\n", item_number);
638                 temp = g_list_prepend(temp, text);
639                 *length += strlen(text);
640                 }
641
642         *length += 1; /* ending nul char */
643
644         uri_text = g_malloc(*length);
645         ptr = uri_text;
646
647         work = g_list_last(temp);
648         while (work)
649                 {
650                 gint len;
651                 gchar *text = work->data;
652
653                 work = work->prev;
654
655                 len = strlen(text);
656                 memcpy(ptr, text, len);
657                 ptr += len;
658                 }
659
660         ptr[0] = '\0';
661
662         string_list_free(temp);
663
664         return uri_text;
665 }
666
667 gint collection_info_valid(CollectionData *cd, CollectInfo *info)
668 {
669         if (collection_to_number(cd) < 0) return FALSE;
670
671         return (g_list_index(cd->list, info) != 0);
672 }
673
674 CollectInfo *collection_next_by_info(CollectionData *cd, CollectInfo *info)
675 {
676         GList *work;
677
678         work = g_list_find(cd->list, info);
679
680         if (!work) return NULL;
681         work = work->next;
682         if (work) return work->data;
683         return NULL;
684 }
685
686 CollectInfo *collection_prev_by_info(CollectionData *cd, CollectInfo *info)
687 {
688         GList *work;
689
690         work = g_list_find(cd->list, info);
691
692         if (!work) return NULL;
693         work = work->prev;
694         if (work) return work->data;
695         return NULL;
696 }
697
698 CollectInfo *collection_get_first(CollectionData *cd)
699 {
700         if (cd->list) return cd->list->data;
701
702         return NULL;
703 }
704
705 CollectInfo *collection_get_last(CollectionData *cd)
706 {
707         GList *list;
708
709         list = g_list_last(cd->list);
710
711         if (list) return list->data;
712
713         return NULL;
714 }
715
716 void collection_set_sort_method(CollectionData *cd, SortType method)
717 {
718         if (!cd) return;
719
720         if (cd->sort_method == method) return;
721
722         cd->sort_method = method;
723         cd->list = collection_list_sort(cd->list, cd->sort_method);
724         if (cd->list) cd->changed = TRUE;
725
726         collection_window_refresh(collection_window_find(cd));
727 }
728
729 void collection_randomize(CollectionData *cd)
730 {
731         if (!cd) return;
732
733         cd->list = collection_list_randomize(cd->list);
734         cd->sort_method = SORT_NONE;
735         if (cd->list) cd->changed = TRUE;
736
737         collection_window_refresh(collection_window_find(cd));
738 }
739
740 void collection_set_update_info_func(CollectionData *cd,
741                                      void (*func)(CollectionData *, CollectInfo *, gpointer), gpointer data)
742 {
743         cd->info_updated_func = func;
744         cd->info_updated_data = data;
745 }
746
747 static CollectInfo *collection_info_new_if_not_exists(CollectionData *cd, struct stat *st, FileData *fd)
748 {
749         CollectInfo *ci;
750
751         if (g_hash_table_lookup(cd->existence, fd->path)) return NULL;
752
753         ci = collection_info_new(fd, st, NULL);
754         if (ci) g_hash_table_insert(cd->existence, fd->path, "");
755         return ci;
756 }
757
758 gboolean collection_add_check(CollectionData *cd, FileData *fd, gboolean sorted, gboolean must_exist)
759 {
760         struct stat st;
761         gboolean valid;
762
763         if (!fd) return FALSE;
764
765         g_assert(fd->magick == FD_MAGICK);
766
767         if (must_exist)
768                 {
769                 valid = (stat_utf8(fd->path, &st) && !S_ISDIR(st.st_mode));
770                 }
771         else
772                 {
773                 valid = TRUE;
774                 st.st_size = 0;
775                 st.st_mtime = 0;
776                 }
777
778         if (valid)
779                 {
780                 CollectInfo *ci;
781
782                 ci = collection_info_new_if_not_exists(cd, &st, fd);
783                 if (!ci) return FALSE;
784                 DEBUG_3("add to collection: %s", fd->path);
785
786                 cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE);
787                 cd->changed = TRUE;
788
789                 if (!sorted || cd->sort_method == SORT_NONE)
790                         {
791                         collection_window_add(collection_window_find(cd), ci);
792                         }
793                 else
794                         {
795                         collection_window_insert(collection_window_find(cd), ci);
796                         }
797                 }
798
799         return valid;
800 }
801
802 gboolean collection_add(CollectionData *cd, FileData *fd, gboolean sorted)
803 {
804         return collection_add_check(cd, fd, sorted, TRUE);
805 }
806
807 gboolean collection_insert(CollectionData *cd, FileData *fd, CollectInfo *insert_ci, gboolean sorted)
808 {
809         struct stat st;
810
811         if (!insert_ci) return collection_add(cd, fd, sorted);
812
813         if (stat_utf8(fd->path, &st) >= 0 && !S_ISDIR(st.st_mode))
814                 {
815                 CollectInfo *ci;
816
817                 ci = collection_info_new_if_not_exists(cd, &st, fd);
818                 if (!ci) return FALSE;
819
820                 DEBUG_3("insert in collection: %s", fd->path);
821
822                 cd->list = collection_list_insert(cd->list, ci, insert_ci, sorted ? cd->sort_method : SORT_NONE);
823                 cd->changed = TRUE;
824
825                 collection_window_insert(collection_window_find(cd), ci);
826
827                 return TRUE;
828                 }
829
830         return FALSE;
831 }
832
833 gboolean collection_remove(CollectionData *cd, FileData *fd)
834 {
835         CollectInfo *ci;
836
837         ci = collection_list_find_fd(cd->list, fd);
838
839         if (!ci) return FALSE;
840
841         g_hash_table_remove(cd->existence, fd->path);
842
843         cd->list = g_list_remove(cd->list, ci);
844         cd->changed = TRUE;
845
846         collection_window_remove(collection_window_find(cd), ci);
847         collection_info_free(ci);
848
849         return TRUE;
850 }
851
852 static void collection_remove_by_info(CollectionData *cd, CollectInfo *info)
853 {
854         if (!info || !g_list_find(cd->list, info)) return;
855
856         cd->list = g_list_remove(cd->list, info);
857         cd->changed = (cd->list != NULL);
858
859         collection_window_remove(collection_window_find(cd), info);
860         collection_info_free(info);
861 }
862
863 void collection_remove_by_info_list(CollectionData *cd, GList *list)
864 {
865         GList *work;
866
867         if (!list) return;
868
869         if (!list->next)
870                 {
871                 /* more efficient (in collect-table) to remove a single item this way */
872                 collection_remove_by_info(cd, (CollectInfo *)list->data);
873                 return;
874                 }
875
876         work = list;
877         while (work)
878                 {
879                 cd->list = collection_list_remove(cd->list, work->data);
880                 work = work->next;
881                 }
882         cd->changed = (cd->list != NULL);
883
884         collection_window_refresh(collection_window_find(cd));
885 }
886
887 gboolean collection_rename(CollectionData *cd, FileData *fd)
888 {
889         CollectInfo *ci;
890         ci = collection_list_find_fd(cd->list, fd);
891
892         if (!ci) return FALSE;
893
894         cd->changed = TRUE;
895
896         collection_window_update(collection_window_find(cd), ci);
897
898         return TRUE;
899 }
900
901 void collection_update_geometry(CollectionData *cd)
902 {
903         collection_window_get_geometry(collection_window_find(cd));
904 }
905
906 /*
907  *-------------------------------------------------------------------
908  * simple maintenance for renaming, deleting
909  *-------------------------------------------------------------------
910  */
911
912 static void collection_notify_cb(FileData *fd, NotifyType type, gpointer data)
913 {
914         CollectionData *cd = data;
915
916         if (!(type & NOTIFY_CHANGE) || !fd->change) return;
917
918         DEBUG_1("Notify collection: %s %04x", fd->path, type);
919
920         switch (fd->change->type)
921                 {
922                 case FILEDATA_CHANGE_MOVE:
923                 case FILEDATA_CHANGE_RENAME:
924                         collection_rename(cd, fd);
925                         break;
926                 case FILEDATA_CHANGE_COPY:
927                         break;
928                 case FILEDATA_CHANGE_DELETE:
929                         while (collection_remove(cd, fd));
930                         break;
931                 case FILEDATA_CHANGE_UNSPECIFIED:
932                 case FILEDATA_CHANGE_WRITE_METADATA:
933                         break;
934                 }
935
936 }
937
938
939 /*
940  *-------------------------------------------------------------------
941  * window key presses
942  *-------------------------------------------------------------------
943  */
944
945 static gboolean collection_window_keypress(GtkWidget *UNUSED(widget), GdkEventKey *event, gpointer data)
946 {
947         CollectWindow *cw = data;
948         gboolean stop_signal = FALSE;
949         GList *list;
950
951         if (event->state & GDK_CONTROL_MASK)
952                 {
953                 stop_signal = TRUE;
954                 switch (event->keyval)
955                         {
956                         case '1':
957                         case '2':
958                         case '3':
959                         case '4':
960                         case '5':
961                         case '6':
962                         case '7':
963                         case '8':
964                         case '9':
965                         case '0':
966                                 break;
967                         case 'A': case 'a':
968                                 if (event->state & GDK_SHIFT_MASK)
969                                         {
970                                         collection_table_unselect_all(cw->table);
971                                         }
972                                 else
973                                         {
974                                         collection_table_select_all(cw->table);
975                                         }
976                                 break;
977                         case 'L': case 'l':
978                                 list = layout_list(NULL);
979                                 if (list)
980                                         {
981                                         collection_table_add_filelist(cw->table, list);
982                                         filelist_free(list);
983                                         }
984                                 break;
985                         case 'C': case 'c':
986                                 file_util_copy(NULL, collection_table_selection_get_list(cw->table), NULL, cw->window);
987                                 break;
988                         case 'M': case 'm':
989                                 file_util_move(NULL, collection_table_selection_get_list(cw->table), NULL, cw->window);
990                                 break;
991                         case 'R': case 'r':
992                                 file_util_rename(NULL, collection_table_selection_get_list(cw->table), cw->window);
993                                 break;
994                         case 'D': case 'd':
995                                 options->file_ops.safe_delete_enable = TRUE;
996                                 file_util_delete(NULL, collection_table_selection_get_list(cw->table), cw->window);
997                                 break;
998                         case 'S': case 's':
999                                 collection_dialog_save_as(NULL, cw->cd);
1000                                 break;
1001                         case 'W': case 'w':
1002                                 collection_window_close(cw);
1003                                 break;
1004                         default:
1005                                 stop_signal = FALSE;
1006                                 break;
1007                         }
1008                 }
1009         else
1010                 {
1011                 stop_signal = TRUE;
1012                 switch (event->keyval)
1013                         {
1014                         case GDK_KEY_Return: case GDK_KEY_KP_Enter:
1015                                 layout_image_set_collection(NULL, cw->cd,
1016                                         collection_table_get_focus_info(cw->table));
1017                                 break;
1018                         case 'V': case 'v':
1019                                 view_window_new_from_collection(cw->cd,
1020                                         collection_table_get_focus_info(cw->table));
1021                                 break;
1022                         case 'S': case 's':
1023                                 if (!cw->cd->path)
1024                                         {
1025                                         collection_dialog_save_as(NULL, cw->cd);
1026                                         }
1027                                 else if (!collection_save(cw->cd, cw->cd->path))
1028                                         {
1029                                         log_printf("failed saving to collection path: %s\n", cw->cd->path);
1030                                         }
1031                                 break;
1032                         case 'A': case 'a':
1033                                 collection_dialog_append(NULL, cw->cd);
1034                                 break;
1035                         case 'N': case 'n':
1036                                 collection_set_sort_method(cw->cd, SORT_NAME);
1037                                 break;
1038 #ifdef HAVE_STRVERSCMP
1039                         case 'I': case 'i':
1040                                 collection_set_sort_method(cw->cd, SORT_NUMBER);
1041                                 break;
1042 #endif
1043                         case 'D': case 'd':
1044                                 collection_set_sort_method(cw->cd, SORT_TIME);
1045                                 break;
1046                         case 'B': case 'b':
1047                                 collection_set_sort_method(cw->cd, SORT_SIZE);
1048                                 break;
1049                         case 'P': case 'p':
1050                                 if (event->state & GDK_SHIFT_MASK)
1051                                         {
1052                                         CollectInfo *info;
1053
1054                                         info = collection_table_get_focus_info(cw->table);
1055
1056                                         print_window_new(info->fd, collection_table_selection_get_list(cw->table),
1057                                                          collection_list_to_filelist(cw->cd->list), cw->window);
1058                                         }
1059                                 else
1060                                         {
1061                                         collection_set_sort_method(cw->cd, SORT_PATH);
1062                                         }
1063                                 break;
1064                         case 'R': case 'r':
1065                                 if (event->state & GDK_MOD1_MASK)
1066                                         {
1067                                                 options->collections.rectangular_selection = !(options->collections.rectangular_selection);
1068                                         }
1069                                 break;
1070                         case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
1071                                 list = g_list_copy(cw->table->selection);
1072                                 if (list)
1073                                         {
1074                                         collection_remove_by_info_list(cw->cd, list);
1075                                         g_list_free(list);
1076                                         }
1077                                 else
1078                                         {
1079                                         collection_remove_by_info(cw->cd, collection_table_get_focus_info(cw->table));
1080                                         }
1081                                 break;
1082                         default:
1083                                 stop_signal = FALSE;
1084                                 break;
1085                         }
1086                 }
1087         if (!stop_signal && is_help_key(event))
1088                 {
1089                 help_window_show("GuideCollections.html");
1090                 stop_signal = TRUE;
1091                 }
1092
1093         return stop_signal;
1094 }
1095
1096 /*
1097  *-------------------------------------------------------------------
1098  * window
1099  *-------------------------------------------------------------------
1100  */
1101 static void collection_window_get_geometry(CollectWindow *cw)
1102 {
1103         CollectionData *cd;
1104         GdkWindow *window;
1105
1106         if (!cw) return;
1107
1108         cd = cw->cd;
1109         window = gtk_widget_get_window(cw->window);
1110         gdk_window_get_position(window, &cd->window_x, &cd->window_y);
1111         cd->window_w = gdk_window_get_width(window);
1112         cd->window_h = gdk_window_get_height(window);
1113         cd->window_read = TRUE;
1114 }
1115
1116 static void collection_window_refresh(CollectWindow *cw)
1117 {
1118         if (!cw) return;
1119
1120         collection_table_refresh(cw->table);
1121 }
1122
1123 static void collection_window_update_title(CollectWindow *cw)
1124 {
1125         gboolean free_name = FALSE;
1126         gchar *name;
1127         gchar *buf;
1128
1129         if (!cw) return;
1130
1131         if (file_extension_match(cw->cd->name, GQ_COLLECTION_EXT))
1132                 {
1133                 name = remove_extension_from_path(cw->cd->name);
1134                 free_name = TRUE;
1135                 }
1136         else
1137                 {
1138                 name = cw->cd->name;
1139                 }
1140
1141         buf = g_strdup_printf(_("%s - Collection - %s"), name, GQ_APPNAME);
1142         if (free_name) g_free(name);
1143         gtk_window_set_title(GTK_WINDOW(cw->window), buf);
1144         g_free(buf);
1145 }
1146
1147 static void collection_window_update_info(CollectionData *UNUSED(cd), CollectInfo *ci, gpointer data)
1148 {
1149         CollectWindow *cw = data;
1150
1151         collection_table_file_update(cw->table, ci);
1152 }
1153
1154 static void collection_window_add(CollectWindow *cw, CollectInfo *ci)
1155 {
1156         if (!cw) return;
1157
1158         if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1159         collection_table_file_add(cw->table, ci);
1160 }
1161
1162 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci)
1163 {
1164         if (!cw) return;
1165
1166         if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1167         collection_table_file_insert(cw->table, ci);
1168         if (!cw) return;
1169 }
1170
1171 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci)
1172 {
1173         if (!cw) return;
1174
1175         collection_table_file_remove(cw->table, ci);
1176 }
1177
1178 static void collection_window_update(CollectWindow *cw, CollectInfo *ci)
1179 {
1180         if (!cw) return;
1181
1182         collection_table_file_update(cw->table, ci);
1183         collection_table_file_update(cw->table, NULL);
1184 }
1185
1186 static void collection_window_close_final(CollectWindow *cw)
1187 {
1188         if (cw->close_dialog) return;
1189
1190         collection_window_list = g_list_remove(collection_window_list, cw);
1191         collection_window_get_geometry(cw);
1192
1193         gtk_widget_destroy(cw->window);
1194
1195         collection_set_update_info_func(cw->cd, NULL, NULL);
1196         collection_unref(cw->cd);
1197
1198         g_free(cw);
1199 }
1200
1201 static void collection_close_save_cb(GenericDialog *gd, gpointer data)
1202 {
1203         CollectWindow *cw = data;
1204
1205         cw->close_dialog = NULL;
1206         generic_dialog_close(gd);
1207
1208         if (!cw->cd->path)
1209                 {
1210                 collection_dialog_save_close(NULL, cw->cd);
1211                 return;
1212                 }
1213         else if (!collection_save(cw->cd, cw->cd->path))
1214                 {
1215                 gchar *buf;
1216                 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), cw->cd->path);
1217                 warning_dialog(_("Save Failed"), buf, GTK_STOCK_DIALOG_ERROR, cw->window);
1218                 g_free(buf);
1219                 return;
1220                 }
1221
1222         collection_window_close_final(cw);
1223 }
1224
1225 static void collection_close_close_cb(GenericDialog *gd, gpointer data)
1226 {
1227         CollectWindow *cw = data;
1228
1229         cw->close_dialog = NULL;
1230         generic_dialog_close(gd);
1231
1232         collection_window_close_final(cw);
1233 }
1234
1235 static void collection_close_cancel_cb(GenericDialog *gd, gpointer data)
1236 {
1237         CollectWindow *cw = data;
1238
1239         cw->close_dialog = NULL;
1240         generic_dialog_close(gd);
1241 }
1242
1243 static void collection_close_dlg_show(CollectWindow *cw)
1244 {
1245         GenericDialog *gd;
1246
1247         if (cw->close_dialog)
1248                 {
1249                 gtk_window_present(GTK_WINDOW(cw->close_dialog));
1250                 return;
1251                 }
1252
1253         gd = generic_dialog_new(_("Close collection"),
1254                                 "close_collection", cw->window, FALSE,
1255                                 collection_close_cancel_cb, cw);
1256         generic_dialog_add_message(gd, GTK_STOCK_DIALOG_QUESTION,
1257                                    _("Close collection"),
1258                                    _("Collection has been modified.\nSave first?"), TRUE);
1259
1260         generic_dialog_add_button(gd, GTK_STOCK_SAVE, NULL, collection_close_save_cb, TRUE);
1261         generic_dialog_add_button(gd, GTK_STOCK_DELETE, _("_Discard"), collection_close_close_cb, FALSE);
1262
1263         cw->close_dialog = gd->dialog;
1264
1265         gtk_widget_show(gd->dialog);
1266 }
1267
1268 static void collection_window_close(CollectWindow *cw)
1269 {
1270         if (!cw->cd->changed && !cw->close_dialog)
1271                 {
1272                 collection_window_close_final(cw);
1273                 return;
1274                 }
1275
1276         collection_close_dlg_show(cw);
1277 }
1278
1279 void collection_window_close_by_collection(CollectionData *cd)
1280 {
1281         CollectWindow *cw;
1282
1283         cw = collection_window_find(cd);
1284         if (cw) collection_window_close_final(cw);
1285 }
1286
1287 /**
1288  * @brief Check if any Collection windows have unsaved data
1289  * @returns TRUE if unsaved data exists
1290  * 
1291  * Also saves window geometry for Collection windows that have
1292  * no unsaved data
1293  */
1294 gboolean collection_window_modified_exists(void)
1295 {
1296         GList *work;
1297         gboolean ret;
1298
1299         ret = FALSE;
1300
1301         work = collection_window_list;
1302         while (work)
1303                 {
1304                 CollectWindow *cw = work->data;
1305                 if (cw->cd->changed)
1306                         {
1307                         ret = TRUE;
1308                         }
1309                 else
1310                         {
1311                         if (!collection_save(cw->table->cd, cw->table->cd->path))
1312                                 {
1313                                 log_printf("failed saving to collection path: %s\n", cw->table->cd->path);
1314                                 }
1315                         }
1316                 work = work->next;
1317                 }
1318
1319         return ret;
1320 }
1321
1322 static gboolean collection_window_delete(GtkWidget *UNUSED(widget), GdkEvent *UNUSED(event), gpointer data)
1323 {
1324         CollectWindow *cw = data;
1325         collection_window_close(cw);
1326
1327         return TRUE;
1328 }
1329
1330 CollectWindow *collection_window_new(const gchar *path)
1331 {
1332         CollectWindow *cw;
1333         GtkWidget *vbox;
1334         GtkWidget *frame;
1335         GtkWidget *status_label;
1336         GtkWidget *extra_label;
1337         GdkGeometry geometry;
1338
1339         /* If the collection is already opened in another window, return that one */
1340         cw = collection_window_find_by_path(path);
1341         if (cw)
1342                 {
1343                 return cw;
1344                 }
1345
1346         cw = g_new0(CollectWindow, 1);
1347
1348         collection_window_list = g_list_append(collection_window_list, cw);
1349
1350         cw->cd = collection_new(path);
1351
1352         cw->window = window_new(GTK_WINDOW_TOPLEVEL, "collection", PIXBUF_INLINE_ICON_BOOK, NULL, NULL);
1353         DEBUG_NAME(cw->window);
1354
1355         geometry.min_width = DEFAULT_MINIMAL_WINDOW_SIZE;
1356         geometry.min_height = DEFAULT_MINIMAL_WINDOW_SIZE;
1357         geometry.base_width = COLLECT_DEF_WIDTH;
1358         geometry.base_height = COLLECT_DEF_HEIGHT;
1359         gtk_window_set_geometry_hints(GTK_WINDOW(cw->window), NULL, &geometry,
1360                                       GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE);
1361
1362         if (options->collections_on_top)
1363                 {
1364                 gtk_window_set_keep_above(GTK_WINDOW(cw->window), TRUE);
1365                 }
1366
1367         if (options->save_window_positions && path && collection_load_only_geometry(cw->cd, path))
1368                 {
1369                 gtk_window_set_default_size(GTK_WINDOW(cw->window), cw->cd->window_w, cw->cd->window_h);
1370                 gtk_window_move(GTK_WINDOW(cw->window), cw->cd->window_x, cw->cd->window_y);
1371                 }
1372         else
1373                 {
1374                 gtk_window_set_default_size(GTK_WINDOW(cw->window), COLLECT_DEF_WIDTH, COLLECT_DEF_HEIGHT);
1375                 }
1376
1377         gtk_window_set_resizable(GTK_WINDOW(cw->window), TRUE);
1378         collection_window_update_title(cw);
1379         gtk_container_set_border_width(GTK_CONTAINER(cw->window), 0);
1380
1381         g_signal_connect(G_OBJECT(cw->window), "delete_event",
1382                          G_CALLBACK(collection_window_delete), cw);
1383
1384         g_signal_connect(G_OBJECT(cw->window), "key_press_event",
1385                          G_CALLBACK(collection_window_keypress), cw);
1386
1387         vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1388         gtk_container_add(GTK_CONTAINER(cw->window), vbox);
1389         gtk_widget_show(vbox);
1390
1391         cw->table = collection_table_new(cw->cd);
1392         gtk_box_pack_start(GTK_BOX(vbox), cw->table->scrolled, TRUE, TRUE, 0);
1393         gtk_widget_show(cw->table->scrolled);
1394
1395         cw->status_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1396         gtk_box_pack_start(GTK_BOX(vbox), cw->status_box, FALSE, FALSE, 0);
1397         gtk_widget_show(cw->status_box);
1398
1399         frame = gtk_frame_new(NULL);
1400         DEBUG_NAME(frame);
1401         gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
1402         gtk_box_pack_start(GTK_BOX(cw->status_box), frame, TRUE, TRUE, 0);
1403         gtk_widget_show(frame);
1404
1405         status_label = gtk_label_new("");
1406         gtk_container_add(GTK_CONTAINER(frame), status_label);
1407         gtk_widget_show(status_label);
1408
1409         extra_label = gtk_progress_bar_new();
1410         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(extra_label), 0.0);
1411         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(extra_label), "");
1412         gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(extra_label), TRUE);
1413
1414         gtk_box_pack_start(GTK_BOX(cw->status_box), extra_label, TRUE, TRUE, 0);
1415         gtk_widget_show(extra_label);
1416
1417         collection_table_set_labels(cw->table, status_label, extra_label);
1418
1419         gtk_widget_show(cw->window);
1420         gtk_widget_grab_focus(cw->table->listview);
1421
1422         collection_set_update_info_func(cw->cd, collection_window_update_info, cw);
1423
1424         if (path && *path == G_DIR_SEPARATOR) collection_load_begin(cw->cd, NULL, COLLECTION_LOAD_NONE);
1425
1426         return cw;
1427 }
1428 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */