collection_info_list_to_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 n;
431
432         if (strncmp(data, "COLLECTION:", 11) != 0) return NULL;
433
434         n = (gint)strtol(data + 11, NULL, 10);
435         cd = collection_from_number(n);
436
437         if (!cd || (!list && !info_list))
438                 {
439                 return cd;
440                 }
441         else
442                 {
443                 GList *work = NULL;
444                 GList *infol = NULL;
445                 gint b, e;
446
447                 b = 0;
448                 while (data[b] != '\0' && data[b] != '\n' ) b++;
449                 b++;
450                 e = b;
451
452                 while (data[b] != '\0')
453                         {
454                         CollectInfo *info;
455
456                         while (data[e] != '\n' && data[e] != '\0') e++;
457                         n = (gint)strtol(data + b, NULL, 10);
458
459                         info = g_list_nth_data(cd->list, n);
460                         if (info && list) work = g_list_append(work, file_data_ref(info->fd));
461                         if (info && info_list) infol = g_list_append(infol, info);
462
463                         while (data[e] == '\n') e++;
464                         b = e;
465                         }
466                 if (list) *list = work;
467                 if (info_list) *info_list = infol;
468                 }
469
470         return cd;
471 }
472
473 gchar *collection_info_list_to_dnd_data(CollectionData *cd, GList *list, gint *length)
474 {
475         GList *work;
476         GList *temp = NULL;
477         gchar *ptr;
478         gchar *text;
479         gchar *uri_text;
480         gint collection_number;
481
482         *length = 0;
483         if (!list) return NULL;
484
485         collection_number = collection_to_number(cd);
486         if (collection_number < 0) return NULL;
487
488         text = g_strdup_printf("COLLECTION:%d\n", collection_number);
489         *length += strlen(text);
490         temp = g_list_prepend(temp, text);
491
492         work = list;
493         while (work)
494                 {
495                 gint item_number = g_list_index(cd->list, work->data);
496
497                 work = work->next;
498
499                 if (item_number < 0) continue;
500                 
501                 text = g_strdup_printf("%d\n", item_number);
502                 temp = g_list_prepend(temp, text);
503                 *length += strlen(text);
504                 }
505
506         *length += 1; /* ending nul char */
507
508         uri_text = g_malloc(*length);
509         ptr = uri_text;
510
511         work = g_list_last(temp);
512         while (work)
513                 {
514                 gint len;
515                 gchar *text = work->data;
516
517                 work = work->prev;
518
519                 len = strlen(text);
520                 memcpy(ptr, text, len);
521                 ptr += len;
522                 }
523
524         ptr[0] = '\0';
525
526         string_list_free(temp);
527
528         return uri_text;
529 }
530
531 gint collection_info_valid(CollectionData *cd, CollectInfo *info)
532 {
533         if (collection_to_number(cd) < 0) return FALSE;
534
535         return (g_list_index(cd->list, info) != 0);
536 }
537
538 CollectInfo *collection_next_by_info(CollectionData *cd, CollectInfo *info)
539 {
540         GList *work;
541
542         work = g_list_find(cd->list, info);
543
544         if (!work) return NULL;
545         work = work->next;
546         if (work) return work->data;
547         return NULL;
548 }
549
550 CollectInfo *collection_prev_by_info(CollectionData *cd, CollectInfo *info)
551 {
552         GList *work;
553
554         work = g_list_find(cd->list, info);
555
556         if (!work) return NULL;
557         work = work->prev;
558         if (work) return work->data;
559         return NULL;
560 }
561
562 CollectInfo *collection_get_first(CollectionData *cd)
563 {
564         if (cd->list) return cd->list->data;
565
566         return NULL;
567 }
568
569 CollectInfo *collection_get_last(CollectionData *cd)
570 {
571         GList *list;
572
573         list = g_list_last(cd->list);
574
575         if (list) return list->data;
576
577         return NULL;
578 }
579
580 void collection_set_sort_method(CollectionData *cd, SortType method)
581 {
582         if (!cd) return;
583
584         if (cd->sort_method == method) return;
585
586         cd->sort_method = method;
587         cd->list = collection_list_sort(cd->list, cd->sort_method);
588         if (cd->list) cd->changed = TRUE;
589
590         collection_window_refresh(collection_window_find(cd));
591 }
592
593 void collection_set_update_info_func(CollectionData *cd,
594                                      void (*func)(CollectionData *, CollectInfo *, gpointer), gpointer data)
595 {
596         cd->info_updated_func = func;
597         cd->info_updated_data = data;
598 }
599
600 static CollectInfo *collection_info_new_if_not_exists(CollectionData *cd, struct stat *st, FileData *fd)
601 {
602         CollectInfo *ci;
603
604         if (g_hash_table_lookup(cd->existence, fd->path)) return NULL;
605
606         ci = collection_info_new(fd, st, NULL);
607         if (ci) g_hash_table_insert(cd->existence, fd->path, "");
608         return ci;
609 }
610
611 gint collection_add_check(CollectionData *cd, FileData *fd, gint sorted, gint must_exist)
612 {
613         struct stat st;
614         gint valid;
615
616         if (must_exist)
617                 {
618                 valid = (stat_utf8(fd->path, &st) && !S_ISDIR(st.st_mode));
619                 }
620         else
621                 {
622                 valid = TRUE;
623                 st.st_size = 0;
624                 st.st_mtime = 0;
625                 }
626
627         if (valid)
628                 {
629                 CollectInfo *ci;
630
631                 ci = collection_info_new_if_not_exists(cd, &st, fd);
632                 if (!ci) return FALSE;
633                 DEBUG_3("add to collection: %s", fd->path);
634
635                 cd->list = collection_list_add(cd->list, ci, sorted ? cd->sort_method : SORT_NONE);
636                 cd->changed = TRUE;
637
638                 if (!sorted || cd->sort_method == SORT_NONE)
639                         {
640                         collection_window_add(collection_window_find(cd), ci);
641                         }
642                 else
643                         {
644                         collection_window_insert(collection_window_find(cd), ci);
645                         }
646                 }
647
648         return valid;
649 }
650
651 gint collection_add(CollectionData *cd, FileData *fd, gint sorted)
652 {
653         return collection_add_check(cd, fd, sorted, TRUE);
654 }
655
656 gint collection_insert(CollectionData *cd, FileData *fd, CollectInfo *insert_ci, gint sorted)
657 {
658         struct stat st;
659
660         if (!insert_ci) return collection_add(cd, fd, sorted);
661
662         if (stat_utf8(fd->path, &st) >= 0 && !S_ISDIR(st.st_mode))
663                 {
664                 CollectInfo *ci;
665
666                 ci = collection_info_new_if_not_exists(cd, &st, fd);
667                 if (!ci) return FALSE;
668
669                 DEBUG_3("insert in collection: %s", fd->path);
670
671                 cd->list = collection_list_insert(cd->list, ci, insert_ci, sorted ? cd->sort_method : SORT_NONE);
672                 cd->changed = TRUE;
673
674                 collection_window_insert(collection_window_find(cd), ci);
675
676                 return TRUE;
677                 }
678
679         return FALSE;
680 }
681
682 gint collection_remove(CollectionData *cd, FileData *fd)
683 {
684         CollectInfo *ci;
685
686         ci = collection_list_find_fd(cd->list, fd);
687
688         if (!ci) return FALSE;
689
690         g_hash_table_remove(cd->existence, fd->path);
691
692         cd->list = g_list_remove(cd->list, ci);
693         cd->changed = TRUE;
694
695         collection_window_remove(collection_window_find(cd), ci);
696         collection_info_free(ci);
697
698         return TRUE;
699 }
700
701 static void collection_remove_by_info(CollectionData *cd, CollectInfo *info)
702 {
703         if (!info || !g_list_find(cd->list, info)) return;
704
705         cd->list = g_list_remove(cd->list, info);
706         cd->changed = (cd->list != NULL);
707
708         collection_window_remove(collection_window_find(cd), info);
709         collection_info_free(info);
710 }
711
712 void collection_remove_by_info_list(CollectionData *cd, GList *list)
713 {
714         GList *work;
715
716         if (!list) return;
717
718         if (!list->next)
719                 {
720                 /* more efficient (in collect-table) to remove a single item this way */
721                 collection_remove_by_info(cd, (CollectInfo *)list->data);
722                 return;
723                 }
724
725         work = list;
726         while (work)
727                 {
728                 cd->list = collection_list_remove(cd->list, work->data);
729                 work = work->next;
730                 }
731         cd->changed = (cd->list != NULL);
732
733         collection_window_refresh(collection_window_find(cd));
734 }
735
736 gint collection_rename(CollectionData *cd, FileData *fd)
737 {
738         CollectInfo *ci;
739         ci = collection_list_find_fd(cd->list, fd);
740
741         if (!ci) return FALSE;
742
743         cd->changed = TRUE;
744
745         collection_window_update(collection_window_find(cd), ci);
746
747         return TRUE;
748 }
749
750 void collection_update_geometry(CollectionData *cd)
751 {
752         collection_window_get_geometry(collection_window_find(cd));
753 }
754
755 /*
756  *-------------------------------------------------------------------
757  * simple maintenance for renaming, deleting
758  *-------------------------------------------------------------------
759  */
760
761 static void collection_notify_cb(FileData *fd, NotifyType type, gpointer data)
762 {
763         CollectionData *cd = data;
764
765         if (type != NOTIFY_TYPE_CHANGE || !fd->change) return;
766         
767         switch(fd->change->type)
768                 {
769                 case FILEDATA_CHANGE_MOVE:
770                 case FILEDATA_CHANGE_RENAME:
771                         collection_rename(cd, fd);
772                         break;
773                 case FILEDATA_CHANGE_COPY:
774                         break;
775                 case FILEDATA_CHANGE_DELETE:
776                         while (collection_remove(cd, fd));
777                         break;
778                 case FILEDATA_CHANGE_UNSPECIFIED:
779                         break;
780                 }
781
782 }
783
784
785 /*
786  *-------------------------------------------------------------------
787  * window key presses
788  *-------------------------------------------------------------------
789  */
790
791 static gint collection_window_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data)
792 {
793         CollectWindow *cw = data;
794         gint stop_signal = FALSE;
795         gint edit_val = -1;
796         GList *list;
797
798         if (event->state & GDK_CONTROL_MASK)
799                 {
800                 stop_signal = TRUE;
801                 switch (event->keyval)
802                         {
803                         case '1':
804                                 edit_val = 0;
805                                 break;
806                         case '2':
807                                 edit_val = 1;
808                                 break;
809                         case '3':
810                                 edit_val = 2;
811                                 break;
812                         case '4':
813                                 edit_val = 3;
814                                 break;
815                         case '5':
816                                 edit_val = 4;
817                                 break;
818                         case '6':
819                                 edit_val = 5;
820                                 break;
821                         case '7':
822                                 edit_val = 6;
823                                 break;
824                         case '8':
825                                 edit_val = 7;
826                                 break;
827                         case '9':
828                                 edit_val = 8;
829                                 break;
830                         case '0':
831                                 edit_val = 9;
832                                 break;
833                         case 'A': case 'a':
834                                 if (event->state & GDK_SHIFT_MASK)
835                                         {
836                                         collection_table_unselect_all(cw->table);
837                                         }
838                                 else
839                                         {
840                                         collection_table_select_all(cw->table);
841                                         }
842                                 break;
843                         case 'L': case 'l':
844                                 list = layout_list(NULL);
845                                 if (list)
846                                         {
847                                         collection_table_add_filelist(cw->table, list);
848                                         filelist_free(list);
849                                         }
850                                 break;
851                         case 'C': case 'c':
852                                 file_util_copy(NULL, collection_table_selection_get_list(cw->table), NULL, cw->window);
853                                 break;
854                         case 'M': case 'm':
855                                 file_util_move(NULL, collection_table_selection_get_list(cw->table), NULL, cw->window);
856                                 break;
857                         case 'R': case 'r':
858                                 file_util_rename(NULL, collection_table_selection_get_list(cw->table), cw->window);
859                                 break;
860                         case 'D': case 'd':
861                                 file_util_delete(NULL, collection_table_selection_get_list(cw->table), cw->window);
862                                 break;
863                         case 'P': case 'p':
864                                 info_window_new(NULL, collection_table_selection_get_list(cw->table), NULL);
865                                 break;
866                         case 'S': case 's':
867                                 collection_dialog_save_as(NULL, cw->cd);
868                                 break;
869                         case 'W': case 'w':
870                                 collection_window_close(cw);
871                                 break;
872                         default:
873                                 stop_signal = FALSE;
874                                 break;
875                         }
876                 }
877         else
878                 {
879                 stop_signal = TRUE;
880                 switch (event->keyval)
881                         {
882                         case GDK_Return: case GDK_KP_Enter:
883                                 layout_image_set_collection(NULL, cw->cd,
884                                         collection_table_get_focus_info(cw->table));
885                                 break;
886                         case 'V': case 'v':
887                                 view_window_new_from_collection(cw->cd,
888                                         collection_table_get_focus_info(cw->table));
889                                 break;
890                         case 'S': case 's':
891                                 if (!cw->cd->path)
892                                         {
893                                         collection_dialog_save_as(NULL, cw->cd);
894                                         }
895                                 else if (!collection_save(cw->cd, cw->cd->path))
896                                         {
897                                         log_printf("failed saving to collection path: %s\n", cw->cd->path);
898                                         }
899                                 break;
900                         case 'A': case 'a':
901                                 collection_dialog_append(NULL, cw->cd);
902                                 break;
903                         case 'N': case 'n':
904                                 collection_set_sort_method(cw->cd, SORT_NAME);
905                                 break;
906 #ifdef HAVE_STRVERSCMP
907                         case 'I': case 'i':
908                                 collection_set_sort_method(cw->cd, SORT_NUMBER);
909                                 break;
910 #endif
911                         case 'D': case 'd':
912                                 collection_set_sort_method(cw->cd, SORT_TIME);
913                                 break;
914                         case 'B': case 'b':
915                                 collection_set_sort_method(cw->cd, SORT_SIZE);
916                                 break;
917                         case 'P': case 'p':
918                                 if (event->state & GDK_SHIFT_MASK)
919                                         {
920                                         CollectInfo *info;
921
922                                         info = collection_table_get_focus_info(cw->table);
923
924                                         print_window_new(info->fd, collection_table_selection_get_list(cw->table),
925                                                          collection_list_to_filelist(cw->cd->list), cw->window);
926                                         }
927                                 else
928                                         {
929                                         collection_set_sort_method(cw->cd, SORT_PATH);
930                                         }
931                                 break;
932                         case GDK_Delete: case GDK_KP_Delete:
933                                 list = g_list_copy(cw->table->selection);
934                                 if (list)
935                                         {
936                                         collection_remove_by_info_list(cw->cd, list);
937                                         g_list_free(list);
938                                         }
939                                 else
940                                         {
941                                         collection_remove_by_info(cw->cd, collection_table_get_focus_info(cw->table));
942                                         }
943                                 break;
944                         default:
945                                 stop_signal = FALSE;
946                                 break;
947                         }
948                 }
949
950         if (edit_val != -1)
951                 {
952                 list = collection_table_selection_get_list(cw->table);
953                 file_util_start_editor_from_filelist(edit_val, list, cw->window);
954                 filelist_free(list);
955                 }
956
957         return stop_signal;
958 }
959
960 /*
961  *-------------------------------------------------------------------
962  * window
963  *-------------------------------------------------------------------
964  */
965 static void collection_window_get_geometry(CollectWindow *cw)
966 {
967         CollectionData *cd;
968
969         if (!cw) return;
970
971         cd = cw->cd;
972         gdk_window_get_position(cw->window->window, &cd->window_x, &cd->window_y);
973         gdk_drawable_get_size(cw->window->window, &cd->window_w, &cd->window_h);
974         cd->window_read = TRUE;
975 }
976
977 static void collection_window_refresh(CollectWindow *cw)
978 {
979         if (!cw) return;
980
981         collection_table_refresh(cw->table);
982 }
983
984 static void collection_window_update_title(CollectWindow *cw)
985 {
986         gchar *buf;
987
988         if (!cw) return;
989
990         buf = g_strdup_printf(_("%s - Collection - %s"), cw->cd->name, GQ_APPNAME);
991         gtk_window_set_title(GTK_WINDOW(cw->window), buf);
992         g_free(buf);
993 }
994
995 static void collection_window_update_info(CollectionData *cd, CollectInfo *ci, gpointer data)
996 {
997         CollectWindow *cw = data;
998
999         collection_table_file_update(cw->table, ci);
1000 }
1001
1002 static void collection_window_add(CollectWindow *cw, CollectInfo *ci)
1003 {
1004         if (!cw) return;
1005
1006         if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1007         collection_table_file_add(cw->table, ci);
1008 }
1009
1010 static void collection_window_insert(CollectWindow *cw, CollectInfo *ci)
1011 {
1012         if (!cw) return;
1013
1014         if (!ci->pixbuf) collection_load_thumb_idle(cw->cd);
1015         collection_table_file_insert(cw->table, ci);
1016         if (!cw) return;
1017 }
1018
1019 #if 0
1020 static void collection_window_move(CollectWindow *cw, CollectInfo *ci)
1021 {
1022         if (!cw) return;
1023 }
1024 #endif
1025
1026 static void collection_window_remove(CollectWindow *cw, CollectInfo *ci)
1027 {
1028         if (!cw) return;
1029
1030         collection_table_file_remove(cw->table, ci);
1031 }
1032
1033 static void collection_window_update(CollectWindow *cw, CollectInfo *ci)
1034 {
1035         if (!cw) return;
1036
1037         collection_table_file_update(cw->table, ci);
1038         collection_table_file_update(cw->table, NULL);
1039 }
1040
1041 static void collection_window_close_final(CollectWindow *cw)
1042 {
1043         if (cw->close_dialog) return;
1044
1045         collection_window_list = g_list_remove(collection_window_list, cw);
1046         collection_window_get_geometry(cw);
1047
1048         gtk_widget_destroy(cw->window);
1049
1050         collection_set_update_info_func(cw->cd, NULL, NULL);
1051         collection_unref(cw->cd);
1052
1053         g_free(cw);
1054 }
1055
1056 static void collection_close_save_cb(GenericDialog *gd, gpointer data)
1057 {
1058         CollectWindow *cw = data;
1059
1060         cw->close_dialog = NULL;
1061         generic_dialog_close(gd);
1062
1063         if (!cw->cd->path)
1064                 {
1065                 collection_dialog_save_close(NULL, cw->cd);
1066                 return;
1067                 }
1068         else if (!collection_save(cw->cd, cw->cd->path))
1069                 {
1070                 gchar *buf;
1071                 buf = g_strdup_printf(_("Failed to save the collection:\n%s"), cw->cd->path);
1072                 warning_dialog(_("Save Failed"), buf, GTK_STOCK_DIALOG_ERROR, cw->window);
1073                 g_free(buf);
1074                 return;
1075                 }
1076
1077         collection_window_close_final(cw);
1078 }
1079
1080 static void collection_close_close_cb(GenericDialog *gd, gpointer data)
1081 {
1082         CollectWindow *cw = data;
1083
1084         cw->close_dialog = NULL;
1085         generic_dialog_close(gd);
1086
1087         collection_window_close_final(cw);
1088 }
1089
1090 static void collection_close_cancel_cb(GenericDialog *gd, gpointer data)
1091 {
1092         CollectWindow *cw = data;
1093
1094         cw->close_dialog = NULL;
1095         generic_dialog_close(gd);
1096 }
1097
1098 static void collection_close_dlg_show(CollectWindow *cw)
1099 {
1100         GenericDialog *gd;
1101
1102         if (cw->close_dialog)
1103                 {
1104                 gtk_window_present(GTK_WINDOW(cw->close_dialog));
1105                 return;
1106                 }
1107
1108         gd = generic_dialog_new(_("Close collection"),
1109                                 GQ_WMCLASS, "close_collection", cw->window, FALSE,
1110                                 collection_close_cancel_cb, cw);
1111         generic_dialog_add_message(gd, GTK_STOCK_DIALOG_QUESTION,
1112                                    _("Close collection"),
1113                                    _("Collection has been modified.\nSave first?"));
1114
1115         generic_dialog_add_button(gd, GTK_STOCK_SAVE, NULL, collection_close_save_cb, TRUE);
1116         generic_dialog_add_button(gd, GTK_STOCK_DELETE, _("_Discard"), collection_close_close_cb, FALSE);
1117
1118         cw->close_dialog = gd->dialog;
1119
1120         gtk_widget_show(gd->dialog);
1121 }
1122
1123 static void collection_window_close(CollectWindow *cw)
1124 {
1125         if (!cw->cd->changed && !cw->close_dialog)
1126                 {
1127                 collection_window_close_final(cw);
1128                 return;
1129                 }
1130
1131         collection_close_dlg_show(cw);
1132 }
1133
1134 void collection_window_close_by_collection(CollectionData *cd)
1135 {
1136         CollectWindow *cw;
1137
1138         cw = collection_window_find(cd);
1139         if (cw) collection_window_close_final(cw);
1140 }
1141
1142 gint collection_window_modified_exists(void)
1143 {
1144         GList *work;
1145
1146         work = collection_window_list;
1147         while (work)
1148                 {
1149                 CollectWindow *cw = work->data;
1150                 if (cw->cd->changed) return TRUE;
1151                 work = work->next;
1152                 }
1153
1154         return FALSE;
1155 }
1156
1157 static gint collection_window_delete(GtkWidget *widget, GdkEvent *event, gpointer data)
1158 {
1159         CollectWindow *cw = data;
1160         collection_window_close(cw);
1161
1162         return TRUE;
1163 }
1164
1165 CollectWindow *collection_window_new(const gchar *path)
1166 {
1167         CollectWindow *cw;
1168         GtkWidget *vbox;
1169         GtkWidget *frame;
1170         GtkWidget *status_label;
1171         GtkWidget *extra_label;
1172         GdkGeometry geometry;
1173
1174         cw = g_new0(CollectWindow, 1);
1175
1176         cw->close_dialog = NULL;
1177
1178         collection_window_list = g_list_append(collection_window_list, cw);
1179
1180         cw->cd = collection_new(path);
1181
1182         cw->window = window_new(GTK_WINDOW_TOPLEVEL, "collection", PIXBUF_INLINE_ICON_BOOK, NULL, NULL);
1183
1184         geometry.min_width = 32;
1185         geometry.min_height = 32;
1186         geometry.base_width = COLLECT_DEF_WIDTH;
1187         geometry.base_height = COLLECT_DEF_HEIGHT;
1188         gtk_window_set_geometry_hints(GTK_WINDOW(cw->window), NULL, &geometry,
1189                                       GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE);
1190
1191
1192         if (options->layout.save_window_positions && path && collection_load_only_geometry(cw->cd, path))
1193                 {
1194                 /* FIXME: x, y is not implemented */
1195                 gtk_window_set_default_size(GTK_WINDOW(cw->window), cw->cd->window_w, cw->cd->window_h);
1196                 }
1197         else
1198                 {
1199                 gtk_window_set_default_size(GTK_WINDOW(cw->window), COLLECT_DEF_WIDTH, COLLECT_DEF_HEIGHT);
1200                 }
1201
1202         gtk_window_set_resizable(GTK_WINDOW(cw->window), TRUE);
1203         collection_window_update_title(cw);
1204         gtk_container_set_border_width(GTK_CONTAINER(cw->window), 0);
1205
1206         g_signal_connect(G_OBJECT(cw->window), "delete_event",
1207                          G_CALLBACK(collection_window_delete), cw);
1208
1209         g_signal_connect(G_OBJECT(cw->window), "key_press_event",
1210                          G_CALLBACK(collection_window_keypress), cw);
1211
1212         vbox = gtk_vbox_new(FALSE, 0);
1213         gtk_container_add(GTK_CONTAINER(cw->window), vbox);
1214         gtk_widget_show(vbox);
1215
1216         cw->table = collection_table_new(cw->cd);
1217         gtk_box_pack_start(GTK_BOX(vbox), cw->table->scrolled, TRUE, TRUE, 0);
1218         gtk_widget_show(cw->table->scrolled);
1219
1220         cw->status_box = gtk_hbox_new(TRUE, 0);
1221         gtk_box_pack_start(GTK_BOX(vbox), cw->status_box, FALSE, FALSE, 0);
1222         gtk_widget_show(cw->status_box);
1223
1224         frame = gtk_frame_new(NULL);
1225         gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
1226         gtk_box_pack_start(GTK_BOX(cw->status_box), frame, TRUE, TRUE, 0);
1227         gtk_widget_show(frame);
1228
1229         status_label = gtk_label_new("");
1230         gtk_container_add(GTK_CONTAINER(frame), status_label);
1231         gtk_widget_show(status_label);
1232
1233         extra_label = gtk_progress_bar_new();
1234         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(extra_label), 0.0);
1235         gtk_box_pack_start(GTK_BOX(cw->status_box), extra_label, TRUE, TRUE, 0);
1236         gtk_widget_show(extra_label);
1237
1238         collection_table_set_labels(cw->table, status_label, extra_label);
1239
1240         gtk_widget_show(cw->window);
1241         gtk_widget_grab_focus(cw->table->listview);
1242
1243         collection_set_update_info_func(cw->cd, collection_window_update_info, cw);
1244
1245         if (path && *path == G_DIR_SEPARATOR) collection_load_begin(cw->cd, NULL, COLLECTION_LOAD_NONE);
1246
1247         return cw;
1248 }