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