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