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