ed5c5f0ec64cff252cca94292d68fe9795b77a95
[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 gboolean vf_file_filter_class_cb(GtkWidget *widget, gpointer data)
973 {
974         ViewFile *vf = data;
975         gint i;
976
977         gboolean state = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
978
979         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
980                 {
981                 if (g_strcmp0(format_class_list[i], gtk_menu_item_get_label(GTK_MENU_ITEM(widget))) == 0)
982                         {
983                         options->class_filter[i] = state;
984                         }
985                 }
986         vf_refresh(vf);
987
988         return TRUE;
989 }
990
991 static gboolean vf_file_filter_class_set_all_cb(GtkWidget *widget, gpointer data)
992 {
993         ViewFile *vf = data;
994         GtkWidget *parent;
995         GList *children;
996         GtkWidget *child;
997         gint i;
998         gboolean state;
999
1000         if (g_strcmp0(_("Select all"), gtk_menu_item_get_label(GTK_MENU_ITEM(widget))) == 0)
1001                 {
1002                 state = TRUE;
1003                 }
1004         else
1005                 {
1006                 state = FALSE;
1007                 }
1008
1009         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
1010                 {
1011                 options->class_filter[i] = state;
1012                 }
1013
1014         i = 0;
1015         parent = gtk_widget_get_parent(widget);
1016         children = gtk_container_get_children(GTK_CONTAINER(parent));
1017         while (children)
1018                 {
1019                 child = children->data;
1020                 if (i < FILE_FORMAT_CLASSES)
1021                         {
1022                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(child), state);
1023                         }
1024                 i++;
1025                 children = children->next;
1026                 }
1027         g_list_free(children);
1028         vf_refresh(vf);
1029
1030         return TRUE;
1031 }
1032
1033 static GtkWidget *class_filter_menu (ViewFile *vf)
1034 {
1035         GtkWidget *menu;
1036         GtkWidget *menu_item;
1037         int i;
1038
1039         menu = gtk_menu_new();
1040
1041         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
1042             {
1043                 menu_item = gtk_check_menu_item_new_with_label(format_class_list[i]);
1044                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), options->class_filter[i]);
1045                 g_signal_connect(G_OBJECT(menu_item), "toggled", G_CALLBACK(vf_file_filter_class_cb), vf);
1046                 gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1047                 gtk_widget_show(menu_item);
1048                 }
1049
1050         menu_item = gtk_menu_item_new_with_label(_("Select all"));
1051         gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1052         gtk_widget_show(menu_item);
1053         g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(vf_file_filter_class_set_all_cb), vf);
1054
1055         menu_item = gtk_menu_item_new_with_label(_("Select none"));
1056         gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1057         gtk_widget_show(menu_item);
1058         g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(vf_file_filter_class_set_all_cb), vf);
1059
1060         return menu;
1061 }
1062
1063 static void case_sensitive_cb(GtkWidget *widget, gpointer data)
1064 {
1065         ViewFile *vf = data;
1066
1067         vf->file_filter.case_sensitive = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
1068         vf_refresh(vf);
1069 }
1070
1071 static GtkWidget *vf_file_filter_init(ViewFile *vf)
1072 {
1073         GtkWidget *frame = gtk_frame_new(NULL);
1074         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
1075         GList *work;
1076         gint n = 0;
1077         GtkWidget *combo_entry;
1078         GtkWidget *menubar;
1079         GtkWidget *menuitem;
1080         GtkWidget *case_sensitive;
1081
1082         vf->file_filter.combo = gtk_combo_box_text_new_with_entry();
1083         combo_entry = gtk_bin_get_child(GTK_BIN(vf->file_filter.combo));
1084         gtk_widget_show(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)));
1085         gtk_widget_show((GTK_WIDGET(vf->file_filter.combo)));
1086         gtk_widget_set_tooltip_text(GTK_WIDGET(vf->file_filter.combo), "Use regular expressions");
1087
1088         work = history_list_get_by_key("file_filter");
1089         while (work)
1090                 {
1091                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), (gchar *)work->data);
1092                 work = work->next;
1093                 n++;
1094                 vf->file_filter.count = n;
1095                 }
1096         gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), 0);
1097
1098         g_signal_connect(G_OBJECT(combo_entry), "activate",
1099                 G_CALLBACK(vf_file_filter_save_cb), vf);
1100                 
1101         g_signal_connect(G_OBJECT(vf->file_filter.combo), "changed",
1102                 G_CALLBACK(vf_file_filter_cb), vf);
1103
1104         g_signal_connect(G_OBJECT(combo_entry), "button_press_event",
1105                          G_CALLBACK(vf_file_filter_press_cb), vf);
1106
1107         gtk_box_pack_start(GTK_BOX(hbox), vf->file_filter.combo, FALSE, FALSE, 0);
1108         gtk_widget_show(vf->file_filter.combo);
1109         gtk_container_add(GTK_CONTAINER(frame), hbox);
1110         gtk_widget_show(hbox);
1111
1112         case_sensitive = gtk_check_button_new_with_label("Case");
1113         gtk_box_pack_start(GTK_BOX(hbox), case_sensitive, FALSE, FALSE, 0);
1114         gtk_widget_set_tooltip_text(GTK_WIDGET(case_sensitive), _("Case sensitive"));
1115         g_signal_connect(G_OBJECT(case_sensitive), "clicked", G_CALLBACK(case_sensitive_cb), vf);
1116         gtk_widget_show(case_sensitive);
1117
1118         menubar = gtk_menu_bar_new();
1119         gtk_box_pack_start(GTK_BOX(hbox), menubar, FALSE, TRUE, 0);
1120         gtk_widget_show(menubar);
1121
1122         menuitem = gtk_menu_item_new_with_label(_("Class"));
1123         gtk_widget_set_tooltip_text(GTK_WIDGET(menuitem), _("Select Class filter"));
1124         gtk_menu_item_set_submenu(GTK_MENU_ITEM (menuitem), class_filter_menu(vf));
1125         gtk_menu_shell_append(GTK_MENU_SHELL (menubar), menuitem);
1126         gtk_widget_show(menuitem);
1127
1128         gtk_widget_show(menuitem);
1129
1130         return frame;
1131 }
1132
1133 void vf_mark_filter_toggle(ViewFile *vf, gint mark)
1134 {
1135         gint n = mark - 1;
1136         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vf->filter_check[n]),
1137                                      !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[n])));
1138 }
1139
1140 ViewFile *vf_new(FileViewType type, FileData *dir_fd)
1141 {
1142         ViewFile *vf;
1143
1144         vf = g_new0(ViewFile, 1);
1145
1146         vf->type = type;
1147         vf->sort_method = SORT_NAME;
1148         vf->sort_ascend = TRUE;
1149         vf->read_metadata_in_idle_id = 0;
1150
1151         vf->scrolled = gtk_scrolled_window_new(NULL, NULL);
1152         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vf->scrolled), GTK_SHADOW_IN);
1153         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vf->scrolled),
1154                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1155
1156         vf->filter = vf_marks_filter_init(vf);
1157         vf->file_filter.frame = vf_file_filter_init(vf);
1158
1159         vf->widget = gtk_vbox_new(FALSE, 0);
1160         gtk_box_pack_start(GTK_BOX(vf->widget), vf->filter, FALSE, FALSE, 0);
1161         gtk_box_pack_start(GTK_BOX(vf->widget), vf->file_filter.frame, FALSE, FALSE, 0);
1162         gtk_box_pack_start(GTK_BOX(vf->widget), vf->scrolled, TRUE, TRUE, 0);
1163         gtk_widget_show(vf->scrolled);
1164
1165         g_signal_connect(G_OBJECT(vf->widget), "destroy",
1166                          G_CALLBACK(vf_destroy_cb), vf);
1167
1168         switch (type)
1169         {
1170         case FILEVIEW_LIST: vf = vflist_new(vf, dir_fd); break;
1171         case FILEVIEW_ICON: vf = vficon_new(vf, dir_fd); break;
1172         }
1173
1174         vf_dnd_init(vf);
1175
1176         g_signal_connect(G_OBJECT(vf->listview), "key_press_event",
1177                          G_CALLBACK(vf_press_key_cb), vf);
1178         g_signal_connect(G_OBJECT(vf->listview), "button_press_event",
1179                          G_CALLBACK(vf_press_cb), vf);
1180         g_signal_connect(G_OBJECT(vf->listview), "button_release_event",
1181                          G_CALLBACK(vf_release_cb), vf);
1182
1183         gtk_container_add(GTK_CONTAINER(vf->scrolled), vf->listview);
1184         gtk_widget_show(vf->listview);
1185
1186         if (dir_fd) vf_set_fd(vf, dir_fd);
1187
1188         return vf;
1189 }
1190
1191 void vf_set_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gpointer data), gpointer data)
1192 {
1193         vf->func_status = func;
1194         vf->data_status = data;
1195 }
1196
1197 void vf_set_thumb_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gdouble val, const gchar *text, gpointer data), gpointer data)
1198 {
1199         vf->func_thumb_status = func;
1200         vf->data_thumb_status = data;
1201 }
1202
1203 void vf_thumb_set(ViewFile *vf, gboolean enable)
1204 {
1205         switch (vf->type)
1206         {
1207         case FILEVIEW_LIST: vflist_thumb_set(vf, enable); break;
1208         case FILEVIEW_ICON: /*vficon_thumb_set(vf, enable);*/ break;
1209         }
1210 }
1211
1212
1213 static gboolean vf_thumb_next(ViewFile *vf);
1214
1215 static gdouble vf_thumb_progress(ViewFile *vf)
1216 {
1217         gint count = 0;
1218         gint done = 0;
1219
1220         switch (vf->type)
1221         {
1222         case FILEVIEW_LIST: vflist_thumb_progress_count(vf->list, &count, &done); break;
1223         case FILEVIEW_ICON: vficon_thumb_progress_count(vf->list, &count, &done); break;
1224         }
1225
1226         DEBUG_1("thumb progress: %d of %d", done, count);
1227         return (gdouble)done / count;
1228 }
1229
1230 static gdouble vf_read_metadata_in_idle_progress(ViewFile *vf)
1231 {
1232         gint count = 0;
1233         gint done = 0;
1234
1235         switch (vf->type)
1236                 {
1237                 case FILEVIEW_LIST: vflist_read_metadata_progress_count(vf->list, &count, &done); break;
1238                 case FILEVIEW_ICON: vficon_read_metadata_progress_count(vf->list, &count, &done); break;
1239                 }
1240
1241         return (gdouble)done / count;
1242 }
1243
1244 static void vf_set_thumb_fd(ViewFile *vf, FileData *fd)
1245 {
1246         switch (vf->type)
1247         {
1248         case FILEVIEW_LIST: vflist_set_thumb_fd(vf, fd); break;
1249         case FILEVIEW_ICON: vficon_set_thumb_fd(vf, fd); break;
1250         }
1251 }
1252
1253 static void vf_thumb_status(ViewFile *vf, gdouble val, const gchar *text)
1254 {
1255         if (vf->func_thumb_status)
1256                 {
1257                 vf->func_thumb_status(vf, val, text, vf->data_thumb_status);
1258                 }
1259 }
1260
1261 static void vf_thumb_do(ViewFile *vf, FileData *fd)
1262 {
1263         if (!fd) return;
1264
1265         vf_set_thumb_fd(vf, fd);
1266         vf_thumb_status(vf, vf_thumb_progress(vf), _("Loading thumbs..."));
1267 }
1268
1269 void vf_thumb_cleanup(ViewFile *vf)
1270 {
1271         vf_thumb_status(vf, 0.0, NULL);
1272
1273         vf->thumbs_running = FALSE;
1274
1275         thumb_loader_free(vf->thumbs_loader);
1276         vf->thumbs_loader = NULL;
1277
1278         vf->thumbs_filedata = NULL;
1279 }
1280
1281 void vf_thumb_stop(ViewFile *vf)
1282 {
1283         if (vf->thumbs_running) vf_thumb_cleanup(vf);
1284 }
1285
1286 static void vf_thumb_common_cb(ThumbLoader *tl, gpointer data)
1287 {
1288         ViewFile *vf = data;
1289
1290         if (vf->thumbs_filedata && vf->thumbs_loader == tl)
1291                 {
1292                 vf_thumb_do(vf, vf->thumbs_filedata);
1293                 }
1294
1295         while (vf_thumb_next(vf));
1296 }
1297
1298 static void vf_thumb_error_cb(ThumbLoader *tl, gpointer data)
1299 {
1300         vf_thumb_common_cb(tl, data);
1301 }
1302
1303 static void vf_thumb_done_cb(ThumbLoader *tl, gpointer data)
1304 {
1305         vf_thumb_common_cb(tl, data);
1306 }
1307
1308 static gboolean vf_thumb_next(ViewFile *vf)
1309 {
1310         FileData *fd = NULL;
1311
1312         if (!gtk_widget_get_realized(vf->listview))
1313                 {
1314                 vf_thumb_status(vf, 0.0, NULL);
1315                 return FALSE;
1316                 }
1317
1318         switch (vf->type)
1319         {
1320         case FILEVIEW_LIST: fd = vflist_thumb_next_fd(vf); break;
1321         case FILEVIEW_ICON: fd = vficon_thumb_next_fd(vf); break;
1322         }
1323
1324         if (!fd)
1325                 {
1326                 /* done */
1327                 vf_thumb_cleanup(vf);
1328                 return FALSE;
1329                 }
1330
1331         vf->thumbs_filedata = fd;
1332
1333         thumb_loader_free(vf->thumbs_loader);
1334
1335         vf->thumbs_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
1336         thumb_loader_set_callbacks(vf->thumbs_loader,
1337                                    vf_thumb_done_cb,
1338                                    vf_thumb_error_cb,
1339                                    NULL,
1340                                    vf);
1341
1342         if (!thumb_loader_start(vf->thumbs_loader, fd))
1343                 {
1344                 /* set icon to unknown, continue */
1345                 DEBUG_1("thumb loader start failed %s", fd->path);
1346                 vf_thumb_do(vf, fd);
1347
1348                 return TRUE;
1349                 }
1350
1351         return FALSE;
1352 }
1353
1354 static void vf_thumb_reset_all(ViewFile *vf)
1355 {
1356         GList *work;
1357
1358         for (work = vf->list; work; work = work->next)
1359                 {
1360                 FileData *fd = work->data;
1361                 if (fd->thumb_pixbuf)
1362                         {
1363                         g_object_unref(fd->thumb_pixbuf);
1364                         fd->thumb_pixbuf = NULL;
1365                         }
1366                 }
1367 }
1368
1369 void vf_thumb_update(ViewFile *vf)
1370 {
1371         vf_thumb_stop(vf);
1372
1373         if (vf->type == FILEVIEW_LIST && !VFLIST(vf)->thumbs_enabled) return;
1374
1375         vf_thumb_status(vf, 0.0, _("Loading thumbs..."));
1376         vf->thumbs_running = TRUE;
1377
1378         if (thumb_format_changed)
1379                 {
1380                 vf_thumb_reset_all(vf);
1381                 thumb_format_changed = FALSE;
1382                 }
1383
1384         while (vf_thumb_next(vf));
1385 }
1386
1387
1388 void vf_marks_set(ViewFile *vf, gboolean enable)
1389 {
1390         if (vf->marks_enabled == enable) return;
1391
1392         vf->marks_enabled = enable;
1393
1394         switch (vf->type)
1395         {
1396         case FILEVIEW_LIST: vflist_marks_set(vf, enable); break;
1397         case FILEVIEW_ICON: vficon_marks_set(vf, enable); break;
1398         }
1399         if (enable)
1400                 gtk_widget_show(vf->filter);
1401         else
1402                 gtk_widget_hide(vf->filter);
1403
1404         vf_refresh_idle(vf);
1405 }
1406
1407 void vf_star_rating_set(ViewFile *vf, gboolean enable)
1408 {
1409         if (options->show_star_rating == enable) return;
1410         options->show_star_rating = enable;
1411
1412         switch (vf->type)
1413                 {
1414                 case FILEVIEW_LIST: vflist_star_rating_set(vf, enable); break;
1415                 case FILEVIEW_ICON: vficon_star_rating_set(vf, enable); break;
1416                 }
1417         vf_refresh_idle(vf);
1418 }
1419
1420 guint vf_marks_get_filter(ViewFile *vf)
1421 {
1422         guint ret = 0;
1423         gint i;
1424         if (!vf->marks_enabled) return 0;
1425
1426         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
1427                 {
1428                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[i])))
1429                         {
1430                         ret |= 1 << i;
1431                         }
1432                 }
1433         return ret;
1434 }
1435
1436 GRegex *vf_file_filter_get_filter(ViewFile *vf)
1437 {
1438         GRegex *ret = NULL;
1439         GError *error = NULL;
1440         gchar *file_filter_text = NULL;
1441
1442         if (!gtk_widget_get_visible(vf->file_filter.combo))
1443                 {
1444                 return g_regex_new("", 0, 0, NULL);
1445                 }
1446
1447         file_filter_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
1448
1449         if (file_filter_text[0] != '\0')
1450                 {
1451                 ret = g_regex_new(file_filter_text, vf->file_filter.case_sensitive ? 0 : G_REGEX_CASELESS, 0, &error);
1452                 if (error)
1453                         {
1454                         log_printf("Error: could not compile regular expression %s\n%s\n", file_filter_text, error->message);
1455                         g_error_free(error);
1456                         error = NULL;
1457                         ret = g_regex_new("", 0, 0, NULL);
1458                         }
1459                 g_free(file_filter_text);
1460                 }
1461         else
1462                 {
1463                 ret = g_regex_new("", 0, 0, NULL);
1464                 }
1465
1466         return ret;
1467 }
1468
1469 guint vf_class_get_filter(ViewFile *vf)
1470 {
1471         guint ret = 0;
1472         gint i;
1473
1474         if (!gtk_widget_get_visible(vf->file_filter.combo))
1475                 {
1476                 return G_MAXUINT;
1477                 }
1478
1479         for ( i = 0; i < FILE_FORMAT_CLASSES; i++)
1480                 {
1481                 if (options->class_filter[i])
1482                         {
1483                         ret |= 1 << i;
1484                         }
1485                 }
1486
1487         return ret;
1488 }
1489
1490 void vf_set_layout(ViewFile *vf, LayoutWindow *layout)
1491 {
1492         vf->layout = layout;
1493 }
1494
1495
1496 /*
1497  *-----------------------------------------------------------------------------
1498  * maintenance (for rename, move, remove)
1499  *-----------------------------------------------------------------------------
1500  */
1501
1502 static gboolean vf_refresh_idle_cb(gpointer data)
1503 {
1504         ViewFile *vf = data;
1505
1506         vf_refresh(vf);
1507         vf->refresh_idle_id = 0;
1508         return FALSE;
1509 }
1510
1511 void vf_refresh_idle_cancel(ViewFile *vf)
1512 {
1513         if (vf->refresh_idle_id)
1514                 {
1515                 g_source_remove(vf->refresh_idle_id);
1516                 vf->refresh_idle_id = 0;
1517                 }
1518 }
1519
1520
1521 void vf_refresh_idle(ViewFile *vf)
1522 {
1523         if (!vf->refresh_idle_id)
1524                 {
1525                 vf->time_refresh_set = time(NULL);
1526                 /* file operations run with G_PRIORITY_DEFAULT_IDLE */
1527                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE + 50, vf_refresh_idle_cb, vf, NULL);
1528                 }
1529         else if (time(NULL) - vf->time_refresh_set > 1)
1530                 {
1531                 /* more than 1 sec since last update - increase priority */
1532                 vf_refresh_idle_cancel(vf);
1533                 vf->time_refresh_set = time(NULL);
1534                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 50, vf_refresh_idle_cb, vf, NULL);
1535                 }
1536 }
1537
1538 void vf_notify_cb(FileData *fd, NotifyType type, gpointer data)
1539 {
1540         ViewFile *vf = data;
1541         gboolean refresh;
1542
1543         NotifyType interested = NOTIFY_CHANGE | NOTIFY_REREAD | NOTIFY_GROUPING;
1544         if (vf->marks_enabled) interested |= NOTIFY_MARKS | NOTIFY_METADATA;
1545         /* FIXME: NOTIFY_METADATA should be checked by the keyword-to-mark functions and converted to NOTIFY_MARKS only if there was a change */
1546
1547         if (!(type & interested) || vf->refresh_idle_id || !vf->dir_fd) return;
1548
1549         refresh = (fd == vf->dir_fd);
1550
1551         if (!refresh)
1552                 {
1553                 gchar *base = remove_level_from_path(fd->path);
1554                 refresh = (g_strcmp0(base, vf->dir_fd->path) == 0);
1555                 g_free(base);
1556                 }
1557
1558         if ((type & NOTIFY_CHANGE) && fd->change)
1559                 {
1560                 if (!refresh && fd->change->dest)
1561                         {
1562                         gchar *dest_base = remove_level_from_path(fd->change->dest);
1563                         refresh = (g_strcmp0(dest_base, vf->dir_fd->path) == 0);
1564                         g_free(dest_base);
1565                         }
1566
1567                 if (!refresh && fd->change->source)
1568                         {
1569                         gchar *source_base = remove_level_from_path(fd->change->source);
1570                         refresh = (g_strcmp0(source_base, vf->dir_fd->path) == 0);
1571                         g_free(source_base);
1572                         }
1573                 }
1574
1575         if (refresh)
1576                 {
1577                 DEBUG_1("Notify vf: %s %04x", fd->path, type);
1578                 vf_refresh_idle(vf);
1579                 }
1580 }
1581
1582 static gboolean vf_read_metadata_in_idle_cb(gpointer data)
1583 {
1584         FileData *fd;
1585         ViewFile *vf = data;
1586         GList *list_entry;
1587         GList *work;
1588
1589         vf_thumb_status(vf, vf_read_metadata_in_idle_progress(vf), _("Loading meta..."));
1590
1591         work = vf->list;
1592
1593         while (work)
1594                 {
1595                 fd = work->data;
1596
1597                 if (fd && !fd->metadata_in_idle_loaded)
1598                         {
1599                         if (!fd->exifdate)
1600                                 {
1601                                 read_exif_time_data(fd);
1602                                 }
1603                         if (!fd->exifdate_digitized)
1604                                 {
1605                                 read_exif_time_digitized_data(fd);
1606                                 }
1607                         if (fd->rating == STAR_RATING_NOT_READ)
1608                                 {
1609                                 read_rating_data(fd);
1610                                 }
1611                         fd->metadata_in_idle_loaded = TRUE;
1612                         return TRUE;
1613                         }
1614                 work = work->next;
1615                 }
1616
1617         vf_thumb_status(vf, 0.0, NULL);
1618         vf->read_metadata_in_idle_id = 0;
1619         vf_refresh(vf);
1620         return FALSE;
1621 }
1622
1623 static void vf_read_metadata_in_idle_finished_cb(gpointer data)
1624 {
1625         ViewFile *vf = data;
1626
1627         vf_thumb_status(vf, 0.0, "Loading meta...");
1628         vf->read_metadata_in_idle_id = 0;
1629 }
1630
1631 void vf_read_metadata_in_idle(ViewFile *vf)
1632 {
1633         GList *work;
1634         FileData *fd;
1635
1636         if (!vf) return;
1637
1638         if (vf->read_metadata_in_idle_id)
1639                 {
1640                 g_idle_remove_by_data(vf);
1641                 }
1642         vf->read_metadata_in_idle_id = 0;
1643
1644         if (vf->list)
1645                 {
1646                 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);
1647                 }
1648
1649         return;
1650 }
1651
1652 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */