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