Fix #437: Delete to standard Trash directory
[geeqie.git] / src / view_file / view_file.c
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * Author: Laurent Monin
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "main.h"
22 #include "view_file.h"
23
24 #include "dupe.h"
25 #include "collect.h"
26 #include "collect-table.h"
27 #include "editors.h"
28 #include "history_list.h"
29 #include "layout.h"
30 #include "menu.h"
31 #include "pixbuf_util.h"
32 #include "thumb.h"
33 #include "ui_menu.h"
34 #include "ui_fileops.h"
35 #include "ui_misc.h"
36 #include "utilops.h"
37 #include "view_file/view_file_list.h"
38 #include "view_file/view_file_icon.h"
39 #include "window.h"
40
41 /*
42  *-----------------------------------------------------------------------------
43  * signals
44  *-----------------------------------------------------------------------------
45  */
46
47 void vf_send_update(ViewFile *vf)
48 {
49         if (vf->func_status) vf->func_status(vf, vf->data_status);
50 }
51
52 /*
53  *-----------------------------------------------------------------------------
54  * misc
55  *-----------------------------------------------------------------------------
56  */
57
58 void vf_sort_set(ViewFile *vf, SortType type, gboolean ascend)
59 {
60         switch (vf->type)
61         {
62         case FILEVIEW_LIST: vflist_sort_set(vf, type, ascend); break;
63         case FILEVIEW_ICON: vficon_sort_set(vf, type, ascend); break;
64         }
65 }
66
67 /*
68  *-----------------------------------------------------------------------------
69  * row stuff
70  *-----------------------------------------------------------------------------
71  */
72
73 FileData *vf_index_get_data(ViewFile *vf, gint row)
74 {
75         return g_list_nth_data(vf->list, row);
76 }
77
78 gint vf_index_by_fd(ViewFile *vf, FileData *fd)
79 {
80         switch (vf->type)
81         {
82         case FILEVIEW_LIST: return vflist_index_by_fd(vf, fd);
83         case FILEVIEW_ICON: return vficon_index_by_fd(vf, fd);
84         }
85 }
86
87 guint vf_count(ViewFile *vf, gint64 *bytes)
88 {
89         if (bytes)
90                 {
91                 gint64 b = 0;
92                 GList *work;
93
94                 work = vf->list;
95                 while (work)
96                         {
97                         FileData *fd = work->data;
98                         work = work->next;
99
100                         b += fd->size;
101                         }
102
103                 *bytes = b;
104                 }
105
106         return g_list_length(vf->list);
107 }
108
109 GList *vf_get_list(ViewFile *vf)
110 {
111         GList *list = NULL;
112         GList *work;
113         for (work = vf->list; work; work = work->next)
114                 {
115                 FileData *fd = work->data;
116                 list = g_list_prepend(list, file_data_ref(fd));
117                 }
118
119         return g_list_reverse(list);
120 }
121
122 /*
123  *-------------------------------------------------------------------
124  * keyboard
125  *-------------------------------------------------------------------
126  */
127
128 static gboolean vf_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
129 {
130         ViewFile *vf = data;
131
132         switch (vf->type)
133         {
134         case FILEVIEW_LIST: return vflist_press_key_cb(widget, event, data);
135         case FILEVIEW_ICON: return vficon_press_key_cb(widget, event, data);
136         }
137 }
138
139 /*
140  *-------------------------------------------------------------------
141  * mouse
142  *-------------------------------------------------------------------
143  */
144
145 static gboolean vf_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
146 {
147         ViewFile *vf = data;
148
149         switch (vf->type)
150         {
151         case FILEVIEW_LIST: return vflist_press_cb(widget, bevent, data);
152         case FILEVIEW_ICON: return vficon_press_cb(widget, bevent, data);
153         }
154 }
155
156 static gboolean vf_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
157 {
158         ViewFile *vf = data;
159
160         switch (vf->type)
161         {
162         case FILEVIEW_LIST: return vflist_release_cb(widget, bevent, data);
163         case FILEVIEW_ICON: return vficon_release_cb(widget, bevent, data);
164         }
165 }
166
167
168 /*
169  *-----------------------------------------------------------------------------
170  * selections
171  *-----------------------------------------------------------------------------
172  */
173
174 guint vf_selection_count(ViewFile *vf, gint64 *bytes)
175 {
176         switch (vf->type)
177         {
178         case FILEVIEW_LIST: return vflist_selection_count(vf, bytes);
179         case FILEVIEW_ICON: return vficon_selection_count(vf, bytes);
180         }
181 }
182
183 GList *vf_selection_get_list(ViewFile *vf)
184 {
185         switch (vf->type)
186         {
187         case FILEVIEW_LIST: return vflist_selection_get_list(vf);
188         case FILEVIEW_ICON: return vficon_selection_get_list(vf);
189         }
190 }
191
192 GList *vf_selection_get_list_by_index(ViewFile *vf)
193 {
194         switch (vf->type)
195         {
196         case FILEVIEW_LIST: return vflist_selection_get_list_by_index(vf);
197         case FILEVIEW_ICON: return vficon_selection_get_list_by_index(vf);
198         }
199 }
200
201 void vf_select_all(ViewFile *vf)
202 {
203         switch (vf->type)
204         {
205         case FILEVIEW_LIST: vflist_select_all(vf); break;
206         case FILEVIEW_ICON: vficon_select_all(vf); break;
207         }
208 }
209
210 void vf_select_none(ViewFile *vf)
211 {
212         switch (vf->type)
213         {
214         case FILEVIEW_LIST: vflist_select_none(vf); break;
215         case FILEVIEW_ICON: vficon_select_none(vf); break;
216         }
217 }
218
219 void vf_select_invert(ViewFile *vf)
220 {
221         switch (vf->type)
222         {
223         case FILEVIEW_LIST: vflist_select_invert(vf); break;
224         case FILEVIEW_ICON: vficon_select_invert(vf); break;
225         }
226 }
227
228 void vf_select_by_fd(ViewFile *vf, FileData *fd)
229 {
230         switch (vf->type)
231         {
232         case FILEVIEW_LIST: vflist_select_by_fd(vf, fd); break;
233         case FILEVIEW_ICON: vficon_select_by_fd(vf, fd); break;
234         }
235 }
236
237 void vf_mark_to_selection(ViewFile *vf, gint mark, MarkToSelectionMode mode)
238 {
239         switch (vf->type)
240         {
241         case FILEVIEW_LIST: vflist_mark_to_selection(vf, mark, mode); break;
242         case FILEVIEW_ICON: vficon_mark_to_selection(vf, mark, mode); break;
243         }
244 }
245
246 void vf_selection_to_mark(ViewFile *vf, gint mark, SelectionToMarkMode mode)
247 {
248         switch (vf->type)
249         {
250         case FILEVIEW_LIST: vflist_selection_to_mark(vf, mark, mode); break;
251         case FILEVIEW_ICON: vficon_selection_to_mark(vf, mark, mode); break;
252         }
253 }
254
255 /*
256  *-----------------------------------------------------------------------------
257  * dnd
258  *-----------------------------------------------------------------------------
259  */
260
261
262 static void vf_dnd_init(ViewFile *vf)
263 {
264         switch (vf->type)
265         {
266         case FILEVIEW_LIST: vflist_dnd_init(vf); break;
267         case FILEVIEW_ICON: vficon_dnd_init(vf); break;
268         }
269 }
270
271 /*
272  *-----------------------------------------------------------------------------
273  * pop-up menu
274  *-----------------------------------------------------------------------------
275  */
276
277 GList *vf_pop_menu_file_list(ViewFile *vf)
278 {
279         switch (vf->type)
280         {
281         case FILEVIEW_LIST: return vflist_pop_menu_file_list(vf);
282         case FILEVIEW_ICON: return vficon_pop_menu_file_list(vf);
283         }
284 }
285
286 GList *vf_selection_get_one(ViewFile *vf, FileData *fd)
287 {
288         switch (vf->type)
289         {
290         case FILEVIEW_LIST: return vflist_selection_get_one(vf, fd);
291         case FILEVIEW_ICON: return vficon_selection_get_one(vf, fd);
292         }
293 }
294
295 static void vf_pop_menu_edit_cb(GtkWidget *widget, gpointer data)
296 {
297         ViewFile *vf;
298         const gchar *key = data;
299
300         vf = submenu_item_get_data(widget);
301
302         if (!vf) return;
303
304         file_util_start_editor_from_filelist(key, vf_pop_menu_file_list(vf), vf->dir_fd->path, vf->listview);
305 }
306
307 static void vf_pop_menu_view_cb(GtkWidget *widget, gpointer data)
308 {
309         ViewFile *vf = data;
310
311         switch (vf->type)
312         {
313         case FILEVIEW_LIST: vflist_pop_menu_view_cb(widget, data); break;
314         case FILEVIEW_ICON: vficon_pop_menu_view_cb(widget, data); break;
315         }
316 }
317
318 static void vf_pop_menu_copy_cb(GtkWidget *widget, gpointer data)
319 {
320         ViewFile *vf = data;
321
322         file_util_copy(NULL, vf_pop_menu_file_list(vf), NULL, vf->listview);
323 }
324
325 static void vf_pop_menu_move_cb(GtkWidget *widget, gpointer data)
326 {
327         ViewFile *vf = data;
328
329         file_util_move(NULL, vf_pop_menu_file_list(vf), NULL, vf->listview);
330 }
331
332 static void vf_pop_menu_rename_cb(GtkWidget *widget, gpointer data)
333 {
334         ViewFile *vf = data;
335
336         switch (vf->type)
337         {
338         case FILEVIEW_LIST: vflist_pop_menu_rename_cb(widget, data); break;
339         case FILEVIEW_ICON: vficon_pop_menu_rename_cb(widget, data); break;
340         }
341 }
342
343 static void vf_pop_menu_delete_cb(GtkWidget *widget, gpointer data)
344 {
345         ViewFile *vf = data;
346
347         options->file_ops.safe_delete_enable = FALSE;
348         file_util_delete(NULL, vf_pop_menu_file_list(vf), vf->listview);
349 }
350
351 static void vf_pop_menu_move_to_trash_cb(GtkWidget *widget, gpointer data)
352 {
353         ViewFile *vf = data;
354
355         options->file_ops.safe_delete_enable = TRUE;
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_divider(menu);
638         menu_item_add_stock_sensitive(menu,
639                                 options->file_ops.confirm_move_to_trash ? _("Move to Trash...") :
640                                         _("Move to Trash"), PIXBUF_INLINE_ICON_TRASH, active,
641                                 G_CALLBACK(vf_pop_menu_move_to_trash_cb), vf);
642         menu_item_add_stock_sensitive(menu,
643                                 options->file_ops.confirm_delete ? _("_Delete...") :
644                                         _("_Delete"), GTK_STOCK_DELETE, active,
645                                 G_CALLBACK(vf_pop_menu_delete_cb), vf);
646         menu_item_add_divider(menu);
647
648         menu_item_add_sensitive(menu, _("Enable file _grouping"), active,
649                                 G_CALLBACK(vf_pop_menu_enable_grouping_cb), vf);
650         menu_item_add_sensitive(menu, _("Disable file groupi_ng"), active,
651                                 G_CALLBACK(vf_pop_menu_disable_grouping_cb), vf);
652
653         menu_item_add_divider(menu);
654         menu_item_add_stock_sensitive(menu, _("_Find duplicates..."), GTK_STOCK_FIND, active,
655                                 G_CALLBACK(vf_pop_menu_duplicates_cb), vf);
656         menu_item_add_divider(menu);
657
658         submenu = submenu_add_collections(menu, &item,
659                                 G_CALLBACK(vf_pop_menu_collections_cb), vf);
660         gtk_widget_set_sensitive(item, active);
661         menu_item_add_divider(menu);
662
663         submenu = submenu_add_sort(NULL, G_CALLBACK(vf_pop_menu_sort_cb), vf,
664                                    FALSE, FALSE, TRUE, vf->sort_method);
665         menu_item_add_divider(submenu);
666         menu_item_add_check(submenu, _("Ascending"), vf->sort_ascend,
667                             G_CALLBACK(vf_pop_menu_sort_ascend_cb), vf);
668
669         item = menu_item_add(menu, _("_Sort"), NULL, NULL);
670         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
671
672         item = menu_item_add_radio(menu, _("View as _List"), GINT_TO_POINTER(FILEVIEW_LIST), vf->type == FILEVIEW_LIST,
673                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
674
675         item = menu_item_add_radio(menu, _("View as _Icons"), GINT_TO_POINTER(FILEVIEW_ICON), vf->type == FILEVIEW_ICON,
676                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
677
678         switch (vf->type)
679         {
680         case FILEVIEW_LIST:
681                 menu_item_add_check(menu, _("Show _thumbnails"), VFLIST(vf)->thumbs_enabled,
682                                     G_CALLBACK(vflist_pop_menu_thumbs_cb), vf);
683                 break;
684         case FILEVIEW_ICON:
685                 menu_item_add_check(menu, _("Show filename _text"), VFICON(vf)->show_text,
686                                     G_CALLBACK(vficon_pop_menu_show_names_cb), vf);
687                 break;
688         }
689
690         switch (vf->type)
691         {
692         case FILEVIEW_LIST:
693                 menu_item_add_check(menu, _("Show star rating"), options->show_star_rating,
694                                     G_CALLBACK(vflist_pop_menu_show_star_rating_cb), vf);
695                 break;
696         case FILEVIEW_ICON:
697                 menu_item_add_check(menu, _("Show star rating"), options->show_star_rating,
698                                     G_CALLBACK(vficon_pop_menu_show_star_rating_cb), vf);
699                 break;
700         }
701
702         menu_item_add_stock(menu, _("Re_fresh"), GTK_STOCK_REFRESH, G_CALLBACK(vf_pop_menu_refresh_cb), vf);
703
704         return menu;
705 }
706
707 gboolean vf_refresh(ViewFile *vf)
708 {
709         switch (vf->type)
710         {
711         case FILEVIEW_LIST: return vflist_refresh(vf);
712         case FILEVIEW_ICON: return vficon_refresh(vf);
713         }
714 }
715
716 gboolean vf_set_fd(ViewFile *vf, FileData *dir_fd)
717 {
718         switch (vf->type)
719         {
720         case FILEVIEW_LIST: return vflist_set_fd(vf, dir_fd);
721         case FILEVIEW_ICON: return vficon_set_fd(vf, dir_fd);
722         }
723 }
724
725 static void vf_destroy_cb(GtkWidget *widget, gpointer data)
726 {
727         ViewFile *vf = data;
728
729         switch (vf->type)
730         {
731         case FILEVIEW_LIST: vflist_destroy_cb(widget, data); break;
732         case FILEVIEW_ICON: vficon_destroy_cb(widget, data); break;
733         }
734
735         if (vf->popup)
736                 {
737                 g_signal_handlers_disconnect_matched(G_OBJECT(vf->popup), G_SIGNAL_MATCH_DATA,
738                                                      0, 0, 0, NULL, vf);
739                 gtk_widget_destroy(vf->popup);
740                 }
741
742         if (vf->read_metadata_in_idle_id)
743                 {
744                 g_idle_remove_by_data(vf);
745                 }
746         file_data_unref(vf->dir_fd);
747         g_free(vf->info);
748         g_free(vf);
749 }
750
751 static void vf_marks_filter_toggle_cb(GtkWidget *widget, gpointer data)
752 {
753         ViewFile *vf = data;
754         vf_refresh_idle(vf);
755 }
756
757 typedef struct _MarksTextEntry MarksTextEntry;
758 struct _MarksTextEntry {
759         GenericDialog *gd;
760         gint mark_no;
761         GtkWidget *edit_widget;
762         gchar *text_entry;
763         GtkWidget *parent;
764 };
765
766 static void vf_marks_tooltip_cancel_cb(GenericDialog *gd, gpointer data)
767 {
768         MarksTextEntry *mte = data;
769
770         g_free(mte->text_entry);
771         generic_dialog_close(gd);
772 }
773
774 static void vf_marks_tooltip_ok_cb(GenericDialog *gd, gpointer data)
775 {
776         MarksTextEntry *mte = data;
777
778         g_free(options->marks_tooltips[mte->mark_no]);
779         options->marks_tooltips[mte->mark_no] = g_strdup(gtk_entry_get_text(GTK_ENTRY(mte->edit_widget)));
780
781         gtk_widget_set_tooltip_text(mte->parent, options->marks_tooltips[mte->mark_no]);
782
783         g_free(mte->text_entry);
784         generic_dialog_close(gd);
785 }
786
787 void vf_marks_filter_on_icon_press(GtkEntry *entry, GtkEntryIconPosition pos,
788                                                                         GdkEvent *event, gpointer userdata)
789 {
790         MarksTextEntry *mte = userdata;
791
792         g_free(mte->text_entry);
793         mte->text_entry = g_strdup("");
794         gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), "");
795 }
796
797 static void vf_marks_tooltip_help_cb(GenericDialog *gd, gpointer data)
798 {
799         help_window_show("GuideImageMarks.html");
800 }
801
802 static gboolean vf_marks_tooltip_cb(GtkWidget *widget,
803                                                                                 GdkEventButton *event,
804                                                                                 gpointer user_data)
805 {
806         GtkWidget *table;
807         gint i = GPOINTER_TO_INT(user_data);
808         MarksTextEntry *mte;
809
810         if (event->button == MOUSE_BUTTON_RIGHT)
811                 {
812                 mte = g_new0(MarksTextEntry, 1);
813                 mte->mark_no = i;
814                 mte->text_entry = g_strdup(options->marks_tooltips[i]);
815                 mte->parent = widget;
816
817                 mte->gd = generic_dialog_new(_("Mark text"), "mark_text",
818                                                 widget, FALSE,
819                                                 vf_marks_tooltip_cancel_cb, mte);
820                 generic_dialog_add_message(mte->gd, GTK_STOCK_DIALOG_QUESTION, _("Set mark text"),
821                                             _("This will set or clear the mark text."), FALSE);
822                 generic_dialog_add_button(mte->gd, GTK_STOCK_OK, NULL,
823                                                         vf_marks_tooltip_ok_cb, TRUE);
824                 generic_dialog_add_button(mte->gd, GTK_STOCK_HELP, NULL,
825                                                 vf_marks_tooltip_help_cb, FALSE);
826
827                 table = pref_table_new(mte->gd->vbox, 3, 1, FALSE, TRUE);
828                 pref_table_label(table, 0, 0, g_strdup_printf("%s%d", _("Mark "), mte->mark_no + 1), 1.0);
829                 mte->edit_widget = gtk_entry_new();
830                 gtk_widget_set_size_request(mte->edit_widget, 300, -1);
831                 if (mte->text_entry)
832                         {
833                         gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), mte->text_entry);
834                         }
835                 gtk_table_attach_defaults(GTK_TABLE(table), mte->edit_widget, 1, 2, 0, 1);
836                 generic_dialog_attach_default(mte->gd, mte->edit_widget);
837
838                 gtk_entry_set_icon_from_stock(GTK_ENTRY(mte->edit_widget),
839                                                         GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
840                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY(mte->edit_widget),
841                                                         GTK_ENTRY_ICON_SECONDARY, "Clear");
842                 g_signal_connect(GTK_ENTRY(mte->edit_widget), "icon-press",
843                                                         G_CALLBACK(vf_marks_filter_on_icon_press), mte);
844
845                 gtk_widget_show(mte->edit_widget);
846                 gtk_widget_grab_focus(mte->edit_widget);
847                 gtk_widget_show(GTK_WIDGET(mte->gd->dialog));
848
849                 return TRUE;
850                 }
851
852         return FALSE;
853 }
854
855 static void vf_file_filter_save_cb(GtkWidget *widget, gpointer data)
856 {
857         ViewFile *vf = data;
858         gchar *entry_text;
859         gchar *remove_text = NULL;
860         gchar *index_text = NULL;
861         gboolean text_found = FALSE;
862         gint i;
863
864         entry_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)))));
865
866         if (entry_text[0] == '\0' && vf->file_filter.last_selected >= 0)
867                 {
868                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), vf->file_filter.last_selected);
869                 remove_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
870                 history_list_item_remove("file_filter", remove_text);
871                 gtk_combo_box_text_remove(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), vf->file_filter.last_selected);
872                 g_free(remove_text);
873
874                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), -1);
875                 vf->file_filter.last_selected = - 1;
876                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo))), "");
877                 vf->file_filter.count--;
878                 }
879         else
880                 {
881                 if (entry_text[0] != '\0')
882                         {
883                         for (i = 0; i < vf->file_filter.count; i++)
884                                 {
885                                 if (index_text)
886                                         {
887                                         g_free(index_text);
888                                         }
889                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), i);
890                                 index_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
891
892                                 if (g_strcmp0(index_text, entry_text) == 0)
893                                         {
894                                         text_found = TRUE;
895                                         break;
896                                         }
897                                 }
898
899                         g_free(index_text);
900                         if (!text_found)
901                                 {
902                                 history_list_add_to_key("file_filter", entry_text, 10);
903                                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), entry_text);
904                                 vf->file_filter.count++;
905                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), vf->file_filter.count - 1);
906                                 }
907                         }
908                 }
909         vf_refresh(vf);
910
911         g_free(entry_text);
912 }
913
914 static void vf_file_filter_cb(GtkWidget *widget, gpointer data)
915 {
916         ViewFile *vf = data;
917
918         vf_refresh(vf);
919 }
920
921 static gboolean vf_file_filter_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
922 {
923         ViewFile *vf = data;
924         vf->file_filter.last_selected = gtk_combo_box_get_active(GTK_COMBO_BOX(vf->file_filter.combo));
925
926         gtk_widget_grab_focus(widget);
927
928         return TRUE;
929 }
930
931 static GtkWidget *vf_marks_filter_init(ViewFile *vf)
932 {
933         GtkWidget *frame = gtk_frame_new(NULL);
934         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
935
936         gint i;
937
938         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
939                 {
940                 GtkWidget *check = gtk_check_button_new();
941                 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, FALSE, 0);
942                 g_signal_connect(G_OBJECT(check), "toggled",
943                          G_CALLBACK(vf_marks_filter_toggle_cb), vf);
944                 g_signal_connect(G_OBJECT(check), "button_press_event",
945                          G_CALLBACK(vf_marks_tooltip_cb), GINT_TO_POINTER(i));
946                 gtk_widget_set_tooltip_text(check, options->marks_tooltips[i]);
947
948                 gtk_widget_show(check);
949                 vf->filter_check[i] = check;
950                 }
951         gtk_container_add(GTK_CONTAINER(frame), hbox);
952         gtk_widget_show(hbox);
953         return frame;
954 }
955
956 void vf_file_filter_set(ViewFile *vf, gboolean enable)
957 {
958         if (enable)
959                 {
960                 gtk_widget_show(vf->file_filter.combo);
961                 gtk_widget_show(vf->file_filter.frame);
962                 }
963         else
964                 {
965                 gtk_widget_hide(vf->file_filter.combo);
966                 gtk_widget_hide(vf->file_filter.frame);
967                 }
968
969         vf_refresh(vf);
970 }
971
972 static GtkWidget *vf_file_filter_init(ViewFile *vf)
973 {
974         GtkWidget *frame = gtk_frame_new(NULL);
975         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
976         GList *work;
977         gint n = 0;
978         GtkWidget *combo_entry;
979
980         vf->file_filter.combo = gtk_combo_box_text_new_with_entry();
981         combo_entry = gtk_bin_get_child(GTK_BIN(vf->file_filter.combo));
982         gtk_widget_show(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)));
983         gtk_widget_show((GTK_WIDGET(vf->file_filter.combo)));
984         gtk_widget_set_tooltip_text(GTK_WIDGET(vf->file_filter.combo), "Use regular expressions");
985
986         work = history_list_get_by_key("file_filter");
987         while (work)
988                 {
989                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), (gchar *)work->data);
990                 work = work->next;
991                 n++;
992                 vf->file_filter.count = n;
993                 }
994         gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), 0);
995
996         g_signal_connect(G_OBJECT(combo_entry), "activate",
997                 G_CALLBACK(vf_file_filter_save_cb), vf);
998                 
999         g_signal_connect(G_OBJECT(vf->file_filter.combo), "changed",
1000                 G_CALLBACK(vf_file_filter_cb), vf);
1001
1002         g_signal_connect(G_OBJECT(combo_entry), "button_press_event",
1003                          G_CALLBACK(vf_file_filter_press_cb), vf);
1004
1005         gtk_box_pack_start(GTK_BOX(hbox), vf->file_filter.combo, FALSE, FALSE, 0);
1006         gtk_widget_show(vf->file_filter.combo);
1007         gtk_container_add(GTK_CONTAINER(frame), hbox);
1008         gtk_widget_show(hbox);
1009
1010         return frame;
1011 }
1012
1013 void vf_mark_filter_toggle(ViewFile *vf, gint mark)
1014 {
1015         gint n = mark - 1;
1016         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vf->filter_check[n]),
1017                                      !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[n])));
1018 }
1019
1020 ViewFile *vf_new(FileViewType type, FileData *dir_fd)
1021 {
1022         ViewFile *vf;
1023
1024         vf = g_new0(ViewFile, 1);
1025
1026         vf->type = type;
1027         vf->sort_method = SORT_NAME;
1028         vf->sort_ascend = TRUE;
1029         vf->read_metadata_in_idle_id = 0;
1030
1031         vf->scrolled = gtk_scrolled_window_new(NULL, NULL);
1032         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vf->scrolled), GTK_SHADOW_IN);
1033         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vf->scrolled),
1034                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1035
1036         vf->filter = vf_marks_filter_init(vf);
1037         vf->file_filter.frame = vf_file_filter_init(vf);
1038
1039         vf->widget = gtk_vbox_new(FALSE, 0);
1040         gtk_box_pack_start(GTK_BOX(vf->widget), vf->filter, FALSE, FALSE, 0);
1041         gtk_box_pack_start(GTK_BOX(vf->widget), vf->file_filter.frame, FALSE, FALSE, 0);
1042         gtk_box_pack_start(GTK_BOX(vf->widget), vf->scrolled, TRUE, TRUE, 0);
1043         gtk_widget_show(vf->scrolled);
1044
1045         g_signal_connect(G_OBJECT(vf->widget), "destroy",
1046                          G_CALLBACK(vf_destroy_cb), vf);
1047
1048         switch (type)
1049         {
1050         case FILEVIEW_LIST: vf = vflist_new(vf, dir_fd); break;
1051         case FILEVIEW_ICON: vf = vficon_new(vf, dir_fd); break;
1052         }
1053
1054         vf_dnd_init(vf);
1055
1056         g_signal_connect(G_OBJECT(vf->listview), "key_press_event",
1057                          G_CALLBACK(vf_press_key_cb), vf);
1058         g_signal_connect(G_OBJECT(vf->listview), "button_press_event",
1059                          G_CALLBACK(vf_press_cb), vf);
1060         g_signal_connect(G_OBJECT(vf->listview), "button_release_event",
1061                          G_CALLBACK(vf_release_cb), vf);
1062
1063         gtk_container_add(GTK_CONTAINER(vf->scrolled), vf->listview);
1064         gtk_widget_show(vf->listview);
1065
1066         if (dir_fd) vf_set_fd(vf, dir_fd);
1067
1068         return vf;
1069 }
1070
1071 void vf_set_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gpointer data), gpointer data)
1072 {
1073         vf->func_status = func;
1074         vf->data_status = data;
1075 }
1076
1077 void vf_set_thumb_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gdouble val, const gchar *text, gpointer data), gpointer data)
1078 {
1079         vf->func_thumb_status = func;
1080         vf->data_thumb_status = data;
1081 }
1082
1083 void vf_thumb_set(ViewFile *vf, gboolean enable)
1084 {
1085         switch (vf->type)
1086         {
1087         case FILEVIEW_LIST: vflist_thumb_set(vf, enable); break;
1088         case FILEVIEW_ICON: /*vficon_thumb_set(vf, enable);*/ break;
1089         }
1090 }
1091
1092
1093 static gboolean vf_thumb_next(ViewFile *vf);
1094
1095 static gdouble vf_thumb_progress(ViewFile *vf)
1096 {
1097         gint count = 0;
1098         gint done = 0;
1099
1100         switch (vf->type)
1101         {
1102         case FILEVIEW_LIST: vflist_thumb_progress_count(vf->list, &count, &done); break;
1103         case FILEVIEW_ICON: vficon_thumb_progress_count(vf->list, &count, &done); break;
1104         }
1105
1106         DEBUG_1("thumb progress: %d of %d", done, count);
1107         return (gdouble)done / count;
1108 }
1109
1110 static gdouble vf_read_metadata_in_idle_progress(ViewFile *vf)
1111 {
1112         gint count = 0;
1113         gint done = 0;
1114
1115         switch (vf->type)
1116                 {
1117                 case FILEVIEW_LIST: vflist_read_metadata_progress_count(vf->list, &count, &done); break;
1118                 case FILEVIEW_ICON: vficon_read_metadata_progress_count(vf->list, &count, &done); break;
1119                 }
1120
1121         return (gdouble)done / count;
1122 }
1123
1124 static void vf_set_thumb_fd(ViewFile *vf, FileData *fd)
1125 {
1126         switch (vf->type)
1127         {
1128         case FILEVIEW_LIST: vflist_set_thumb_fd(vf, fd); break;
1129         case FILEVIEW_ICON: vficon_set_thumb_fd(vf, fd); break;
1130         }
1131 }
1132
1133 static void vf_thumb_status(ViewFile *vf, gdouble val, const gchar *text)
1134 {
1135         if (vf->func_thumb_status)
1136                 {
1137                 vf->func_thumb_status(vf, val, text, vf->data_thumb_status);
1138                 }
1139 }
1140
1141 static void vf_thumb_do(ViewFile *vf, FileData *fd)
1142 {
1143         if (!fd) return;
1144
1145         vf_set_thumb_fd(vf, fd);
1146         vf_thumb_status(vf, vf_thumb_progress(vf), _("Loading thumbs..."));
1147 }
1148
1149 void vf_thumb_cleanup(ViewFile *vf)
1150 {
1151         vf_thumb_status(vf, 0.0, NULL);
1152
1153         vf->thumbs_running = FALSE;
1154
1155         thumb_loader_free(vf->thumbs_loader);
1156         vf->thumbs_loader = NULL;
1157
1158         vf->thumbs_filedata = NULL;
1159 }
1160
1161 void vf_thumb_stop(ViewFile *vf)
1162 {
1163         if (vf->thumbs_running) vf_thumb_cleanup(vf);
1164 }
1165
1166 static void vf_thumb_common_cb(ThumbLoader *tl, gpointer data)
1167 {
1168         ViewFile *vf = data;
1169
1170         if (vf->thumbs_filedata && vf->thumbs_loader == tl)
1171                 {
1172                 vf_thumb_do(vf, vf->thumbs_filedata);
1173                 }
1174
1175         while (vf_thumb_next(vf));
1176 }
1177
1178 static void vf_thumb_error_cb(ThumbLoader *tl, gpointer data)
1179 {
1180         vf_thumb_common_cb(tl, data);
1181 }
1182
1183 static void vf_thumb_done_cb(ThumbLoader *tl, gpointer data)
1184 {
1185         vf_thumb_common_cb(tl, data);
1186 }
1187
1188 static gboolean vf_thumb_next(ViewFile *vf)
1189 {
1190         FileData *fd = NULL;
1191
1192         if (!gtk_widget_get_realized(vf->listview))
1193                 {
1194                 vf_thumb_status(vf, 0.0, NULL);
1195                 return FALSE;
1196                 }
1197
1198         switch (vf->type)
1199         {
1200         case FILEVIEW_LIST: fd = vflist_thumb_next_fd(vf); break;
1201         case FILEVIEW_ICON: fd = vficon_thumb_next_fd(vf); break;
1202         }
1203
1204         if (!fd)
1205                 {
1206                 /* done */
1207                 vf_thumb_cleanup(vf);
1208                 return FALSE;
1209                 }
1210
1211         vf->thumbs_filedata = fd;
1212
1213         thumb_loader_free(vf->thumbs_loader);
1214
1215         vf->thumbs_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
1216         thumb_loader_set_callbacks(vf->thumbs_loader,
1217                                    vf_thumb_done_cb,
1218                                    vf_thumb_error_cb,
1219                                    NULL,
1220                                    vf);
1221
1222         if (!thumb_loader_start(vf->thumbs_loader, fd))
1223                 {
1224                 /* set icon to unknown, continue */
1225                 DEBUG_1("thumb loader start failed %s", fd->path);
1226                 vf_thumb_do(vf, fd);
1227
1228                 return TRUE;
1229                 }
1230
1231         return FALSE;
1232 }
1233
1234 static void vf_thumb_reset_all(ViewFile *vf)
1235 {
1236         GList *work;
1237
1238         for (work = vf->list; work; work = work->next)
1239                 {
1240                 FileData *fd = work->data;
1241                 if (fd->thumb_pixbuf)
1242                         {
1243                         g_object_unref(fd->thumb_pixbuf);
1244                         fd->thumb_pixbuf = NULL;
1245                         }
1246                 }
1247 }
1248
1249 void vf_thumb_update(ViewFile *vf)
1250 {
1251         vf_thumb_stop(vf);
1252
1253         if (vf->type == FILEVIEW_LIST && !VFLIST(vf)->thumbs_enabled) return;
1254
1255         vf_thumb_status(vf, 0.0, _("Loading thumbs..."));
1256         vf->thumbs_running = TRUE;
1257
1258         if (thumb_format_changed)
1259                 {
1260                 vf_thumb_reset_all(vf);
1261                 thumb_format_changed = FALSE;
1262                 }
1263
1264         while (vf_thumb_next(vf));
1265 }
1266
1267
1268 void vf_marks_set(ViewFile *vf, gboolean enable)
1269 {
1270         if (vf->marks_enabled == enable) return;
1271
1272         vf->marks_enabled = enable;
1273
1274         switch (vf->type)
1275         {
1276         case FILEVIEW_LIST: vflist_marks_set(vf, enable); break;
1277         case FILEVIEW_ICON: vficon_marks_set(vf, enable); break;
1278         }
1279         if (enable)
1280                 gtk_widget_show(vf->filter);
1281         else
1282                 gtk_widget_hide(vf->filter);
1283
1284         vf_refresh_idle(vf);
1285 }
1286
1287 void vf_star_rating_set(ViewFile *vf, gboolean enable)
1288 {
1289         if (options->show_star_rating == enable) return;
1290         options->show_star_rating = enable;
1291
1292         switch (vf->type)
1293                 {
1294                 case FILEVIEW_LIST: vflist_star_rating_set(vf, enable); break;
1295                 case FILEVIEW_ICON: vficon_star_rating_set(vf, enable); break;
1296                 }
1297         vf_refresh_idle(vf);
1298 }
1299
1300 guint vf_marks_get_filter(ViewFile *vf)
1301 {
1302         guint ret = 0;
1303         gint i;
1304         if (!vf->marks_enabled) return 0;
1305
1306         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
1307                 {
1308                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[i])))
1309                         {
1310                         ret |= 1 << i;
1311                         }
1312                 }
1313         return ret;
1314 }
1315
1316 GRegex *vf_file_filter_get_filter(ViewFile *vf)
1317 {
1318         GRegex *ret = NULL;
1319         GError *error = NULL;
1320         gchar *file_filter_text = NULL;
1321
1322         if (!gtk_widget_get_visible(vf->file_filter.combo))
1323                 {
1324                 return g_regex_new("", 0, 0, NULL);
1325                 }
1326
1327         file_filter_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
1328
1329         if (file_filter_text[0] != '\0')
1330                 {
1331                 ret = g_regex_new(file_filter_text, 0, 0, &error);
1332                 if (error)
1333                         {
1334                         log_printf("Error: could not compile regular expression %s\n%s\n", file_filter_text, error->message);
1335                         g_error_free(error);
1336                         error = NULL;
1337                         ret = g_regex_new("", 0, 0, NULL);
1338                         }
1339                 g_free(file_filter_text);
1340                 }
1341         else
1342                 {
1343                 ret = g_regex_new("", 0, 0, NULL);
1344                 }
1345
1346         return ret;
1347 }
1348
1349 void vf_set_layout(ViewFile *vf, LayoutWindow *layout)
1350 {
1351         vf->layout = layout;
1352 }
1353
1354
1355 /*
1356  *-----------------------------------------------------------------------------
1357  * maintenance (for rename, move, remove)
1358  *-----------------------------------------------------------------------------
1359  */
1360
1361 static gboolean vf_refresh_idle_cb(gpointer data)
1362 {
1363         ViewFile *vf = data;
1364
1365         vf_refresh(vf);
1366         vf->refresh_idle_id = 0;
1367         return FALSE;
1368 }
1369
1370 void vf_refresh_idle_cancel(ViewFile *vf)
1371 {
1372         if (vf->refresh_idle_id)
1373                 {
1374                 g_source_remove(vf->refresh_idle_id);
1375                 vf->refresh_idle_id = 0;
1376                 }
1377 }
1378
1379
1380 void vf_refresh_idle(ViewFile *vf)
1381 {
1382         if (!vf->refresh_idle_id)
1383                 {
1384                 vf->time_refresh_set = time(NULL);
1385                 /* file operations run with G_PRIORITY_DEFAULT_IDLE */
1386                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE + 50, vf_refresh_idle_cb, vf, NULL);
1387                 }
1388         else if (time(NULL) - vf->time_refresh_set > 1)
1389                 {
1390                 /* more than 1 sec since last update - increase priority */
1391                 vf_refresh_idle_cancel(vf);
1392                 vf->time_refresh_set = time(NULL);
1393                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 50, vf_refresh_idle_cb, vf, NULL);
1394                 }
1395 }
1396
1397 void vf_notify_cb(FileData *fd, NotifyType type, gpointer data)
1398 {
1399         ViewFile *vf = data;
1400         gboolean refresh;
1401
1402         NotifyType interested = NOTIFY_CHANGE | NOTIFY_REREAD | NOTIFY_GROUPING;
1403         if (vf->marks_enabled) interested |= NOTIFY_MARKS | NOTIFY_METADATA;
1404         /* FIXME: NOTIFY_METADATA should be checked by the keyword-to-mark functions and converted to NOTIFY_MARKS only if there was a change */
1405
1406         if (!(type & interested) || vf->refresh_idle_id || !vf->dir_fd) return;
1407
1408         refresh = (fd == vf->dir_fd);
1409
1410         if (!refresh)
1411                 {
1412                 gchar *base = remove_level_from_path(fd->path);
1413                 refresh = (g_strcmp0(base, vf->dir_fd->path) == 0);
1414                 g_free(base);
1415                 }
1416
1417         if ((type & NOTIFY_CHANGE) && fd->change)
1418                 {
1419                 if (!refresh && fd->change->dest)
1420                         {
1421                         gchar *dest_base = remove_level_from_path(fd->change->dest);
1422                         refresh = (g_strcmp0(dest_base, vf->dir_fd->path) == 0);
1423                         g_free(dest_base);
1424                         }
1425
1426                 if (!refresh && fd->change->source)
1427                         {
1428                         gchar *source_base = remove_level_from_path(fd->change->source);
1429                         refresh = (g_strcmp0(source_base, vf->dir_fd->path) == 0);
1430                         g_free(source_base);
1431                         }
1432                 }
1433
1434         if (refresh)
1435                 {
1436                 DEBUG_1("Notify vf: %s %04x", fd->path, type);
1437                 vf_refresh_idle(vf);
1438                 }
1439 }
1440
1441 static gboolean vf_read_metadata_in_idle_cb(gpointer data)
1442 {
1443         FileData *fd;
1444         ViewFile *vf = data;
1445         GList *list_entry;
1446         GList *work;
1447
1448         vf_thumb_status(vf, vf_read_metadata_in_idle_progress(vf), _("Loading meta..."));
1449
1450         work = vf->list;
1451
1452         while (work)
1453                 {
1454                 fd = work->data;
1455
1456                 if (fd && !fd->metadata_in_idle_loaded)
1457                         {
1458                         if (!fd->exifdate)
1459                                 {
1460                                 read_exif_time_data(fd);
1461                                 }
1462                         if (!fd->exifdate_digitized)
1463                                 {
1464                                 read_exif_time_digitized_data(fd);
1465                                 }
1466                         if (fd->rating == STAR_RATING_NOT_READ)
1467                                 {
1468                                 read_rating_data(fd);
1469                                 }
1470                         fd->metadata_in_idle_loaded = TRUE;
1471                         return TRUE;
1472                         }
1473                 work = work->next;
1474                 }
1475
1476         vf_thumb_status(vf, 0.0, NULL);
1477         vf->read_metadata_in_idle_id = 0;
1478         vf_refresh(vf);
1479         return FALSE;
1480 }
1481
1482 static void vf_read_metadata_in_idle_finished_cb(gpointer data)
1483 {
1484         ViewFile *vf = data;
1485
1486         vf_thumb_status(vf, 0.0, "Loading meta...");
1487         vf->read_metadata_in_idle_id = 0;
1488 }
1489
1490 void vf_read_metadata_in_idle(ViewFile *vf)
1491 {
1492         GList *work;
1493         FileData *fd;
1494
1495         if (!vf) return;
1496
1497         if (vf->read_metadata_in_idle_id)
1498                 {
1499                 g_idle_remove_by_data(vf);
1500                 }
1501         vf->read_metadata_in_idle_id = 0;
1502
1503         if (vf->list)
1504                 {
1505                 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);
1506                 }
1507
1508         return;
1509 }
1510
1511 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */