Addl fix #299: File Compression and Archiving
[geeqie.git] / src / view_file / view_file.c
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * Author: Laurent Monin
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "main.h"
22 #include "view_file.h"
23
24 #include "dupe.h"
25 #include "collect.h"
26 #include "collect-table.h"
27 #include "editors.h"
28 #include "history_list.h"
29 #include "layout.h"
30 #include "menu.h"
31 #include "misc.h"
32 #include "pixbuf_util.h"
33 #include "thumb.h"
34 #include "ui_menu.h"
35 #include "ui_fileops.h"
36 #include "ui_misc.h"
37 #include "utilops.h"
38 #include "view_file/view_file_list.h"
39 #include "view_file/view_file_icon.h"
40 #include "window.h"
41
42 /*
43  *-----------------------------------------------------------------------------
44  * signals
45  *-----------------------------------------------------------------------------
46  */
47
48 void vf_send_update(ViewFile *vf)
49 {
50         if (vf->func_status) vf->func_status(vf, vf->data_status);
51 }
52
53 /*
54  *-----------------------------------------------------------------------------
55  * misc
56  *-----------------------------------------------------------------------------
57  */
58
59 void vf_sort_set(ViewFile *vf, SortType type, gboolean ascend)
60 {
61         switch (vf->type)
62         {
63         case FILEVIEW_LIST: vflist_sort_set(vf, type, ascend); break;
64         case FILEVIEW_ICON: vficon_sort_set(vf, type, ascend); break;
65         }
66 }
67
68 /*
69  *-----------------------------------------------------------------------------
70  * row stuff
71  *-----------------------------------------------------------------------------
72  */
73
74 FileData *vf_index_get_data(ViewFile *vf, gint row)
75 {
76         return g_list_nth_data(vf->list, row);
77 }
78
79 gint vf_index_by_fd(ViewFile *vf, FileData *fd)
80 {
81         gint ret;
82
83         switch (vf->type)
84         {
85         case FILEVIEW_LIST: ret = vflist_index_by_fd(vf, fd); break;
86         case FILEVIEW_ICON: ret = vficon_index_by_fd(vf, fd); break;
87         default: ret = 0;
88         }
89
90         return ret;
91 }
92
93 guint vf_count(ViewFile *vf, gint64 *bytes)
94 {
95         if (bytes)
96                 {
97                 gint64 b = 0;
98                 GList *work;
99
100                 work = vf->list;
101                 while (work)
102                         {
103                         FileData *fd = work->data;
104                         work = work->next;
105
106                         b += fd->size;
107                         }
108
109                 *bytes = b;
110                 }
111
112         return g_list_length(vf->list);
113 }
114
115 GList *vf_get_list(ViewFile *vf)
116 {
117         GList *list = NULL;
118         GList *work;
119         for (work = vf->list; work; work = work->next)
120                 {
121                 FileData *fd = work->data;
122                 list = g_list_prepend(list, file_data_ref(fd));
123                 }
124
125         return g_list_reverse(list);
126 }
127
128 /*
129  *-------------------------------------------------------------------
130  * keyboard
131  *-------------------------------------------------------------------
132  */
133
134 static gboolean vf_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
135 {
136         ViewFile *vf = data;
137         gboolean ret;
138
139         switch (vf->type)
140         {
141         case FILEVIEW_LIST: ret = vflist_press_key_cb(widget, event, data); break;
142         case FILEVIEW_ICON: ret = vficon_press_key_cb(widget, event, data); break;
143         default: ret = FALSE;
144         }
145
146         return ret;
147 }
148
149 /*
150  *-------------------------------------------------------------------
151  * mouse
152  *-------------------------------------------------------------------
153  */
154
155 static gboolean vf_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
156 {
157         ViewFile *vf = data;
158         gboolean ret;
159
160         switch (vf->type)
161         {
162         case FILEVIEW_LIST: ret = vflist_press_cb(widget, bevent, data); break;
163         case FILEVIEW_ICON: ret = vficon_press_cb(widget, bevent, data); break;
164         default: ret = FALSE;
165         }
166
167         return ret;
168 }
169
170 static gboolean vf_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
171 {
172         ViewFile *vf = data;
173         gboolean ret;
174
175         switch (vf->type)
176         {
177         case FILEVIEW_LIST: ret = vflist_release_cb(widget, bevent, data); break;
178         case FILEVIEW_ICON: ret = vficon_release_cb(widget, bevent, data); break;
179         default: ret = FALSE;
180         }
181
182         return ret;
183 }
184
185
186 /*
187  *-----------------------------------------------------------------------------
188  * selections
189  *-----------------------------------------------------------------------------
190  */
191
192 guint vf_selection_count(ViewFile *vf, gint64 *bytes)
193 {
194         guint ret;
195
196         switch (vf->type)
197         {
198         case FILEVIEW_LIST: ret = vflist_selection_count(vf, bytes); break;
199         case FILEVIEW_ICON: ret = vficon_selection_count(vf, bytes); break;
200         default: ret = 0;
201         }
202
203         return ret;
204 }
205
206 GList *vf_selection_get_list(ViewFile *vf)
207 {
208         GList *ret;
209
210         switch (vf->type)
211         {
212         case FILEVIEW_LIST: ret = vflist_selection_get_list(vf); break;
213         case FILEVIEW_ICON: ret = vficon_selection_get_list(vf); break;
214         default: ret = NULL;
215         }
216
217         return ret;
218 }
219
220 GList *vf_selection_get_list_by_index(ViewFile *vf)
221 {
222         GList *ret;
223
224         switch (vf->type)
225         {
226         case FILEVIEW_LIST: ret = vflist_selection_get_list_by_index(vf); break;
227         case FILEVIEW_ICON: ret = vficon_selection_get_list_by_index(vf); break;
228         default: ret = NULL;
229         }
230
231         return ret;
232 }
233
234 void vf_select_all(ViewFile *vf)
235 {
236         switch (vf->type)
237         {
238         case FILEVIEW_LIST: vflist_select_all(vf); break;
239         case FILEVIEW_ICON: vficon_select_all(vf); break;
240         }
241 }
242
243 void vf_select_none(ViewFile *vf)
244 {
245         switch (vf->type)
246         {
247         case FILEVIEW_LIST: vflist_select_none(vf); break;
248         case FILEVIEW_ICON: vficon_select_none(vf); break;
249         }
250 }
251
252 void vf_select_invert(ViewFile *vf)
253 {
254         switch (vf->type)
255         {
256         case FILEVIEW_LIST: vflist_select_invert(vf); break;
257         case FILEVIEW_ICON: vficon_select_invert(vf); break;
258         }
259 }
260
261 void vf_select_by_fd(ViewFile *vf, FileData *fd)
262 {
263         switch (vf->type)
264         {
265         case FILEVIEW_LIST: vflist_select_by_fd(vf, fd); break;
266         case FILEVIEW_ICON: vficon_select_by_fd(vf, fd); break;
267         }
268 }
269
270 void vf_select_list(ViewFile *vf, GList *list)
271 {
272         switch (vf->type)
273         {
274         case FILEVIEW_LIST: vflist_select_list(vf, list); break;
275         case FILEVIEW_ICON: vficon_select_list(vf, list); break;
276         }
277 }
278
279 void vf_mark_to_selection(ViewFile *vf, gint mark, MarkToSelectionMode mode)
280 {
281         switch (vf->type)
282         {
283         case FILEVIEW_LIST: vflist_mark_to_selection(vf, mark, mode); break;
284         case FILEVIEW_ICON: vficon_mark_to_selection(vf, mark, mode); break;
285         }
286 }
287
288 void vf_selection_to_mark(ViewFile *vf, gint mark, SelectionToMarkMode mode)
289 {
290         switch (vf->type)
291         {
292         case FILEVIEW_LIST: vflist_selection_to_mark(vf, mark, mode); break;
293         case FILEVIEW_ICON: vficon_selection_to_mark(vf, mark, mode); break;
294         }
295 }
296
297 /*
298  *-----------------------------------------------------------------------------
299  * dnd
300  *-----------------------------------------------------------------------------
301  */
302
303
304 static void vf_dnd_init(ViewFile *vf)
305 {
306         switch (vf->type)
307         {
308         case FILEVIEW_LIST: vflist_dnd_init(vf); break;
309         case FILEVIEW_ICON: vficon_dnd_init(vf); break;
310         }
311 }
312
313 /*
314  *-----------------------------------------------------------------------------
315  * pop-up menu
316  *-----------------------------------------------------------------------------
317  */
318
319 GList *vf_pop_menu_file_list(ViewFile *vf)
320 {
321         GList *ret;
322
323         switch (vf->type)
324         {
325         case FILEVIEW_LIST: ret = vflist_pop_menu_file_list(vf); break;
326         case FILEVIEW_ICON: ret = vficon_pop_menu_file_list(vf); break;
327         default: ret = NULL;
328         }
329
330         return ret;
331 }
332
333 GList *vf_selection_get_one(ViewFile *vf, FileData *fd)
334 {
335         GList *ret;
336
337         switch (vf->type)
338         {
339         case FILEVIEW_LIST: ret = vflist_selection_get_one(vf, fd); break;
340         case FILEVIEW_ICON: ret = vficon_selection_get_one(vf, fd); break;
341         default: ret = NULL;
342         }
343
344         return ret;
345 }
346
347 static void vf_pop_menu_edit_cb(GtkWidget *widget, gpointer data)
348 {
349         ViewFile *vf;
350         const gchar *key = data;
351
352         vf = submenu_item_get_data(widget);
353
354         if (!vf) return;
355
356         file_util_start_editor_from_filelist(key, vf_pop_menu_file_list(vf), vf->dir_fd->path, vf->listview);
357 }
358
359 static void vf_pop_menu_view_cb(GtkWidget *widget, gpointer data)
360 {
361         ViewFile *vf = data;
362
363         switch (vf->type)
364         {
365         case FILEVIEW_LIST: vflist_pop_menu_view_cb(widget, data); break;
366         case FILEVIEW_ICON: vficon_pop_menu_view_cb(widget, data); break;
367         }
368 }
369
370 static void vf_pop_menu_open_archive_cb(GtkWidget *widget, gpointer data)
371 {
372         ViewFile *vf = data;
373         LayoutWindow *lw_new;
374         FileData *fd;
375         gchar *dest_dir;
376
377         switch (vf->type)
378         {
379         case FILEVIEW_LIST:
380                 fd = (VFLIST(vf)->click_fd);
381                 break;
382         case FILEVIEW_ICON:
383                 fd = (VFICON(vf)->click_fd);
384                 break;
385         }
386
387         dest_dir = open_archive(fd);
388         if (dest_dir)
389                 {
390                 lw_new = layout_new_from_default();
391                 layout_set_path(lw_new, dest_dir);
392                 g_free(dest_dir);
393                 }
394         else
395                 {
396                 warning_dialog(_("Cannot open archive file"), _("See the Log Window"), GTK_STOCK_DIALOG_WARNING, NULL);
397                 }
398 }
399
400 static void vf_pop_menu_copy_cb(GtkWidget *widget, gpointer data)
401 {
402         ViewFile *vf = data;
403
404         file_util_copy(NULL, vf_pop_menu_file_list(vf), NULL, vf->listview);
405 }
406
407 static void vf_pop_menu_move_cb(GtkWidget *widget, gpointer data)
408 {
409         ViewFile *vf = data;
410
411         file_util_move(NULL, vf_pop_menu_file_list(vf), NULL, vf->listview);
412 }
413
414 static void vf_pop_menu_rename_cb(GtkWidget *widget, gpointer data)
415 {
416         ViewFile *vf = data;
417
418         switch (vf->type)
419         {
420         case FILEVIEW_LIST: vflist_pop_menu_rename_cb(widget, data); break;
421         case FILEVIEW_ICON: vficon_pop_menu_rename_cb(widget, data); break;
422         }
423 }
424
425 static void vf_pop_menu_delete_cb(GtkWidget *widget, gpointer data)
426 {
427         ViewFile *vf = data;
428
429         options->file_ops.safe_delete_enable = FALSE;
430         file_util_delete(NULL, vf_pop_menu_file_list(vf), vf->listview);
431 }
432
433 static void vf_pop_menu_move_to_trash_cb(GtkWidget *widget, gpointer data)
434 {
435         ViewFile *vf = data;
436
437         options->file_ops.safe_delete_enable = TRUE;
438         file_util_delete(NULL, vf_pop_menu_file_list(vf), vf->listview);
439 }
440
441 static void vf_pop_menu_copy_path_cb(GtkWidget *widget, gpointer data)
442 {
443         ViewFile *vf = data;
444
445         file_util_copy_path_list_to_clipboard(vf_pop_menu_file_list(vf), TRUE);
446 }
447
448 static void vf_pop_menu_copy_path_unquoted_cb(GtkWidget *widget, gpointer data)
449 {
450         ViewFile *vf = data;
451
452         file_util_copy_path_list_to_clipboard(vf_pop_menu_file_list(vf), FALSE);
453 }
454
455 static void vf_pop_menu_enable_grouping_cb(GtkWidget *widget, gpointer data)
456 {
457         ViewFile *vf = data;
458
459         file_data_disable_grouping_list(vf_pop_menu_file_list(vf), FALSE);
460 }
461
462 static void vf_pop_menu_duplicates_cb(GtkWidget *widget, gpointer data)
463 {
464         ViewFile *vf = data;
465         DupeWindow *dw;
466
467         dw = dupe_window_new();
468         dupe_window_add_files(dw, vf_pop_menu_file_list(vf), FALSE);
469 }
470
471 static void vf_pop_menu_disable_grouping_cb(GtkWidget *widget, gpointer data)
472 {
473         ViewFile *vf = data;
474
475         file_data_disable_grouping_list(vf_pop_menu_file_list(vf), TRUE);
476 }
477
478 static void vf_pop_menu_sort_cb(GtkWidget *widget, gpointer data)
479 {
480         ViewFile *vf;
481         SortType type;
482
483         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
484
485         vf = submenu_item_get_data(widget);
486         if (!vf) return;
487
488         type = (SortType)GPOINTER_TO_INT(data);
489
490         if (type == SORT_EXIFTIME || type == SORT_EXIFTIMEDIGITIZED || type == SORT_RATING)
491                 {
492                 vf_read_metadata_in_idle(vf);
493                 }
494
495         if (vf->layout)
496                 {
497                 layout_sort_set(vf->layout, type, vf->sort_ascend);
498                 }
499         else
500                 {
501                 vf_sort_set(vf, type, vf->sort_ascend);
502                 }
503 }
504
505 static void vf_pop_menu_sort_ascend_cb(GtkWidget *widget, gpointer data)
506 {
507         ViewFile *vf = data;
508
509         if (vf->layout)
510                 {
511                 layout_sort_set(vf->layout, vf->sort_method, !vf->sort_ascend);
512                 }
513         else
514                 {
515                 vf_sort_set(vf, vf->sort_method, !vf->sort_ascend);
516                 }
517 }
518
519 static void vf_pop_menu_sel_mark_cb(GtkWidget *widget, gpointer data)
520 {
521         ViewFile *vf = data;
522         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_SET);
523 }
524
525 static void vf_pop_menu_sel_mark_and_cb(GtkWidget *widget, gpointer data)
526 {
527         ViewFile *vf = data;
528         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_AND);
529 }
530
531 static void vf_pop_menu_sel_mark_or_cb(GtkWidget *widget, gpointer data)
532 {
533         ViewFile *vf = data;
534         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_OR);
535 }
536
537 static void vf_pop_menu_sel_mark_minus_cb(GtkWidget *widget, gpointer data)
538 {
539         ViewFile *vf = data;
540         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_MINUS);
541 }
542
543 static void vf_pop_menu_set_mark_sel_cb(GtkWidget *widget, gpointer data)
544 {
545         ViewFile *vf = data;
546         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_SET);
547 }
548
549 static void vf_pop_menu_res_mark_sel_cb(GtkWidget *widget, gpointer data)
550 {
551         ViewFile *vf = data;
552         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_RESET);
553 }
554
555 static void vf_pop_menu_toggle_mark_sel_cb(GtkWidget *widget, gpointer data)
556 {
557         ViewFile *vf = data;
558         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_TOGGLE);
559 }
560
561 static void vf_pop_menu_toggle_view_type_cb(GtkWidget *widget, gpointer data)
562 {
563         ViewFile *vf = data;
564         FileViewType new_type = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
565         if (!vf->layout) return;
566
567         layout_views_set(vf->layout, vf->layout->options.dir_view_type, new_type);
568 }
569
570 static void vf_pop_menu_refresh_cb(GtkWidget *widget, gpointer data)
571 {
572         ViewFile *vf = data;
573
574         switch (vf->type)
575         {
576         case FILEVIEW_LIST: vflist_pop_menu_refresh_cb(widget, data); break;
577         case FILEVIEW_ICON: vficon_pop_menu_refresh_cb(widget, data); break;
578         }
579 }
580
581 static void vf_popup_destroy_cb(GtkWidget *widget, gpointer data)
582 {
583         ViewFile *vf = data;
584
585         switch (vf->type)
586         {
587         case FILEVIEW_LIST: vflist_popup_destroy_cb(widget, data); break;
588         case FILEVIEW_ICON: vficon_popup_destroy_cb(widget, data); break;
589         }
590
591         filelist_free(vf->editmenu_fd_list);
592         vf->editmenu_fd_list = NULL;
593 }
594
595 /**
596  * @brief Add file selection list to a collection
597  * @param[in] widget 
598  * @param[in] data Index to the collection list menu item selected, or -1 for new collection
599  * 
600  * 
601  */
602 static void vf_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
603 {
604         ViewFile *vf;
605         GList *selection_list;
606
607         vf = submenu_item_get_data(widget);
608         selection_list = vf_selection_get_list(vf);
609         pop_menu_collections(selection_list, data);
610
611         filelist_free(selection_list);
612 }
613
614 GtkWidget *vf_pop_menu(ViewFile *vf)
615 {
616         GtkWidget *menu;
617         GtkWidget *item;
618         GtkWidget *submenu;
619         gboolean active = FALSE;
620         gboolean class_archive = FALSE;
621
622         switch (vf->type)
623         {
624         case FILEVIEW_LIST:
625                 vflist_color_set(vf, VFLIST(vf)->click_fd, TRUE);
626                 active = (VFLIST(vf)->click_fd != NULL);
627                 class_archive = (VFLIST(vf)->click_fd->format_class == FORMAT_CLASS_ARCHIVE);
628                 break;
629         case FILEVIEW_ICON:
630                 active = (VFICON(vf)->click_fd != NULL);
631                 class_archive = (VFICON(vf)->click_fd->format_class == FORMAT_CLASS_ARCHIVE);
632                 break;
633         }
634
635         menu = popup_menu_short_lived();
636
637         g_signal_connect(G_OBJECT(menu), "destroy",
638                          G_CALLBACK(vf_popup_destroy_cb), vf);
639
640         if (vf->clicked_mark > 0)
641                 {
642                 gint mark = vf->clicked_mark;
643                 gchar *str_set_mark = g_strdup_printf(_("_Set mark %d"), mark);
644                 gchar *str_res_mark = g_strdup_printf(_("_Reset mark %d"), mark);
645                 gchar *str_toggle_mark = g_strdup_printf(_("_Toggle mark %d"), mark);
646                 gchar *str_sel_mark = g_strdup_printf(_("_Select mark %d"), mark);
647                 gchar *str_sel_mark_or = g_strdup_printf(_("_Add mark %d"), mark);
648                 gchar *str_sel_mark_and = g_strdup_printf(_("_Intersection with mark %d"), mark);
649                 gchar *str_sel_mark_minus = g_strdup_printf(_("_Unselect mark %d"), mark);
650
651                 g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
652
653                 vf->active_mark = mark;
654                 vf->clicked_mark = 0;
655
656                 menu_item_add_sensitive(menu, str_set_mark, active,
657                                         G_CALLBACK(vf_pop_menu_set_mark_sel_cb), vf);
658
659                 menu_item_add_sensitive(menu, str_res_mark, active,
660                                         G_CALLBACK(vf_pop_menu_res_mark_sel_cb), vf);
661
662                 menu_item_add_sensitive(menu, str_toggle_mark, active,
663                                         G_CALLBACK(vf_pop_menu_toggle_mark_sel_cb), vf);
664
665                 menu_item_add_divider(menu);
666
667                 menu_item_add_sensitive(menu, str_sel_mark, active,
668                                         G_CALLBACK(vf_pop_menu_sel_mark_cb), vf);
669                 menu_item_add_sensitive(menu, str_sel_mark_or, active,
670                                         G_CALLBACK(vf_pop_menu_sel_mark_or_cb), vf);
671                 menu_item_add_sensitive(menu, str_sel_mark_and, active,
672                                         G_CALLBACK(vf_pop_menu_sel_mark_and_cb), vf);
673                 menu_item_add_sensitive(menu, str_sel_mark_minus, active,
674                                         G_CALLBACK(vf_pop_menu_sel_mark_minus_cb), vf);
675
676                 menu_item_add_divider(menu);
677
678                 g_free(str_set_mark);
679                 g_free(str_res_mark);
680                 g_free(str_toggle_mark);
681                 g_free(str_sel_mark);
682                 g_free(str_sel_mark_and);
683                 g_free(str_sel_mark_or);
684                 g_free(str_sel_mark_minus);
685                 }
686
687         vf->editmenu_fd_list = vf_pop_menu_file_list(vf);
688         submenu_add_edit(menu, &item, G_CALLBACK(vf_pop_menu_edit_cb), vf, vf->editmenu_fd_list);
689         gtk_widget_set_sensitive(item, active);
690
691         menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, active,
692                                       G_CALLBACK(vf_pop_menu_view_cb), vf);
693
694         menu_item_add_stock_sensitive(menu, _("Open archive"), GTK_STOCK_OPEN, active & class_archive, G_CALLBACK(vf_pop_menu_open_archive_cb), vf);
695
696         menu_item_add_divider(menu);
697         menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, active,
698                                       G_CALLBACK(vf_pop_menu_copy_cb), vf);
699         menu_item_add_sensitive(menu, _("_Move..."), active,
700                                 G_CALLBACK(vf_pop_menu_move_cb), vf);
701         menu_item_add_sensitive(menu, _("_Rename..."), active,
702                                 G_CALLBACK(vf_pop_menu_rename_cb), vf);
703         menu_item_add_sensitive(menu, _("_Copy path"), active,
704                                 G_CALLBACK(vf_pop_menu_copy_path_cb), vf);
705         menu_item_add_sensitive(menu, _("_Copy path unquoted"), active,
706                                 G_CALLBACK(vf_pop_menu_copy_path_unquoted_cb), vf);
707         menu_item_add_divider(menu);
708         menu_item_add_stock_sensitive(menu,
709                                 options->file_ops.confirm_move_to_trash ? _("Move to Trash...") :
710                                         _("Move to Trash"), PIXBUF_INLINE_ICON_TRASH, active,
711                                 G_CALLBACK(vf_pop_menu_move_to_trash_cb), vf);
712         menu_item_add_stock_sensitive(menu,
713                                 options->file_ops.confirm_delete ? _("_Delete...") :
714                                         _("_Delete"), GTK_STOCK_DELETE, active,
715                                 G_CALLBACK(vf_pop_menu_delete_cb), vf);
716         menu_item_add_divider(menu);
717
718         menu_item_add_sensitive(menu, _("Enable file _grouping"), active,
719                                 G_CALLBACK(vf_pop_menu_enable_grouping_cb), vf);
720         menu_item_add_sensitive(menu, _("Disable file groupi_ng"), active,
721                                 G_CALLBACK(vf_pop_menu_disable_grouping_cb), vf);
722
723         menu_item_add_divider(menu);
724         menu_item_add_stock_sensitive(menu, _("_Find duplicates..."), GTK_STOCK_FIND, active,
725                                 G_CALLBACK(vf_pop_menu_duplicates_cb), vf);
726         menu_item_add_divider(menu);
727
728         submenu = submenu_add_collections(menu, &item,
729                                 G_CALLBACK(vf_pop_menu_collections_cb), vf);
730         gtk_widget_set_sensitive(item, active);
731         menu_item_add_divider(menu);
732
733         submenu = submenu_add_sort(NULL, G_CALLBACK(vf_pop_menu_sort_cb), vf,
734                                    FALSE, FALSE, TRUE, vf->sort_method);
735         menu_item_add_divider(submenu);
736         menu_item_add_check(submenu, _("Ascending"), vf->sort_ascend,
737                             G_CALLBACK(vf_pop_menu_sort_ascend_cb), vf);
738
739         item = menu_item_add(menu, _("_Sort"), NULL, NULL);
740         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
741
742         item = menu_item_add_radio(menu, _("View as _List"), GINT_TO_POINTER(FILEVIEW_LIST), vf->type == FILEVIEW_LIST,
743                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
744
745         item = menu_item_add_radio(menu, _("View as _Icons"), GINT_TO_POINTER(FILEVIEW_ICON), vf->type == FILEVIEW_ICON,
746                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
747
748         switch (vf->type)
749         {
750         case FILEVIEW_LIST:
751                 menu_item_add_check(menu, _("Show _thumbnails"), VFLIST(vf)->thumbs_enabled,
752                                     G_CALLBACK(vflist_pop_menu_thumbs_cb), vf);
753                 break;
754         case FILEVIEW_ICON:
755                 menu_item_add_check(menu, _("Show filename _text"), VFICON(vf)->show_text,
756                                     G_CALLBACK(vficon_pop_menu_show_names_cb), vf);
757                 break;
758         }
759
760         switch (vf->type)
761         {
762         case FILEVIEW_LIST:
763                 menu_item_add_check(menu, _("Show star rating"), options->show_star_rating,
764                                     G_CALLBACK(vflist_pop_menu_show_star_rating_cb), vf);
765                 break;
766         case FILEVIEW_ICON:
767                 menu_item_add_check(menu, _("Show star rating"), options->show_star_rating,
768                                     G_CALLBACK(vficon_pop_menu_show_star_rating_cb), vf);
769                 break;
770         }
771
772         menu_item_add_stock(menu, _("Re_fresh"), GTK_STOCK_REFRESH, G_CALLBACK(vf_pop_menu_refresh_cb), vf);
773
774         return menu;
775 }
776
777 gboolean vf_refresh(ViewFile *vf)
778 {
779         gboolean ret;
780
781         switch (vf->type)
782         {
783         case FILEVIEW_LIST: ret = vflist_refresh(vf); break;
784         case FILEVIEW_ICON: ret = vficon_refresh(vf); break;
785         default: ret = FALSE;
786         }
787
788         return ret;
789 }
790
791 gboolean vf_set_fd(ViewFile *vf, FileData *dir_fd)
792 {
793         gboolean ret;
794
795         switch (vf->type)
796         {
797         case FILEVIEW_LIST: ret = vflist_set_fd(vf, dir_fd); break;
798         case FILEVIEW_ICON: ret = vficon_set_fd(vf, dir_fd); break;
799         default: ret = FALSE;
800         }
801
802         return ret;
803 }
804
805 static void vf_destroy_cb(GtkWidget *widget, gpointer data)
806 {
807         ViewFile *vf = data;
808
809         switch (vf->type)
810         {
811         case FILEVIEW_LIST: vflist_destroy_cb(widget, data); break;
812         case FILEVIEW_ICON: vficon_destroy_cb(widget, data); break;
813         }
814
815         if (vf->popup)
816                 {
817                 g_signal_handlers_disconnect_matched(G_OBJECT(vf->popup), G_SIGNAL_MATCH_DATA,
818                                                      0, 0, 0, NULL, vf);
819                 gtk_widget_destroy(vf->popup);
820                 }
821
822         if (vf->read_metadata_in_idle_id)
823                 {
824                 g_idle_remove_by_data(vf);
825                 }
826         file_data_unref(vf->dir_fd);
827         g_free(vf->info);
828         g_free(vf);
829 }
830
831 static void vf_marks_filter_toggle_cb(GtkWidget *widget, gpointer data)
832 {
833         ViewFile *vf = data;
834         vf_refresh_idle(vf);
835 }
836
837 typedef struct _MarksTextEntry MarksTextEntry;
838 struct _MarksTextEntry {
839         GenericDialog *gd;
840         gint mark_no;
841         GtkWidget *edit_widget;
842         gchar *text_entry;
843         GtkWidget *parent;
844 };
845
846 static void vf_marks_tooltip_cancel_cb(GenericDialog *gd, gpointer data)
847 {
848         MarksTextEntry *mte = data;
849
850         g_free(mte->text_entry);
851         generic_dialog_close(gd);
852 }
853
854 static void vf_marks_tooltip_ok_cb(GenericDialog *gd, gpointer data)
855 {
856         MarksTextEntry *mte = data;
857
858         g_free(options->marks_tooltips[mte->mark_no]);
859         options->marks_tooltips[mte->mark_no] = g_strdup(gtk_entry_get_text(GTK_ENTRY(mte->edit_widget)));
860
861         gtk_widget_set_tooltip_text(mte->parent, options->marks_tooltips[mte->mark_no]);
862
863         g_free(mte->text_entry);
864         generic_dialog_close(gd);
865 }
866
867 void vf_marks_filter_on_icon_press(GtkEntry *entry, GtkEntryIconPosition pos,
868                                                                         GdkEvent *event, gpointer userdata)
869 {
870         MarksTextEntry *mte = userdata;
871
872         g_free(mte->text_entry);
873         mte->text_entry = g_strdup("");
874         gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), "");
875 }
876
877 static void vf_marks_tooltip_help_cb(GenericDialog *gd, gpointer data)
878 {
879         help_window_show("GuideImageMarks.html");
880 }
881
882 static gboolean vf_marks_tooltip_cb(GtkWidget *widget,
883                                                                                 GdkEventButton *event,
884                                                                                 gpointer user_data)
885 {
886         GtkWidget *table;
887         gint i = GPOINTER_TO_INT(user_data);
888         MarksTextEntry *mte;
889
890         if (event->button == MOUSE_BUTTON_RIGHT)
891                 {
892                 mte = g_new0(MarksTextEntry, 1);
893                 mte->mark_no = i;
894                 mte->text_entry = g_strdup(options->marks_tooltips[i]);
895                 mte->parent = widget;
896
897                 mte->gd = generic_dialog_new(_("Mark text"), "mark_text",
898                                                 widget, FALSE,
899                                                 vf_marks_tooltip_cancel_cb, mte);
900                 generic_dialog_add_message(mte->gd, GTK_STOCK_DIALOG_QUESTION, _("Set mark text"),
901                                             _("This will set or clear the mark text."), FALSE);
902                 generic_dialog_add_button(mte->gd, GTK_STOCK_OK, NULL,
903                                                         vf_marks_tooltip_ok_cb, TRUE);
904                 generic_dialog_add_button(mte->gd, GTK_STOCK_HELP, NULL,
905                                                 vf_marks_tooltip_help_cb, FALSE);
906
907                 table = pref_table_new(mte->gd->vbox, 3, 1, FALSE, TRUE);
908                 pref_table_label(table, 0, 0, g_strdup_printf("%s%d", _("Mark "), mte->mark_no + 1), 1.0);
909                 mte->edit_widget = gtk_entry_new();
910                 gtk_widget_set_size_request(mte->edit_widget, 300, -1);
911                 if (mte->text_entry)
912                         {
913                         gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), mte->text_entry);
914                         }
915                 gtk_table_attach_defaults(GTK_TABLE(table), mte->edit_widget, 1, 2, 0, 1);
916                 generic_dialog_attach_default(mte->gd, mte->edit_widget);
917
918                 gtk_entry_set_icon_from_stock(GTK_ENTRY(mte->edit_widget),
919                                                         GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
920                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY(mte->edit_widget),
921                                                         GTK_ENTRY_ICON_SECONDARY, "Clear");
922                 g_signal_connect(GTK_ENTRY(mte->edit_widget), "icon-press",
923                                                         G_CALLBACK(vf_marks_filter_on_icon_press), mte);
924
925                 gtk_widget_show(mte->edit_widget);
926                 gtk_widget_grab_focus(mte->edit_widget);
927                 gtk_widget_show(GTK_WIDGET(mte->gd->dialog));
928
929                 return TRUE;
930                 }
931
932         return FALSE;
933 }
934
935 static void vf_file_filter_save_cb(GtkWidget *widget, gpointer data)
936 {
937         ViewFile *vf = data;
938         gchar *entry_text;
939         gchar *remove_text = NULL;
940         gchar *index_text = NULL;
941         gboolean text_found = FALSE;
942         gint i;
943
944         entry_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)))));
945
946         if (entry_text[0] == '\0' && vf->file_filter.last_selected >= 0)
947                 {
948                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), vf->file_filter.last_selected);
949                 remove_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
950                 history_list_item_remove("file_filter", remove_text);
951                 gtk_combo_box_text_remove(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), vf->file_filter.last_selected);
952                 g_free(remove_text);
953
954                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), -1);
955                 vf->file_filter.last_selected = - 1;
956                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo))), "");
957                 vf->file_filter.count--;
958                 }
959         else
960                 {
961                 if (entry_text[0] != '\0')
962                         {
963                         for (i = 0; i < vf->file_filter.count; i++)
964                                 {
965                                 if (index_text)
966                                         {
967                                         g_free(index_text);
968                                         }
969                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), i);
970                                 index_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
971
972                                 if (g_strcmp0(index_text, entry_text) == 0)
973                                         {
974                                         text_found = TRUE;
975                                         break;
976                                         }
977                                 }
978
979                         g_free(index_text);
980                         if (!text_found)
981                                 {
982                                 history_list_add_to_key("file_filter", entry_text, 10);
983                                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), entry_text);
984                                 vf->file_filter.count++;
985                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), vf->file_filter.count - 1);
986                                 }
987                         }
988                 }
989         vf_refresh(vf);
990
991         g_free(entry_text);
992 }
993
994 static void vf_file_filter_cb(GtkWidget *widget, gpointer data)
995 {
996         ViewFile *vf = data;
997
998         vf_refresh(vf);
999 }
1000
1001 static gboolean vf_file_filter_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1002 {
1003         ViewFile *vf = data;
1004         vf->file_filter.last_selected = gtk_combo_box_get_active(GTK_COMBO_BOX(vf->file_filter.combo));
1005
1006         gtk_widget_grab_focus(widget);
1007
1008         return TRUE;
1009 }
1010
1011 static GtkWidget *vf_marks_filter_init(ViewFile *vf)
1012 {
1013         GtkWidget *frame = gtk_frame_new(NULL);
1014         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
1015
1016         gint i;
1017
1018         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
1019                 {
1020                 GtkWidget *check = gtk_check_button_new();
1021                 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, FALSE, 0);
1022                 g_signal_connect(G_OBJECT(check), "toggled",
1023                          G_CALLBACK(vf_marks_filter_toggle_cb), vf);
1024                 g_signal_connect(G_OBJECT(check), "button_press_event",
1025                          G_CALLBACK(vf_marks_tooltip_cb), GINT_TO_POINTER(i));
1026                 gtk_widget_set_tooltip_text(check, options->marks_tooltips[i]);
1027
1028                 gtk_widget_show(check);
1029                 vf->filter_check[i] = check;
1030                 }
1031         gtk_container_add(GTK_CONTAINER(frame), hbox);
1032         gtk_widget_show(hbox);
1033         return frame;
1034 }
1035
1036 void vf_file_filter_set(ViewFile *vf, gboolean enable)
1037 {
1038         if (enable)
1039                 {
1040                 gtk_widget_show(vf->file_filter.combo);
1041                 gtk_widget_show(vf->file_filter.frame);
1042                 }
1043         else
1044                 {
1045                 gtk_widget_hide(vf->file_filter.combo);
1046                 gtk_widget_hide(vf->file_filter.frame);
1047                 }
1048
1049         vf_refresh(vf);
1050 }
1051
1052 static gboolean vf_file_filter_class_cb(GtkWidget *widget, gpointer data)
1053 {
1054         ViewFile *vf = data;
1055         gint i;
1056
1057         gboolean state = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
1058
1059         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
1060                 {
1061                 if (g_strcmp0(format_class_list[i], gtk_menu_item_get_label(GTK_MENU_ITEM(widget))) == 0)
1062                         {
1063                         options->class_filter[i] = state;
1064                         }
1065                 }
1066         vf_refresh(vf);
1067
1068         return TRUE;
1069 }
1070
1071 static gboolean vf_file_filter_class_set_all_cb(GtkWidget *widget, gpointer data)
1072 {
1073         ViewFile *vf = data;
1074         GtkWidget *parent;
1075         GList *children;
1076         GtkWidget *child;
1077         gint i;
1078         gboolean state;
1079
1080         if (g_strcmp0(_("Select all"), gtk_menu_item_get_label(GTK_MENU_ITEM(widget))) == 0)
1081                 {
1082                 state = TRUE;
1083                 }
1084         else
1085                 {
1086                 state = FALSE;
1087                 }
1088
1089         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
1090                 {
1091                 options->class_filter[i] = state;
1092                 }
1093
1094         i = 0;
1095         parent = gtk_widget_get_parent(widget);
1096         children = gtk_container_get_children(GTK_CONTAINER(parent));
1097         while (children)
1098                 {
1099                 child = children->data;
1100                 if (i < FILE_FORMAT_CLASSES)
1101                         {
1102                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(child), state);
1103                         }
1104                 i++;
1105                 children = children->next;
1106                 }
1107         g_list_free(children);
1108         vf_refresh(vf);
1109
1110         return TRUE;
1111 }
1112
1113 static GtkWidget *class_filter_menu (ViewFile *vf)
1114 {
1115         GtkWidget *menu;
1116         GtkWidget *menu_item;
1117         int i;
1118
1119         menu = gtk_menu_new();
1120
1121         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
1122             {
1123                 menu_item = gtk_check_menu_item_new_with_label(format_class_list[i]);
1124                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), options->class_filter[i]);
1125                 g_signal_connect(G_OBJECT(menu_item), "toggled", G_CALLBACK(vf_file_filter_class_cb), vf);
1126                 gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1127                 gtk_widget_show(menu_item);
1128                 }
1129
1130         menu_item = gtk_menu_item_new_with_label(_("Select all"));
1131         gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1132         gtk_widget_show(menu_item);
1133         g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(vf_file_filter_class_set_all_cb), vf);
1134
1135         menu_item = gtk_menu_item_new_with_label(_("Select none"));
1136         gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1137         gtk_widget_show(menu_item);
1138         g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(vf_file_filter_class_set_all_cb), vf);
1139
1140         return menu;
1141 }
1142
1143 static void case_sensitive_cb(GtkWidget *widget, gpointer data)
1144 {
1145         ViewFile *vf = data;
1146
1147         vf->file_filter.case_sensitive = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
1148         vf_refresh(vf);
1149 }
1150
1151 static GtkWidget *vf_file_filter_init(ViewFile *vf)
1152 {
1153         GtkWidget *frame = gtk_frame_new(NULL);
1154         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
1155         GList *work;
1156         gint n = 0;
1157         GtkWidget *combo_entry;
1158         GtkWidget *menubar;
1159         GtkWidget *menuitem;
1160         GtkWidget *case_sensitive;
1161
1162         vf->file_filter.combo = gtk_combo_box_text_new_with_entry();
1163         combo_entry = gtk_bin_get_child(GTK_BIN(vf->file_filter.combo));
1164         gtk_widget_show(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)));
1165         gtk_widget_show((GTK_WIDGET(vf->file_filter.combo)));
1166         gtk_widget_set_tooltip_text(GTK_WIDGET(vf->file_filter.combo), "Use regular expressions");
1167
1168         work = history_list_get_by_key("file_filter");
1169         while (work)
1170                 {
1171                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), (gchar *)work->data);
1172                 work = work->next;
1173                 n++;
1174                 vf->file_filter.count = n;
1175                 }
1176         gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), 0);
1177
1178         g_signal_connect(G_OBJECT(combo_entry), "activate",
1179                 G_CALLBACK(vf_file_filter_save_cb), vf);
1180                 
1181         g_signal_connect(G_OBJECT(vf->file_filter.combo), "changed",
1182                 G_CALLBACK(vf_file_filter_cb), vf);
1183
1184         g_signal_connect(G_OBJECT(combo_entry), "button_press_event",
1185                          G_CALLBACK(vf_file_filter_press_cb), vf);
1186
1187         gtk_box_pack_start(GTK_BOX(hbox), vf->file_filter.combo, FALSE, FALSE, 0);
1188         gtk_widget_show(vf->file_filter.combo);
1189         gtk_container_add(GTK_CONTAINER(frame), hbox);
1190         gtk_widget_show(hbox);
1191
1192         case_sensitive = gtk_check_button_new_with_label("Case");
1193         gtk_box_pack_start(GTK_BOX(hbox), case_sensitive, FALSE, FALSE, 0);
1194         gtk_widget_set_tooltip_text(GTK_WIDGET(case_sensitive), _("Case sensitive"));
1195         g_signal_connect(G_OBJECT(case_sensitive), "clicked", G_CALLBACK(case_sensitive_cb), vf);
1196         gtk_widget_show(case_sensitive);
1197
1198         menubar = gtk_menu_bar_new();
1199         gtk_box_pack_start(GTK_BOX(hbox), menubar, FALSE, TRUE, 0);
1200         gtk_widget_show(menubar);
1201
1202         menuitem = gtk_menu_item_new_with_label(_("Class"));
1203         gtk_widget_set_tooltip_text(GTK_WIDGET(menuitem), _("Select Class filter"));
1204         gtk_menu_item_set_submenu(GTK_MENU_ITEM (menuitem), class_filter_menu(vf));
1205         gtk_menu_shell_append(GTK_MENU_SHELL (menubar), menuitem);
1206         gtk_widget_show(menuitem);
1207
1208         gtk_widget_show(menuitem);
1209
1210         return frame;
1211 }
1212
1213 void vf_mark_filter_toggle(ViewFile *vf, gint mark)
1214 {
1215         gint n = mark - 1;
1216         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vf->filter_check[n]),
1217                                      !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[n])));
1218 }
1219
1220 ViewFile *vf_new(FileViewType type, FileData *dir_fd)
1221 {
1222         ViewFile *vf;
1223
1224         vf = g_new0(ViewFile, 1);
1225
1226         vf->type = type;
1227         vf->sort_method = SORT_NAME;
1228         vf->sort_ascend = TRUE;
1229         vf->read_metadata_in_idle_id = 0;
1230
1231         vf->scrolled = gtk_scrolled_window_new(NULL, NULL);
1232         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vf->scrolled), GTK_SHADOW_IN);
1233         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vf->scrolled),
1234                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1235
1236         vf->filter = vf_marks_filter_init(vf);
1237         vf->file_filter.frame = vf_file_filter_init(vf);
1238
1239         vf->widget = gtk_vbox_new(FALSE, 0);
1240         gtk_box_pack_start(GTK_BOX(vf->widget), vf->filter, FALSE, FALSE, 0);
1241         gtk_box_pack_start(GTK_BOX(vf->widget), vf->file_filter.frame, FALSE, FALSE, 0);
1242         gtk_box_pack_start(GTK_BOX(vf->widget), vf->scrolled, TRUE, TRUE, 0);
1243         gtk_widget_show(vf->scrolled);
1244
1245         g_signal_connect(G_OBJECT(vf->widget), "destroy",
1246                          G_CALLBACK(vf_destroy_cb), vf);
1247
1248         switch (type)
1249         {
1250         case FILEVIEW_LIST: vf = vflist_new(vf, dir_fd); break;
1251         case FILEVIEW_ICON: vf = vficon_new(vf, dir_fd); break;
1252         }
1253
1254         vf_dnd_init(vf);
1255
1256         g_signal_connect(G_OBJECT(vf->listview), "key_press_event",
1257                          G_CALLBACK(vf_press_key_cb), vf);
1258         g_signal_connect(G_OBJECT(vf->listview), "button_press_event",
1259                          G_CALLBACK(vf_press_cb), vf);
1260         g_signal_connect(G_OBJECT(vf->listview), "button_release_event",
1261                          G_CALLBACK(vf_release_cb), vf);
1262
1263         gtk_container_add(GTK_CONTAINER(vf->scrolled), vf->listview);
1264         gtk_widget_show(vf->listview);
1265
1266         if (dir_fd) vf_set_fd(vf, dir_fd);
1267
1268         return vf;
1269 }
1270
1271 void vf_set_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gpointer data), gpointer data)
1272 {
1273         vf->func_status = func;
1274         vf->data_status = data;
1275 }
1276
1277 void vf_set_thumb_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gdouble val, const gchar *text, gpointer data), gpointer data)
1278 {
1279         vf->func_thumb_status = func;
1280         vf->data_thumb_status = data;
1281 }
1282
1283 void vf_thumb_set(ViewFile *vf, gboolean enable)
1284 {
1285         switch (vf->type)
1286         {
1287         case FILEVIEW_LIST: vflist_thumb_set(vf, enable); break;
1288         case FILEVIEW_ICON: /*vficon_thumb_set(vf, enable);*/ break;
1289         }
1290 }
1291
1292
1293 static gboolean vf_thumb_next(ViewFile *vf);
1294
1295 static gdouble vf_thumb_progress(ViewFile *vf)
1296 {
1297         gint count = 0;
1298         gint done = 0;
1299
1300         switch (vf->type)
1301         {
1302         case FILEVIEW_LIST: vflist_thumb_progress_count(vf->list, &count, &done); break;
1303         case FILEVIEW_ICON: vficon_thumb_progress_count(vf->list, &count, &done); break;
1304         }
1305
1306         DEBUG_1("thumb progress: %d of %d", done, count);
1307         return (gdouble)done / count;
1308 }
1309
1310 static gdouble vf_read_metadata_in_idle_progress(ViewFile *vf)
1311 {
1312         gint count = 0;
1313         gint done = 0;
1314
1315         switch (vf->type)
1316                 {
1317                 case FILEVIEW_LIST: vflist_read_metadata_progress_count(vf->list, &count, &done); break;
1318                 case FILEVIEW_ICON: vficon_read_metadata_progress_count(vf->list, &count, &done); break;
1319                 }
1320
1321         return (gdouble)done / count;
1322 }
1323
1324 static void vf_set_thumb_fd(ViewFile *vf, FileData *fd)
1325 {
1326         switch (vf->type)
1327         {
1328         case FILEVIEW_LIST: vflist_set_thumb_fd(vf, fd); break;
1329         case FILEVIEW_ICON: vficon_set_thumb_fd(vf, fd); break;
1330         }
1331 }
1332
1333 static void vf_thumb_status(ViewFile *vf, gdouble val, const gchar *text)
1334 {
1335         if (vf->func_thumb_status)
1336                 {
1337                 vf->func_thumb_status(vf, val, text, vf->data_thumb_status);
1338                 }
1339 }
1340
1341 static void vf_thumb_do(ViewFile *vf, FileData *fd)
1342 {
1343         if (!fd) return;
1344
1345         vf_set_thumb_fd(vf, fd);
1346         vf_thumb_status(vf, vf_thumb_progress(vf), _("Loading thumbs..."));
1347 }
1348
1349 void vf_thumb_cleanup(ViewFile *vf)
1350 {
1351         vf_thumb_status(vf, 0.0, NULL);
1352
1353         vf->thumbs_running = FALSE;
1354
1355         thumb_loader_free(vf->thumbs_loader);
1356         vf->thumbs_loader = NULL;
1357
1358         vf->thumbs_filedata = NULL;
1359 }
1360
1361 void vf_thumb_stop(ViewFile *vf)
1362 {
1363         if (vf->thumbs_running) vf_thumb_cleanup(vf);
1364 }
1365
1366 static void vf_thumb_common_cb(ThumbLoader *tl, gpointer data)
1367 {
1368         ViewFile *vf = data;
1369
1370         if (vf->thumbs_filedata && vf->thumbs_loader == tl)
1371                 {
1372                 vf_thumb_do(vf, vf->thumbs_filedata);
1373                 }
1374
1375         while (vf_thumb_next(vf));
1376 }
1377
1378 static void vf_thumb_error_cb(ThumbLoader *tl, gpointer data)
1379 {
1380         vf_thumb_common_cb(tl, data);
1381 }
1382
1383 static void vf_thumb_done_cb(ThumbLoader *tl, gpointer data)
1384 {
1385         vf_thumb_common_cb(tl, data);
1386 }
1387
1388 static gboolean vf_thumb_next(ViewFile *vf)
1389 {
1390         FileData *fd = NULL;
1391
1392         if (!gtk_widget_get_realized(vf->listview))
1393                 {
1394                 vf_thumb_status(vf, 0.0, NULL);
1395                 return FALSE;
1396                 }
1397
1398         switch (vf->type)
1399         {
1400         case FILEVIEW_LIST: fd = vflist_thumb_next_fd(vf); break;
1401         case FILEVIEW_ICON: fd = vficon_thumb_next_fd(vf); break;
1402         }
1403
1404         if (!fd)
1405                 {
1406                 /* done */
1407                 vf_thumb_cleanup(vf);
1408                 return FALSE;
1409                 }
1410
1411         vf->thumbs_filedata = fd;
1412
1413         thumb_loader_free(vf->thumbs_loader);
1414
1415         vf->thumbs_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
1416         thumb_loader_set_callbacks(vf->thumbs_loader,
1417                                    vf_thumb_done_cb,
1418                                    vf_thumb_error_cb,
1419                                    NULL,
1420                                    vf);
1421
1422         if (!thumb_loader_start(vf->thumbs_loader, fd))
1423                 {
1424                 /* set icon to unknown, continue */
1425                 DEBUG_1("thumb loader start failed %s", fd->path);
1426                 vf_thumb_do(vf, fd);
1427
1428                 return TRUE;
1429                 }
1430
1431         return FALSE;
1432 }
1433
1434 static void vf_thumb_reset_all(ViewFile *vf)
1435 {
1436         GList *work;
1437
1438         for (work = vf->list; work; work = work->next)
1439                 {
1440                 FileData *fd = work->data;
1441                 if (fd->thumb_pixbuf)
1442                         {
1443                         g_object_unref(fd->thumb_pixbuf);
1444                         fd->thumb_pixbuf = NULL;
1445                         }
1446                 }
1447 }
1448
1449 void vf_thumb_update(ViewFile *vf)
1450 {
1451         vf_thumb_stop(vf);
1452
1453         if (vf->type == FILEVIEW_LIST && !VFLIST(vf)->thumbs_enabled) return;
1454
1455         vf_thumb_status(vf, 0.0, _("Loading thumbs..."));
1456         vf->thumbs_running = TRUE;
1457
1458         if (thumb_format_changed)
1459                 {
1460                 vf_thumb_reset_all(vf);
1461                 thumb_format_changed = FALSE;
1462                 }
1463
1464         while (vf_thumb_next(vf));
1465 }
1466
1467 void vf_star_cleanup(ViewFile *vf)
1468 {
1469         if (vf->stars_id != 0)
1470                 {
1471                 g_source_remove(vf->stars_id);
1472                 }
1473
1474         vf->stars_id = 0;
1475         vf->stars_filedata = NULL;
1476 }
1477
1478 void vf_star_stop(ViewFile *vf)
1479 {
1480          vf_star_cleanup(vf);
1481 }
1482
1483 static void vf_set_star_fd(ViewFile *vf, FileData *fd)
1484 {
1485         switch (vf->type)
1486                 {
1487                 case FILEVIEW_LIST: vflist_set_star_fd(vf, fd); break;
1488                 case FILEVIEW_ICON: vficon_set_star_fd(vf, fd); break;
1489                 default: break;
1490                 }
1491 }
1492
1493 static void vf_star_do(ViewFile *vf, FileData *fd)
1494 {
1495         if (!fd) return;
1496
1497         vf_set_star_fd(vf, fd);
1498 }
1499
1500 static gboolean vf_star_next(ViewFile *vf)
1501 {
1502         FileData *fd = NULL;
1503
1504         switch (vf->type)
1505                 {
1506                 case FILEVIEW_LIST: fd = vflist_star_next_fd(vf); break;
1507                 case FILEVIEW_ICON: fd = vficon_star_next_fd(vf); break;
1508                 default: break;
1509                 }
1510
1511         if (!fd)
1512                 {
1513                 /* done */
1514                 vf_star_cleanup(vf);
1515                 return FALSE;
1516                 }
1517
1518         return TRUE;
1519 }
1520
1521 gboolean vf_stars_cb(gpointer data)
1522 {
1523         ViewFile *vf = data;
1524         FileData *fd = vf->stars_filedata;
1525
1526         if (fd)
1527                 {
1528                 read_rating_data(fd);
1529
1530                 vf_star_do(vf, fd);
1531
1532                 if (vf_star_next(vf))
1533                         {
1534                         return TRUE;
1535                         }
1536                 else
1537                         {
1538                         vf->stars_filedata = NULL;
1539                         vf->stars_id = 0;
1540                         return FALSE;
1541                         }
1542                 }
1543
1544         return FALSE;
1545 }
1546
1547 void vf_star_update(ViewFile *vf)
1548 {
1549         vf_star_stop(vf);
1550
1551         if (!options->show_star_rating)
1552                 {
1553                 return;
1554                 }
1555
1556         vf_star_next(vf);
1557 }
1558
1559 void vf_marks_set(ViewFile *vf, gboolean enable)
1560 {
1561         if (vf->marks_enabled == enable) return;
1562
1563         vf->marks_enabled = enable;
1564
1565         switch (vf->type)
1566         {
1567         case FILEVIEW_LIST: vflist_marks_set(vf, enable); break;
1568         case FILEVIEW_ICON: vficon_marks_set(vf, enable); break;
1569         }
1570         if (enable)
1571                 gtk_widget_show(vf->filter);
1572         else
1573                 gtk_widget_hide(vf->filter);
1574
1575         vf_refresh_idle(vf);
1576 }
1577
1578 void vf_star_rating_set(ViewFile *vf, gboolean enable)
1579 {
1580         if (options->show_star_rating == enable) return;
1581         options->show_star_rating = enable;
1582
1583         switch (vf->type)
1584                 {
1585                 case FILEVIEW_LIST: vflist_star_rating_set(vf, enable); break;
1586                 case FILEVIEW_ICON: vficon_star_rating_set(vf, enable); break;
1587                 }
1588         vf_refresh_idle(vf);
1589 }
1590
1591 guint vf_marks_get_filter(ViewFile *vf)
1592 {
1593         guint ret = 0;
1594         gint i;
1595         if (!vf->marks_enabled) return 0;
1596
1597         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
1598                 {
1599                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[i])))
1600                         {
1601                         ret |= 1 << i;
1602                         }
1603                 }
1604         return ret;
1605 }
1606
1607 GRegex *vf_file_filter_get_filter(ViewFile *vf)
1608 {
1609         GRegex *ret = NULL;
1610         GError *error = NULL;
1611         gchar *file_filter_text = NULL;
1612
1613         if (!gtk_widget_get_visible(vf->file_filter.combo))
1614                 {
1615                 return g_regex_new("", 0, 0, NULL);
1616                 }
1617
1618         file_filter_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
1619
1620         if (file_filter_text[0] != '\0')
1621                 {
1622                 ret = g_regex_new(file_filter_text, vf->file_filter.case_sensitive ? 0 : G_REGEX_CASELESS, 0, &error);
1623                 if (error)
1624                         {
1625                         log_printf("Error: could not compile regular expression %s\n%s\n", file_filter_text, error->message);
1626                         g_error_free(error);
1627                         error = NULL;
1628                         ret = g_regex_new("", 0, 0, NULL);
1629                         }
1630                 g_free(file_filter_text);
1631                 }
1632         else
1633                 {
1634                 ret = g_regex_new("", 0, 0, NULL);
1635                 }
1636
1637         return ret;
1638 }
1639
1640 guint vf_class_get_filter(ViewFile *vf)
1641 {
1642         guint ret = 0;
1643         gint i;
1644
1645         if (!gtk_widget_get_visible(vf->file_filter.combo))
1646                 {
1647                 return G_MAXUINT;
1648                 }
1649
1650         for ( i = 0; i < FILE_FORMAT_CLASSES; i++)
1651                 {
1652                 if (options->class_filter[i])
1653                         {
1654                         ret |= 1 << i;
1655                         }
1656                 }
1657
1658         return ret;
1659 }
1660
1661 void vf_set_layout(ViewFile *vf, LayoutWindow *layout)
1662 {
1663         vf->layout = layout;
1664 }
1665
1666
1667 /*
1668  *-----------------------------------------------------------------------------
1669  * maintenance (for rename, move, remove)
1670  *-----------------------------------------------------------------------------
1671  */
1672
1673 static gboolean vf_refresh_idle_cb(gpointer data)
1674 {
1675         ViewFile *vf = data;
1676
1677         vf_refresh(vf);
1678         vf->refresh_idle_id = 0;
1679         return FALSE;
1680 }
1681
1682 void vf_refresh_idle_cancel(ViewFile *vf)
1683 {
1684         if (vf->refresh_idle_id)
1685                 {
1686                 g_source_remove(vf->refresh_idle_id);
1687                 vf->refresh_idle_id = 0;
1688                 }
1689 }
1690
1691
1692 void vf_refresh_idle(ViewFile *vf)
1693 {
1694         if (!vf->refresh_idle_id)
1695                 {
1696                 vf->time_refresh_set = time(NULL);
1697                 /* file operations run with G_PRIORITY_DEFAULT_IDLE */
1698                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE + 50, vf_refresh_idle_cb, vf, NULL);
1699                 }
1700         else if (time(NULL) - vf->time_refresh_set > 1)
1701                 {
1702                 /* more than 1 sec since last update - increase priority */
1703                 vf_refresh_idle_cancel(vf);
1704                 vf->time_refresh_set = time(NULL);
1705                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 50, vf_refresh_idle_cb, vf, NULL);
1706                 }
1707 }
1708
1709 void vf_notify_cb(FileData *fd, NotifyType type, gpointer data)
1710 {
1711         ViewFile *vf = data;
1712         gboolean refresh;
1713
1714         NotifyType interested = NOTIFY_CHANGE | NOTIFY_REREAD | NOTIFY_GROUPING;
1715         if (vf->marks_enabled) interested |= NOTIFY_MARKS | NOTIFY_METADATA;
1716         /** @FIXME NOTIFY_METADATA should be checked by the keyword-to-mark functions and converted to NOTIFY_MARKS only if there was a change */
1717
1718         if (!(type & interested) || vf->refresh_idle_id || !vf->dir_fd) return;
1719
1720         refresh = (fd == vf->dir_fd);
1721
1722         if (!refresh)
1723                 {
1724                 gchar *base = remove_level_from_path(fd->path);
1725                 refresh = (g_strcmp0(base, vf->dir_fd->path) == 0);
1726                 g_free(base);
1727                 }
1728
1729         if ((type & NOTIFY_CHANGE) && fd->change)
1730                 {
1731                 if (!refresh && fd->change->dest)
1732                         {
1733                         gchar *dest_base = remove_level_from_path(fd->change->dest);
1734                         refresh = (g_strcmp0(dest_base, vf->dir_fd->path) == 0);
1735                         g_free(dest_base);
1736                         }
1737
1738                 if (!refresh && fd->change->source)
1739                         {
1740                         gchar *source_base = remove_level_from_path(fd->change->source);
1741                         refresh = (g_strcmp0(source_base, vf->dir_fd->path) == 0);
1742                         g_free(source_base);
1743                         }
1744                 }
1745
1746         if (refresh)
1747                 {
1748                 DEBUG_1("Notify vf: %s %04x", fd->path, type);
1749                 vf_refresh_idle(vf);
1750                 }
1751 }
1752
1753 static gboolean vf_read_metadata_in_idle_cb(gpointer data)
1754 {
1755         FileData *fd;
1756         ViewFile *vf = data;
1757         GList *work;
1758
1759         vf_thumb_status(vf, vf_read_metadata_in_idle_progress(vf), _("Loading meta..."));
1760
1761         work = vf->list;
1762
1763         while (work)
1764                 {
1765                 fd = work->data;
1766
1767                 if (fd && !fd->metadata_in_idle_loaded)
1768                         {
1769                         if (!fd->exifdate)
1770                                 {
1771                                 read_exif_time_data(fd);
1772                                 }
1773                         if (!fd->exifdate_digitized)
1774                                 {
1775                                 read_exif_time_digitized_data(fd);
1776                                 }
1777                         if (fd->rating == STAR_RATING_NOT_READ)
1778                                 {
1779                                 read_rating_data(fd);
1780                                 }
1781                         fd->metadata_in_idle_loaded = TRUE;
1782                         return TRUE;
1783                         }
1784                 work = work->next;
1785                 }
1786
1787         vf_thumb_status(vf, 0.0, NULL);
1788         vf->read_metadata_in_idle_id = 0;
1789         vf_refresh(vf);
1790         return FALSE;
1791 }
1792
1793 static void vf_read_metadata_in_idle_finished_cb(gpointer data)
1794 {
1795         ViewFile *vf = data;
1796
1797         vf_thumb_status(vf, 0.0, "Loading meta...");
1798         vf->read_metadata_in_idle_id = 0;
1799 }
1800
1801 void vf_read_metadata_in_idle(ViewFile *vf)
1802 {
1803         if (!vf) return;
1804
1805         if (vf->read_metadata_in_idle_id)
1806                 {
1807                 g_idle_remove_by_data(vf);
1808                 }
1809         vf->read_metadata_in_idle_id = 0;
1810
1811         if (vf->list)
1812                 {
1813                 vf->read_metadata_in_idle_id = g_idle_add_full(G_PRIORITY_LOW, vf_read_metadata_in_idle_cb, vf, vf_read_metadata_in_idle_finished_cb);
1814                 }
1815
1816         return;
1817 }
1818
1819 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */