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