Bug fix: Change all .desktop files to RDNS style
[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, 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.width = COLLECT_DEF_WIDTH;
459         cd->window.height = COLLECT_DEF_HEIGHT;
460         cd->existence = g_hash_table_new(nullptr, nullptr);
461
462         if (path)
463                 {
464                 cd->path = g_strdup(path);
465                 cd->name = g_strdup(filename_from_path(cd->path));
466                 /* load it */
467                 }
468         else
469                 {
470                 if (untitled_counter == 0)
471                         {
472                         cd->name = g_strdup(_("Untitled"));
473                         }
474                 else
475                         {
476                         cd->name = g_strdup_printf(_("Untitled (%d)"), untitled_counter + 1);
477                         }
478
479                 untitled_counter++;
480                 }
481
482         file_data_register_notify_func(collection_notify_cb, cd, NOTIFY_PRIORITY_MEDIUM);
483
484
485         collection_list = g_list_append(collection_list, cd);
486
487         return cd;
488 }
489
490 void collection_free(CollectionData *cd)
491 {
492         if (!cd) return;
493
494         DEBUG_1("collection \"%s\" freed", cd->name);
495
496         collection_load_stop(cd);
497         g_list_free_full(cd->list, reinterpret_cast<GDestroyNotify>(collection_info_free));
498
499         file_data_unregister_notify_func(collection_notify_cb, cd);
500
501         collection_list = g_list_remove(collection_list, cd);
502
503         g_hash_table_destroy(cd->existence);
504
505         g_free(cd->collection_path);
506         g_free(cd->path);
507         g_free(cd->name);
508
509         g_free(cd);
510 }
511
512 void collection_ref(CollectionData *cd)
513 {
514         cd->ref++;
515
516         DEBUG_1("collection \"%s\" ref count = %d", cd->name, cd->ref);
517 }
518
519 void collection_unref(CollectionData *cd)
520 {
521         cd->ref--;
522
523         DEBUG_1("collection \"%s\" ref count = %d", cd->name, cd->ref);
524
525         if (cd->ref < 1)
526                 {
527                 collection_free(cd);
528                 }
529 }
530
531 void collection_path_changed(CollectionData *cd)
532 {
533         collection_window_update_title(collection_window_find(cd));
534 }
535
536 gint collection_to_number(CollectionData *cd)
537 {
538         return g_list_index(collection_list, cd);
539 }
540
541 CollectionData *collection_from_number(gint n)
542 {
543         return static_cast<CollectionData *>(g_list_nth_data(collection_list, n));
544 }
545
546 CollectionData *collection_from_dnd_data(const gchar *data, GList **list, GList **info_list)
547 {
548         CollectionData *cd;
549         gint collection_number;
550         const gchar *ptr;
551
552         if (list) *list = nullptr;
553         if (info_list) *info_list = nullptr;
554
555         if (strncmp(data, "COLLECTION:", 11) != 0) return nullptr;
556
557         ptr = data + 11;
558
559         collection_number = atoi(ptr);
560         cd = collection_from_number(collection_number);
561         if (!cd) return nullptr;
562
563         if (!list && !info_list) return cd;
564
565         while (*ptr != '\0' && *ptr != '\n' ) ptr++;
566         if (*ptr == '\0') return cd;
567         ptr++;
568
569         while (*ptr != '\0')
570                 {
571                 guint item_number;
572                 CollectInfo *info;
573
574                 item_number = static_cast<guint>(atoi(ptr));
575                 while (*ptr != '\n' && *ptr != '\0') ptr++;
576                 if (*ptr == '\0')
577                         break;
578                 else
579                         while (*ptr == '\n') ptr++;
580
581                 info = static_cast<CollectInfo *>(g_list_nth_data(cd->list, item_number));
582                 if (!info) continue;
583
584                 if (list) *list = g_list_append(*list, file_data_ref(info->fd));
585                 if (info_list) *info_list = g_list_append(*info_list, info);
586                 }
587
588         return cd;
589 }
590
591 gchar *collection_info_list_to_dnd_data(CollectionData *cd, GList *list, gint *length)
592 {
593         GList *work;
594         GList *temp = nullptr;
595         gchar *ptr;
596         gchar *text;
597         gchar *uri_text;
598         gint collection_number;
599
600         *length = 0;
601         if (!list) return nullptr;
602
603         collection_number = collection_to_number(cd);
604         if (collection_number < 0) return nullptr;
605
606         text = g_strdup_printf("COLLECTION:%d\n", collection_number);
607         *length += strlen(text);
608         temp = g_list_prepend(temp, text);
609
610         work = list;
611         while (work)
612                 {
613                 gint item_number = g_list_index(cd->list, work->data);
614
615                 work = work->next;
616
617                 if (item_number < 0) continue;
618
619                 text = g_strdup_printf("%d\n", item_number);
620                 temp = g_list_prepend(temp, text);
621                 *length += strlen(text);
622                 }
623
624         *length += 1; /* ending nul char */
625
626         uri_text = static_cast<gchar *>(g_malloc(*length));
627         ptr = uri_text;
628
629         work = g_list_last(temp);
630         while (work)
631                 {
632                 gint len;
633                 auto text = static_cast<gchar *>(work->data);
634
635                 work = work->prev;
636
637                 len = strlen(text);
638                 memcpy(ptr, text, len);
639                 ptr += len;
640                 }
641
642         ptr[0] = '\0';
643
644         g_list_free_full(temp, g_free);
645
646         return uri_text;
647 }
648
649 gint collection_info_valid(CollectionData *cd, CollectInfo *info)
650 {
651         if (collection_to_number(cd) < 0) return FALSE;
652
653         return (g_list_index(cd->list, info) != 0);
654 }
655
656 CollectInfo *collection_next_by_info(CollectionData *cd, CollectInfo *info)
657 {
658         GList *work;
659
660         work = g_list_find(cd->list, info);
661
662         if (!work) return nullptr;
663         work = work->next;
664         if (work) return static_cast<CollectInfo *>(work->data);
665         return nullptr;
666 }
667
668 CollectInfo *collection_prev_by_info(CollectionData *cd, CollectInfo *info)
669 {
670         GList *work;
671
672         work = g_list_find(cd->list, info);
673
674         if (!work) return nullptr;
675         work = work->prev;
676         if (work) return static_cast<CollectInfo *>(work->data);
677         return nullptr;
678 }
679
680 CollectInfo *collection_get_first(CollectionData *cd)
681 {
682         if (cd->list) return static_cast<CollectInfo *>(cd->list->data);
683
684         return nullptr;
685 }
686
687 CollectInfo *collection_get_last(CollectionData *cd)
688 {
689         GList *list;
690
691         list = g_list_last(cd->list);
692
693         if (list) return static_cast<CollectInfo *>(list->data);
694
695         return nullptr;
696 }
697
698 void collection_set_sort_method(CollectionData *cd, SortType method)
699 {
700         if (!cd) return;
701
702         if (cd->sort_method == method) return;
703
704         cd->sort_method = method;
705         cd->list = collection_list_sort(cd->list, cd->sort_method);
706         if (cd->list) cd->changed = TRUE;
707
708         collection_window_refresh(collection_window_find(cd));
709 }
710
711 void collection_randomize(CollectionData *cd)
712 {
713         if (!cd) return;
714
715         cd->list = collection_list_randomize(cd->list);
716         cd->sort_method = SORT_NONE;
717         if (cd->list) cd->changed = TRUE;
718
719         collection_window_refresh(collection_window_find(cd));
720 }
721
722 void collection_set_update_info_func(CollectionData *cd,
723                                      void (*func)(CollectionData *, CollectInfo *, gpointer), gpointer data)
724 {
725         cd->info_updated_func = func;
726         cd->info_updated_data = data;
727 }
728
729 static CollectInfo *collection_info_new_if_not_exists(CollectionData *cd, struct stat *st, FileData *fd)
730 {
731         CollectInfo *ci;
732
733         if (!options->collections_duplicates)
734                 {
735                 if (g_hash_table_lookup(cd->existence, fd->path)) return nullptr;
736                 }
737
738         ci = collection_info_new(fd, st, nullptr);
739         if (ci) g_hash_table_insert(cd->existence, fd->path, g_strdup(""));
740         return ci;
741 }
742
743 gboolean collection_add_check(CollectionData *cd, FileData *fd, gboolean sorted, gboolean must_exist)
744 {
745         struct stat st;
746         gboolean valid;
747
748         if (!fd) return FALSE;
749
750         g_assert(fd->magick == FD_MAGICK);
751
752         if (must_exist)
753                 {
754                 valid = (stat_utf8(fd->path, &st) && !S_ISDIR(st.st_mode));
755                 }
756         else
757                 {
758                 valid = TRUE;
759                 st.st_size = 0;
760                 st.st_mtime = 0;
761                 }
762
763         if (valid)
764                 {
765                 CollectInfo *ci;
766
767                 ci = collection_info_new_if_not_exists(cd, &st, fd);
768                 if (!ci) return FALSE;
769                 DEBUG_3("add to collection: %s", fd->path);
770
771                 cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE);
772                 cd->changed = TRUE;
773
774                 if (!sorted || cd->sort_method == SORT_NONE)
775                         {
776                         collection_window_add(collection_window_find(cd), ci);
777                         }
778                 else
779                         {
780                         collection_window_insert(collection_window_find(cd), ci);
781                         }
782                 }
783
784         return valid;
785 }
786
787 gboolean collection_add(CollectionData *cd, FileData *fd, gboolean sorted)
788 {
789         return collection_add_check(cd, fd, sorted, TRUE);
790 }
791
792 gboolean collection_insert(CollectionData *cd, FileData *fd, CollectInfo *insert_ci, gboolean sorted)
793 {
794         struct stat st;
795
796         if (!insert_ci) return collection_add(cd, fd, sorted);
797
798         if (stat_utf8(fd->path, &st) >= 0 && !S_ISDIR(st.st_mode))
799                 {
800                 CollectInfo *ci;
801
802                 ci = collection_info_new_if_not_exists(cd, &st, fd);
803                 if (!ci) return FALSE;
804
805                 DEBUG_3("insert in collection: %s", fd->path);
806
807                 cd->list = collection_list_insert(cd->list, ci, insert_ci, sorted ? cd->sort_method : SORT_NONE);
808                 cd->changed = TRUE;
809
810                 collection_window_insert(collection_window_find(cd), ci);
811
812                 return TRUE;
813                 }
814
815         return FALSE;
816 }
817
818 gboolean collection_remove(CollectionData *cd, FileData *fd)
819 {
820         CollectInfo *ci;
821
822         ci = collection_list_find_fd(cd->list, fd);
823
824         if (!ci) return FALSE;
825
826         g_hash_table_remove(cd->existence, fd->path);
827
828         cd->list = g_list_remove(cd->list, ci);
829         cd->changed = TRUE;
830
831         collection_window_remove(collection_window_find(cd), ci);
832         collection_info_free(ci);
833
834         return TRUE;
835 }
836
837 static void collection_remove_by_info(CollectionData *cd, CollectInfo *info)
838 {
839         if (!info || !g_list_find(cd->list, info)) return;
840
841         cd->list = g_list_remove(cd->list, info);
842         cd->changed = (cd->list != nullptr);
843
844         collection_window_remove(collection_window_find(cd), info);
845         collection_info_free(info);
846 }
847
848 void collection_remove_by_info_list(CollectionData *cd, GList *list)
849 {
850         GList *work;
851
852         if (!list) return;
853
854         if (!list->next)
855                 {
856                 /* more efficient (in collect-table) to remove a single item this way */
857                 collection_remove_by_info(cd, static_cast<CollectInfo *>(list->data));
858                 return;
859                 }
860
861         work = list;
862         while (work)
863                 {
864                 cd->list = collection_list_remove(cd->list, static_cast<CollectInfo *>(work->data));
865                 work = work->next;
866                 }
867         cd->changed = (cd->list != nullptr);
868
869         collection_window_refresh(collection_window_find(cd));
870 }
871
872 gboolean collection_rename(CollectionData *cd, FileData *fd)
873 {
874         CollectInfo *ci;
875         ci = collection_list_find_fd(cd->list, fd);
876
877         if (!ci) return FALSE;
878
879         cd->changed = TRUE;
880
881         collection_window_update(collection_window_find(cd), ci);
882
883         return TRUE;
884 }
885
886 void collection_update_geometry(CollectionData *cd)
887 {
888         collection_window_get_geometry(collection_window_find(cd));
889 }
890
891 /*
892  *-------------------------------------------------------------------
893  * simple maintenance for renaming, deleting
894  *-------------------------------------------------------------------
895  */
896
897 static void collection_notify_cb(FileData *fd, NotifyType type, gpointer data)
898 {
899         auto cd = static_cast<CollectionData *>(data);
900
901         if (!(type & NOTIFY_CHANGE) || !fd->change) return;
902
903         DEBUG_1("Notify collection: %s %04x", fd->path, type);
904
905         switch (fd->change->type)
906                 {
907                 case FILEDATA_CHANGE_MOVE:
908                 case FILEDATA_CHANGE_RENAME:
909                         collection_rename(cd, fd);
910                         break;
911                 case FILEDATA_CHANGE_COPY:
912                         break;
913                 case FILEDATA_CHANGE_DELETE:
914                         while (collection_remove(cd, fd));
915                         break;
916                 case FILEDATA_CHANGE_UNSPECIFIED:
917                 case FILEDATA_CHANGE_WRITE_METADATA:
918                         break;
919                 }
920
921 }
922
923
924 /*
925  *-------------------------------------------------------------------
926  * window key presses
927  *-------------------------------------------------------------------
928  */
929
930 static gboolean collection_window_keypress(GtkWidget *, GdkEventKey *event, gpointer data)
931 {
932         auto cw = static_cast<CollectWindow *>(data);
933         gboolean stop_signal = FALSE;
934         GList *list;
935
936         if (event->state & GDK_CONTROL_MASK)
937                 {
938                 stop_signal = TRUE;
939                 switch (event->keyval)
940                         {
941                         case '1':
942                         case '2':
943                         case '3':
944                         case '4':
945                         case '5':
946                         case '6':
947                         case '7':
948                         case '8':
949                         case '9':
950                         case '0':
951                                 break;
952                         case 'A': case 'a':
953                                 if (event->state & GDK_SHIFT_MASK)
954                                         {
955                                         collection_table_unselect_all(cw->table);
956                                         }
957                                 else
958                                         {
959                                         collection_table_select_all(cw->table);
960                                         }
961                                 break;
962                         case 'L': case 'l':
963                                 list = layout_list(nullptr);
964                                 if (list)
965                                         {
966                                         collection_table_add_filelist(cw->table, list);
967                                         filelist_free(list);
968                                         }
969                                 break;
970                         case 'C': case 'c':
971                                 file_util_copy(nullptr, collection_table_selection_get_list(cw->table), nullptr, cw->window);
972                                 break;
973                         case 'M': case 'm':
974                                 file_util_move(nullptr, collection_table_selection_get_list(cw->table), nullptr, cw->window);
975                                 break;
976                         case 'R': case 'r':
977                                 file_util_rename(nullptr, collection_table_selection_get_list(cw->table), cw->window);
978                                 break;
979                         case 'D': case 'd':
980                                 options->file_ops.safe_delete_enable = TRUE;
981                                 file_util_delete(nullptr, collection_table_selection_get_list(cw->table), cw->window);
982                                 break;
983                         case 'S': case 's':
984                                 collection_dialog_save_as(cw->cd);
985                                 break;
986                         case 'W': case 'w':
987                                 collection_window_close(cw);
988                                 break;
989                         default:
990                                 stop_signal = FALSE;
991                                 break;
992                         }
993                 }
994         else
995                 {
996                 stop_signal = TRUE;
997                 switch (event->keyval)
998                         {
999                         case GDK_KEY_Return: case GDK_KEY_KP_Enter:
1000                                 layout_image_set_collection(nullptr, cw->cd,
1001                                         collection_table_get_focus_info(cw->table));
1002                                 break;
1003                         case 'V': case 'v':
1004                                 view_window_new_from_collection(cw->cd,
1005                                         collection_table_get_focus_info(cw->table));
1006                                 break;
1007                         case 'S': case 's':
1008                                 if (!cw->cd->path)
1009                                         {
1010                                         collection_dialog_save_as(cw->cd);
1011                                         }
1012                                 else if (!collection_save(cw->cd, cw->cd->path))
1013                                         {
1014                                         log_printf("failed saving to collection path: %s\n", cw->cd->path);
1015                                         }
1016                                 break;
1017                         case 'A': case 'a':
1018                                 collection_dialog_append(cw->cd);
1019                                 break;
1020                         case 'N': case 'n':
1021                                 collection_set_sort_method(cw->cd, SORT_NAME);
1022                                 break;
1023                         case 'D': case 'd':
1024                                 collection_set_sort_method(cw->cd, SORT_TIME);
1025                                 break;
1026                         case 'B': case 'b':
1027                                 collection_set_sort_method(cw->cd, SORT_SIZE);
1028                                 break;
1029                         case 'P': case 'p':
1030                                 if (event->state & GDK_SHIFT_MASK)
1031                                         {
1032                                         CollectInfo *info;
1033
1034                                         info = collection_table_get_focus_info(cw->table);
1035
1036                                         print_window_new(info->fd, collection_table_selection_get_list(cw->table),
1037                                                          collection_list_to_filelist(cw->cd->list), cw->window);
1038                                         }
1039                                 else
1040                                         {
1041                                         collection_set_sort_method(cw->cd, SORT_PATH);
1042                                         }
1043                                 break;
1044                         case 'R': case 'r':
1045                                 if (event->state & GDK_MOD1_MASK)
1046                                         {
1047                                                 options->collections.rectangular_selection = !(options->collections.rectangular_selection);
1048                                         }
1049                                 break;
1050                         case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
1051                                 list = g_list_copy(cw->table->selection);
1052                                 if (list)
1053                                         {
1054                                         collection_remove_by_info_list(cw->cd, list);
1055                                         collection_table_refresh(cw->table);
1056                                         g_list_free(list);
1057                                         }
1058                                 else
1059                                         {
1060                                         collection_remove_by_info(cw->cd, collection_table_get_focus_info(cw->table));
1061                                         }
1062                                 break;
1063                         default:
1064                                 stop_signal = FALSE;
1065                                 break;
1066                         }
1067                 }
1068         if (!stop_signal && is_help_key(event))
1069                 {
1070                 help_window_show("GuideCollections.html");
1071                 stop_signal = TRUE;
1072                 }
1073
1074         return stop_signal;
1075 }
1076
1077 /*
1078  *-------------------------------------------------------------------
1079  * window
1080  *-------------------------------------------------------------------
1081  */
1082 static void collection_window_get_geometry(CollectWindow *cw)
1083 {
1084         CollectionData *cd;
1085         GdkWindow *window;
1086
1087         if (!cw) return;
1088
1089         cd = cw->cd;
1090         window = gtk_widget_get_window(cw->window);
1091         gdk_window_get_position(window, &cd->window.x, &cd->window.y);
1092         cd->window.width = gdk_window_get_width(window);
1093         cd->window.height = gdk_window_get_height(window);
1094         cd->window_read = TRUE;
1095 }
1096
1097 static void collection_window_refresh(CollectWindow *cw)
1098 {
1099         if (!cw) return;
1100
1101         collection_table_refresh(cw->table);
1102 }
1103
1104 static void collection_window_update_title(CollectWindow *cw)
1105 {
1106         gboolean free_name = FALSE;
1107         gchar *name;
1108         gchar *buf;
1109
1110         if (!cw) return;
1111
1112         if (file_extension_match(cw->cd->name, GQ_COLLECTION_EXT))
1113                 {
1114                 name = remove_extension_from_path(cw->cd->name);
1115                 free_name = TRUE;
1116                 }
1117         else
1118                 {
1119                 name = cw->cd->name;
1120                 }
1121
1122         buf = g_strdup_printf(_("%s - Collection - %s"), name, GQ_APPNAME);
1123         if (free_name) g_free(name);
1124         gtk_window_set_title(GTK_WINDOW(cw->window), buf);
1125         g_free(buf);
1126 }
1127
1128 static void collection_window_update_info(CollectionData *, CollectInfo *ci, gpointer data)
1129 {
1130         auto cw = static_cast<CollectWindow *>(data);
1131
1132         collection_table_file_update(cw->table, ci);
1133 }
1134
1135 static void collection_window_add(CollectWindow *cw, CollectInfo *ci)
1136 {
1137         if (!cw) return;
1138
1139         if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1140         collection_table_file_add(cw->table, ci);
1141 }
1142
1143 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci)
1144 {
1145         if (!cw) return;
1146
1147         if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1148         collection_table_file_insert(cw->table, ci);
1149         if (!cw) return;
1150 }
1151
1152 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci)
1153 {
1154         if (!cw) return;
1155
1156         collection_table_file_remove(cw->table, ci);
1157 }
1158
1159 static void collection_window_update(CollectWindow *cw, CollectInfo *ci)
1160 {
1161         if (!cw) return;
1162
1163         collection_table_file_update(cw->table, ci);
1164         collection_table_file_update(cw->table, nullptr);
1165 }
1166
1167 static void collection_window_close_final(CollectWindow *cw)
1168 {
1169         if (cw->close_dialog) return;
1170
1171         collection_window_list = g_list_remove(collection_window_list, cw);
1172         collection_window_get_geometry(cw);
1173
1174         gq_gtk_widget_destroy(cw->window);
1175
1176         collection_set_update_info_func(cw->cd, nullptr, nullptr);
1177         collection_unref(cw->cd);
1178
1179         g_free(cw);
1180 }
1181
1182 static void collection_close_save_cb(GenericDialog *gd, gpointer data)
1183 {
1184         auto cw = static_cast<CollectWindow *>(data);
1185
1186         cw->close_dialog = nullptr;
1187         generic_dialog_close(gd);
1188
1189         if (!cw->cd->path)
1190                 {
1191                 collection_dialog_save_close(cw->cd);
1192                 return;
1193                 }
1194         else if (!collection_save(cw->cd, cw->cd->path))
1195                 {
1196                 gchar *buf;
1197                 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), cw->cd->path);
1198                 warning_dialog(_("Save Failed"), buf, GQ_ICON_DIALOG_ERROR, cw->window);
1199                 g_free(buf);
1200                 return;
1201                 }
1202
1203         collection_window_close_final(cw);
1204 }
1205
1206 static void collection_close_close_cb(GenericDialog *gd, gpointer data)
1207 {
1208         auto cw = static_cast<CollectWindow *>(data);
1209
1210         cw->close_dialog = nullptr;
1211         generic_dialog_close(gd);
1212
1213         collection_window_close_final(cw);
1214 }
1215
1216 static void collection_close_cancel_cb(GenericDialog *gd, gpointer data)
1217 {
1218         auto cw = static_cast<CollectWindow *>(data);
1219
1220         cw->close_dialog = nullptr;
1221         generic_dialog_close(gd);
1222 }
1223
1224 static void collection_close_dlg_show(CollectWindow *cw)
1225 {
1226         GenericDialog *gd;
1227
1228         if (cw->close_dialog)
1229                 {
1230                 gtk_window_present(GTK_WINDOW(cw->close_dialog));
1231                 return;
1232                 }
1233
1234         gd = generic_dialog_new(_("Close collection"),
1235                                 "close_collection", cw->window, FALSE,
1236                                 collection_close_cancel_cb, cw);
1237         generic_dialog_add_message(gd, GQ_ICON_DIALOG_QUESTION,
1238                                    _("Close collection"),
1239                                    _("Collection has been modified.\nSave first?"), TRUE);
1240
1241         generic_dialog_add_button(gd, GQ_ICON_SAVE, _("Save"), collection_close_save_cb, TRUE);
1242         generic_dialog_add_button(gd, GQ_ICON_DELETE, _("_Discard"), collection_close_close_cb, FALSE);
1243
1244         cw->close_dialog = gd->dialog;
1245
1246         gtk_widget_show(gd->dialog);
1247 }
1248
1249 static void collection_window_close(CollectWindow *cw)
1250 {
1251         if (!cw->cd->changed && !cw->close_dialog)
1252                 {
1253                 collection_window_close_final(cw);
1254                 return;
1255                 }
1256
1257         collection_close_dlg_show(cw);
1258 }
1259
1260 void collection_window_close_by_collection(CollectionData *cd)
1261 {
1262         CollectWindow *cw;
1263
1264         cw = collection_window_find(cd);
1265         if (cw) collection_window_close_final(cw);
1266 }
1267
1268 /**
1269  * @brief Check if any Collection windows have unsaved data
1270  * @returns TRUE if unsaved data exists
1271  *
1272  * Also saves window geometry for Collection windows that have
1273  * no unsaved data
1274  */
1275 gboolean collection_window_modified_exists()
1276 {
1277         GList *work;
1278         gboolean ret;
1279
1280         ret = FALSE;
1281
1282         work = collection_window_list;
1283         while (work)
1284                 {
1285                 auto cw = static_cast<CollectWindow *>(work->data);
1286                 if (cw->cd->changed)
1287                         {
1288                         ret = TRUE;
1289                         }
1290                 else
1291                         {
1292                         if (!collection_save(cw->table->cd, cw->table->cd->path))
1293                                 {
1294                                 log_printf("failed saving to collection path: %s\n", cw->table->cd->path);
1295                                 }
1296                         }
1297                 work = work->next;
1298                 }
1299
1300         return ret;
1301 }
1302
1303 static gboolean collection_window_delete(GtkWidget *, GdkEvent *, gpointer data)
1304 {
1305         auto cw = static_cast<CollectWindow *>(data);
1306         collection_window_close(cw);
1307
1308         return TRUE;
1309 }
1310
1311 CollectWindow *collection_window_new(const gchar *path)
1312 {
1313         CollectWindow *cw;
1314         GtkWidget *vbox;
1315         GtkWidget *frame;
1316         GtkWidget *status_label;
1317         GtkWidget *extra_label;
1318         GdkGeometry geometry;
1319
1320         /* If the collection is already opened in another window, return that one */
1321         cw = collection_window_find_by_path(path);
1322         if (cw)
1323                 {
1324                 return cw;
1325                 }
1326
1327         cw = g_new0(CollectWindow, 1);
1328
1329         collection_window_list = g_list_append(collection_window_list, cw);
1330
1331         cw->cd = collection_new(path);
1332
1333         cw->window = window_new("collection", PIXBUF_INLINE_ICON_BOOK, nullptr, nullptr);
1334         DEBUG_NAME(cw->window);
1335
1336         geometry.min_width = DEFAULT_MINIMAL_WINDOW_SIZE;
1337         geometry.min_height = DEFAULT_MINIMAL_WINDOW_SIZE;
1338         geometry.base_width = COLLECT_DEF_WIDTH;
1339         geometry.base_height = COLLECT_DEF_HEIGHT;
1340         gtk_window_set_geometry_hints(GTK_WINDOW(cw->window), nullptr, &geometry,
1341                                       static_cast<GdkWindowHints>(GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE));
1342
1343         if (options->collections_on_top)
1344                 {
1345                 gq_gtk_window_set_keep_above(GTK_WINDOW(cw->window), TRUE);
1346                 }
1347
1348         if (options->save_window_positions && path && collection_load_only_geometry(cw->cd, path))
1349                 {
1350                 gtk_window_set_default_size(GTK_WINDOW(cw->window), cw->cd->window.width, cw->cd->window.height);
1351                 gq_gtk_window_move(GTK_WINDOW(cw->window), cw->cd->window.x, cw->cd->window.y);
1352                 }
1353         else
1354                 {
1355                 gtk_window_set_default_size(GTK_WINDOW(cw->window), COLLECT_DEF_WIDTH, COLLECT_DEF_HEIGHT);
1356                 }
1357
1358         gtk_window_set_resizable(GTK_WINDOW(cw->window), TRUE);
1359         collection_window_update_title(cw);
1360         gtk_container_set_border_width(GTK_CONTAINER(cw->window), 0);
1361
1362         g_signal_connect(G_OBJECT(cw->window), "delete_event",
1363                          G_CALLBACK(collection_window_delete), cw);
1364
1365         g_signal_connect(G_OBJECT(cw->window), "key_press_event",
1366                          G_CALLBACK(collection_window_keypress), cw);
1367
1368         vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1369         gq_gtk_container_add(GTK_WIDGET(cw->window), vbox);
1370         gtk_widget_show(vbox);
1371
1372         cw->table = collection_table_new(cw->cd);
1373         gq_gtk_box_pack_start(GTK_BOX(vbox), cw->table->scrolled, TRUE, TRUE, 0);
1374         gtk_widget_show(cw->table->scrolled);
1375
1376         cw->status_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1377         gq_gtk_box_pack_start(GTK_BOX(vbox), cw->status_box, FALSE, FALSE, 0);
1378         gtk_widget_show(cw->status_box);
1379
1380         frame = gtk_frame_new(nullptr);
1381         DEBUG_NAME(frame);
1382         gq_gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
1383         gq_gtk_box_pack_start(GTK_BOX(cw->status_box), frame, TRUE, TRUE, 0);
1384         gtk_widget_show(frame);
1385
1386         status_label = gtk_label_new("");
1387         gq_gtk_container_add(GTK_WIDGET(frame), status_label);
1388         gtk_widget_show(status_label);
1389
1390         extra_label = gtk_progress_bar_new();
1391         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(extra_label), 0.0);
1392         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(extra_label), "");
1393         gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(extra_label), TRUE);
1394
1395         gq_gtk_box_pack_start(GTK_BOX(cw->status_box), extra_label, TRUE, TRUE, 0);
1396         gtk_widget_show(extra_label);
1397
1398         collection_table_set_labels(cw->table, status_label, extra_label);
1399
1400         gtk_widget_show(cw->window);
1401         gtk_widget_grab_focus(cw->table->listview);
1402
1403         collection_set_update_info_func(cw->cd, collection_window_update_info, cw);
1404
1405         if (path && *path == G_DIR_SEPARATOR) collection_load_begin(cw->cd, nullptr, COLLECTION_LOAD_NONE);
1406
1407         return cw;
1408 }
1409 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */