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