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