66fe6c8ca319142f859a723893d02100a98bd207
[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         gchar *remove_text = NULL;
853         gchar *index_text = NULL;
854         gboolean text_found = FALSE;
855         gint i;
856
857         entry_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)))));
858
859         if (entry_text[0] == '\0' && vf->file_filter.last_selected >= 0)
860                 {
861                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), vf->file_filter.last_selected);
862                 remove_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
863                 history_list_item_remove("file_filter", remove_text);
864                 gtk_combo_box_text_remove(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), vf->file_filter.last_selected);
865                 g_free(remove_text);
866
867                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), -1);
868                 vf->file_filter.last_selected = - 1;
869                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo))), "");
870                 vf->file_filter.count--;
871                 }
872         else
873                 {
874                 if (entry_text[0] != '\0')
875                         {
876                         for (i = 0; i < vf->file_filter.count; i++)
877                                 {
878                                 if (index_text)
879                                         {
880                                         g_free(index_text);
881                                         }
882                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), i);
883                                 index_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
884
885                                 if (g_strcmp0(index_text, entry_text) == 0)
886                                         {
887                                         text_found = TRUE;
888                                         break;
889                                         }
890                                 }
891
892                         g_free(index_text);
893                         if (!text_found)
894                                 {
895                                 history_list_add_to_key("file_filter", entry_text, 10);
896                                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), entry_text);
897                                 vf->file_filter.count++;
898                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), vf->file_filter.count - 1);
899                                 }
900                         }
901                 }
902         vf_refresh(vf);
903
904         g_free(entry_text);
905 }
906
907 static void vf_file_filter_cb(GtkWidget *widget, gpointer data)
908 {
909         ViewFile *vf = data;
910
911         vf_refresh(vf);
912 }
913
914 static gboolean vf_file_filter_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
915 {
916         ViewFile *vf = data;
917         vf->file_filter.last_selected = gtk_combo_box_get_active(GTK_COMBO_BOX(vf->file_filter.combo));
918
919         gtk_widget_grab_focus(widget);
920
921         return TRUE;
922 }
923
924 static GtkWidget *vf_marks_filter_init(ViewFile *vf)
925 {
926         GtkWidget *frame = gtk_frame_new(NULL);
927         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
928
929         gint i;
930
931         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
932                 {
933                 GtkWidget *check = gtk_check_button_new();
934                 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, FALSE, 0);
935                 g_signal_connect(G_OBJECT(check), "toggled",
936                          G_CALLBACK(vf_marks_filter_toggle_cb), vf);
937                 g_signal_connect(G_OBJECT(check), "button_press_event",
938                          G_CALLBACK(vf_marks_tooltip_cb), GINT_TO_POINTER(i));
939                 gtk_widget_set_tooltip_text(check, options->marks_tooltips[i]);
940
941                 gtk_widget_show(check);
942                 vf->filter_check[i] = check;
943                 }
944         gtk_container_add(GTK_CONTAINER(frame), hbox);
945         gtk_widget_show(hbox);
946         return frame;
947 }
948
949 void vf_file_filter_set(ViewFile *vf, gboolean enable)
950 {
951         if (enable)
952                 {
953                 gtk_widget_show(vf->file_filter.combo);
954                 gtk_widget_show(vf->file_filter.frame);
955                 }
956         else
957                 {
958                 gtk_widget_hide(vf->file_filter.combo);
959                 gtk_widget_hide(vf->file_filter.frame);
960                 }
961
962         vf_refresh(vf);
963 }
964
965 static GtkWidget *vf_file_filter_init(ViewFile *vf)
966 {
967         GtkWidget *frame = gtk_frame_new(NULL);
968         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
969         GList *work;
970         gint n = 0;
971         GtkWidget *combo_entry;
972
973         vf->file_filter.combo = gtk_combo_box_text_new_with_entry();
974         combo_entry = gtk_bin_get_child(GTK_BIN(vf->file_filter.combo));
975         gtk_widget_show(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)));
976         gtk_widget_show((GTK_WIDGET(vf->file_filter.combo)));
977         gtk_widget_set_tooltip_text(GTK_WIDGET(vf->file_filter.combo), "Use regular expressions");
978
979         work = history_list_get_by_key("file_filter");
980         while (work)
981                 {
982                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), (gchar *)work->data);
983                 work = work->next;
984                 n++;
985                 vf->file_filter.count = n;
986                 }
987         gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), 0);
988
989         g_signal_connect(G_OBJECT(combo_entry), "activate",
990                 G_CALLBACK(vf_file_filter_save_cb), vf);
991                 
992         g_signal_connect(G_OBJECT(vf->file_filter.combo), "changed",
993                 G_CALLBACK(vf_file_filter_cb), vf);
994
995         g_signal_connect(G_OBJECT(combo_entry), "button_press_event",
996                          G_CALLBACK(vf_file_filter_press_cb), vf);
997
998         gtk_box_pack_start(GTK_BOX(hbox), vf->file_filter.combo, FALSE, FALSE, 0);
999         gtk_widget_show(vf->file_filter.combo);
1000         gtk_container_add(GTK_CONTAINER(frame), hbox);
1001         gtk_widget_show(hbox);
1002
1003         return frame;
1004 }
1005
1006 void vf_mark_filter_toggle(ViewFile *vf, gint mark)
1007 {
1008         gint n = mark - 1;
1009         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vf->filter_check[n]),
1010                                      !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[n])));
1011 }
1012
1013 ViewFile *vf_new(FileViewType type, FileData *dir_fd)
1014 {
1015         ViewFile *vf;
1016
1017         vf = g_new0(ViewFile, 1);
1018
1019         vf->type = type;
1020         vf->sort_method = SORT_NAME;
1021         vf->sort_ascend = TRUE;
1022         vf->read_metadata_in_idle_id = 0;
1023
1024         vf->scrolled = gtk_scrolled_window_new(NULL, NULL);
1025         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vf->scrolled), GTK_SHADOW_IN);
1026         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vf->scrolled),
1027                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1028
1029         vf->filter = vf_marks_filter_init(vf);
1030         vf->file_filter.frame = vf_file_filter_init(vf);
1031
1032         vf->widget = gtk_vbox_new(FALSE, 0);
1033         gtk_box_pack_start(GTK_BOX(vf->widget), vf->filter, FALSE, FALSE, 0);
1034         gtk_box_pack_start(GTK_BOX(vf->widget), vf->file_filter.frame, FALSE, FALSE, 0);
1035         gtk_box_pack_start(GTK_BOX(vf->widget), vf->scrolled, TRUE, TRUE, 0);
1036         gtk_widget_show(vf->scrolled);
1037
1038         g_signal_connect(G_OBJECT(vf->widget), "destroy",
1039                          G_CALLBACK(vf_destroy_cb), vf);
1040
1041         switch (type)
1042         {
1043         case FILEVIEW_LIST: vf = vflist_new(vf, dir_fd); break;
1044         case FILEVIEW_ICON: vf = vficon_new(vf, dir_fd); break;
1045         }
1046
1047         vf_dnd_init(vf);
1048
1049         g_signal_connect(G_OBJECT(vf->listview), "key_press_event",
1050                          G_CALLBACK(vf_press_key_cb), vf);
1051         g_signal_connect(G_OBJECT(vf->listview), "button_press_event",
1052                          G_CALLBACK(vf_press_cb), vf);
1053         g_signal_connect(G_OBJECT(vf->listview), "button_release_event",
1054                          G_CALLBACK(vf_release_cb), vf);
1055
1056         gtk_container_add(GTK_CONTAINER(vf->scrolled), vf->listview);
1057         gtk_widget_show(vf->listview);
1058
1059         if (dir_fd) vf_set_fd(vf, dir_fd);
1060
1061         return vf;
1062 }
1063
1064 void vf_set_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gpointer data), gpointer data)
1065 {
1066         vf->func_status = func;
1067         vf->data_status = data;
1068 }
1069
1070 void vf_set_thumb_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gdouble val, const gchar *text, gpointer data), gpointer data)
1071 {
1072         vf->func_thumb_status = func;
1073         vf->data_thumb_status = data;
1074 }
1075
1076 void vf_thumb_set(ViewFile *vf, gboolean enable)
1077 {
1078         switch (vf->type)
1079         {
1080         case FILEVIEW_LIST: vflist_thumb_set(vf, enable); break;
1081         case FILEVIEW_ICON: /*vficon_thumb_set(vf, enable);*/ break;
1082         }
1083 }
1084
1085
1086 static gboolean vf_thumb_next(ViewFile *vf);
1087
1088 static gdouble vf_thumb_progress(ViewFile *vf)
1089 {
1090         gint count = 0;
1091         gint done = 0;
1092
1093         switch (vf->type)
1094         {
1095         case FILEVIEW_LIST: vflist_thumb_progress_count(vf->list, &count, &done); break;
1096         case FILEVIEW_ICON: vficon_thumb_progress_count(vf->list, &count, &done); break;
1097         }
1098
1099         DEBUG_1("thumb progress: %d of %d", done, count);
1100         return (gdouble)done / count;
1101 }
1102
1103 static gdouble vf_read_metadata_in_idle_progress(ViewFile *vf)
1104 {
1105         gint count = 0;
1106         gint done = 0;
1107
1108         switch (vf->type)
1109                 {
1110                 case FILEVIEW_LIST: vflist_read_metadata_progress_count(vf->list, &count, &done); break;
1111                 case FILEVIEW_ICON: vficon_read_metadata_progress_count(vf->list, &count, &done); break;
1112                 }
1113
1114         return (gdouble)done / count;
1115 }
1116
1117 static void vf_set_thumb_fd(ViewFile *vf, FileData *fd)
1118 {
1119         switch (vf->type)
1120         {
1121         case FILEVIEW_LIST: vflist_set_thumb_fd(vf, fd); break;
1122         case FILEVIEW_ICON: vficon_set_thumb_fd(vf, fd); break;
1123         }
1124 }
1125
1126 static void vf_thumb_status(ViewFile *vf, gdouble val, const gchar *text)
1127 {
1128         if (vf->func_thumb_status)
1129                 {
1130                 vf->func_thumb_status(vf, val, text, vf->data_thumb_status);
1131                 }
1132 }
1133
1134 static void vf_thumb_do(ViewFile *vf, FileData *fd)
1135 {
1136         if (!fd) return;
1137
1138         vf_set_thumb_fd(vf, fd);
1139         vf_thumb_status(vf, vf_thumb_progress(vf), _("Loading thumbs..."));
1140 }
1141
1142 void vf_thumb_cleanup(ViewFile *vf)
1143 {
1144         vf_thumb_status(vf, 0.0, NULL);
1145
1146         vf->thumbs_running = FALSE;
1147
1148         thumb_loader_free(vf->thumbs_loader);
1149         vf->thumbs_loader = NULL;
1150
1151         vf->thumbs_filedata = NULL;
1152 }
1153
1154 void vf_thumb_stop(ViewFile *vf)
1155 {
1156         if (vf->thumbs_running) vf_thumb_cleanup(vf);
1157 }
1158
1159 static void vf_thumb_common_cb(ThumbLoader *tl, gpointer data)
1160 {
1161         ViewFile *vf = data;
1162
1163         if (vf->thumbs_filedata && vf->thumbs_loader == tl)
1164                 {
1165                 vf_thumb_do(vf, vf->thumbs_filedata);
1166                 }
1167
1168         while (vf_thumb_next(vf));
1169 }
1170
1171 static void vf_thumb_error_cb(ThumbLoader *tl, gpointer data)
1172 {
1173         vf_thumb_common_cb(tl, data);
1174 }
1175
1176 static void vf_thumb_done_cb(ThumbLoader *tl, gpointer data)
1177 {
1178         vf_thumb_common_cb(tl, data);
1179 }
1180
1181 static gboolean vf_thumb_next(ViewFile *vf)
1182 {
1183         FileData *fd = NULL;
1184
1185         if (!gtk_widget_get_realized(vf->listview))
1186                 {
1187                 vf_thumb_status(vf, 0.0, NULL);
1188                 return FALSE;
1189                 }
1190
1191         switch (vf->type)
1192         {
1193         case FILEVIEW_LIST: fd = vflist_thumb_next_fd(vf); break;
1194         case FILEVIEW_ICON: fd = vficon_thumb_next_fd(vf); break;
1195         }
1196
1197         if (!fd)
1198                 {
1199                 /* done */
1200                 vf_thumb_cleanup(vf);
1201                 return FALSE;
1202                 }
1203
1204         vf->thumbs_filedata = fd;
1205
1206         thumb_loader_free(vf->thumbs_loader);
1207
1208         vf->thumbs_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
1209         thumb_loader_set_callbacks(vf->thumbs_loader,
1210                                    vf_thumb_done_cb,
1211                                    vf_thumb_error_cb,
1212                                    NULL,
1213                                    vf);
1214
1215         if (!thumb_loader_start(vf->thumbs_loader, fd))
1216                 {
1217                 /* set icon to unknown, continue */
1218                 DEBUG_1("thumb loader start failed %s", fd->path);
1219                 vf_thumb_do(vf, fd);
1220
1221                 return TRUE;
1222                 }
1223
1224         return FALSE;
1225 }
1226
1227 static void vf_thumb_reset_all(ViewFile *vf)
1228 {
1229         GList *work;
1230
1231         for (work = vf->list; work; work = work->next)
1232                 {
1233                 FileData *fd = work->data;
1234                 if (fd->thumb_pixbuf)
1235                         {
1236                         g_object_unref(fd->thumb_pixbuf);
1237                         fd->thumb_pixbuf = NULL;
1238                         }
1239                 }
1240 }
1241
1242 void vf_thumb_update(ViewFile *vf)
1243 {
1244         vf_thumb_stop(vf);
1245
1246         if (vf->type == FILEVIEW_LIST && !VFLIST(vf)->thumbs_enabled) return;
1247
1248         vf_thumb_status(vf, 0.0, _("Loading thumbs..."));
1249         vf->thumbs_running = TRUE;
1250
1251         if (thumb_format_changed)
1252                 {
1253                 vf_thumb_reset_all(vf);
1254                 thumb_format_changed = FALSE;
1255                 }
1256
1257         while (vf_thumb_next(vf));
1258 }
1259
1260
1261 void vf_marks_set(ViewFile *vf, gboolean enable)
1262 {
1263         if (vf->marks_enabled == enable) return;
1264
1265         vf->marks_enabled = enable;
1266
1267         switch (vf->type)
1268         {
1269         case FILEVIEW_LIST: vflist_marks_set(vf, enable); break;
1270         case FILEVIEW_ICON: vficon_marks_set(vf, enable); break;
1271         }
1272         if (enable)
1273                 gtk_widget_show(vf->filter);
1274         else
1275                 gtk_widget_hide(vf->filter);
1276
1277         vf_refresh_idle(vf);
1278 }
1279
1280 void vf_star_rating_set(ViewFile *vf, gboolean enable)
1281 {
1282         if (options->show_star_rating == enable) return;
1283         options->show_star_rating = enable;
1284
1285         switch (vf->type)
1286                 {
1287                 case FILEVIEW_LIST: vflist_star_rating_set(vf, enable); break;
1288                 case FILEVIEW_ICON: vficon_star_rating_set(vf, enable); break;
1289                 }
1290         vf_refresh_idle(vf);
1291 }
1292
1293 guint vf_marks_get_filter(ViewFile *vf)
1294 {
1295         guint ret = 0;
1296         gint i;
1297         if (!vf->marks_enabled) return 0;
1298
1299         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
1300                 {
1301                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[i])))
1302                         {
1303                         ret |= 1 << i;
1304                         }
1305                 }
1306         return ret;
1307 }
1308
1309 GRegex *vf_file_filter_get_filter(ViewFile *vf)
1310 {
1311         GRegex *ret = NULL;
1312         GError *error = NULL;
1313         gchar *file_filter_text = NULL;
1314
1315         if (!gtk_widget_get_visible(vf->file_filter.combo))
1316                 {
1317                 return g_regex_new("", 0, 0, NULL);
1318                 }
1319
1320         file_filter_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
1321
1322         if (file_filter_text[0] != '\0')
1323                 {
1324                 ret = g_regex_new(file_filter_text, 0, 0, &error);
1325                 if (error)
1326                         {
1327                         log_printf("Error: could not compile regular expression %s\n%s\n", file_filter_text, error->message);
1328                         g_error_free(error);
1329                         error = NULL;
1330                         ret = g_regex_new("", 0, 0, NULL);
1331                         }
1332                 g_free(file_filter_text);
1333                 }
1334         else
1335                 {
1336                 ret = g_regex_new("", 0, 0, NULL);
1337                 }
1338
1339         return ret;
1340 }
1341
1342 void vf_set_layout(ViewFile *vf, LayoutWindow *layout)
1343 {
1344         vf->layout = layout;
1345 }
1346
1347
1348 /*
1349  *-----------------------------------------------------------------------------
1350  * maintenance (for rename, move, remove)
1351  *-----------------------------------------------------------------------------
1352  */
1353
1354 static gboolean vf_refresh_idle_cb(gpointer data)
1355 {
1356         ViewFile *vf = data;
1357
1358         vf_refresh(vf);
1359         vf->refresh_idle_id = 0;
1360         return FALSE;
1361 }
1362
1363 void vf_refresh_idle_cancel(ViewFile *vf)
1364 {
1365         if (vf->refresh_idle_id)
1366                 {
1367                 g_source_remove(vf->refresh_idle_id);
1368                 vf->refresh_idle_id = 0;
1369                 }
1370 }
1371
1372
1373 void vf_refresh_idle(ViewFile *vf)
1374 {
1375         if (!vf->refresh_idle_id)
1376                 {
1377                 vf->time_refresh_set = time(NULL);
1378                 /* file operations run with G_PRIORITY_DEFAULT_IDLE */
1379                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE + 50, vf_refresh_idle_cb, vf, NULL);
1380                 }
1381         else if (time(NULL) - vf->time_refresh_set > 1)
1382                 {
1383                 /* more than 1 sec since last update - increase priority */
1384                 vf_refresh_idle_cancel(vf);
1385                 vf->time_refresh_set = time(NULL);
1386                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 50, vf_refresh_idle_cb, vf, NULL);
1387                 }
1388 }
1389
1390 void vf_notify_cb(FileData *fd, NotifyType type, gpointer data)
1391 {
1392         ViewFile *vf = data;
1393         gboolean refresh;
1394
1395         NotifyType interested = NOTIFY_CHANGE | NOTIFY_REREAD | NOTIFY_GROUPING;
1396         if (vf->marks_enabled) interested |= NOTIFY_MARKS | NOTIFY_METADATA;
1397         /* FIXME: NOTIFY_METADATA should be checked by the keyword-to-mark functions and converted to NOTIFY_MARKS only if there was a change */
1398
1399         if (!(type & interested) || vf->refresh_idle_id || !vf->dir_fd) return;
1400
1401         refresh = (fd == vf->dir_fd);
1402
1403         if (!refresh)
1404                 {
1405                 gchar *base = remove_level_from_path(fd->path);
1406                 refresh = (g_strcmp0(base, vf->dir_fd->path) == 0);
1407                 g_free(base);
1408                 }
1409
1410         if ((type & NOTIFY_CHANGE) && fd->change)
1411                 {
1412                 if (!refresh && fd->change->dest)
1413                         {
1414                         gchar *dest_base = remove_level_from_path(fd->change->dest);
1415                         refresh = (g_strcmp0(dest_base, vf->dir_fd->path) == 0);
1416                         g_free(dest_base);
1417                         }
1418
1419                 if (!refresh && fd->change->source)
1420                         {
1421                         gchar *source_base = remove_level_from_path(fd->change->source);
1422                         refresh = (g_strcmp0(source_base, vf->dir_fd->path) == 0);
1423                         g_free(source_base);
1424                         }
1425                 }
1426
1427         if (refresh)
1428                 {
1429                 DEBUG_1("Notify vf: %s %04x", fd->path, type);
1430                 vf_refresh_idle(vf);
1431                 }
1432 }
1433
1434 static gboolean vf_read_metadata_in_idle_cb(gpointer data)
1435 {
1436         FileData *fd;
1437         ViewFile *vf = data;
1438         GList *list_entry;
1439         GList *work;
1440
1441         vf_thumb_status(vf, vf_read_metadata_in_idle_progress(vf), _("Loading meta..."));
1442
1443         work = vf->list;
1444
1445         while (work)
1446                 {
1447                 fd = work->data;
1448
1449                 if (fd && !fd->metadata_in_idle_loaded)
1450                         {
1451                         if (!fd->exifdate)
1452                                 {
1453                                 read_exif_time_data(fd);
1454                                 }
1455                         if (!fd->exifdate_digitized)
1456                                 {
1457                                 read_exif_time_digitized_data(fd);
1458                                 }
1459                         if (fd->rating == STAR_RATING_NOT_READ)
1460                                 {
1461                                 read_rating_data(fd);
1462                                 }
1463                         fd->metadata_in_idle_loaded = TRUE;
1464                         return TRUE;
1465                         }
1466                 work = work->next;
1467                 }
1468
1469         vf_thumb_status(vf, 0.0, NULL);
1470         vf->read_metadata_in_idle_id = 0;
1471         vf_refresh(vf);
1472         return FALSE;
1473 }
1474
1475 static void vf_read_metadata_in_idle_finished_cb(gpointer data)
1476 {
1477         ViewFile *vf = data;
1478
1479         vf_thumb_status(vf, 0.0, "Loading meta...");
1480         vf->read_metadata_in_idle_id = 0;
1481 }
1482
1483 void vf_read_metadata_in_idle(ViewFile *vf)
1484 {
1485         GList *work;
1486         FileData *fd;
1487
1488         if (!vf) return;
1489
1490         if (vf->read_metadata_in_idle_id)
1491                 {
1492                 g_idle_remove_by_data(vf);
1493                 }
1494         vf->read_metadata_in_idle_id = 0;
1495
1496         if (vf->list)
1497                 {
1498                 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);
1499                 }
1500
1501         return;
1502 }
1503
1504 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */