Ref #761: Severe stall/hang at start-up
[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_refresh_cb(GtkWidget *widget, gpointer data)
489 {
490         ViewFile *vf = data;
491
492         switch (vf->type)
493         {
494         case FILEVIEW_LIST: vflist_pop_menu_refresh_cb(widget, data); break;
495         case FILEVIEW_ICON: vficon_pop_menu_refresh_cb(widget, data); break;
496         }
497 }
498
499 static void vf_popup_destroy_cb(GtkWidget *widget, gpointer data)
500 {
501         ViewFile *vf = data;
502
503         switch (vf->type)
504         {
505         case FILEVIEW_LIST: vflist_popup_destroy_cb(widget, data); break;
506         case FILEVIEW_ICON: vficon_popup_destroy_cb(widget, data); break;
507         }
508
509         filelist_free(vf->editmenu_fd_list);
510         vf->editmenu_fd_list = NULL;
511 }
512
513 /**
514  * @brief Add file selection list to a collection
515  * @param[in] widget 
516  * @param[in] data Index to the collection list menu item selected, or -1 for new collection
517  * 
518  * 
519  */
520 static void vf_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
521 {
522         ViewFile *vf;
523         GList *selection_list;
524
525         vf = submenu_item_get_data(widget);
526         selection_list = vf_selection_get_list(vf);
527         pop_menu_collections(selection_list, data);
528
529         filelist_free(selection_list);
530 }
531
532 GtkWidget *vf_pop_menu(ViewFile *vf)
533 {
534         GtkWidget *menu;
535         GtkWidget *item;
536         GtkWidget *submenu;
537         gboolean active = FALSE;
538
539         switch (vf->type)
540         {
541         case FILEVIEW_LIST:
542                 vflist_color_set(vf, VFLIST(vf)->click_fd, TRUE);
543                 active = (VFLIST(vf)->click_fd != NULL);
544                 break;
545         case FILEVIEW_ICON:
546                 active = (VFICON(vf)->click_fd != NULL);
547                 break;
548         }
549
550         menu = popup_menu_short_lived();
551
552         g_signal_connect(G_OBJECT(menu), "destroy",
553                          G_CALLBACK(vf_popup_destroy_cb), vf);
554
555         if (vf->clicked_mark > 0)
556                 {
557                 gint mark = vf->clicked_mark;
558                 gchar *str_set_mark = g_strdup_printf(_("_Set mark %d"), mark);
559                 gchar *str_res_mark = g_strdup_printf(_("_Reset mark %d"), mark);
560                 gchar *str_toggle_mark = g_strdup_printf(_("_Toggle mark %d"), mark);
561                 gchar *str_sel_mark = g_strdup_printf(_("_Select mark %d"), mark);
562                 gchar *str_sel_mark_or = g_strdup_printf(_("_Add mark %d"), mark);
563                 gchar *str_sel_mark_and = g_strdup_printf(_("_Intersection with mark %d"), mark);
564                 gchar *str_sel_mark_minus = g_strdup_printf(_("_Unselect mark %d"), mark);
565
566                 g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
567
568                 vf->active_mark = mark;
569                 vf->clicked_mark = 0;
570
571                 menu_item_add_sensitive(menu, str_set_mark, active,
572                                         G_CALLBACK(vf_pop_menu_set_mark_sel_cb), vf);
573
574                 menu_item_add_sensitive(menu, str_res_mark, active,
575                                         G_CALLBACK(vf_pop_menu_res_mark_sel_cb), vf);
576
577                 menu_item_add_sensitive(menu, str_toggle_mark, active,
578                                         G_CALLBACK(vf_pop_menu_toggle_mark_sel_cb), vf);
579
580                 menu_item_add_divider(menu);
581
582                 menu_item_add_sensitive(menu, str_sel_mark, active,
583                                         G_CALLBACK(vf_pop_menu_sel_mark_cb), vf);
584                 menu_item_add_sensitive(menu, str_sel_mark_or, active,
585                                         G_CALLBACK(vf_pop_menu_sel_mark_or_cb), vf);
586                 menu_item_add_sensitive(menu, str_sel_mark_and, active,
587                                         G_CALLBACK(vf_pop_menu_sel_mark_and_cb), vf);
588                 menu_item_add_sensitive(menu, str_sel_mark_minus, active,
589                                         G_CALLBACK(vf_pop_menu_sel_mark_minus_cb), vf);
590
591                 menu_item_add_divider(menu);
592
593                 g_free(str_set_mark);
594                 g_free(str_res_mark);
595                 g_free(str_toggle_mark);
596                 g_free(str_sel_mark);
597                 g_free(str_sel_mark_and);
598                 g_free(str_sel_mark_or);
599                 g_free(str_sel_mark_minus);
600                 }
601
602         vf->editmenu_fd_list = vf_pop_menu_file_list(vf);
603         submenu_add_edit(menu, &item, G_CALLBACK(vf_pop_menu_edit_cb), vf, vf->editmenu_fd_list);
604         gtk_widget_set_sensitive(item, active);
605
606         menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, active,
607                                       G_CALLBACK(vf_pop_menu_view_cb), vf);
608
609         menu_item_add_divider(menu);
610         menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, active,
611                                       G_CALLBACK(vf_pop_menu_copy_cb), vf);
612         menu_item_add_sensitive(menu, _("_Move..."), active,
613                                 G_CALLBACK(vf_pop_menu_move_cb), vf);
614         menu_item_add_sensitive(menu, _("_Rename..."), active,
615                                 G_CALLBACK(vf_pop_menu_rename_cb), vf);
616         menu_item_add_sensitive(menu, _("_Copy path"), active,
617                                 G_CALLBACK(vf_pop_menu_copy_path_cb), vf);
618         menu_item_add_sensitive(menu, _("_Copy path unquoted"), active,
619                                 G_CALLBACK(vf_pop_menu_copy_path_unquoted_cb), vf);
620         menu_item_add_divider(menu);
621         menu_item_add_stock_sensitive(menu,
622                                 options->file_ops.confirm_move_to_trash ? _("Move to Trash...") :
623                                         _("Move to Trash"), PIXBUF_INLINE_ICON_TRASH, active,
624                                 G_CALLBACK(vf_pop_menu_move_to_trash_cb), vf);
625         menu_item_add_stock_sensitive(menu,
626                                 options->file_ops.confirm_delete ? _("_Delete...") :
627                                         _("_Delete"), GTK_STOCK_DELETE, active,
628                                 G_CALLBACK(vf_pop_menu_delete_cb), vf);
629         menu_item_add_divider(menu);
630
631         menu_item_add_sensitive(menu, _("Enable file _grouping"), active,
632                                 G_CALLBACK(vf_pop_menu_enable_grouping_cb), vf);
633         menu_item_add_sensitive(menu, _("Disable file groupi_ng"), active,
634                                 G_CALLBACK(vf_pop_menu_disable_grouping_cb), vf);
635
636         menu_item_add_divider(menu);
637         menu_item_add_stock_sensitive(menu, _("_Find duplicates..."), GTK_STOCK_FIND, active,
638                                 G_CALLBACK(vf_pop_menu_duplicates_cb), vf);
639         menu_item_add_divider(menu);
640
641         submenu = submenu_add_collections(menu, &item,
642                                 G_CALLBACK(vf_pop_menu_collections_cb), vf);
643         gtk_widget_set_sensitive(item, active);
644         menu_item_add_divider(menu);
645
646         submenu = submenu_add_sort(NULL, G_CALLBACK(vf_pop_menu_sort_cb), vf,
647                                    FALSE, FALSE, TRUE, vf->sort_method);
648         menu_item_add_divider(submenu);
649         menu_item_add_check(submenu, _("Ascending"), vf->sort_ascend,
650                             G_CALLBACK(vf_pop_menu_sort_ascend_cb), vf);
651
652         item = menu_item_add(menu, _("_Sort"), NULL, NULL);
653         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
654
655         item = menu_item_add_radio(menu, _("View as _List"), GINT_TO_POINTER(FILEVIEW_LIST), vf->type == FILEVIEW_LIST,
656                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
657
658         item = menu_item_add_radio(menu, _("View as _Icons"), GINT_TO_POINTER(FILEVIEW_ICON), vf->type == FILEVIEW_ICON,
659                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
660
661         switch (vf->type)
662         {
663         case FILEVIEW_LIST:
664                 menu_item_add_check(menu, _("Show _thumbnails"), VFLIST(vf)->thumbs_enabled,
665                                     G_CALLBACK(vflist_pop_menu_thumbs_cb), vf);
666                 break;
667         case FILEVIEW_ICON:
668                 menu_item_add_check(menu, _("Show filename _text"), VFICON(vf)->show_text,
669                                     G_CALLBACK(vficon_pop_menu_show_names_cb), vf);
670                 break;
671         }
672
673         switch (vf->type)
674         {
675         case FILEVIEW_LIST:
676                 menu_item_add_check(menu, _("Show star rating"), options->show_star_rating,
677                                     G_CALLBACK(vflist_pop_menu_show_star_rating_cb), vf);
678                 break;
679         case FILEVIEW_ICON:
680                 menu_item_add_check(menu, _("Show star rating"), options->show_star_rating,
681                                     G_CALLBACK(vficon_pop_menu_show_star_rating_cb), vf);
682                 break;
683         }
684
685         menu_item_add_stock(menu, _("Re_fresh"), GTK_STOCK_REFRESH, G_CALLBACK(vf_pop_menu_refresh_cb), vf);
686
687         return menu;
688 }
689
690 gboolean vf_refresh(ViewFile *vf)
691 {
692         switch (vf->type)
693         {
694         case FILEVIEW_LIST: return vflist_refresh(vf);
695         case FILEVIEW_ICON: return vficon_refresh(vf);
696         }
697 }
698
699 gboolean vf_set_fd(ViewFile *vf, FileData *dir_fd)
700 {
701         switch (vf->type)
702         {
703         case FILEVIEW_LIST: return vflist_set_fd(vf, dir_fd);
704         case FILEVIEW_ICON: return vficon_set_fd(vf, dir_fd);
705         }
706 }
707
708 static void vf_destroy_cb(GtkWidget *widget, gpointer data)
709 {
710         ViewFile *vf = data;
711
712         switch (vf->type)
713         {
714         case FILEVIEW_LIST: vflist_destroy_cb(widget, data); break;
715         case FILEVIEW_ICON: vficon_destroy_cb(widget, data); break;
716         }
717
718         if (vf->popup)
719                 {
720                 g_signal_handlers_disconnect_matched(G_OBJECT(vf->popup), G_SIGNAL_MATCH_DATA,
721                                                      0, 0, 0, NULL, vf);
722                 gtk_widget_destroy(vf->popup);
723                 }
724
725         if (vf->read_metadata_in_idle_id)
726                 {
727                 g_idle_remove_by_data(vf);
728                 }
729         file_data_unref(vf->dir_fd);
730         g_free(vf->info);
731         g_free(vf);
732 }
733
734 static void vf_marks_filter_toggle_cb(GtkWidget *widget, gpointer data)
735 {
736         ViewFile *vf = data;
737         vf_refresh_idle(vf);
738 }
739
740 typedef struct _MarksTextEntry MarksTextEntry;
741 struct _MarksTextEntry {
742         GenericDialog *gd;
743         gint mark_no;
744         GtkWidget *edit_widget;
745         gchar *text_entry;
746         GtkWidget *parent;
747 };
748
749 static void vf_marks_tooltip_cancel_cb(GenericDialog *gd, gpointer data)
750 {
751         MarksTextEntry *mte = data;
752
753         g_free(mte->text_entry);
754         generic_dialog_close(gd);
755 }
756
757 static void vf_marks_tooltip_ok_cb(GenericDialog *gd, gpointer data)
758 {
759         MarksTextEntry *mte = data;
760
761         g_free(options->marks_tooltips[mte->mark_no]);
762         options->marks_tooltips[mte->mark_no] = g_strdup(gtk_entry_get_text(GTK_ENTRY(mte->edit_widget)));
763
764         gtk_widget_set_tooltip_text(mte->parent, options->marks_tooltips[mte->mark_no]);
765
766         g_free(mte->text_entry);
767         generic_dialog_close(gd);
768 }
769
770 void vf_marks_filter_on_icon_press(GtkEntry *entry, GtkEntryIconPosition pos,
771                                                                         GdkEvent *event, gpointer userdata)
772 {
773         MarksTextEntry *mte = userdata;
774
775         g_free(mte->text_entry);
776         mte->text_entry = g_strdup("");
777         gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), "");
778 }
779
780 static void vf_marks_tooltip_help_cb(GenericDialog *gd, gpointer data)
781 {
782         help_window_show("GuideImageMarks.html");
783 }
784
785 static gboolean vf_marks_tooltip_cb(GtkWidget *widget,
786                                                                                 GdkEventButton *event,
787                                                                                 gpointer user_data)
788 {
789         GtkWidget *table;
790         gint i = GPOINTER_TO_INT(user_data);
791         MarksTextEntry *mte;
792
793         if (event->button == MOUSE_BUTTON_RIGHT)
794                 {
795                 mte = g_new0(MarksTextEntry, 1);
796                 mte->mark_no = i;
797                 mte->text_entry = g_strdup(options->marks_tooltips[i]);
798                 mte->parent = widget;
799
800                 mte->gd = generic_dialog_new(_("Mark text"), "mark_text",
801                                                 widget, FALSE,
802                                                 vf_marks_tooltip_cancel_cb, mte);
803                 generic_dialog_add_message(mte->gd, GTK_STOCK_DIALOG_QUESTION, _("Set mark text"),
804                                             _("This will set or clear the mark text."), FALSE);
805                 generic_dialog_add_button(mte->gd, GTK_STOCK_OK, NULL,
806                                                         vf_marks_tooltip_ok_cb, TRUE);
807                 generic_dialog_add_button(mte->gd, GTK_STOCK_HELP, NULL,
808                                                 vf_marks_tooltip_help_cb, FALSE);
809
810                 table = pref_table_new(mte->gd->vbox, 3, 1, FALSE, TRUE);
811                 pref_table_label(table, 0, 0, g_strdup_printf("%s%d", _("Mark "), mte->mark_no + 1), 1.0);
812                 mte->edit_widget = gtk_entry_new();
813                 gtk_widget_set_size_request(mte->edit_widget, 300, -1);
814                 if (mte->text_entry)
815                         {
816                         gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), mte->text_entry);
817                         }
818                 gtk_table_attach_defaults(GTK_TABLE(table), mte->edit_widget, 1, 2, 0, 1);
819                 generic_dialog_attach_default(mte->gd, mte->edit_widget);
820
821                 gtk_entry_set_icon_from_stock(GTK_ENTRY(mte->edit_widget),
822                                                         GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
823                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY(mte->edit_widget),
824                                                         GTK_ENTRY_ICON_SECONDARY, "Clear");
825                 g_signal_connect(GTK_ENTRY(mte->edit_widget), "icon-press",
826                                                         G_CALLBACK(vf_marks_filter_on_icon_press), mte);
827
828                 gtk_widget_show(mte->edit_widget);
829                 gtk_widget_grab_focus(mte->edit_widget);
830                 gtk_widget_show(GTK_WIDGET(mte->gd->dialog));
831
832                 return TRUE;
833                 }
834
835         return FALSE;
836 }
837
838 static void vf_file_filter_save_cb(GtkWidget *widget, gpointer data)
839 {
840         ViewFile *vf = data;
841         gchar *entry_text;
842         gchar *remove_text = NULL;
843         gchar *index_text = NULL;
844         gboolean text_found = FALSE;
845         gint i;
846
847         entry_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)))));
848
849         if (entry_text[0] == '\0' && vf->file_filter.last_selected >= 0)
850                 {
851                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), vf->file_filter.last_selected);
852                 remove_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
853                 history_list_item_remove("file_filter", remove_text);
854                 gtk_combo_box_text_remove(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), vf->file_filter.last_selected);
855                 g_free(remove_text);
856
857                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), -1);
858                 vf->file_filter.last_selected = - 1;
859                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo))), "");
860                 vf->file_filter.count--;
861                 }
862         else
863                 {
864                 if (entry_text[0] != '\0')
865                         {
866                         for (i = 0; i < vf->file_filter.count; i++)
867                                 {
868                                 if (index_text)
869                                         {
870                                         g_free(index_text);
871                                         }
872                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), i);
873                                 index_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
874
875                                 if (g_strcmp0(index_text, entry_text) == 0)
876                                         {
877                                         text_found = TRUE;
878                                         break;
879                                         }
880                                 }
881
882                         g_free(index_text);
883                         if (!text_found)
884                                 {
885                                 history_list_add_to_key("file_filter", entry_text, 10);
886                                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), entry_text);
887                                 vf->file_filter.count++;
888                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), vf->file_filter.count - 1);
889                                 }
890                         }
891                 }
892         vf_refresh(vf);
893
894         g_free(entry_text);
895 }
896
897 static void vf_file_filter_cb(GtkWidget *widget, gpointer data)
898 {
899         ViewFile *vf = data;
900
901         vf_refresh(vf);
902 }
903
904 static gboolean vf_file_filter_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
905 {
906         ViewFile *vf = data;
907         vf->file_filter.last_selected = gtk_combo_box_get_active(GTK_COMBO_BOX(vf->file_filter.combo));
908
909         gtk_widget_grab_focus(widget);
910
911         return TRUE;
912 }
913
914 static GtkWidget *vf_marks_filter_init(ViewFile *vf)
915 {
916         GtkWidget *frame = gtk_frame_new(NULL);
917         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
918
919         gint i;
920
921         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
922                 {
923                 GtkWidget *check = gtk_check_button_new();
924                 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, FALSE, 0);
925                 g_signal_connect(G_OBJECT(check), "toggled",
926                          G_CALLBACK(vf_marks_filter_toggle_cb), vf);
927                 g_signal_connect(G_OBJECT(check), "button_press_event",
928                          G_CALLBACK(vf_marks_tooltip_cb), GINT_TO_POINTER(i));
929                 gtk_widget_set_tooltip_text(check, options->marks_tooltips[i]);
930
931                 gtk_widget_show(check);
932                 vf->filter_check[i] = check;
933                 }
934         gtk_container_add(GTK_CONTAINER(frame), hbox);
935         gtk_widget_show(hbox);
936         return frame;
937 }
938
939 void vf_file_filter_set(ViewFile *vf, gboolean enable)
940 {
941         if (enable)
942                 {
943                 gtk_widget_show(vf->file_filter.combo);
944                 gtk_widget_show(vf->file_filter.frame);
945                 }
946         else
947                 {
948                 gtk_widget_hide(vf->file_filter.combo);
949                 gtk_widget_hide(vf->file_filter.frame);
950                 }
951
952         vf_refresh(vf);
953 }
954
955 static gboolean vf_file_filter_class_cb(GtkWidget *widget, gpointer data)
956 {
957         ViewFile *vf = data;
958         gint i;
959
960         gboolean state = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
961
962         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
963                 {
964                 if (g_strcmp0(format_class_list[i], gtk_menu_item_get_label(GTK_MENU_ITEM(widget))) == 0)
965                         {
966                         options->class_filter[i] = state;
967                         }
968                 }
969         vf_refresh(vf);
970
971         return TRUE;
972 }
973
974 static gboolean vf_file_filter_class_set_all_cb(GtkWidget *widget, gpointer data)
975 {
976         ViewFile *vf = data;
977         GtkWidget *parent;
978         GList *children;
979         GtkWidget *child;
980         gint i;
981         gboolean state;
982
983         if (g_strcmp0(_("Select all"), gtk_menu_item_get_label(GTK_MENU_ITEM(widget))) == 0)
984                 {
985                 state = TRUE;
986                 }
987         else
988                 {
989                 state = FALSE;
990                 }
991
992         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
993                 {
994                 options->class_filter[i] = state;
995                 }
996
997         i = 0;
998         parent = gtk_widget_get_parent(widget);
999         children = gtk_container_get_children(GTK_CONTAINER(parent));
1000         while (children)
1001                 {
1002                 child = children->data;
1003                 if (i < FILE_FORMAT_CLASSES)
1004                         {
1005                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(child), state);
1006                         }
1007                 i++;
1008                 children = children->next;
1009                 }
1010         g_list_free(children);
1011         vf_refresh(vf);
1012
1013         return TRUE;
1014 }
1015
1016 static GtkWidget *class_filter_menu (ViewFile *vf)
1017 {
1018         GtkWidget *menu;
1019         GtkWidget *menu_item;
1020         int i;
1021
1022         menu = gtk_menu_new();
1023
1024         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
1025             {
1026                 menu_item = gtk_check_menu_item_new_with_label(format_class_list[i]);
1027                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), options->class_filter[i]);
1028                 g_signal_connect(G_OBJECT(menu_item), "toggled", G_CALLBACK(vf_file_filter_class_cb), vf);
1029                 gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1030                 gtk_widget_show(menu_item);
1031                 }
1032
1033         menu_item = gtk_menu_item_new_with_label(_("Select all"));
1034         gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1035         gtk_widget_show(menu_item);
1036         g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(vf_file_filter_class_set_all_cb), vf);
1037
1038         menu_item = gtk_menu_item_new_with_label(_("Select none"));
1039         gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1040         gtk_widget_show(menu_item);
1041         g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(vf_file_filter_class_set_all_cb), vf);
1042
1043         return menu;
1044 }
1045
1046 static void case_sensitive_cb(GtkWidget *widget, gpointer data)
1047 {
1048         ViewFile *vf = data;
1049
1050         vf->file_filter.case_sensitive = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
1051         vf_refresh(vf);
1052 }
1053
1054 static GtkWidget *vf_file_filter_init(ViewFile *vf)
1055 {
1056         GtkWidget *frame = gtk_frame_new(NULL);
1057         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
1058         GList *work;
1059         gint n = 0;
1060         GtkWidget *combo_entry;
1061         GtkWidget *menubar;
1062         GtkWidget *menuitem;
1063         GtkWidget *case_sensitive;
1064
1065         vf->file_filter.combo = gtk_combo_box_text_new_with_entry();
1066         combo_entry = gtk_bin_get_child(GTK_BIN(vf->file_filter.combo));
1067         gtk_widget_show(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)));
1068         gtk_widget_show((GTK_WIDGET(vf->file_filter.combo)));
1069         gtk_widget_set_tooltip_text(GTK_WIDGET(vf->file_filter.combo), "Use regular expressions");
1070
1071         work = history_list_get_by_key("file_filter");
1072         while (work)
1073                 {
1074                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), (gchar *)work->data);
1075                 work = work->next;
1076                 n++;
1077                 vf->file_filter.count = n;
1078                 }
1079         gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), 0);
1080
1081         g_signal_connect(G_OBJECT(combo_entry), "activate",
1082                 G_CALLBACK(vf_file_filter_save_cb), vf);
1083                 
1084         g_signal_connect(G_OBJECT(vf->file_filter.combo), "changed",
1085                 G_CALLBACK(vf_file_filter_cb), vf);
1086
1087         g_signal_connect(G_OBJECT(combo_entry), "button_press_event",
1088                          G_CALLBACK(vf_file_filter_press_cb), vf);
1089
1090         gtk_box_pack_start(GTK_BOX(hbox), vf->file_filter.combo, FALSE, FALSE, 0);
1091         gtk_widget_show(vf->file_filter.combo);
1092         gtk_container_add(GTK_CONTAINER(frame), hbox);
1093         gtk_widget_show(hbox);
1094
1095         case_sensitive = gtk_check_button_new_with_label("Case");
1096         gtk_box_pack_start(GTK_BOX(hbox), case_sensitive, FALSE, FALSE, 0);
1097         gtk_widget_set_tooltip_text(GTK_WIDGET(case_sensitive), _("Case sensitive"));
1098         g_signal_connect(G_OBJECT(case_sensitive), "clicked", G_CALLBACK(case_sensitive_cb), vf);
1099         gtk_widget_show(case_sensitive);
1100
1101         menubar = gtk_menu_bar_new();
1102         gtk_box_pack_start(GTK_BOX(hbox), menubar, FALSE, TRUE, 0);
1103         gtk_widget_show(menubar);
1104
1105         menuitem = gtk_menu_item_new_with_label(_("Class"));
1106         gtk_widget_set_tooltip_text(GTK_WIDGET(menuitem), _("Select Class filter"));
1107         gtk_menu_item_set_submenu(GTK_MENU_ITEM (menuitem), class_filter_menu(vf));
1108         gtk_menu_shell_append(GTK_MENU_SHELL (menubar), menuitem);
1109         gtk_widget_show(menuitem);
1110
1111         gtk_widget_show(menuitem);
1112
1113         return frame;
1114 }
1115
1116 void vf_mark_filter_toggle(ViewFile *vf, gint mark)
1117 {
1118         gint n = mark - 1;
1119         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vf->filter_check[n]),
1120                                      !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[n])));
1121 }
1122
1123 ViewFile *vf_new(FileViewType type, FileData *dir_fd)
1124 {
1125         ViewFile *vf;
1126
1127         vf = g_new0(ViewFile, 1);
1128
1129         vf->type = type;
1130         vf->sort_method = SORT_NAME;
1131         vf->sort_ascend = TRUE;
1132         vf->read_metadata_in_idle_id = 0;
1133
1134         vf->scrolled = gtk_scrolled_window_new(NULL, NULL);
1135         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vf->scrolled), GTK_SHADOW_IN);
1136         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vf->scrolled),
1137                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1138
1139         vf->filter = vf_marks_filter_init(vf);
1140         vf->file_filter.frame = vf_file_filter_init(vf);
1141
1142         vf->widget = gtk_vbox_new(FALSE, 0);
1143         gtk_box_pack_start(GTK_BOX(vf->widget), vf->filter, FALSE, FALSE, 0);
1144         gtk_box_pack_start(GTK_BOX(vf->widget), vf->file_filter.frame, FALSE, FALSE, 0);
1145         gtk_box_pack_start(GTK_BOX(vf->widget), vf->scrolled, TRUE, TRUE, 0);
1146         gtk_widget_show(vf->scrolled);
1147
1148         g_signal_connect(G_OBJECT(vf->widget), "destroy",
1149                          G_CALLBACK(vf_destroy_cb), vf);
1150
1151         switch (type)
1152         {
1153         case FILEVIEW_LIST: vf = vflist_new(vf, dir_fd); break;
1154         case FILEVIEW_ICON: vf = vficon_new(vf, dir_fd); break;
1155         }
1156
1157         vf_dnd_init(vf);
1158
1159         g_signal_connect(G_OBJECT(vf->listview), "key_press_event",
1160                          G_CALLBACK(vf_press_key_cb), vf);
1161         g_signal_connect(G_OBJECT(vf->listview), "button_press_event",
1162                          G_CALLBACK(vf_press_cb), vf);
1163         g_signal_connect(G_OBJECT(vf->listview), "button_release_event",
1164                          G_CALLBACK(vf_release_cb), vf);
1165
1166         gtk_container_add(GTK_CONTAINER(vf->scrolled), vf->listview);
1167         gtk_widget_show(vf->listview);
1168
1169         if (dir_fd) vf_set_fd(vf, dir_fd);
1170
1171         return vf;
1172 }
1173
1174 void vf_set_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gpointer data), gpointer data)
1175 {
1176         vf->func_status = func;
1177         vf->data_status = data;
1178 }
1179
1180 void vf_set_thumb_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gdouble val, const gchar *text, gpointer data), gpointer data)
1181 {
1182         vf->func_thumb_status = func;
1183         vf->data_thumb_status = data;
1184 }
1185
1186 void vf_thumb_set(ViewFile *vf, gboolean enable)
1187 {
1188         switch (vf->type)
1189         {
1190         case FILEVIEW_LIST: vflist_thumb_set(vf, enable); break;
1191         case FILEVIEW_ICON: /*vficon_thumb_set(vf, enable);*/ break;
1192         }
1193 }
1194
1195
1196 static gboolean vf_thumb_next(ViewFile *vf);
1197
1198 static gdouble vf_thumb_progress(ViewFile *vf)
1199 {
1200         gint count = 0;
1201         gint done = 0;
1202
1203         switch (vf->type)
1204         {
1205         case FILEVIEW_LIST: vflist_thumb_progress_count(vf->list, &count, &done); break;
1206         case FILEVIEW_ICON: vficon_thumb_progress_count(vf->list, &count, &done); break;
1207         }
1208
1209         DEBUG_1("thumb progress: %d of %d", done, count);
1210         return (gdouble)done / count;
1211 }
1212
1213 static gdouble vf_read_metadata_in_idle_progress(ViewFile *vf)
1214 {
1215         gint count = 0;
1216         gint done = 0;
1217
1218         switch (vf->type)
1219                 {
1220                 case FILEVIEW_LIST: vflist_read_metadata_progress_count(vf->list, &count, &done); break;
1221                 case FILEVIEW_ICON: vficon_read_metadata_progress_count(vf->list, &count, &done); break;
1222                 }
1223
1224         return (gdouble)done / count;
1225 }
1226
1227 static void vf_set_thumb_fd(ViewFile *vf, FileData *fd)
1228 {
1229         switch (vf->type)
1230         {
1231         case FILEVIEW_LIST: vflist_set_thumb_fd(vf, fd); break;
1232         case FILEVIEW_ICON: vficon_set_thumb_fd(vf, fd); break;
1233         }
1234 }
1235
1236 static void vf_thumb_status(ViewFile *vf, gdouble val, const gchar *text)
1237 {
1238         if (vf->func_thumb_status)
1239                 {
1240                 vf->func_thumb_status(vf, val, text, vf->data_thumb_status);
1241                 }
1242 }
1243
1244 static void vf_thumb_do(ViewFile *vf, FileData *fd)
1245 {
1246         if (!fd) return;
1247
1248         vf_set_thumb_fd(vf, fd);
1249         vf_thumb_status(vf, vf_thumb_progress(vf), _("Loading thumbs..."));
1250 }
1251
1252 void vf_thumb_cleanup(ViewFile *vf)
1253 {
1254         vf_thumb_status(vf, 0.0, NULL);
1255
1256         vf->thumbs_running = FALSE;
1257
1258         thumb_loader_free(vf->thumbs_loader);
1259         vf->thumbs_loader = NULL;
1260
1261         vf->thumbs_filedata = NULL;
1262 }
1263
1264 void vf_thumb_stop(ViewFile *vf)
1265 {
1266         if (vf->thumbs_running) vf_thumb_cleanup(vf);
1267 }
1268
1269 static void vf_thumb_common_cb(ThumbLoader *tl, gpointer data)
1270 {
1271         ViewFile *vf = data;
1272
1273         if (vf->thumbs_filedata && vf->thumbs_loader == tl)
1274                 {
1275                 vf_thumb_do(vf, vf->thumbs_filedata);
1276                 }
1277
1278         while (vf_thumb_next(vf));
1279 }
1280
1281 static void vf_thumb_error_cb(ThumbLoader *tl, gpointer data)
1282 {
1283         vf_thumb_common_cb(tl, data);
1284 }
1285
1286 static void vf_thumb_done_cb(ThumbLoader *tl, gpointer data)
1287 {
1288         vf_thumb_common_cb(tl, data);
1289 }
1290
1291 static gboolean vf_thumb_next(ViewFile *vf)
1292 {
1293         FileData *fd = NULL;
1294
1295         if (!gtk_widget_get_realized(vf->listview))
1296                 {
1297                 vf_thumb_status(vf, 0.0, NULL);
1298                 return FALSE;
1299                 }
1300
1301         switch (vf->type)
1302         {
1303         case FILEVIEW_LIST: fd = vflist_thumb_next_fd(vf); break;
1304         case FILEVIEW_ICON: fd = vficon_thumb_next_fd(vf); break;
1305         }
1306
1307         if (!fd)
1308                 {
1309                 /* done */
1310                 vf_thumb_cleanup(vf);
1311                 return FALSE;
1312                 }
1313
1314         vf->thumbs_filedata = fd;
1315
1316         thumb_loader_free(vf->thumbs_loader);
1317
1318         vf->thumbs_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
1319         thumb_loader_set_callbacks(vf->thumbs_loader,
1320                                    vf_thumb_done_cb,
1321                                    vf_thumb_error_cb,
1322                                    NULL,
1323                                    vf);
1324
1325         if (!thumb_loader_start(vf->thumbs_loader, fd))
1326                 {
1327                 /* set icon to unknown, continue */
1328                 DEBUG_1("thumb loader start failed %s", fd->path);
1329                 vf_thumb_do(vf, fd);
1330
1331                 return TRUE;
1332                 }
1333
1334         return FALSE;
1335 }
1336
1337 static void vf_thumb_reset_all(ViewFile *vf)
1338 {
1339         GList *work;
1340
1341         for (work = vf->list; work; work = work->next)
1342                 {
1343                 FileData *fd = work->data;
1344                 if (fd->thumb_pixbuf)
1345                         {
1346                         g_object_unref(fd->thumb_pixbuf);
1347                         fd->thumb_pixbuf = NULL;
1348                         }
1349                 }
1350 }
1351
1352 void vf_thumb_update(ViewFile *vf)
1353 {
1354         vf_thumb_stop(vf);
1355
1356         if (vf->type == FILEVIEW_LIST && !VFLIST(vf)->thumbs_enabled) return;
1357
1358         vf_thumb_status(vf, 0.0, _("Loading thumbs..."));
1359         vf->thumbs_running = TRUE;
1360
1361         if (thumb_format_changed)
1362                 {
1363                 vf_thumb_reset_all(vf);
1364                 thumb_format_changed = FALSE;
1365                 }
1366
1367         while (vf_thumb_next(vf));
1368 }
1369
1370 void vf_star_cleanup(ViewFile *vf)
1371 {
1372         if (vf->stars_id != 0)
1373                 {
1374                 g_source_remove(vf->stars_id);
1375                 }
1376
1377         vf->stars_id = 0;
1378         vf->stars_filedata = NULL;
1379 }
1380
1381 void vf_star_stop(ViewFile *vf)
1382 {
1383          vf_star_cleanup(vf);
1384 }
1385
1386 static void vf_set_star_fd(ViewFile *vf, FileData *fd)
1387 {
1388         switch (vf->type)
1389                 {
1390                 case FILEVIEW_LIST: vflist_set_star_fd(vf, fd); break;
1391                 case FILEVIEW_ICON: vficon_set_star_fd(vf, fd); break;
1392                 default: break;
1393                 }
1394 }
1395
1396 static void vf_star_do(ViewFile *vf, FileData *fd)
1397 {
1398         if (!fd) return;
1399
1400         vf_set_star_fd(vf, fd);
1401 }
1402
1403 static gboolean vf_star_next(ViewFile *vf)
1404 {
1405         FileData *fd = NULL;
1406
1407         switch (vf->type)
1408                 {
1409                 case FILEVIEW_LIST: fd = vflist_star_next_fd(vf); break;
1410                 case FILEVIEW_ICON: fd = vficon_star_next_fd(vf); break;
1411                 default: break;
1412                 }
1413
1414         if (!fd)
1415                 {
1416                 /* done */
1417                 vf_star_cleanup(vf);
1418                 return FALSE;
1419                 }
1420
1421         return TRUE;
1422 }
1423
1424 gboolean vf_stars_cb(gpointer data)
1425 {
1426         ViewFile *vf = data;
1427         FileData *fd = vf->stars_filedata;
1428
1429         if (fd)
1430                 {
1431                 read_rating_data(fd);
1432
1433                 vf_star_do(vf, fd);
1434
1435                 if (vf_star_next(vf))
1436                         {
1437                         return TRUE;
1438                         }
1439                 else
1440                         {
1441                         vf->stars_filedata = NULL;
1442                         vf->stars_id = 0;
1443                         return FALSE;
1444                         }
1445                 }
1446
1447         return FALSE;
1448 }
1449
1450 void vf_star_update(ViewFile *vf)
1451 {
1452         vf_star_stop(vf);
1453
1454         if (!options->show_star_rating)
1455                 {
1456                 return;
1457                 }
1458
1459         vf_star_next(vf);
1460 }
1461
1462 void vf_marks_set(ViewFile *vf, gboolean enable)
1463 {
1464         if (vf->marks_enabled == enable) return;
1465
1466         vf->marks_enabled = enable;
1467
1468         switch (vf->type)
1469         {
1470         case FILEVIEW_LIST: vflist_marks_set(vf, enable); break;
1471         case FILEVIEW_ICON: vficon_marks_set(vf, enable); break;
1472         }
1473         if (enable)
1474                 gtk_widget_show(vf->filter);
1475         else
1476                 gtk_widget_hide(vf->filter);
1477
1478         vf_refresh_idle(vf);
1479 }
1480
1481 void vf_star_rating_set(ViewFile *vf, gboolean enable)
1482 {
1483         if (options->show_star_rating == enable) return;
1484         options->show_star_rating = enable;
1485
1486         switch (vf->type)
1487                 {
1488                 case FILEVIEW_LIST: vflist_star_rating_set(vf, enable); break;
1489                 case FILEVIEW_ICON: vficon_star_rating_set(vf, enable); break;
1490                 }
1491         vf_refresh_idle(vf);
1492 }
1493
1494 guint vf_marks_get_filter(ViewFile *vf)
1495 {
1496         guint ret = 0;
1497         gint i;
1498         if (!vf->marks_enabled) return 0;
1499
1500         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
1501                 {
1502                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[i])))
1503                         {
1504                         ret |= 1 << i;
1505                         }
1506                 }
1507         return ret;
1508 }
1509
1510 GRegex *vf_file_filter_get_filter(ViewFile *vf)
1511 {
1512         GRegex *ret = NULL;
1513         GError *error = NULL;
1514         gchar *file_filter_text = NULL;
1515
1516         if (!gtk_widget_get_visible(vf->file_filter.combo))
1517                 {
1518                 return g_regex_new("", 0, 0, NULL);
1519                 }
1520
1521         file_filter_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
1522
1523         if (file_filter_text[0] != '\0')
1524                 {
1525                 ret = g_regex_new(file_filter_text, vf->file_filter.case_sensitive ? 0 : G_REGEX_CASELESS, 0, &error);
1526                 if (error)
1527                         {
1528                         log_printf("Error: could not compile regular expression %s\n%s\n", file_filter_text, error->message);
1529                         g_error_free(error);
1530                         error = NULL;
1531                         ret = g_regex_new("", 0, 0, NULL);
1532                         }
1533                 g_free(file_filter_text);
1534                 }
1535         else
1536                 {
1537                 ret = g_regex_new("", 0, 0, NULL);
1538                 }
1539
1540         return ret;
1541 }
1542
1543 guint vf_class_get_filter(ViewFile *vf)
1544 {
1545         guint ret = 0;
1546         gint i;
1547
1548         if (!gtk_widget_get_visible(vf->file_filter.combo))
1549                 {
1550                 return G_MAXUINT;
1551                 }
1552
1553         for ( i = 0; i < FILE_FORMAT_CLASSES; i++)
1554                 {
1555                 if (options->class_filter[i])
1556                         {
1557                         ret |= 1 << i;
1558                         }
1559                 }
1560
1561         return ret;
1562 }
1563
1564 void vf_set_layout(ViewFile *vf, LayoutWindow *layout)
1565 {
1566         vf->layout = layout;
1567 }
1568
1569
1570 /*
1571  *-----------------------------------------------------------------------------
1572  * maintenance (for rename, move, remove)
1573  *-----------------------------------------------------------------------------
1574  */
1575
1576 static gboolean vf_refresh_idle_cb(gpointer data)
1577 {
1578         ViewFile *vf = data;
1579
1580         vf_refresh(vf);
1581         vf->refresh_idle_id = 0;
1582         return FALSE;
1583 }
1584
1585 void vf_refresh_idle_cancel(ViewFile *vf)
1586 {
1587         if (vf->refresh_idle_id)
1588                 {
1589                 g_source_remove(vf->refresh_idle_id);
1590                 vf->refresh_idle_id = 0;
1591                 }
1592 }
1593
1594
1595 void vf_refresh_idle(ViewFile *vf)
1596 {
1597         if (!vf->refresh_idle_id)
1598                 {
1599                 vf->time_refresh_set = time(NULL);
1600                 /* file operations run with G_PRIORITY_DEFAULT_IDLE */
1601                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE + 50, vf_refresh_idle_cb, vf, NULL);
1602                 }
1603         else if (time(NULL) - vf->time_refresh_set > 1)
1604                 {
1605                 /* more than 1 sec since last update - increase priority */
1606                 vf_refresh_idle_cancel(vf);
1607                 vf->time_refresh_set = time(NULL);
1608                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 50, vf_refresh_idle_cb, vf, NULL);
1609                 }
1610 }
1611
1612 void vf_notify_cb(FileData *fd, NotifyType type, gpointer data)
1613 {
1614         ViewFile *vf = data;
1615         gboolean refresh;
1616
1617         NotifyType interested = NOTIFY_CHANGE | NOTIFY_REREAD | NOTIFY_GROUPING;
1618         if (vf->marks_enabled) interested |= NOTIFY_MARKS | NOTIFY_METADATA;
1619         /* FIXME: NOTIFY_METADATA should be checked by the keyword-to-mark functions and converted to NOTIFY_MARKS only if there was a change */
1620
1621         if (!(type & interested) || vf->refresh_idle_id || !vf->dir_fd) return;
1622
1623         refresh = (fd == vf->dir_fd);
1624
1625         if (!refresh)
1626                 {
1627                 gchar *base = remove_level_from_path(fd->path);
1628                 refresh = (g_strcmp0(base, vf->dir_fd->path) == 0);
1629                 g_free(base);
1630                 }
1631
1632         if ((type & NOTIFY_CHANGE) && fd->change)
1633                 {
1634                 if (!refresh && fd->change->dest)
1635                         {
1636                         gchar *dest_base = remove_level_from_path(fd->change->dest);
1637                         refresh = (g_strcmp0(dest_base, vf->dir_fd->path) == 0);
1638                         g_free(dest_base);
1639                         }
1640
1641                 if (!refresh && fd->change->source)
1642                         {
1643                         gchar *source_base = remove_level_from_path(fd->change->source);
1644                         refresh = (g_strcmp0(source_base, vf->dir_fd->path) == 0);
1645                         g_free(source_base);
1646                         }
1647                 }
1648
1649         if (refresh)
1650                 {
1651                 DEBUG_1("Notify vf: %s %04x", fd->path, type);
1652                 vf_refresh_idle(vf);
1653                 }
1654 }
1655
1656 static gboolean vf_read_metadata_in_idle_cb(gpointer data)
1657 {
1658         FileData *fd;
1659         ViewFile *vf = data;
1660         GList *work;
1661
1662         vf_thumb_status(vf, vf_read_metadata_in_idle_progress(vf), _("Loading meta..."));
1663
1664         work = vf->list;
1665
1666         while (work)
1667                 {
1668                 fd = work->data;
1669
1670                 if (fd && !fd->metadata_in_idle_loaded)
1671                         {
1672                         if (!fd->exifdate)
1673                                 {
1674                                 read_exif_time_data(fd);
1675                                 }
1676                         if (!fd->exifdate_digitized)
1677                                 {
1678                                 read_exif_time_digitized_data(fd);
1679                                 }
1680                         if (fd->rating == STAR_RATING_NOT_READ)
1681                                 {
1682                                 read_rating_data(fd);
1683                                 }
1684                         fd->metadata_in_idle_loaded = TRUE;
1685                         return TRUE;
1686                         }
1687                 work = work->next;
1688                 }
1689
1690         vf_thumb_status(vf, 0.0, NULL);
1691         vf->read_metadata_in_idle_id = 0;
1692         vf_refresh(vf);
1693         return FALSE;
1694 }
1695
1696 static void vf_read_metadata_in_idle_finished_cb(gpointer data)
1697 {
1698         ViewFile *vf = data;
1699
1700         vf_thumb_status(vf, 0.0, "Loading meta...");
1701         vf->read_metadata_in_idle_id = 0;
1702 }
1703
1704 void vf_read_metadata_in_idle(ViewFile *vf)
1705 {
1706         if (!vf) return;
1707
1708         if (vf->read_metadata_in_idle_id)
1709                 {
1710                 g_idle_remove_by_data(vf);
1711                 }
1712         vf->read_metadata_in_idle_id = 0;
1713
1714         if (vf->list)
1715                 {
1716                 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);
1717                 }
1718
1719         return;
1720 }
1721
1722 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */