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