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