Move open_archive() to separate module
[geeqie.git] / src / view-file / view-file.cc
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 "view-file.h"
22
23 #include <gdk/gdk.h>
24 #include <glib-object.h>
25
26 #include "archives.h"
27 #include "compat.h"
28 #include "debug.h"
29 #include "dupe.h"
30 #include "filedata.h"
31 #include "history-list.h"
32 #include "intl.h"
33 #include "layout.h"
34 #include "main-defines.h"
35 #include "main.h"
36 #include "menu.h"
37 #include "misc.h"
38 #include "options.h"
39 #include "thumb.h"
40 #include "ui-fileops.h"
41 #include "ui-menu.h"
42 #include "ui-misc.h"
43 #include "ui-utildlg.h"
44 #include "utilops.h"
45 #include "view-file/view-file-icon.h"
46 #include "view-file/view-file-list.h"
47 #include "window.h"
48
49 /*
50  *-----------------------------------------------------------------------------
51  * signals
52  *-----------------------------------------------------------------------------
53  */
54
55 void vf_send_update(ViewFile *vf)
56 {
57         if (vf->func_status) vf->func_status(vf, vf->data_status);
58 }
59
60 /*
61  *-----------------------------------------------------------------------------
62  * misc
63  *-----------------------------------------------------------------------------
64  */
65
66 void vf_sort_set(ViewFile *vf, SortType type, gboolean ascend, gboolean case_sensitive)
67 {
68         switch (vf->type)
69         {
70         case FILEVIEW_LIST: vflist_sort_set(vf, type, ascend, case_sensitive); break;
71         case FILEVIEW_ICON: vficon_sort_set(vf, type, ascend, case_sensitive); break;
72         }
73 }
74
75 /*
76  *-----------------------------------------------------------------------------
77  * row stuff
78  *-----------------------------------------------------------------------------
79  */
80
81 FileData *vf_index_get_data(ViewFile *vf, gint row)
82 {
83         return static_cast<FileData *>(g_list_nth_data(vf->list, row));
84 }
85
86 gint vf_index_by_fd(ViewFile *vf, FileData *fd)
87 {
88         gint ret;
89
90         switch (vf->type)
91         {
92         case FILEVIEW_LIST: ret = vflist_index_by_fd(vf, fd); break;
93         case FILEVIEW_ICON: ret = vficon_index_by_fd(vf, fd); break;
94         default: ret = 0;
95         }
96
97         return ret;
98 }
99
100 guint vf_count(ViewFile *vf, gint64 *bytes)
101 {
102         if (bytes)
103                 {
104                 gint64 b = 0;
105                 GList *work;
106
107                 work = vf->list;
108                 while (work)
109                         {
110                         auto fd = static_cast<FileData *>(work->data);
111                         work = work->next;
112
113                         b += fd->size;
114                         }
115
116                 *bytes = b;
117                 }
118
119         return g_list_length(vf->list);
120 }
121
122 GList *vf_get_list(ViewFile *vf)
123 {
124         return filelist_copy(vf->list);
125 }
126
127 /*
128  *-------------------------------------------------------------------
129  * keyboard
130  *-------------------------------------------------------------------
131  */
132
133 static gboolean vf_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
134 {
135         auto vf = static_cast<ViewFile *>(data);
136         gboolean ret;
137
138         switch (vf->type)
139         {
140         case FILEVIEW_LIST: ret = vflist_press_key_cb(widget, event, data); break;
141         case FILEVIEW_ICON: ret = vficon_press_key_cb(widget, event, data); break;
142         default: ret = FALSE;
143         }
144
145         return ret;
146 }
147
148 /*
149  *-------------------------------------------------------------------
150  * mouse
151  *-------------------------------------------------------------------
152  */
153
154 static gboolean vf_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
155 {
156         auto vf = static_cast<ViewFile *>(data);
157         gboolean ret;
158
159         switch (vf->type)
160         {
161         case FILEVIEW_LIST: ret = vflist_press_cb(widget, bevent, data); break;
162         case FILEVIEW_ICON: ret = vficon_press_cb(widget, bevent, data); break;
163         default: ret = FALSE;
164         }
165
166         return ret;
167 }
168
169 static gboolean vf_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
170 {
171         auto vf = static_cast<ViewFile *>(data);
172         gboolean ret;
173
174         switch (vf->type)
175         {
176         case FILEVIEW_LIST: ret = vflist_release_cb(widget, bevent, data); break;
177         case FILEVIEW_ICON: ret = vficon_release_cb(widget, bevent, data); break;
178         default: ret = FALSE;
179         }
180
181         return ret;
182 }
183
184
185 /*
186  *-----------------------------------------------------------------------------
187  * selections
188  *-----------------------------------------------------------------------------
189  */
190
191 guint vf_selection_count(ViewFile *vf, gint64 *bytes)
192 {
193         guint ret;
194
195         switch (vf->type)
196         {
197         case FILEVIEW_LIST: ret = vflist_selection_count(vf, bytes); break;
198         case FILEVIEW_ICON: ret = vficon_selection_count(vf, bytes); break;
199         default: ret = 0;
200         }
201
202         return ret;
203 }
204
205 GList *vf_selection_get_list(ViewFile *vf)
206 {
207         GList *ret;
208
209         switch (vf->type)
210         {
211         case FILEVIEW_LIST: ret = vflist_selection_get_list(vf); break;
212         case FILEVIEW_ICON: ret = vficon_selection_get_list(vf); break;
213         default: ret = nullptr;
214         }
215
216         return ret;
217 }
218
219 GList *vf_selection_get_list_by_index(ViewFile *vf)
220 {
221         GList *ret;
222
223         switch (vf->type)
224         {
225         case FILEVIEW_LIST: ret = vflist_selection_get_list_by_index(vf); break;
226         case FILEVIEW_ICON: ret = vficon_selection_get_list_by_index(vf); break;
227         default: ret = nullptr;
228         }
229
230         return ret;
231 }
232
233 void vf_select_all(ViewFile *vf)
234 {
235         switch (vf->type)
236         {
237         case FILEVIEW_LIST: vflist_select_all(vf); break;
238         case FILEVIEW_ICON: vficon_select_all(vf); break;
239         }
240 }
241
242 void vf_select_none(ViewFile *vf)
243 {
244         switch (vf->type)
245         {
246         case FILEVIEW_LIST: vflist_select_none(vf); break;
247         case FILEVIEW_ICON: vficon_select_none(vf); break;
248         }
249 }
250
251 void vf_select_invert(ViewFile *vf)
252 {
253         switch (vf->type)
254         {
255         case FILEVIEW_LIST: vflist_select_invert(vf); break;
256         case FILEVIEW_ICON: vficon_select_invert(vf); break;
257         }
258 }
259
260 void vf_select_by_fd(ViewFile *vf, FileData *fd)
261 {
262         switch (vf->type)
263         {
264         case FILEVIEW_LIST: vflist_select_by_fd(vf, fd); break;
265         case FILEVIEW_ICON: vficon_select_by_fd(vf, fd); break;
266         }
267 }
268
269 void vf_select_list(ViewFile *vf, GList *list)
270 {
271         switch (vf->type)
272         {
273         case FILEVIEW_LIST: vflist_select_list(vf, list); break;
274         case FILEVIEW_ICON: vficon_select_list(vf, list); break;
275         }
276 }
277
278 void vf_mark_to_selection(ViewFile *vf, gint mark, MarkToSelectionMode mode)
279 {
280         switch (vf->type)
281         {
282         case FILEVIEW_LIST: vflist_mark_to_selection(vf, mark, mode); break;
283         case FILEVIEW_ICON: vficon_mark_to_selection(vf, mark, mode); break;
284         }
285 }
286
287 void vf_selection_to_mark(ViewFile *vf, gint mark, SelectionToMarkMode mode)
288 {
289         switch (vf->type)
290         {
291         case FILEVIEW_LIST: vflist_selection_to_mark(vf, mark, mode); break;
292         case FILEVIEW_ICON: vficon_selection_to_mark(vf, mark, mode); break;
293         }
294 }
295
296 /*
297  *-----------------------------------------------------------------------------
298  * dnd
299  *-----------------------------------------------------------------------------
300  */
301
302
303 static void vf_dnd_init(ViewFile *vf)
304 {
305         switch (vf->type)
306         {
307         case FILEVIEW_LIST: vflist_dnd_init(vf); break;
308         case FILEVIEW_ICON: vficon_dnd_init(vf); break;
309         }
310 }
311
312 /*
313  *-----------------------------------------------------------------------------
314  * pop-up menu
315  *-----------------------------------------------------------------------------
316  */
317
318 GList *vf_pop_menu_file_list(ViewFile *vf)
319 {
320         GList *ret;
321
322         switch (vf->type)
323         {
324         case FILEVIEW_LIST: ret = vflist_pop_menu_file_list(vf); break;
325         case FILEVIEW_ICON: ret = vficon_pop_menu_file_list(vf); break;
326         default: ret = nullptr;
327         }
328
329         return ret;
330 }
331
332 GList *vf_selection_get_one(ViewFile *vf, FileData *fd)
333 {
334         GList *ret;
335
336         switch (vf->type)
337         {
338         case FILEVIEW_LIST: ret = vflist_selection_get_one(vf, fd); break;
339         case FILEVIEW_ICON: ret = vficon_selection_get_one(vf, fd); break;
340         default: ret = nullptr;
341         }
342
343         return ret;
344 }
345
346 static void vf_pop_menu_edit_cb(GtkWidget *widget, gpointer data)
347 {
348         ViewFile *vf;
349         auto key = static_cast<const gchar *>(data);
350
351         vf = static_cast<ViewFile *>(submenu_item_get_data(widget));
352
353         if (!vf) return;
354
355         file_util_start_editor_from_filelist(key, vf_pop_menu_file_list(vf), vf->dir_fd->path, vf->listview);
356 }
357
358 static void vf_pop_menu_view_cb(GtkWidget *widget, gpointer data)
359 {
360         auto vf = static_cast<ViewFile *>(data);
361
362         switch (vf->type)
363         {
364         case FILEVIEW_LIST: vflist_pop_menu_view_cb(widget, data); break;
365         case FILEVIEW_ICON: vficon_pop_menu_view_cb(widget, data); break;
366         }
367 }
368
369 static void vf_pop_menu_open_archive_cb(GtkWidget *, gpointer data)
370 {
371         auto vf = static_cast<ViewFile *>(data);
372         LayoutWindow *lw_new;
373         FileData *fd = nullptr;
374         gchar *dest_dir;
375
376         switch (vf->type)
377         {
378         case FILEVIEW_LIST:
379                 fd = (VFLIST(vf)->click_fd);
380                 break;
381         case FILEVIEW_ICON:
382                 fd = (VFICON(vf)->click_fd);
383                 break;
384         }
385
386         dest_dir = open_archive(fd);
387         if (dest_dir)
388                 {
389                 lw_new = layout_new_from_default();
390                 layout_set_path(lw_new, dest_dir);
391                 g_free(dest_dir);
392                 }
393         else
394                 {
395                 warning_dialog(_("Cannot open archive file"), _("See the Log Window"), GQ_ICON_DIALOG_WARNING, nullptr);
396                 }
397 }
398
399 static void vf_pop_menu_copy_cb(GtkWidget *, gpointer data)
400 {
401         auto vf = static_cast<ViewFile *>(data);
402
403         file_util_copy(nullptr, vf_pop_menu_file_list(vf), nullptr, vf->listview);
404 }
405
406 static void vf_pop_menu_move_cb(GtkWidget *, gpointer data)
407 {
408         auto vf = static_cast<ViewFile *>(data);
409
410         file_util_move(nullptr, vf_pop_menu_file_list(vf), nullptr, vf->listview);
411 }
412
413 static void vf_pop_menu_rename_cb(GtkWidget *widget, gpointer data)
414 {
415         auto vf = static_cast<ViewFile *>(data);
416
417         switch (vf->type)
418         {
419         case FILEVIEW_LIST: vflist_pop_menu_rename_cb(widget, data); break;
420         case FILEVIEW_ICON: vficon_pop_menu_rename_cb(widget, data); break;
421         }
422 }
423
424 static void vf_pop_menu_delete_cb(GtkWidget *, gpointer data)
425 {
426         auto vf = static_cast<ViewFile *>(data);
427
428         options->file_ops.safe_delete_enable = FALSE;
429         file_util_delete(nullptr, vf_pop_menu_file_list(vf), vf->listview);
430 }
431
432 static void vf_pop_menu_move_to_trash_cb(GtkWidget *, gpointer data)
433 {
434         auto vf = static_cast<ViewFile *>(data);
435
436         options->file_ops.safe_delete_enable = TRUE;
437         file_util_delete(nullptr, vf_pop_menu_file_list(vf), vf->listview);
438 }
439
440 static void vf_pop_menu_copy_path_cb(GtkWidget *, gpointer data)
441 {
442         auto vf = static_cast<ViewFile *>(data);
443
444         file_util_copy_path_list_to_clipboard(vf_pop_menu_file_list(vf), TRUE);
445 }
446
447 static void vf_pop_menu_copy_path_unquoted_cb(GtkWidget *, gpointer data)
448 {
449         auto vf = static_cast<ViewFile *>(data);
450
451         file_util_copy_path_list_to_clipboard(vf_pop_menu_file_list(vf), FALSE);
452 }
453
454 static void vf_pop_menu_enable_grouping_cb(GtkWidget *, gpointer data)
455 {
456         auto vf = static_cast<ViewFile *>(data);
457
458         file_data_disable_grouping_list(vf_pop_menu_file_list(vf), FALSE);
459 }
460
461 static void vf_pop_menu_duplicates_cb(GtkWidget *, gpointer data)
462 {
463         auto vf = static_cast<ViewFile *>(data);
464         DupeWindow *dw;
465
466         dw = dupe_window_new();
467         dupe_window_add_files(dw, vf_pop_menu_file_list(vf), FALSE);
468 }
469
470 static void vf_pop_menu_disable_grouping_cb(GtkWidget *, gpointer data)
471 {
472         auto vf = static_cast<ViewFile *>(data);
473
474         file_data_disable_grouping_list(vf_pop_menu_file_list(vf), TRUE);
475 }
476
477 static void vf_pop_menu_sort_cb(GtkWidget *widget, gpointer data)
478 {
479         ViewFile *vf;
480         SortType type;
481
482         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
483
484         vf = static_cast<ViewFile *>(submenu_item_get_data(widget));
485         if (!vf) return;
486
487         type = static_cast<SortType>GPOINTER_TO_INT(data);
488
489         if (type == SORT_EXIFTIME || type == SORT_EXIFTIMEDIGITIZED || type == SORT_RATING)
490                 {
491                 vf_read_metadata_in_idle(vf);
492                 }
493
494         if (vf->layout)
495                 {
496                 layout_sort_set_files(vf->layout, type, vf->sort_ascend, vf->sort_case);
497                 }
498         else
499                 {
500                 vf_sort_set(vf, type, vf->sort_ascend, vf->sort_case);
501                 }
502 }
503
504 static void vf_pop_menu_sort_ascend_cb(GtkWidget *, gpointer data)
505 {
506         auto vf = static_cast<ViewFile *>(data);
507
508         if (vf->layout)
509                 {
510                 layout_sort_set_files(vf->layout, vf->sort_method, !vf->sort_ascend, vf->sort_case);
511                 }
512         else
513                 {
514                 vf_sort_set(vf, vf->sort_method, !vf->sort_ascend, vf->sort_case);
515                 }
516 }
517
518 static void vf_pop_menu_sort_case_cb(GtkWidget *, gpointer data)
519 {
520         auto vf = static_cast<ViewFile *>(data);
521
522         if (vf->layout)
523                 {
524                 layout_sort_set_files(vf->layout, vf->sort_method, vf->sort_ascend, !vf->sort_case);
525                 }
526         else
527                 {
528                 vf_sort_set(vf, vf->sort_method, vf->sort_ascend, !vf->sort_case);
529                 }
530 }
531
532 static void vf_pop_menu_sel_mark_cb(GtkWidget *, gpointer data)
533 {
534         auto vf = static_cast<ViewFile *>(data);
535         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_SET);
536 }
537
538 static void vf_pop_menu_sel_mark_and_cb(GtkWidget *, gpointer data)
539 {
540         auto vf = static_cast<ViewFile *>(data);
541         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_AND);
542 }
543
544 static void vf_pop_menu_sel_mark_or_cb(GtkWidget *, gpointer data)
545 {
546         auto vf = static_cast<ViewFile *>(data);
547         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_OR);
548 }
549
550 static void vf_pop_menu_sel_mark_minus_cb(GtkWidget *, gpointer data)
551 {
552         auto vf = static_cast<ViewFile *>(data);
553         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_MINUS);
554 }
555
556 static void vf_pop_menu_set_mark_sel_cb(GtkWidget *, gpointer data)
557 {
558         auto vf = static_cast<ViewFile *>(data);
559         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_SET);
560 }
561
562 static void vf_pop_menu_res_mark_sel_cb(GtkWidget *, gpointer data)
563 {
564         auto vf = static_cast<ViewFile *>(data);
565         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_RESET);
566 }
567
568 static void vf_pop_menu_toggle_mark_sel_cb(GtkWidget *, gpointer data)
569 {
570         auto vf = static_cast<ViewFile *>(data);
571         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_TOGGLE);
572 }
573
574 static void vf_pop_menu_toggle_view_type_cb(GtkWidget *widget, gpointer data)
575 {
576         auto vf = static_cast<ViewFile *>(data);
577         auto new_type = static_cast<FileViewType>(GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data")));
578         if (!vf->layout) return;
579
580         layout_views_set(vf->layout, vf->layout->options.dir_view_type, new_type);
581 }
582
583 static void vf_pop_menu_refresh_cb(GtkWidget *widget, gpointer data)
584 {
585         auto vf = static_cast<ViewFile *>(data);
586
587         switch (vf->type)
588         {
589         case FILEVIEW_LIST: vflist_pop_menu_refresh_cb(widget, data); break;
590         case FILEVIEW_ICON: vficon_pop_menu_refresh_cb(widget, data); break;
591         }
592 }
593
594 static void vf_popup_destroy_cb(GtkWidget *widget, gpointer data)
595 {
596         auto vf = static_cast<ViewFile *>(data);
597
598         switch (vf->type)
599         {
600         case FILEVIEW_LIST: vflist_popup_destroy_cb(widget, data); break;
601         case FILEVIEW_ICON: vficon_popup_destroy_cb(widget, data); break;
602         }
603
604         filelist_free(vf->editmenu_fd_list);
605         vf->editmenu_fd_list = nullptr;
606 }
607
608 /**
609  * @brief Add file selection list to a collection
610  * @param[in] widget
611  * @param[in] data Index to the collection list menu item selected, or -1 for new collection
612  *
613  *
614  */
615 static void vf_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
616 {
617         ViewFile *vf;
618         GList *selection_list;
619
620         vf = static_cast<ViewFile *>(submenu_item_get_data(widget));
621         selection_list = vf_selection_get_list(vf);
622         pop_menu_collections(selection_list, data);
623
624         filelist_free(selection_list);
625 }
626
627 GtkWidget *vf_pop_menu(ViewFile *vf)
628 {
629         GtkWidget *menu;
630         GtkWidget *item;
631         GtkWidget *submenu;
632         gboolean active = FALSE;
633         gboolean class_archive = FALSE;
634         GtkAccelGroup *accel_group;
635
636         switch (vf->type)
637         {
638         case FILEVIEW_LIST:
639                 vflist_color_set(vf, VFLIST(vf)->click_fd, TRUE);
640                 active = (VFLIST(vf)->click_fd != nullptr);
641                 class_archive = (VFLIST(vf)->click_fd != nullptr && VFLIST(vf)->click_fd->format_class == FORMAT_CLASS_ARCHIVE);
642                 break;
643         case FILEVIEW_ICON:
644                 active = (VFICON(vf)->click_fd != nullptr);
645                 class_archive = (VFICON(vf)->click_fd != nullptr && VFICON(vf)->click_fd->format_class == FORMAT_CLASS_ARCHIVE);
646                 break;
647         }
648
649         menu = popup_menu_short_lived();
650
651         accel_group = gtk_accel_group_new();
652         gtk_menu_set_accel_group(GTK_MENU(menu), accel_group);
653
654         g_object_set_data(G_OBJECT(menu), "window_keys", nullptr);
655         g_object_set_data(G_OBJECT(menu), "accel_group", accel_group);
656
657         g_signal_connect(G_OBJECT(menu), "destroy",
658                          G_CALLBACK(vf_popup_destroy_cb), vf);
659
660         if (vf->clicked_mark > 0)
661                 {
662                 gint mark = vf->clicked_mark;
663                 gchar *str_set_mark = g_strdup_printf(_("_Set mark %d"), mark);
664                 gchar *str_res_mark = g_strdup_printf(_("_Reset mark %d"), mark);
665                 gchar *str_toggle_mark = g_strdup_printf(_("_Toggle mark %d"), mark);
666                 gchar *str_sel_mark = g_strdup_printf(_("_Select mark %d"), mark);
667                 gchar *str_sel_mark_or = g_strdup_printf(_("_Add mark %d"), mark);
668                 gchar *str_sel_mark_and = g_strdup_printf(_("_Intersection with mark %d"), mark);
669                 gchar *str_sel_mark_minus = g_strdup_printf(_("_Unselect mark %d"), mark);
670
671                 g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
672
673                 vf->active_mark = mark;
674                 vf->clicked_mark = 0;
675
676                 menu_item_add_sensitive(menu, str_set_mark, active,
677                                         G_CALLBACK(vf_pop_menu_set_mark_sel_cb), vf);
678
679                 menu_item_add_sensitive(menu, str_res_mark, active,
680                                         G_CALLBACK(vf_pop_menu_res_mark_sel_cb), vf);
681
682                 menu_item_add_sensitive(menu, str_toggle_mark, active,
683                                         G_CALLBACK(vf_pop_menu_toggle_mark_sel_cb), vf);
684
685                 menu_item_add_divider(menu);
686
687                 menu_item_add_sensitive(menu, str_sel_mark, active,
688                                         G_CALLBACK(vf_pop_menu_sel_mark_cb), vf);
689                 menu_item_add_sensitive(menu, str_sel_mark_or, active,
690                                         G_CALLBACK(vf_pop_menu_sel_mark_or_cb), vf);
691                 menu_item_add_sensitive(menu, str_sel_mark_and, active,
692                                         G_CALLBACK(vf_pop_menu_sel_mark_and_cb), vf);
693                 menu_item_add_sensitive(menu, str_sel_mark_minus, active,
694                                         G_CALLBACK(vf_pop_menu_sel_mark_minus_cb), vf);
695
696                 menu_item_add_divider(menu);
697
698                 g_free(str_set_mark);
699                 g_free(str_res_mark);
700                 g_free(str_toggle_mark);
701                 g_free(str_sel_mark);
702                 g_free(str_sel_mark_and);
703                 g_free(str_sel_mark_or);
704                 g_free(str_sel_mark_minus);
705                 }
706
707         vf->editmenu_fd_list = vf_pop_menu_file_list(vf);
708         submenu_add_edit(menu, &item, G_CALLBACK(vf_pop_menu_edit_cb), vf, vf->editmenu_fd_list);
709         gtk_widget_set_sensitive(item, active);
710
711         menu_item_add_icon_sensitive(menu, _("View in _new window"), GQ_ICON_NEW, active,
712                                       G_CALLBACK(vf_pop_menu_view_cb), vf);
713
714         menu_item_add_icon_sensitive(menu, _("Open archive"), GQ_ICON_OPEN, active & class_archive, G_CALLBACK(vf_pop_menu_open_archive_cb), vf);
715
716         menu_item_add_divider(menu);
717         menu_item_add_icon_sensitive(menu, _("_Copy..."), GQ_ICON_COPY, active,
718                                       G_CALLBACK(vf_pop_menu_copy_cb), vf);
719         menu_item_add_sensitive(menu, _("_Move..."), active,
720                                 G_CALLBACK(vf_pop_menu_move_cb), vf);
721         menu_item_add_sensitive(menu, _("_Rename..."), active,
722                                 G_CALLBACK(vf_pop_menu_rename_cb), vf);
723         menu_item_add_sensitive(menu, _("_Copy path to clipboard"), active,
724                                 G_CALLBACK(vf_pop_menu_copy_path_cb), vf);
725         menu_item_add_sensitive(menu, _("_Copy path unquoted to clipboard"), active,
726                                 G_CALLBACK(vf_pop_menu_copy_path_unquoted_cb), vf);
727         menu_item_add_divider(menu);
728         menu_item_add_icon_sensitive(menu,
729                                 options->file_ops.confirm_move_to_trash ? _("Move to Trash...") :
730                                         _("Move to Trash"), GQ_ICON_DELETE, active,
731                                 G_CALLBACK(vf_pop_menu_move_to_trash_cb), vf);
732         menu_item_add_icon_sensitive(menu,
733                                 options->file_ops.confirm_delete ? _("_Delete...") :
734                                         _("_Delete"), GQ_ICON_DELETE_SHRED, active,
735                                 G_CALLBACK(vf_pop_menu_delete_cb), vf);
736         menu_item_add_divider(menu);
737
738         menu_item_add_sensitive(menu, _("Enable file _grouping"), active,
739                                 G_CALLBACK(vf_pop_menu_enable_grouping_cb), vf);
740         menu_item_add_sensitive(menu, _("Disable file groupi_ng"), active,
741                                 G_CALLBACK(vf_pop_menu_disable_grouping_cb), vf);
742
743         menu_item_add_divider(menu);
744         menu_item_add_icon_sensitive(menu, _("_Find duplicates..."), GQ_ICON_FIND, active,
745                                 G_CALLBACK(vf_pop_menu_duplicates_cb), vf);
746         menu_item_add_divider(menu);
747
748         submenu = submenu_add_collections(menu, &item,
749                                 G_CALLBACK(vf_pop_menu_collections_cb), vf);
750         gtk_widget_set_sensitive(item, active);
751         menu_item_add_divider(menu);
752
753         submenu = submenu_add_sort(nullptr, G_CALLBACK(vf_pop_menu_sort_cb), vf,
754                                    FALSE, FALSE, TRUE, vf->sort_method);
755         menu_item_add_divider(submenu);
756         menu_item_add_check(submenu, _("Ascending"), vf->sort_ascend,
757                             G_CALLBACK(vf_pop_menu_sort_ascend_cb), vf);
758         menu_item_add_check(submenu, _("Case"), vf->sort_ascend,
759                             G_CALLBACK(vf_pop_menu_sort_case_cb), vf);
760
761         item = menu_item_add(menu, _("_Sort"), nullptr, nullptr);
762         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
763
764         item = menu_item_add_radio(menu, _("Images as List"), GINT_TO_POINTER(FILEVIEW_LIST), vf->type == FILEVIEW_LIST,
765                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
766
767         item = menu_item_add_radio(menu, _("Images as Icons"), GINT_TO_POINTER(FILEVIEW_ICON), vf->type == FILEVIEW_ICON,
768                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
769
770         switch (vf->type)
771         {
772         case FILEVIEW_LIST:
773                 menu_item_add_check(menu, _("Show _thumbnails"), VFLIST(vf)->thumbs_enabled,
774                                     G_CALLBACK(vflist_pop_menu_thumbs_cb), vf);
775                 break;
776         case FILEVIEW_ICON:
777                 menu_item_add_check(menu, _("Show filename _text"), VFICON(vf)->show_text,
778                                     G_CALLBACK(vficon_pop_menu_show_names_cb), vf);
779                 break;
780         }
781
782         switch (vf->type)
783         {
784         case FILEVIEW_LIST:
785                 menu_item_add_check(menu, _("Show star rating"), options->show_star_rating,
786                                     G_CALLBACK(vflist_pop_menu_show_star_rating_cb), vf);
787                 break;
788         case FILEVIEW_ICON:
789                 menu_item_add_check(menu, _("Show star rating"), options->show_star_rating,
790                                     G_CALLBACK(vficon_pop_menu_show_star_rating_cb), vf);
791                 break;
792         }
793
794         menu_item_add_icon(menu, _("Re_fresh"), GQ_ICON_REFRESH, G_CALLBACK(vf_pop_menu_refresh_cb), vf);
795
796         return menu;
797 }
798
799 gboolean vf_refresh(ViewFile *vf)
800 {
801         gboolean ret;
802
803         switch (vf->type)
804         {
805         case FILEVIEW_LIST: ret = vflist_refresh(vf); break;
806         case FILEVIEW_ICON: ret = vficon_refresh(vf); break;
807         default: ret = FALSE;
808         }
809
810         return ret;
811 }
812
813 gboolean vf_set_fd(ViewFile *vf, FileData *dir_fd)
814 {
815         gboolean ret;
816
817         switch (vf->type)
818         {
819         case FILEVIEW_LIST: ret = vflist_set_fd(vf, dir_fd); break;
820         case FILEVIEW_ICON: ret = vficon_set_fd(vf, dir_fd); break;
821         default: ret = FALSE;
822         }
823
824         return ret;
825 }
826
827 static void vf_destroy_cb(GtkWidget *widget, gpointer data)
828 {
829         auto vf = static_cast<ViewFile *>(data);
830
831         switch (vf->type)
832         {
833         case FILEVIEW_LIST: vflist_destroy_cb(widget, data); break;
834         case FILEVIEW_ICON: vficon_destroy_cb(widget, data); break;
835         }
836
837         if (vf->popup)
838                 {
839                 g_signal_handlers_disconnect_matched(G_OBJECT(vf->popup), G_SIGNAL_MATCH_DATA,
840                                                      0, 0, nullptr, nullptr, vf);
841                 gq_gtk_widget_destroy(vf->popup);
842                 }
843
844         if (vf->read_metadata_in_idle_id)
845                 {
846                 g_idle_remove_by_data(vf);
847                 }
848         file_data_unref(vf->dir_fd);
849         g_free(vf->info);
850         g_free(vf);
851 }
852
853 static void vf_marks_filter_toggle_cb(GtkWidget *, gpointer data)
854 {
855         auto vf = static_cast<ViewFile *>(data);
856         vf_refresh_idle(vf);
857 }
858
859 struct MarksTextEntry {
860         GenericDialog *gd;
861         gint mark_no;
862         GtkWidget *edit_widget;
863         gchar *text_entry;
864         GtkWidget *parent;
865 };
866
867 static void vf_marks_tooltip_cancel_cb(GenericDialog *gd, gpointer data)
868 {
869         auto mte = static_cast<MarksTextEntry *>(data);
870
871         g_free(mte->text_entry);
872         generic_dialog_close(gd);
873 }
874
875 static void vf_marks_tooltip_ok_cb(GenericDialog *gd, gpointer data)
876 {
877         auto mte = static_cast<MarksTextEntry *>(data);
878
879         g_free(options->marks_tooltips[mte->mark_no]);
880         options->marks_tooltips[mte->mark_no] = g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(mte->edit_widget)));
881
882         gtk_widget_set_tooltip_text(mte->parent, options->marks_tooltips[mte->mark_no]);
883
884         g_free(mte->text_entry);
885         generic_dialog_close(gd);
886 }
887
888 void vf_marks_filter_on_icon_press(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer userdata)
889 {
890         auto mte = static_cast<MarksTextEntry *>(userdata);
891
892         g_free(mte->text_entry);
893         mte->text_entry = g_strdup("");
894         gq_gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), "");
895 }
896
897 static void vf_marks_tooltip_help_cb(GenericDialog *, gpointer)
898 {
899         help_window_show("GuideImageMarks.html");
900 }
901
902 static gboolean vf_marks_tooltip_cb(GtkWidget *widget,
903                                                                                 GdkEventButton *event,
904                                                                                 gpointer user_data)
905 {
906         GtkWidget *table;
907         gint i = GPOINTER_TO_INT(user_data);
908
909         if (event->button != MOUSE_BUTTON_RIGHT)
910                 return FALSE;
911
912         auto mte = g_new0(MarksTextEntry, 1);
913         mte->mark_no = i;
914         mte->text_entry = g_strdup(options->marks_tooltips[i]);
915         mte->parent = widget;
916
917         mte->gd = generic_dialog_new(_("Mark text"), "mark_text",
918                                      widget, FALSE,
919                                      vf_marks_tooltip_cancel_cb, mte);
920         generic_dialog_add_message(mte->gd, GQ_ICON_DIALOG_QUESTION, _("Set mark text"),
921                                    _("This will set or clear the mark text."), FALSE);
922         generic_dialog_add_button(mte->gd, GQ_ICON_OK, "OK",
923                                   vf_marks_tooltip_ok_cb, TRUE);
924         generic_dialog_add_button(mte->gd, GQ_ICON_HELP, _("Help"),
925                                   vf_marks_tooltip_help_cb, FALSE);
926
927         table = pref_table_new(mte->gd->vbox, 3, 1, FALSE, TRUE);
928         pref_table_label(table, 0, 0, g_strdup_printf("%s%d", _("Mark "), mte->mark_no + 1), GTK_ALIGN_END);
929         mte->edit_widget = gtk_entry_new();
930         gtk_widget_set_size_request(mte->edit_widget, 300, -1);
931         if (mte->text_entry)
932                 {
933                 gq_gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), mte->text_entry);
934                 }
935         gq_gtk_grid_attach_default(GTK_GRID(table), mte->edit_widget, 1, 2, 0, 1);
936         generic_dialog_attach_default(mte->gd, mte->edit_widget);
937
938         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(mte->edit_widget),
939                                       GTK_ENTRY_ICON_SECONDARY, GQ_ICON_CLEAR);
940         gtk_entry_set_icon_tooltip_text(GTK_ENTRY(mte->edit_widget),
941                                         GTK_ENTRY_ICON_SECONDARY, _("Clear"));
942         g_signal_connect(GTK_ENTRY(mte->edit_widget), "icon-press",
943                          G_CALLBACK(vf_marks_filter_on_icon_press), mte);
944
945         gtk_widget_show(mte->edit_widget);
946         gtk_widget_grab_focus(mte->edit_widget);
947         gtk_widget_show(GTK_WIDGET(mte->gd->dialog));
948
949         return TRUE;
950 }
951
952 static void vf_file_filter_save_cb(GtkWidget *, gpointer data)
953 {
954         auto vf = static_cast<ViewFile *>(data);
955         gchar *entry_text;
956         gchar *remove_text = nullptr;
957         gchar *index_text = nullptr;
958         gboolean text_found = FALSE;
959         gint i;
960
961         entry_text = g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)))));
962
963         if (entry_text[0] == '\0' && vf->file_filter.last_selected >= 0)
964                 {
965                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), vf->file_filter.last_selected);
966                 remove_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
967                 history_list_item_remove("file_filter", remove_text);
968                 gtk_combo_box_text_remove(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), vf->file_filter.last_selected);
969                 g_free(remove_text);
970
971                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), -1);
972                 vf->file_filter.last_selected = - 1;
973                 gq_gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo))), "");
974                 vf->file_filter.count--;
975                 }
976         else
977                 {
978                 if (entry_text[0] != '\0')
979                         {
980                         for (i = 0; i < vf->file_filter.count; i++)
981                                 {
982                                 if (index_text)
983                                         {
984                                         g_free(index_text);
985                                         }
986                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), i);
987                                 index_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
988
989                                 if (g_strcmp0(index_text, entry_text) == 0)
990                                         {
991                                         text_found = TRUE;
992                                         break;
993                                         }
994                                 }
995
996                         g_free(index_text);
997                         if (!text_found)
998                                 {
999                                 history_list_add_to_key("file_filter", entry_text, 10);
1000                                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), entry_text);
1001                                 vf->file_filter.count++;
1002                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), vf->file_filter.count - 1);
1003                                 }
1004                         }
1005                 }
1006         vf_refresh(vf);
1007
1008         g_free(entry_text);
1009 }
1010
1011 static void vf_file_filter_cb(GtkWidget *, gpointer data)
1012 {
1013         auto vf = static_cast<ViewFile *>(data);
1014
1015         vf_refresh(vf);
1016 }
1017
1018 static gboolean vf_file_filter_press_cb(GtkWidget *widget, GdkEventButton *, gpointer data)
1019 {
1020         auto vf = static_cast<ViewFile *>(data);
1021         vf->file_filter.last_selected = gtk_combo_box_get_active(GTK_COMBO_BOX(vf->file_filter.combo));
1022
1023         gtk_widget_grab_focus(widget);
1024
1025         return TRUE;
1026 }
1027
1028 static GtkWidget *vf_marks_filter_init(ViewFile *vf)
1029 {
1030         GtkWidget *frame = gtk_frame_new(nullptr);
1031         GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1032
1033         gint i;
1034
1035         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
1036                 {
1037                 GtkWidget *check = gtk_check_button_new();
1038                 gq_gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, FALSE, 0);
1039                 g_signal_connect(G_OBJECT(check), "toggled",
1040                          G_CALLBACK(vf_marks_filter_toggle_cb), vf);
1041                 g_signal_connect(G_OBJECT(check), "button_press_event",
1042                          G_CALLBACK(vf_marks_tooltip_cb), GINT_TO_POINTER(i));
1043                 gtk_widget_set_tooltip_text(check, options->marks_tooltips[i]);
1044
1045                 gtk_widget_show(check);
1046                 vf->filter_check[i] = check;
1047                 }
1048         gq_gtk_container_add(GTK_WIDGET(frame), hbox);
1049         gtk_widget_show(hbox);
1050         return frame;
1051 }
1052
1053 void vf_file_filter_set(ViewFile *vf, gboolean enable)
1054 {
1055         if (enable)
1056                 {
1057                 gtk_widget_show(vf->file_filter.combo);
1058                 gtk_widget_show(vf->file_filter.frame);
1059                 }
1060         else
1061                 {
1062                 gtk_widget_hide(vf->file_filter.combo);
1063                 gtk_widget_hide(vf->file_filter.frame);
1064                 }
1065
1066         vf_refresh(vf);
1067 }
1068
1069 static gboolean vf_file_filter_class_cb(GtkWidget *widget, gpointer data)
1070 {
1071         auto vf = static_cast<ViewFile *>(data);
1072         gint i;
1073
1074         gboolean state = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
1075
1076         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
1077                 {
1078                 if (g_strcmp0(format_class_list[i], gtk_menu_item_get_label(GTK_MENU_ITEM(widget))) == 0)
1079                         {
1080                         options->class_filter[i] = state;
1081                         }
1082                 }
1083         vf_refresh(vf);
1084
1085         return TRUE;
1086 }
1087
1088 static gboolean vf_file_filter_class_set_all_cb(GtkWidget *widget, gpointer data)
1089 {
1090         auto vf = static_cast<ViewFile *>(data);
1091         GtkWidget *parent;
1092         GList *children;
1093         GtkWidget *child;
1094         gint i;
1095         gboolean state;
1096
1097         if (g_strcmp0(_("Select all"), gtk_menu_item_get_label(GTK_MENU_ITEM(widget))) == 0)
1098                 {
1099                 state = TRUE;
1100                 }
1101         else
1102                 {
1103                 state = FALSE;
1104                 }
1105
1106         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
1107                 {
1108                 options->class_filter[i] = state;
1109                 }
1110
1111         i = 0;
1112         parent = gtk_widget_get_parent(widget);
1113         children = gtk_container_get_children(GTK_CONTAINER(parent));
1114         while (children)
1115                 {
1116                 child = static_cast<GtkWidget *>(children->data);
1117                 if (i < FILE_FORMAT_CLASSES)
1118                         {
1119                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(child), state);
1120                         }
1121                 i++;
1122                 children = children->next;
1123                 }
1124         g_list_free(children);
1125         vf_refresh(vf);
1126
1127         return TRUE;
1128 }
1129
1130 static GtkWidget *class_filter_menu (ViewFile *vf)
1131 {
1132         GtkWidget *menu;
1133         GtkWidget *menu_item;
1134         int i;
1135
1136         menu = gtk_menu_new();
1137
1138         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
1139             {
1140                 menu_item = gtk_check_menu_item_new_with_label(format_class_list[i]);
1141                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), options->class_filter[i]);
1142                 g_signal_connect(G_OBJECT(menu_item), "toggled", G_CALLBACK(vf_file_filter_class_cb), vf);
1143                 gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1144                 gtk_widget_show(menu_item);
1145                 }
1146
1147         menu_item = gtk_menu_item_new_with_label(_("Select all"));
1148         gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1149         gtk_widget_show(menu_item);
1150         g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(vf_file_filter_class_set_all_cb), vf);
1151
1152         menu_item = gtk_menu_item_new_with_label(_("Select none"));
1153         gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1154         gtk_widget_show(menu_item);
1155         g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(vf_file_filter_class_set_all_cb), vf);
1156
1157         return menu;
1158 }
1159
1160 static void case_sensitive_cb(GtkWidget *widget, gpointer data)
1161 {
1162         auto vf = static_cast<ViewFile *>(data);
1163
1164         vf->file_filter.case_sensitive = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
1165         vf_refresh(vf);
1166 }
1167
1168 static void file_filter_clear_cb(GtkEntry *, GtkEntryIconPosition pos, GdkEvent *, gpointer userdata)
1169 {
1170         if (pos == GTK_ENTRY_ICON_SECONDARY)
1171                 {
1172                 gq_gtk_entry_set_text(GTK_ENTRY(userdata), "");
1173                 gtk_widget_grab_focus(GTK_WIDGET(userdata));
1174                 }
1175 }
1176
1177 static GtkWidget *vf_file_filter_init(ViewFile *vf)
1178 {
1179         GtkWidget *frame = gtk_frame_new(nullptr);
1180         GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1181         GList *work;
1182         gint n = 0;
1183         GtkWidget *combo_entry;
1184         GtkWidget *menubar;
1185         GtkWidget *menuitem;
1186         GtkWidget *case_sensitive;
1187         GtkWidget *box;
1188         GtkWidget *icon;
1189         GtkWidget *label;
1190
1191         vf->file_filter.combo = gtk_combo_box_text_new_with_entry();
1192         combo_entry = gtk_bin_get_child(GTK_BIN(vf->file_filter.combo));
1193         gtk_widget_show(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)));
1194         gtk_widget_show((GTK_WIDGET(vf->file_filter.combo)));
1195         gtk_widget_set_tooltip_text(GTK_WIDGET(vf->file_filter.combo), _("Use regular expressions"));
1196
1197         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(combo_entry), GTK_ENTRY_ICON_SECONDARY, GQ_ICON_CLEAR);
1198         gtk_entry_set_icon_tooltip_text (GTK_ENTRY(combo_entry), GTK_ENTRY_ICON_SECONDARY, _("Clear"));
1199         g_signal_connect(GTK_ENTRY(combo_entry), "icon-press", G_CALLBACK(file_filter_clear_cb), combo_entry);
1200
1201         work = history_list_get_by_key("file_filter");
1202         while (work)
1203                 {
1204                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), static_cast<gchar *>(work->data));
1205                 work = work->next;
1206                 n++;
1207                 vf->file_filter.count = n;
1208                 }
1209         gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), 0);
1210
1211         g_signal_connect(G_OBJECT(combo_entry), "activate",
1212                 G_CALLBACK(vf_file_filter_save_cb), vf);
1213
1214         g_signal_connect(G_OBJECT(vf->file_filter.combo), "changed",
1215                 G_CALLBACK(vf_file_filter_cb), vf);
1216
1217         g_signal_connect(G_OBJECT(combo_entry), "button_press_event",
1218                          G_CALLBACK(vf_file_filter_press_cb), vf);
1219
1220         gq_gtk_box_pack_start(GTK_BOX(hbox), vf->file_filter.combo, FALSE, FALSE, 0);
1221         gtk_widget_show(vf->file_filter.combo);
1222         gq_gtk_container_add(GTK_WIDGET(frame), hbox);
1223         gtk_widget_show(hbox);
1224
1225         case_sensitive = gtk_check_button_new_with_label(_("Case"));
1226         gq_gtk_box_pack_start(GTK_BOX(hbox), case_sensitive, FALSE, FALSE, 0);
1227         gtk_widget_set_tooltip_text(GTK_WIDGET(case_sensitive), _("Case sensitive"));
1228         g_signal_connect(G_OBJECT(case_sensitive), "clicked", G_CALLBACK(case_sensitive_cb), vf);
1229         gtk_widget_show(case_sensitive);
1230
1231         menubar = gtk_menu_bar_new();
1232         gq_gtk_box_pack_start(GTK_BOX(hbox), menubar, FALSE, TRUE, 0);
1233         gtk_widget_show(menubar);
1234
1235         box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
1236         icon = gtk_image_new_from_icon_name(GQ_ICON_PAN_DOWN, GTK_ICON_SIZE_MENU);
1237         label = gtk_label_new(_("Class"));
1238
1239         gq_gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0);
1240         gq_gtk_box_pack_end(GTK_BOX(box), icon, FALSE, FALSE, 0);
1241
1242         menuitem = gtk_menu_item_new();
1243
1244         gtk_widget_set_tooltip_text(GTK_WIDGET(menuitem), _("Select Class filter"));
1245         gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), class_filter_menu(vf));
1246         gtk_menu_shell_append(GTK_MENU_SHELL(menubar), menuitem);
1247         gq_gtk_container_add(GTK_WIDGET(menuitem), box);
1248         gq_gtk_widget_show_all(menuitem);
1249
1250         return frame;
1251 }
1252
1253 void vf_mark_filter_toggle(ViewFile *vf, gint mark)
1254 {
1255         gint n = mark - 1;
1256         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vf->filter_check[n]),
1257                                      !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[n])));
1258 }
1259
1260 ViewFile *vf_new(FileViewType type, FileData *dir_fd)
1261 {
1262         ViewFile *vf;
1263
1264         vf = g_new0(ViewFile, 1);
1265
1266         vf->type = type;
1267         vf->sort_method = SORT_NAME;
1268         vf->sort_ascend = TRUE;
1269         vf->read_metadata_in_idle_id = 0;
1270
1271         vf->scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
1272         gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vf->scrolled), GTK_SHADOW_IN);
1273         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vf->scrolled),
1274                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1275
1276         vf->filter = vf_marks_filter_init(vf);
1277         vf->file_filter.frame = vf_file_filter_init(vf);
1278
1279         vf->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1280         gq_gtk_box_pack_start(GTK_BOX(vf->widget), vf->filter, FALSE, FALSE, 0);
1281         gq_gtk_box_pack_start(GTK_BOX(vf->widget), vf->file_filter.frame, FALSE, FALSE, 0);
1282         gq_gtk_box_pack_start(GTK_BOX(vf->widget), vf->scrolled, TRUE, TRUE, 0);
1283         gtk_widget_show(vf->scrolled);
1284
1285         g_signal_connect(G_OBJECT(vf->widget), "destroy",
1286                          G_CALLBACK(vf_destroy_cb), vf);
1287
1288         switch (type)
1289         {
1290         case FILEVIEW_LIST: vf = vflist_new(vf, dir_fd); break;
1291         case FILEVIEW_ICON: vf = vficon_new(vf, dir_fd); break;
1292         }
1293
1294         vf_dnd_init(vf);
1295
1296         g_signal_connect(G_OBJECT(vf->listview), "key_press_event",
1297                          G_CALLBACK(vf_press_key_cb), vf);
1298         g_signal_connect(G_OBJECT(vf->listview), "button_press_event",
1299                          G_CALLBACK(vf_press_cb), vf);
1300         g_signal_connect(G_OBJECT(vf->listview), "button_release_event",
1301                          G_CALLBACK(vf_release_cb), vf);
1302
1303         gq_gtk_container_add(GTK_WIDGET(vf->scrolled), vf->listview);
1304         gtk_widget_show(vf->listview);
1305
1306         if (dir_fd) vf_set_fd(vf, dir_fd);
1307
1308         return vf;
1309 }
1310
1311 void vf_set_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gpointer data), gpointer data)
1312 {
1313         vf->func_status = func;
1314         vf->data_status = data;
1315 }
1316
1317 void vf_set_thumb_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gdouble val, const gchar *text, gpointer data), gpointer data)
1318 {
1319         vf->func_thumb_status = func;
1320         vf->data_thumb_status = data;
1321 }
1322
1323 void vf_thumb_set(ViewFile *vf, gboolean enable)
1324 {
1325         switch (vf->type)
1326         {
1327         case FILEVIEW_LIST: vflist_thumb_set(vf, enable); break;
1328         case FILEVIEW_ICON: /*vficon_thumb_set(vf, enable);*/ break;
1329         }
1330 }
1331
1332
1333 static gboolean vf_thumb_next(ViewFile *vf);
1334
1335 static gdouble vf_thumb_progress(ViewFile *vf)
1336 {
1337         gint count = 0;
1338         gint done = 0;
1339
1340         switch (vf->type)
1341         {
1342         case FILEVIEW_LIST: vflist_thumb_progress_count(vf->list, &count, &done); break;
1343         case FILEVIEW_ICON: vficon_thumb_progress_count(vf->list, &count, &done); break;
1344         }
1345
1346         DEBUG_1("thumb progress: %d of %d", done, count);
1347         return static_cast<gdouble>(done) / count;
1348 }
1349
1350 static gdouble vf_read_metadata_in_idle_progress(ViewFile *vf)
1351 {
1352         gint count = 0;
1353         gint done = 0;
1354
1355         switch (vf->type)
1356                 {
1357                 case FILEVIEW_LIST: vflist_read_metadata_progress_count(vf->list, &count, &done); break;
1358                 case FILEVIEW_ICON: vficon_read_metadata_progress_count(vf->list, &count, &done); break;
1359                 }
1360
1361         return static_cast<gdouble>(done) / count;
1362 }
1363
1364 static void vf_set_thumb_fd(ViewFile *vf, FileData *fd)
1365 {
1366         switch (vf->type)
1367         {
1368         case FILEVIEW_LIST: vflist_set_thumb_fd(vf, fd); break;
1369         case FILEVIEW_ICON: vficon_set_thumb_fd(vf, fd); break;
1370         }
1371 }
1372
1373 static void vf_thumb_status(ViewFile *vf, gdouble val, const gchar *text)
1374 {
1375         if (vf->func_thumb_status)
1376                 {
1377                 vf->func_thumb_status(vf, val, text, vf->data_thumb_status);
1378                 }
1379 }
1380
1381 static void vf_thumb_do(ViewFile *vf, FileData *fd)
1382 {
1383         if (!fd) return;
1384
1385         vf_set_thumb_fd(vf, fd);
1386         vf_thumb_status(vf, vf_thumb_progress(vf), _("Loading thumbs..."));
1387 }
1388
1389 void vf_thumb_cleanup(ViewFile *vf)
1390 {
1391         vf_thumb_status(vf, 0.0, nullptr);
1392
1393         vf->thumbs_running = FALSE;
1394
1395         thumb_loader_free(vf->thumbs_loader);
1396         vf->thumbs_loader = nullptr;
1397
1398         vf->thumbs_filedata = nullptr;
1399 }
1400
1401 void vf_thumb_stop(ViewFile *vf)
1402 {
1403         if (vf->thumbs_running) vf_thumb_cleanup(vf);
1404 }
1405
1406 static void vf_thumb_common_cb(ThumbLoader *tl, gpointer data)
1407 {
1408         auto vf = static_cast<ViewFile *>(data);
1409
1410         if (vf->thumbs_filedata && vf->thumbs_loader == tl)
1411                 {
1412                 vf_thumb_do(vf, vf->thumbs_filedata);
1413                 }
1414
1415         while (vf_thumb_next(vf));
1416 }
1417
1418 static void vf_thumb_error_cb(ThumbLoader *tl, gpointer data)
1419 {
1420         vf_thumb_common_cb(tl, data);
1421 }
1422
1423 static void vf_thumb_done_cb(ThumbLoader *tl, gpointer data)
1424 {
1425         vf_thumb_common_cb(tl, data);
1426 }
1427
1428 static gboolean vf_thumb_next(ViewFile *vf)
1429 {
1430         FileData *fd = nullptr;
1431
1432         if (!gtk_widget_get_realized(vf->listview))
1433                 {
1434                 vf_thumb_status(vf, 0.0, nullptr);
1435                 return FALSE;
1436                 }
1437
1438         switch (vf->type)
1439         {
1440         case FILEVIEW_LIST: fd = vflist_thumb_next_fd(vf); break;
1441         case FILEVIEW_ICON: fd = vficon_thumb_next_fd(vf); break;
1442         }
1443
1444         if (!fd)
1445                 {
1446                 /* done */
1447                 vf_thumb_cleanup(vf);
1448                 return FALSE;
1449                 }
1450
1451         vf->thumbs_filedata = fd;
1452
1453         thumb_loader_free(vf->thumbs_loader);
1454
1455         vf->thumbs_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
1456         thumb_loader_set_callbacks(vf->thumbs_loader,
1457                                    vf_thumb_done_cb,
1458                                    vf_thumb_error_cb,
1459                                    nullptr,
1460                                    vf);
1461
1462         if (!thumb_loader_start(vf->thumbs_loader, fd))
1463                 {
1464                 /* set icon to unknown, continue */
1465                 DEBUG_1("thumb loader start failed %s", fd->path);
1466                 vf_thumb_do(vf, fd);
1467
1468                 return TRUE;
1469                 }
1470
1471         return FALSE;
1472 }
1473
1474 static void vf_thumb_reset_all(ViewFile *vf)
1475 {
1476         GList *work;
1477
1478         for (work = vf->list; work; work = work->next)
1479                 {
1480                 auto fd = static_cast<FileData *>(work->data);
1481                 if (fd->thumb_pixbuf)
1482                         {
1483                         g_object_unref(fd->thumb_pixbuf);
1484                         fd->thumb_pixbuf = nullptr;
1485                         }
1486                 }
1487 }
1488
1489 void vf_thumb_update(ViewFile *vf)
1490 {
1491         vf_thumb_stop(vf);
1492
1493         if (vf->type == FILEVIEW_LIST && !VFLIST(vf)->thumbs_enabled) return;
1494
1495         vf_thumb_status(vf, 0.0, _("Loading thumbs..."));
1496         vf->thumbs_running = TRUE;
1497
1498         if (thumb_format_changed)
1499                 {
1500                 vf_thumb_reset_all(vf);
1501                 thumb_format_changed = FALSE;
1502                 }
1503
1504         while (vf_thumb_next(vf));
1505 }
1506
1507 void vf_star_cleanup(ViewFile *vf)
1508 {
1509         if (vf->stars_id != 0)
1510                 {
1511                 g_source_remove(vf->stars_id);
1512                 }
1513
1514         vf->stars_id = 0;
1515         vf->stars_filedata = nullptr;
1516 }
1517
1518 void vf_star_stop(ViewFile *vf)
1519 {
1520          vf_star_cleanup(vf);
1521 }
1522
1523 static void vf_set_star_fd(ViewFile *vf, FileData *fd)
1524 {
1525         switch (vf->type)
1526                 {
1527                 case FILEVIEW_LIST: vflist_set_star_fd(vf, fd); break;
1528                 case FILEVIEW_ICON: vficon_set_star_fd(vf, fd); break;
1529                 default: break;
1530                 }
1531 }
1532
1533 static void vf_star_do(ViewFile *vf, FileData *fd)
1534 {
1535         if (!fd) return;
1536
1537         vf_set_star_fd(vf, fd);
1538 }
1539
1540 static gboolean vf_star_next(ViewFile *vf)
1541 {
1542         FileData *fd = nullptr;
1543
1544         switch (vf->type)
1545                 {
1546                 case FILEVIEW_LIST: fd = vflist_star_next_fd(vf); break;
1547                 case FILEVIEW_ICON: fd = vficon_star_next_fd(vf); break;
1548                 default: break;
1549                 }
1550
1551         if (!fd)
1552                 {
1553                 /* done */
1554                 vf_star_cleanup(vf);
1555                 return FALSE;
1556                 }
1557
1558         return TRUE;
1559 }
1560
1561 gboolean vf_stars_cb(gpointer data)
1562 {
1563         auto vf = static_cast<ViewFile *>(data);
1564         FileData *fd = vf->stars_filedata;
1565
1566         if (fd)
1567                 {
1568                 read_rating_data(fd);
1569
1570                 vf_star_do(vf, fd);
1571
1572                 if (vf_star_next(vf))
1573                         {
1574                         return G_SOURCE_CONTINUE;
1575                         }
1576
1577                 vf->stars_filedata = nullptr;
1578                 vf->stars_id = 0;
1579                 return G_SOURCE_REMOVE;
1580                 }
1581
1582         return G_SOURCE_REMOVE;
1583 }
1584
1585 void vf_star_update(ViewFile *vf)
1586 {
1587         vf_star_stop(vf);
1588
1589         if (!options->show_star_rating)
1590                 {
1591                 return;
1592                 }
1593
1594         vf_star_next(vf);
1595 }
1596
1597 void vf_marks_set(ViewFile *vf, gboolean enable)
1598 {
1599         if (vf->marks_enabled == enable) return;
1600
1601         vf->marks_enabled = enable;
1602
1603         switch (vf->type)
1604         {
1605         case FILEVIEW_LIST: vflist_marks_set(vf, enable); break;
1606         case FILEVIEW_ICON: vficon_marks_set(vf, enable); break;
1607         }
1608         if (enable)
1609                 gtk_widget_show(vf->filter);
1610         else
1611                 gtk_widget_hide(vf->filter);
1612
1613         vf_refresh_idle(vf);
1614 }
1615 #pragma GCC diagnostic push
1616 #pragma GCC diagnostic ignored "-Wunused-function"
1617 void vf_star_rating_set_unused(ViewFile *vf, gboolean enable)
1618 {
1619         if (options->show_star_rating == enable) return;
1620         options->show_star_rating = enable;
1621
1622         switch (vf->type)
1623                 {
1624                 case FILEVIEW_LIST: vflist_star_rating_set(vf, enable); break;
1625                 case FILEVIEW_ICON: vficon_star_rating_set(vf, enable); break;
1626                 }
1627         vf_refresh_idle(vf);
1628 }
1629 #pragma GCC diagnostic pop
1630
1631 guint vf_marks_get_filter(ViewFile *vf)
1632 {
1633         guint ret = 0;
1634         gint i;
1635         if (!vf->marks_enabled) return 0;
1636
1637         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
1638                 {
1639                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[i])))
1640                         {
1641                         ret |= 1 << i;
1642                         }
1643                 }
1644         return ret;
1645 }
1646
1647 GRegex *vf_file_filter_get_filter(ViewFile *vf)
1648 {
1649         GRegex *ret = nullptr;
1650         GError *error = nullptr;
1651         gchar *file_filter_text = nullptr;
1652
1653         if (!gtk_widget_get_visible(vf->file_filter.combo))
1654                 {
1655                 return g_regex_new("", static_cast<GRegexCompileFlags>(0), static_cast<GRegexMatchFlags>(0), nullptr);
1656                 }
1657
1658         file_filter_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
1659
1660         if (file_filter_text[0] != '\0')
1661                 {
1662                 ret = g_regex_new(file_filter_text, vf->file_filter.case_sensitive ? static_cast<GRegexCompileFlags>(0) : G_REGEX_CASELESS, static_cast<GRegexMatchFlags>(0), &error);
1663                 if (error)
1664                         {
1665                         log_printf("Error: could not compile regular expression %s\n%s\n", file_filter_text, error->message);
1666                         g_error_free(error);
1667                         error = nullptr;
1668                         ret = g_regex_new("", static_cast<GRegexCompileFlags>(0), static_cast<GRegexMatchFlags>(0), nullptr);
1669                         }
1670                 g_free(file_filter_text);
1671                 }
1672         else
1673                 {
1674                 ret = g_regex_new("", static_cast<GRegexCompileFlags>(0), static_cast<GRegexMatchFlags>(0), nullptr);
1675                 }
1676
1677         return ret;
1678 }
1679
1680 guint vf_class_get_filter(ViewFile *vf)
1681 {
1682         guint ret = 0;
1683         gint i;
1684
1685         if (!gtk_widget_get_visible(vf->file_filter.combo))
1686                 {
1687                 return G_MAXUINT;
1688                 }
1689
1690         for ( i = 0; i < FILE_FORMAT_CLASSES; i++)
1691                 {
1692                 if (options->class_filter[i])
1693                         {
1694                         ret |= 1 << i;
1695                         }
1696                 }
1697
1698         return ret;
1699 }
1700
1701 void vf_set_layout(ViewFile *vf, LayoutWindow *layout)
1702 {
1703         vf->layout = layout;
1704 }
1705
1706
1707 /*
1708  *-----------------------------------------------------------------------------
1709  * maintenance (for rename, move, remove)
1710  *-----------------------------------------------------------------------------
1711  */
1712
1713 static gboolean vf_refresh_idle_cb(gpointer data)
1714 {
1715         auto vf = static_cast<ViewFile *>(data);
1716
1717         vf_refresh(vf);
1718         vf->refresh_idle_id = 0;
1719         return G_SOURCE_REMOVE;
1720 }
1721
1722 void vf_refresh_idle_cancel(ViewFile *vf)
1723 {
1724         if (vf->refresh_idle_id)
1725                 {
1726                 g_source_remove(vf->refresh_idle_id);
1727                 vf->refresh_idle_id = 0;
1728                 }
1729 }
1730
1731
1732 void vf_refresh_idle(ViewFile *vf)
1733 {
1734         if (!vf->refresh_idle_id)
1735                 {
1736                 vf->time_refresh_set = time(nullptr);
1737                 /* file operations run with G_PRIORITY_DEFAULT_IDLE */
1738                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE + 50, vf_refresh_idle_cb, vf, nullptr);
1739                 }
1740         else if (time(nullptr) - vf->time_refresh_set > 1)
1741                 {
1742                 /* more than 1 sec since last update - increase priority */
1743                 vf_refresh_idle_cancel(vf);
1744                 vf->time_refresh_set = time(nullptr);
1745                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 50, vf_refresh_idle_cb, vf, nullptr);
1746                 }
1747 }
1748
1749 void vf_notify_cb(FileData *fd, NotifyType type, gpointer data)
1750 {
1751         auto vf = static_cast<ViewFile *>(data);
1752         gboolean refresh;
1753
1754         auto interested = static_cast<NotifyType>(NOTIFY_CHANGE | NOTIFY_REREAD | NOTIFY_GROUPING);
1755         if (vf->marks_enabled) interested = static_cast<NotifyType>(interested | NOTIFY_MARKS | NOTIFY_METADATA);
1756         /** @FIXME NOTIFY_METADATA should be checked by the keyword-to-mark functions and converted to NOTIFY_MARKS only if there was a change */
1757
1758         if (!(type & interested) || vf->refresh_idle_id || !vf->dir_fd) return;
1759
1760         refresh = (fd == vf->dir_fd);
1761
1762         if (!refresh)
1763                 {
1764                 gchar *base = remove_level_from_path(fd->path);
1765                 refresh = (g_strcmp0(base, vf->dir_fd->path) == 0);
1766                 g_free(base);
1767                 }
1768
1769         if ((type & NOTIFY_CHANGE) && fd->change)
1770                 {
1771                 if (!refresh && fd->change->dest)
1772                         {
1773                         gchar *dest_base = remove_level_from_path(fd->change->dest);
1774                         refresh = (g_strcmp0(dest_base, vf->dir_fd->path) == 0);
1775                         g_free(dest_base);
1776                         }
1777
1778                 if (!refresh && fd->change->source)
1779                         {
1780                         gchar *source_base = remove_level_from_path(fd->change->source);
1781                         refresh = (g_strcmp0(source_base, vf->dir_fd->path) == 0);
1782                         g_free(source_base);
1783                         }
1784                 }
1785
1786         if (refresh)
1787                 {
1788                 DEBUG_1("Notify vf: %s %04x", fd->path, type);
1789                 vf_refresh_idle(vf);
1790                 }
1791 }
1792
1793 static gboolean vf_read_metadata_in_idle_cb(gpointer data)
1794 {
1795         FileData *fd;
1796         auto vf = static_cast<ViewFile *>(data);
1797         GList *work;
1798
1799         vf_thumb_status(vf, vf_read_metadata_in_idle_progress(vf), _("Loading meta..."));
1800
1801         work = vf->list;
1802
1803         while (work)
1804                 {
1805                 fd = static_cast<FileData *>(work->data);
1806
1807                 if (fd && !fd->metadata_in_idle_loaded)
1808                         {
1809                         if (!fd->exifdate)
1810                                 {
1811                                 read_exif_time_data(fd);
1812                                 }
1813                         if (!fd->exifdate_digitized)
1814                                 {
1815                                 read_exif_time_digitized_data(fd);
1816                                 }
1817                         if (fd->rating == STAR_RATING_NOT_READ)
1818                                 {
1819                                 read_rating_data(fd);
1820                                 }
1821                         fd->metadata_in_idle_loaded = TRUE;
1822                         return G_SOURCE_CONTINUE;
1823                         }
1824                 work = work->next;
1825                 }
1826
1827         vf_thumb_status(vf, 0.0, nullptr);
1828         vf->read_metadata_in_idle_id = 0;
1829         vf_refresh(vf);
1830         return G_SOURCE_REMOVE;
1831 }
1832
1833 static void vf_read_metadata_in_idle_finished_cb(gpointer data)
1834 {
1835         auto vf = static_cast<ViewFile *>(data);
1836
1837         vf_thumb_status(vf, 0.0, _("Loading meta..."));
1838         vf->read_metadata_in_idle_id = 0;
1839 }
1840
1841 void vf_read_metadata_in_idle(ViewFile *vf)
1842 {
1843         if (!vf) return;
1844
1845         if (vf->read_metadata_in_idle_id)
1846                 {
1847                 g_idle_remove_by_data(vf);
1848                 }
1849         vf->read_metadata_in_idle_id = 0;
1850
1851         if (vf->list)
1852                 {
1853                 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);
1854                 }
1855 }
1856
1857 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */