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