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