Trim trailing white spaces on empty lines.
[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 (must_exist)
599                 {
600                 valid = (stat_utf8(fd->path, &st) && !S_ISDIR(st.st_mode));
601                 }
602         else
603                 {
604                 valid = TRUE;
605                 st.st_size = 0;
606                 st.st_mtime = 0;
607                 }
608
609         if (valid)
610                 {
611                 CollectInfo *ci;
612
613                 ci = collection_info_new_if_not_exists(cd, &st, fd);
614                 if (!ci) return FALSE;
615                 DEBUG_3("add to collection: %s", fd->path);
616
617                 cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE);
618                 cd->changed = TRUE;
619
620                 if (!sorted || cd->sort_method == SORT_NONE)
621                         {
622                         collection_window_add(collection_window_find(cd), ci);
623                         }
624                 else
625                         {
626                         collection_window_insert(collection_window_find(cd), ci);
627                         }
628                 }
629
630         return valid;
631 }
632
633 gboolean collection_add(CollectionData *cd, FileData *fd, gboolean sorted)
634 {
635         return collection_add_check(cd, fd, sorted, TRUE);
636 }
637
638 gboolean collection_insert(CollectionData *cd, FileData *fd, CollectInfo *insert_ci, gboolean sorted)
639 {
640         struct stat st;
641
642         if (!insert_ci) return collection_add(cd, fd, sorted);
643
644         if (stat_utf8(fd->path, &st) >= 0 && !S_ISDIR(st.st_mode))
645                 {
646                 CollectInfo *ci;
647
648                 ci = collection_info_new_if_not_exists(cd, &st, fd);
649                 if (!ci) return FALSE;
650
651                 DEBUG_3("insert in collection: %s", fd->path);
652
653                 cd->list = collection_list_insert(cd->list, ci, insert_ci, sorted ? cd->sort_method : SORT_NONE);
654                 cd->changed = TRUE;
655
656                 collection_window_insert(collection_window_find(cd), ci);
657
658                 return TRUE;
659                 }
660
661         return FALSE;
662 }
663
664 gboolean collection_remove(CollectionData *cd, FileData *fd)
665 {
666         CollectInfo *ci;
667
668         ci = collection_list_find_fd(cd->list, fd);
669
670         if (!ci) return FALSE;
671
672         g_hash_table_remove(cd->existence, fd->path);
673
674         cd->list = g_list_remove(cd->list, ci);
675         cd->changed = TRUE;
676
677         collection_window_remove(collection_window_find(cd), ci);
678         collection_info_free(ci);
679
680         return TRUE;
681 }
682
683 static void collection_remove_by_info(CollectionData *cd, CollectInfo *info)
684 {
685         if (!info || !g_list_find(cd->list, info)) return;
686
687         cd->list = g_list_remove(cd->list, info);
688         cd->changed = (cd->list != NULL);
689
690         collection_window_remove(collection_window_find(cd), info);
691         collection_info_free(info);
692 }
693
694 void collection_remove_by_info_list(CollectionData *cd, GList *list)
695 {
696         GList *work;
697
698         if (!list) return;
699
700         if (!list->next)
701                 {
702                 /* more efficient (in collect-table) to remove a single item this way */
703                 collection_remove_by_info(cd, (CollectInfo *)list->data);
704                 return;
705                 }
706
707         work = list;
708         while (work)
709                 {
710                 cd->list = collection_list_remove(cd->list, work->data);
711                 work = work->next;
712                 }
713         cd->changed = (cd->list != NULL);
714
715         collection_window_refresh(collection_window_find(cd));
716 }
717
718 gboolean collection_rename(CollectionData *cd, FileData *fd)
719 {
720         CollectInfo *ci;
721         ci = collection_list_find_fd(cd->list, fd);
722
723         if (!ci) return FALSE;
724
725         cd->changed = TRUE;
726
727         collection_window_update(collection_window_find(cd), ci);
728
729         return TRUE;
730 }
731
732 void collection_update_geometry(CollectionData *cd)
733 {
734         collection_window_get_geometry(collection_window_find(cd));
735 }
736
737 /*
738  *-------------------------------------------------------------------
739  * simple maintenance for renaming, deleting
740  *-------------------------------------------------------------------
741  */
742
743 static void collection_notify_cb(FileData *fd, NotifyType type, gpointer data)
744 {
745         CollectionData *cd = data;
746
747         if (!(type & NOTIFY_CHANGE) || !fd->change) return;
748
749         DEBUG_1("Notify collection: %s %04x", fd->path, type);
750
751         switch (fd->change->type)
752                 {
753                 case FILEDATA_CHANGE_MOVE:
754                 case FILEDATA_CHANGE_RENAME:
755                         collection_rename(cd, fd);
756                         break;
757                 case FILEDATA_CHANGE_COPY:
758                         break;
759                 case FILEDATA_CHANGE_DELETE:
760                         while (collection_remove(cd, fd));
761                         break;
762                 case FILEDATA_CHANGE_UNSPECIFIED:
763                 case FILEDATA_CHANGE_WRITE_METADATA:
764                         break;
765                 }
766
767 }
768
769
770 /*
771  *-------------------------------------------------------------------
772  * window key presses
773  *-------------------------------------------------------------------
774  */
775
776 static gboolean collection_window_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data)
777 {
778         CollectWindow *cw = data;
779         gboolean stop_signal = FALSE;
780         GList *list;
781
782         if (event->state & GDK_CONTROL_MASK)
783                 {
784                 stop_signal = TRUE;
785                 switch (event->keyval)
786                         {
787                         case '1':
788                         case '2':
789                         case '3':
790                         case '4':
791                         case '5':
792                         case '6':
793                         case '7':
794                         case '8':
795                         case '9':
796                         case '0':
797                                 break;
798                         case 'A': case 'a':
799                                 if (event->state & GDK_SHIFT_MASK)
800                                         {
801                                         collection_table_unselect_all(cw->table);
802                                         }
803                                 else
804                                         {
805                                         collection_table_select_all(cw->table);
806                                         }
807                                 break;
808                         case 'L': case 'l':
809                                 list = layout_list(NULL);
810                                 if (list)
811                                         {
812                                         collection_table_add_filelist(cw->table, list);
813                                         filelist_free(list);
814                                         }
815                                 break;
816                         case 'C': case 'c':
817                                 file_util_copy(NULL, collection_table_selection_get_list(cw->table), NULL, cw->window);
818                                 break;
819                         case 'M': case 'm':
820                                 file_util_move(NULL, collection_table_selection_get_list(cw->table), NULL, cw->window);
821                                 break;
822                         case 'R': case 'r':
823                                 file_util_rename(NULL, collection_table_selection_get_list(cw->table), cw->window);
824                                 break;
825                         case 'D': case 'd':
826                                 file_util_delete(NULL, collection_table_selection_get_list(cw->table), cw->window);
827                                 break;
828                         case 'S': case 's':
829                                 collection_dialog_save_as(NULL, cw->cd);
830                                 break;
831                         case 'W': case 'w':
832                                 collection_window_close(cw);
833                                 break;
834                         default:
835                                 stop_signal = FALSE;
836                                 break;
837                         }
838                 }
839         else
840                 {
841                 stop_signal = TRUE;
842                 switch (event->keyval)
843                         {
844                         case GDK_KEY_Return: case GDK_KEY_KP_Enter:
845                                 layout_image_set_collection(NULL, cw->cd,
846                                         collection_table_get_focus_info(cw->table));
847                                 break;
848                         case 'V': case 'v':
849                                 view_window_new_from_collection(cw->cd,
850                                         collection_table_get_focus_info(cw->table));
851                                 break;
852                         case 'S': case 's':
853                                 if (!cw->cd->path)
854                                         {
855                                         collection_dialog_save_as(NULL, cw->cd);
856                                         }
857                                 else if (!collection_save(cw->cd, cw->cd->path))
858                                         {
859                                         log_printf("failed saving to collection path: %s\n", cw->cd->path);
860                                         }
861                                 break;
862                         case 'A': case 'a':
863                                 collection_dialog_append(NULL, cw->cd);
864                                 break;
865                         case 'N': case 'n':
866                                 collection_set_sort_method(cw->cd, SORT_NAME);
867                                 break;
868 #ifdef HAVE_STRVERSCMP
869                         case 'I': case 'i':
870                                 collection_set_sort_method(cw->cd, SORT_NUMBER);
871                                 break;
872 #endif
873                         case 'D': case 'd':
874                                 collection_set_sort_method(cw->cd, SORT_TIME);
875                                 break;
876                         case 'B': case 'b':
877                                 collection_set_sort_method(cw->cd, SORT_SIZE);
878                                 break;
879                         case 'P': case 'p':
880                                 if (event->state & GDK_SHIFT_MASK)
881                                         {
882                                         CollectInfo *info;
883
884                                         info = collection_table_get_focus_info(cw->table);
885
886                                         print_window_new(info->fd, collection_table_selection_get_list(cw->table),
887                                                          collection_list_to_filelist(cw->cd->list), cw->window);
888                                         }
889                                 else
890                                         {
891                                         collection_set_sort_method(cw->cd, SORT_PATH);
892                                         }
893                                 break;
894                         case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
895                                 list = g_list_copy(cw->table->selection);
896                                 if (list)
897                                         {
898                                         collection_remove_by_info_list(cw->cd, list);
899                                         g_list_free(list);
900                                         }
901                                 else
902                                         {
903                                         collection_remove_by_info(cw->cd, collection_table_get_focus_info(cw->table));
904                                         }
905                                 break;
906                         default:
907                                 stop_signal = FALSE;
908                                 break;
909                         }
910                 }
911         return stop_signal;
912 }
913
914 /*
915  *-------------------------------------------------------------------
916  * window
917  *-------------------------------------------------------------------
918  */
919 static void collection_window_get_geometry(CollectWindow *cw)
920 {
921         CollectionData *cd;
922         GdkWindow *window;
923
924         if (!cw) return;
925
926         cd = cw->cd;
927         window = gtk_widget_get_window(cw->window);
928         gdk_window_get_position(window, &cd->window_x, &cd->window_y);
929         cd->window_w = gdk_window_get_width(window);
930         cd->window_h = gdk_window_get_height(window);
931         cd->window_read = TRUE;
932 }
933
934 static void collection_window_refresh(CollectWindow *cw)
935 {
936         if (!cw) return;
937
938         collection_table_refresh(cw->table);
939 }
940
941 static void collection_window_update_title(CollectWindow *cw)
942 {
943         gboolean free_name = FALSE;
944         gchar *name;
945         gchar *buf;
946
947         if (!cw) return;
948
949         if (file_extension_match(cw->cd->name, GQ_COLLECTION_EXT))
950                 {
951                 name = remove_extension_from_path(cw->cd->name);
952                 free_name = TRUE;
953                 }
954         else
955                 {
956                 name = cw->cd->name;
957                 }
958
959         buf = g_strdup_printf(_("%s - Collection - %s"), name, GQ_APPNAME);
960         if (free_name) g_free(name);
961         gtk_window_set_title(GTK_WINDOW(cw->window), buf);
962         g_free(buf);
963 }
964
965 static void collection_window_update_info(CollectionData *cd, CollectInfo *ci, gpointer data)
966 {
967         CollectWindow *cw = data;
968
969         collection_table_file_update(cw->table, ci);
970 }
971
972 static void collection_window_add(CollectWindow *cw, CollectInfo *ci)
973 {
974         if (!cw) return;
975
976         if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
977         collection_table_file_add(cw->table, ci);
978 }
979
980 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci)
981 {
982         if (!cw) return;
983
984         if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
985         collection_table_file_insert(cw->table, ci);
986         if (!cw) return;
987 }
988
989 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci)
990 {
991         if (!cw) return;
992
993         collection_table_file_remove(cw->table, ci);
994 }
995
996 static void collection_window_update(CollectWindow *cw, CollectInfo *ci)
997 {
998         if (!cw) return;
999
1000         collection_table_file_update(cw->table, ci);
1001         collection_table_file_update(cw->table, NULL);
1002 }
1003
1004 static void collection_window_close_final(CollectWindow *cw)
1005 {
1006         if (cw->close_dialog) return;
1007
1008         collection_window_list = g_list_remove(collection_window_list, cw);
1009         collection_window_get_geometry(cw);
1010
1011         gtk_widget_destroy(cw->window);
1012
1013         collection_set_update_info_func(cw->cd, NULL, NULL);
1014         collection_unref(cw->cd);
1015
1016         g_free(cw);
1017 }
1018
1019 static void collection_close_save_cb(GenericDialog *gd, gpointer data)
1020 {
1021         CollectWindow *cw = data;
1022
1023         cw->close_dialog = NULL;
1024         generic_dialog_close(gd);
1025
1026         if (!cw->cd->path)
1027                 {
1028                 collection_dialog_save_close(NULL, cw->cd);
1029                 return;
1030                 }
1031         else if (!collection_save(cw->cd, cw->cd->path))
1032                 {
1033                 gchar *buf;
1034                 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), cw->cd->path);
1035                 warning_dialog(_("Save Failed"), buf, GTK_STOCK_DIALOG_ERROR, cw->window);
1036                 g_free(buf);
1037                 return;
1038                 }
1039
1040         collection_window_close_final(cw);
1041 }
1042
1043 static void collection_close_close_cb(GenericDialog *gd, gpointer data)
1044 {
1045         CollectWindow *cw = data;
1046
1047         cw->close_dialog = NULL;
1048         generic_dialog_close(gd);
1049
1050         collection_window_close_final(cw);
1051 }
1052
1053 static void collection_close_cancel_cb(GenericDialog *gd, gpointer data)
1054 {
1055         CollectWindow *cw = data;
1056
1057         cw->close_dialog = NULL;
1058         generic_dialog_close(gd);
1059 }
1060
1061 static void collection_close_dlg_show(CollectWindow *cw)
1062 {
1063         GenericDialog *gd;
1064
1065         if (cw->close_dialog)
1066                 {
1067                 gtk_window_present(GTK_WINDOW(cw->close_dialog));
1068                 return;
1069                 }
1070
1071         gd = generic_dialog_new(_("Close collection"),
1072                                 "close_collection", cw->window, FALSE,
1073                                 collection_close_cancel_cb, cw);
1074         generic_dialog_add_message(gd, GTK_STOCK_DIALOG_QUESTION,
1075                                    _("Close collection"),
1076                                    _("Collection has been modified.\nSave first?"));
1077
1078         generic_dialog_add_button(gd, GTK_STOCK_SAVE, NULL, collection_close_save_cb, TRUE);
1079         generic_dialog_add_button(gd, GTK_STOCK_DELETE, _("_Discard"), collection_close_close_cb, FALSE);
1080
1081         cw->close_dialog = gd->dialog;
1082
1083         gtk_widget_show(gd->dialog);
1084 }
1085
1086 static void collection_window_close(CollectWindow *cw)
1087 {
1088         if (!cw->cd->changed && !cw->close_dialog)
1089                 {
1090                 collection_window_close_final(cw);
1091                 return;
1092                 }
1093
1094         collection_close_dlg_show(cw);
1095 }
1096
1097 void collection_window_close_by_collection(CollectionData *cd)
1098 {
1099         CollectWindow *cw;
1100
1101         cw = collection_window_find(cd);
1102         if (cw) collection_window_close_final(cw);
1103 }
1104
1105 gboolean collection_window_modified_exists(void)
1106 {
1107         GList *work;
1108
1109         work = collection_window_list;
1110         while (work)
1111                 {
1112                 CollectWindow *cw = work->data;
1113                 if (cw->cd->changed) return TRUE;
1114                 work = work->next;
1115                 }
1116
1117         return FALSE;
1118 }
1119
1120 static gboolean collection_window_delete(GtkWidget *widget, GdkEvent *event, gpointer data)
1121 {
1122         CollectWindow *cw = data;
1123         collection_window_close(cw);
1124
1125         return TRUE;
1126 }
1127
1128 CollectWindow *collection_window_new(const gchar *path)
1129 {
1130         CollectWindow *cw;
1131         GtkWidget *vbox;
1132         GtkWidget *frame;
1133         GtkWidget *status_label;
1134         GtkWidget *extra_label;
1135         GdkGeometry geometry;
1136
1137         cw = g_new0(CollectWindow, 1);
1138
1139         collection_window_list = g_list_append(collection_window_list, cw);
1140
1141         cw->cd = collection_new(path);
1142
1143         cw->window = window_new(GTK_WINDOW_TOPLEVEL, "collection", PIXBUF_INLINE_ICON_BOOK, NULL, NULL);
1144
1145         geometry.min_width = DEFAULT_MINIMAL_WINDOW_SIZE;
1146         geometry.min_height = DEFAULT_MINIMAL_WINDOW_SIZE;
1147         geometry.base_width = COLLECT_DEF_WIDTH;
1148         geometry.base_height = COLLECT_DEF_HEIGHT;
1149         gtk_window_set_geometry_hints(GTK_WINDOW(cw->window), NULL, &geometry,
1150                                       GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE);
1151
1152
1153         if (options->save_window_positions && path && collection_load_only_geometry(cw->cd, path))
1154                 {
1155                 /* FIXME: x, y is not implemented */
1156                 gtk_window_set_default_size(GTK_WINDOW(cw->window), cw->cd->window_w, cw->cd->window_h);
1157                 }
1158         else
1159                 {
1160                 gtk_window_set_default_size(GTK_WINDOW(cw->window), COLLECT_DEF_WIDTH, COLLECT_DEF_HEIGHT);
1161                 }
1162
1163         gtk_window_set_resizable(GTK_WINDOW(cw->window), TRUE);
1164         collection_window_update_title(cw);
1165         gtk_container_set_border_width(GTK_CONTAINER(cw->window), 0);
1166
1167         g_signal_connect(G_OBJECT(cw->window), "delete_event",
1168                          G_CALLBACK(collection_window_delete), cw);
1169
1170         g_signal_connect(G_OBJECT(cw->window), "key_press_event",
1171                          G_CALLBACK(collection_window_keypress), cw);
1172
1173         vbox = gtk_vbox_new(FALSE, 0);
1174         gtk_container_add(GTK_CONTAINER(cw->window), vbox);
1175         gtk_widget_show(vbox);
1176
1177         cw->table = collection_table_new(cw->cd);
1178         gtk_box_pack_start(GTK_BOX(vbox), cw->table->scrolled, TRUE, TRUE, 0);
1179         gtk_widget_show(cw->table->scrolled);
1180
1181         cw->status_box = gtk_hbox_new(TRUE, 0);
1182         gtk_box_pack_start(GTK_BOX(vbox), cw->status_box, FALSE, FALSE, 0);
1183         gtk_widget_show(cw->status_box);
1184
1185         frame = gtk_frame_new(NULL);
1186         gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
1187         gtk_box_pack_start(GTK_BOX(cw->status_box), frame, TRUE, TRUE, 0);
1188         gtk_widget_show(frame);
1189
1190         status_label = gtk_label_new("");
1191         gtk_container_add(GTK_CONTAINER(frame), status_label);
1192         gtk_widget_show(status_label);
1193
1194         extra_label = gtk_progress_bar_new();
1195         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(extra_label), 0.0);
1196         gtk_box_pack_start(GTK_BOX(cw->status_box), extra_label, TRUE, TRUE, 0);
1197         gtk_widget_show(extra_label);
1198
1199         collection_table_set_labels(cw->table, status_label, extra_label);
1200
1201         gtk_widget_show(cw->window);
1202         gtk_widget_grab_focus(cw->table->listview);
1203
1204         collection_set_update_info_func(cw->cd, collection_window_update_info, cw);
1205
1206         if (path && *path == G_DIR_SEPARATOR) collection_load_begin(cw->cd, NULL, COLLECTION_LOAD_NONE);
1207
1208         return cw;
1209 }
1210 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */