Fix #637: Remove unused function vf_index_is_selected
[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 "thumb.h"
32 #include "ui_menu.h"
33 #include "ui_fileops.h"
34 #include "ui_misc.h"
35 #include "utilops.h"
36 #include "view_file/view_file_list.h"
37 #include "view_file/view_file_icon.h"
38 #include "window.h"
39
40 /*
41  *-----------------------------------------------------------------------------
42  * signals
43  *-----------------------------------------------------------------------------
44  */
45
46 void vf_send_update(ViewFile *vf)
47 {
48         if (vf->func_status) vf->func_status(vf, vf->data_status);
49 }
50
51 /*
52  *-----------------------------------------------------------------------------
53  * misc
54  *-----------------------------------------------------------------------------
55  */
56
57 void vf_sort_set(ViewFile *vf, SortType type, gboolean ascend)
58 {
59         switch (vf->type)
60         {
61         case FILEVIEW_LIST: vflist_sort_set(vf, type, ascend); break;
62         case FILEVIEW_ICON: vficon_sort_set(vf, type, ascend); break;
63         }
64 }
65
66 /*
67  *-----------------------------------------------------------------------------
68  * row stuff
69  *-----------------------------------------------------------------------------
70  */
71
72 FileData *vf_index_get_data(ViewFile *vf, gint row)
73 {
74         return g_list_nth_data(vf->list, row);
75 }
76
77 gint vf_index_by_fd(ViewFile *vf, FileData *fd)
78 {
79         switch (vf->type)
80         {
81         case FILEVIEW_LIST: return vflist_index_by_fd(vf, fd);
82         case FILEVIEW_ICON: return vficon_index_by_fd(vf, fd);
83         }
84 }
85
86 guint vf_count(ViewFile *vf, gint64 *bytes)
87 {
88         if (bytes)
89                 {
90                 gint64 b = 0;
91                 GList *work;
92
93                 work = vf->list;
94                 while (work)
95                         {
96                         FileData *fd = work->data;
97                         work = work->next;
98
99                         b += fd->size;
100                         }
101
102                 *bytes = b;
103                 }
104
105         return g_list_length(vf->list);
106 }
107
108 GList *vf_get_list(ViewFile *vf)
109 {
110         GList *list = NULL;
111         GList *work;
112         for (work = vf->list; work; work = work->next)
113                 {
114                 FileData *fd = work->data;
115                 list = g_list_prepend(list, file_data_ref(fd));
116                 }
117
118         return g_list_reverse(list);
119 }
120
121 /*
122  *-------------------------------------------------------------------
123  * keyboard
124  *-------------------------------------------------------------------
125  */
126
127 static gboolean vf_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
128 {
129         ViewFile *vf = data;
130
131         switch (vf->type)
132         {
133         case FILEVIEW_LIST: return vflist_press_key_cb(widget, event, data);
134         case FILEVIEW_ICON: return vficon_press_key_cb(widget, event, data);
135         }
136 }
137
138 /*
139  *-------------------------------------------------------------------
140  * mouse
141  *-------------------------------------------------------------------
142  */
143
144 static gboolean vf_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
145 {
146         ViewFile *vf = data;
147
148         switch (vf->type)
149         {
150         case FILEVIEW_LIST: return vflist_press_cb(widget, bevent, data);
151         case FILEVIEW_ICON: return vficon_press_cb(widget, bevent, data);
152         }
153 }
154
155 static gboolean vf_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
156 {
157         ViewFile *vf = data;
158
159         switch (vf->type)
160         {
161         case FILEVIEW_LIST: return vflist_release_cb(widget, bevent, data);
162         case FILEVIEW_ICON: return vficon_release_cb(widget, bevent, data);
163         }
164 }
165
166
167 /*
168  *-----------------------------------------------------------------------------
169  * selections
170  *-----------------------------------------------------------------------------
171  */
172
173 guint vf_selection_count(ViewFile *vf, gint64 *bytes)
174 {
175         switch (vf->type)
176         {
177         case FILEVIEW_LIST: return vflist_selection_count(vf, bytes);
178         case FILEVIEW_ICON: return vficon_selection_count(vf, bytes);
179         }
180 }
181
182 GList *vf_selection_get_list(ViewFile *vf)
183 {
184         switch (vf->type)
185         {
186         case FILEVIEW_LIST: return vflist_selection_get_list(vf);
187         case FILEVIEW_ICON: return vficon_selection_get_list(vf);
188         }
189 }
190
191 GList *vf_selection_get_list_by_index(ViewFile *vf)
192 {
193         switch (vf->type)
194         {
195         case FILEVIEW_LIST: return vflist_selection_get_list_by_index(vf);
196         case FILEVIEW_ICON: return vficon_selection_get_list_by_index(vf);
197         }
198 }
199
200 void vf_select_all(ViewFile *vf)
201 {
202         switch (vf->type)
203         {
204         case FILEVIEW_LIST: vflist_select_all(vf); break;
205         case FILEVIEW_ICON: vficon_select_all(vf); break;
206         }
207 }
208
209 void vf_select_none(ViewFile *vf)
210 {
211         switch (vf->type)
212         {
213         case FILEVIEW_LIST: vflist_select_none(vf); break;
214         case FILEVIEW_ICON: vficon_select_none(vf); break;
215         }
216 }
217
218 void vf_select_invert(ViewFile *vf)
219 {
220         switch (vf->type)
221         {
222         case FILEVIEW_LIST: vflist_select_invert(vf); break;
223         case FILEVIEW_ICON: vficon_select_invert(vf); break;
224         }
225 }
226
227 void vf_select_by_fd(ViewFile *vf, FileData *fd)
228 {
229         switch (vf->type)
230         {
231         case FILEVIEW_LIST: vflist_select_by_fd(vf, fd); break;
232         case FILEVIEW_ICON: vficon_select_by_fd(vf, fd); break;
233         }
234 }
235
236 void vf_mark_to_selection(ViewFile *vf, gint mark, MarkToSelectionMode mode)
237 {
238         switch (vf->type)
239         {
240         case FILEVIEW_LIST: vflist_mark_to_selection(vf, mark, mode); break;
241         case FILEVIEW_ICON: vficon_mark_to_selection(vf, mark, mode); break;
242         }
243 }
244
245 void vf_selection_to_mark(ViewFile *vf, gint mark, SelectionToMarkMode mode)
246 {
247         switch (vf->type)
248         {
249         case FILEVIEW_LIST: vflist_selection_to_mark(vf, mark, mode); break;
250         case FILEVIEW_ICON: vficon_selection_to_mark(vf, mark, mode); break;
251         }
252 }
253
254 /*
255  *-----------------------------------------------------------------------------
256  * dnd
257  *-----------------------------------------------------------------------------
258  */
259
260
261 static void vf_dnd_init(ViewFile *vf)
262 {
263         switch (vf->type)
264         {
265         case FILEVIEW_LIST: vflist_dnd_init(vf); break;
266         case FILEVIEW_ICON: vficon_dnd_init(vf); break;
267         }
268 }
269
270 /*
271  *-----------------------------------------------------------------------------
272  * pop-up menu
273  *-----------------------------------------------------------------------------
274  */
275
276 GList *vf_pop_menu_file_list(ViewFile *vf)
277 {
278         switch (vf->type)
279         {
280         case FILEVIEW_LIST: return vflist_pop_menu_file_list(vf);
281         case FILEVIEW_ICON: return vficon_pop_menu_file_list(vf);
282         }
283 }
284
285 GList *vf_selection_get_one(ViewFile *vf, FileData *fd)
286 {
287         switch (vf->type)
288         {
289         case FILEVIEW_LIST: return vflist_selection_get_one(vf, fd);
290         case FILEVIEW_ICON: return vficon_selection_get_one(vf, fd);
291         }
292 }
293
294 static void vf_pop_menu_edit_cb(GtkWidget *widget, gpointer data)
295 {
296         ViewFile *vf;
297         const gchar *key = data;
298
299         vf = submenu_item_get_data(widget);
300
301         if (!vf) return;
302
303         file_util_start_editor_from_filelist(key, vf_pop_menu_file_list(vf), vf->dir_fd->path, vf->listview);
304 }
305
306 static void vf_pop_menu_view_cb(GtkWidget *widget, gpointer data)
307 {
308         ViewFile *vf = data;
309
310         switch (vf->type)
311         {
312         case FILEVIEW_LIST: vflist_pop_menu_view_cb(widget, data); break;
313         case FILEVIEW_ICON: vficon_pop_menu_view_cb(widget, data); break;
314         }
315 }
316
317 static void vf_pop_menu_copy_cb(GtkWidget *widget, gpointer data)
318 {
319         ViewFile *vf = data;
320
321         file_util_copy(NULL, vf_pop_menu_file_list(vf), NULL, vf->listview);
322 }
323
324 static void vf_pop_menu_move_cb(GtkWidget *widget, gpointer data)
325 {
326         ViewFile *vf = data;
327
328         file_util_move(NULL, vf_pop_menu_file_list(vf), NULL, vf->listview);
329 }
330
331 static void vf_pop_menu_rename_cb(GtkWidget *widget, gpointer data)
332 {
333         ViewFile *vf = data;
334
335         switch (vf->type)
336         {
337         case FILEVIEW_LIST: vflist_pop_menu_rename_cb(widget, data); break;
338         case FILEVIEW_ICON: vficon_pop_menu_rename_cb(widget, data); break;
339         }
340 }
341
342 static void vf_pop_menu_delete_cb(GtkWidget *widget, gpointer data)
343 {
344         ViewFile *vf = data;
345
346         file_util_delete(NULL, vf_pop_menu_file_list(vf), vf->listview);
347 }
348
349 static void vf_pop_menu_copy_path_cb(GtkWidget *widget, gpointer data)
350 {
351         ViewFile *vf = data;
352
353         file_util_copy_path_list_to_clipboard(vf_pop_menu_file_list(vf), TRUE);
354 }
355
356 static void vf_pop_menu_copy_path_unquoted_cb(GtkWidget *widget, gpointer data)
357 {
358         ViewFile *vf = data;
359
360         file_util_copy_path_list_to_clipboard(vf_pop_menu_file_list(vf), FALSE);
361 }
362
363 static void vf_pop_menu_enable_grouping_cb(GtkWidget *widget, gpointer data)
364 {
365         ViewFile *vf = data;
366
367         file_data_disable_grouping_list(vf_pop_menu_file_list(vf), FALSE);
368 }
369
370 static void vf_pop_menu_duplicates_cb(GtkWidget *widget, gpointer data)
371 {
372         ViewFile *vf = data;
373         DupeWindow *dw;
374
375         dw = dupe_window_new();
376         dupe_window_add_files(dw, vf_pop_menu_file_list(vf), FALSE);
377 }
378
379 static void vf_pop_menu_disable_grouping_cb(GtkWidget *widget, gpointer data)
380 {
381         ViewFile *vf = data;
382
383         file_data_disable_grouping_list(vf_pop_menu_file_list(vf), TRUE);
384 }
385
386 static void vf_pop_menu_sort_cb(GtkWidget *widget, gpointer data)
387 {
388         ViewFile *vf;
389         SortType type;
390
391         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
392
393         vf = submenu_item_get_data(widget);
394         if (!vf) return;
395
396         type = (SortType)GPOINTER_TO_INT(data);
397
398         if (type == SORT_EXIFTIME || type == SORT_EXIFTIMEDIGITIZED || type == SORT_RATING)
399                 {
400                 vf_read_metadata_in_idle(vf);
401                 }
402
403         if (vf->layout)
404                 {
405                 layout_sort_set(vf->layout, type, vf->sort_ascend);
406                 }
407         else
408                 {
409                 vf_sort_set(vf, type, vf->sort_ascend);
410                 }
411 }
412
413 static void vf_pop_menu_sort_ascend_cb(GtkWidget *widget, gpointer data)
414 {
415         ViewFile *vf = data;
416
417         if (vf->layout)
418                 {
419                 layout_sort_set(vf->layout, vf->sort_method, !vf->sort_ascend);
420                 }
421         else
422                 {
423                 vf_sort_set(vf, vf->sort_method, !vf->sort_ascend);
424                 }
425 }
426
427 static void vf_pop_menu_sel_mark_cb(GtkWidget *widget, gpointer data)
428 {
429         ViewFile *vf = data;
430         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_SET);
431 }
432
433 static void vf_pop_menu_sel_mark_and_cb(GtkWidget *widget, gpointer data)
434 {
435         ViewFile *vf = data;
436         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_AND);
437 }
438
439 static void vf_pop_menu_sel_mark_or_cb(GtkWidget *widget, gpointer data)
440 {
441         ViewFile *vf = data;
442         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_OR);
443 }
444
445 static void vf_pop_menu_sel_mark_minus_cb(GtkWidget *widget, gpointer data)
446 {
447         ViewFile *vf = data;
448         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_MINUS);
449 }
450
451 static void vf_pop_menu_set_mark_sel_cb(GtkWidget *widget, gpointer data)
452 {
453         ViewFile *vf = data;
454         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_SET);
455 }
456
457 static void vf_pop_menu_res_mark_sel_cb(GtkWidget *widget, gpointer data)
458 {
459         ViewFile *vf = data;
460         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_RESET);
461 }
462
463 static void vf_pop_menu_toggle_mark_sel_cb(GtkWidget *widget, gpointer data)
464 {
465         ViewFile *vf = data;
466         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_TOGGLE);
467 }
468
469 static void vf_pop_menu_toggle_view_type_cb(GtkWidget *widget, gpointer data)
470 {
471         ViewFile *vf = data;
472         FileViewType new_type = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
473         if (!vf->layout) return;
474
475         layout_views_set(vf->layout, vf->layout->options.dir_view_type, new_type);
476 }
477
478 static void vf_pop_menu_toggle_star_rating(ViewFile *vf)
479 {
480         GtkAllocation allocation;
481
482         options->show_star_rating = !options->show_star_rating;
483
484         gtk_widget_get_allocation(vf->listview, &allocation);
485         vf_star_rating_set(vf, options->show_star_rating);
486 }
487
488 static void vf_pop_menu_show_star_rating_cb(GtkWidget *widget, gpointer data)
489 {
490         ViewFile *vf = data;
491
492         vf_pop_menu_toggle_star_rating(vf);
493 }
494
495 static void vf_pop_menu_refresh_cb(GtkWidget *widget, gpointer data)
496 {
497         ViewFile *vf = data;
498
499         switch (vf->type)
500         {
501         case FILEVIEW_LIST: vflist_pop_menu_refresh_cb(widget, data); break;
502         case FILEVIEW_ICON: vficon_pop_menu_refresh_cb(widget, data); break;
503         }
504 }
505
506 static void vf_popup_destroy_cb(GtkWidget *widget, gpointer data)
507 {
508         ViewFile *vf = data;
509
510         switch (vf->type)
511         {
512         case FILEVIEW_LIST: vflist_popup_destroy_cb(widget, data); break;
513         case FILEVIEW_ICON: vficon_popup_destroy_cb(widget, data); break;
514         }
515
516         filelist_free(vf->editmenu_fd_list);
517         vf->editmenu_fd_list = NULL;
518 }
519
520 /**
521  * @brief Add file selection list to a collection
522  * @param[in] widget 
523  * @param[in] data Index to the collection list menu item selected, or -1 for new collection
524  * 
525  * 
526  */
527 static void vf_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
528 {
529         ViewFile *vf;
530         GList *selection_list;
531
532         vf = submenu_item_get_data(widget);
533         selection_list = vf_selection_get_list(vf);
534         pop_menu_collections(selection_list, data);
535
536         filelist_free(selection_list);
537 }
538
539 GtkWidget *vf_pop_menu(ViewFile *vf)
540 {
541         GtkWidget *menu;
542         GtkWidget *item;
543         GtkWidget *submenu;
544         gboolean active = FALSE;
545
546         switch (vf->type)
547         {
548         case FILEVIEW_LIST:
549                 vflist_color_set(vf, VFLIST(vf)->click_fd, TRUE);
550                 active = (VFLIST(vf)->click_fd != NULL);
551                 break;
552         case FILEVIEW_ICON:
553                 active = (VFICON(vf)->click_fd != NULL);
554                 break;
555         }
556
557         menu = popup_menu_short_lived();
558
559         g_signal_connect(G_OBJECT(menu), "destroy",
560                          G_CALLBACK(vf_popup_destroy_cb), vf);
561
562         if (vf->clicked_mark > 0)
563                 {
564                 gint mark = vf->clicked_mark;
565                 gchar *str_set_mark = g_strdup_printf(_("_Set mark %d"), mark);
566                 gchar *str_res_mark = g_strdup_printf(_("_Reset mark %d"), mark);
567                 gchar *str_toggle_mark = g_strdup_printf(_("_Toggle mark %d"), mark);
568                 gchar *str_sel_mark = g_strdup_printf(_("_Select mark %d"), mark);
569                 gchar *str_sel_mark_or = g_strdup_printf(_("_Add mark %d"), mark);
570                 gchar *str_sel_mark_and = g_strdup_printf(_("_Intersection with mark %d"), mark);
571                 gchar *str_sel_mark_minus = g_strdup_printf(_("_Unselect mark %d"), mark);
572
573                 g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
574
575                 vf->active_mark = mark;
576                 vf->clicked_mark = 0;
577
578                 menu_item_add_sensitive(menu, str_set_mark, active,
579                                         G_CALLBACK(vf_pop_menu_set_mark_sel_cb), vf);
580
581                 menu_item_add_sensitive(menu, str_res_mark, active,
582                                         G_CALLBACK(vf_pop_menu_res_mark_sel_cb), vf);
583
584                 menu_item_add_sensitive(menu, str_toggle_mark, active,
585                                         G_CALLBACK(vf_pop_menu_toggle_mark_sel_cb), vf);
586
587                 menu_item_add_divider(menu);
588
589                 menu_item_add_sensitive(menu, str_sel_mark, active,
590                                         G_CALLBACK(vf_pop_menu_sel_mark_cb), vf);
591                 menu_item_add_sensitive(menu, str_sel_mark_or, active,
592                                         G_CALLBACK(vf_pop_menu_sel_mark_or_cb), vf);
593                 menu_item_add_sensitive(menu, str_sel_mark_and, active,
594                                         G_CALLBACK(vf_pop_menu_sel_mark_and_cb), vf);
595                 menu_item_add_sensitive(menu, str_sel_mark_minus, active,
596                                         G_CALLBACK(vf_pop_menu_sel_mark_minus_cb), vf);
597
598                 menu_item_add_divider(menu);
599
600                 g_free(str_set_mark);
601                 g_free(str_res_mark);
602                 g_free(str_toggle_mark);
603                 g_free(str_sel_mark);
604                 g_free(str_sel_mark_and);
605                 g_free(str_sel_mark_or);
606                 g_free(str_sel_mark_minus);
607                 }
608
609         vf->editmenu_fd_list = vf_pop_menu_file_list(vf);
610         submenu_add_edit(menu, &item, G_CALLBACK(vf_pop_menu_edit_cb), vf, vf->editmenu_fd_list);
611         gtk_widget_set_sensitive(item, active);
612
613         menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, active,
614                                       G_CALLBACK(vf_pop_menu_view_cb), vf);
615
616         menu_item_add_divider(menu);
617         menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, active,
618                                       G_CALLBACK(vf_pop_menu_copy_cb), vf);
619         menu_item_add_sensitive(menu, _("_Move..."), active,
620                                 G_CALLBACK(vf_pop_menu_move_cb), vf);
621         menu_item_add_sensitive(menu, _("_Rename..."), active,
622                                 G_CALLBACK(vf_pop_menu_rename_cb), vf);
623         menu_item_add_sensitive(menu, _("_Copy path"), active,
624                                 G_CALLBACK(vf_pop_menu_copy_path_cb), vf);
625         menu_item_add_sensitive(menu, _("_Copy path unquoted"), active,
626                                 G_CALLBACK(vf_pop_menu_copy_path_unquoted_cb), vf);
627         menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, active,
628                                       G_CALLBACK(vf_pop_menu_delete_cb), vf);
629         menu_item_add_divider(menu);
630
631         menu_item_add_sensitive(menu, _("Enable file _grouping"), active,
632                                 G_CALLBACK(vf_pop_menu_enable_grouping_cb), vf);
633         menu_item_add_sensitive(menu, _("Disable file groupi_ng"), active,
634                                 G_CALLBACK(vf_pop_menu_disable_grouping_cb), vf);
635
636         menu_item_add_divider(menu);
637         menu_item_add_stock_sensitive(menu, _("_Find duplicates..."), GTK_STOCK_FIND, active,
638                                 G_CALLBACK(vf_pop_menu_duplicates_cb), vf);
639         menu_item_add_divider(menu);
640
641         submenu = submenu_add_collections(menu, &item,
642                                 G_CALLBACK(vf_pop_menu_collections_cb), vf);
643         gtk_widget_set_sensitive(item, active);
644         menu_item_add_divider(menu);
645
646         submenu = submenu_add_sort(NULL, G_CALLBACK(vf_pop_menu_sort_cb), vf,
647                                    FALSE, FALSE, TRUE, vf->sort_method);
648         menu_item_add_divider(submenu);
649         menu_item_add_check(submenu, _("Ascending"), vf->sort_ascend,
650                             G_CALLBACK(vf_pop_menu_sort_ascend_cb), vf);
651
652         item = menu_item_add(menu, _("_Sort"), NULL, NULL);
653         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
654
655         item = menu_item_add_radio(menu, _("View as _List"), GINT_TO_POINTER(FILEVIEW_LIST), vf->type == FILEVIEW_LIST,
656                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
657
658         item = menu_item_add_radio(menu, _("View as _Icons"), GINT_TO_POINTER(FILEVIEW_ICON), vf->type == FILEVIEW_ICON,
659                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
660
661         switch (vf->type)
662         {
663         case FILEVIEW_LIST:
664                 menu_item_add_check(menu, _("Show _thumbnails"), VFLIST(vf)->thumbs_enabled,
665                                     G_CALLBACK(vflist_pop_menu_thumbs_cb), vf);
666                 break;
667         case FILEVIEW_ICON:
668                 menu_item_add_check(menu, _("Show filename _text"), VFICON(vf)->show_text,
669                                     G_CALLBACK(vficon_pop_menu_show_names_cb), vf);
670                 break;
671         }
672
673         switch (vf->type)
674         {
675         case FILEVIEW_LIST:
676                 menu_item_add_check(menu, _("Show star rating"), options->show_star_rating,
677                                     G_CALLBACK(vflist_pop_menu_show_star_rating_cb), vf);
678                 break;
679         case FILEVIEW_ICON:
680                 menu_item_add_check(menu, _("Show star rating"), options->show_star_rating,
681                                     G_CALLBACK(vficon_pop_menu_show_star_rating_cb), vf);
682                 break;
683         }
684
685         menu_item_add_stock(menu, _("Re_fresh"), GTK_STOCK_REFRESH, G_CALLBACK(vf_pop_menu_refresh_cb), vf);
686
687         return menu;
688 }
689
690 gboolean vf_refresh(ViewFile *vf)
691 {
692         switch (vf->type)
693         {
694         case FILEVIEW_LIST: return vflist_refresh(vf);
695         case FILEVIEW_ICON: return vficon_refresh(vf);
696         }
697 }
698
699 gboolean vf_set_fd(ViewFile *vf, FileData *dir_fd)
700 {
701         switch (vf->type)
702         {
703         case FILEVIEW_LIST: return vflist_set_fd(vf, dir_fd);
704         case FILEVIEW_ICON: return vficon_set_fd(vf, dir_fd);
705         }
706 }
707
708 static void vf_destroy_cb(GtkWidget *widget, gpointer data)
709 {
710         ViewFile *vf = data;
711
712         switch (vf->type)
713         {
714         case FILEVIEW_LIST: vflist_destroy_cb(widget, data); break;
715         case FILEVIEW_ICON: vficon_destroy_cb(widget, data); break;
716         }
717
718         if (vf->popup)
719                 {
720                 g_signal_handlers_disconnect_matched(G_OBJECT(vf->popup), G_SIGNAL_MATCH_DATA,
721                                                      0, 0, 0, NULL, vf);
722                 gtk_widget_destroy(vf->popup);
723                 }
724
725         if (vf->read_metadata_in_idle_id)
726                 {
727                 g_idle_remove_by_data(vf);
728                 }
729         file_data_unref(vf->dir_fd);
730         g_free(vf->info);
731         g_free(vf);
732 }
733
734 static void vf_marks_filter_toggle_cb(GtkWidget *widget, gpointer data)
735 {
736         ViewFile *vf = data;
737         vf_refresh_idle(vf);
738 }
739
740 typedef struct _MarksTextEntry MarksTextEntry;
741 struct _MarksTextEntry {
742         GenericDialog *gd;
743         gint mark_no;
744         GtkWidget *edit_widget;
745         gchar *text_entry;
746         GtkWidget *parent;
747 };
748
749 static void vf_marks_tooltip_cancel_cb(GenericDialog *gd, gpointer data)
750 {
751         MarksTextEntry *mte = data;
752
753         g_free(mte->text_entry);
754         generic_dialog_close(gd);
755 }
756
757 static void vf_marks_tooltip_ok_cb(GenericDialog *gd, gpointer data)
758 {
759         MarksTextEntry *mte = data;
760
761         g_free(options->marks_tooltips[mte->mark_no]);
762         options->marks_tooltips[mte->mark_no] = g_strdup(gtk_entry_get_text(GTK_ENTRY(mte->edit_widget)));
763
764         gtk_widget_set_tooltip_text(mte->parent, options->marks_tooltips[mte->mark_no]);
765
766         g_free(mte->text_entry);
767         generic_dialog_close(gd);
768 }
769
770 void vf_marks_filter_on_icon_press(GtkEntry *entry, GtkEntryIconPosition pos,
771                                                                         GdkEvent *event, gpointer userdata)
772 {
773         MarksTextEntry *mte = userdata;
774
775         g_free(mte->text_entry);
776         mte->text_entry = g_strdup("");
777         gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), "");
778 }
779
780 static void vf_marks_tooltip_help_cb(GenericDialog *gd, gpointer data)
781 {
782         help_window_show("GuideImageMarks.html");
783 }
784
785 static gboolean vf_marks_tooltip_cb(GtkWidget *widget,
786                                                                                 GdkEventButton *event,
787                                                                                 gpointer user_data)
788 {
789         GtkWidget *table;
790         gint i = GPOINTER_TO_INT(user_data);
791         MarksTextEntry *mte;
792
793         if (event->button == MOUSE_BUTTON_RIGHT)
794                 {
795                 mte = g_new0(MarksTextEntry, 1);
796                 mte->mark_no = i;
797                 mte->text_entry = g_strdup(options->marks_tooltips[i]);
798                 mte->parent = widget;
799
800                 mte->gd = generic_dialog_new(_("Mark text"), "mark_text",
801                                                 widget, FALSE,
802                                                 vf_marks_tooltip_cancel_cb, mte);
803                 generic_dialog_add_message(mte->gd, GTK_STOCK_DIALOG_QUESTION, _("Set mark text"),
804                                             _("This will set or clear the mark text."), FALSE);
805                 generic_dialog_add_button(mte->gd, GTK_STOCK_OK, NULL,
806                                                         vf_marks_tooltip_ok_cb, TRUE);
807                 generic_dialog_add_button(mte->gd, GTK_STOCK_HELP, NULL,
808                                                 vf_marks_tooltip_help_cb, FALSE);
809
810                 table = pref_table_new(mte->gd->vbox, 3, 1, FALSE, TRUE);
811                 pref_table_label(table, 0, 0, g_strdup_printf("%s%d", _("Mark "), mte->mark_no + 1), 1.0);
812                 mte->edit_widget = gtk_entry_new();
813                 gtk_widget_set_size_request(mte->edit_widget, 300, -1);
814                 if (mte->text_entry)
815                         {
816                         gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), mte->text_entry);
817                         }
818                 gtk_table_attach_defaults(GTK_TABLE(table), mte->edit_widget, 1, 2, 0, 1);
819                 generic_dialog_attach_default(mte->gd, mte->edit_widget);
820
821                 gtk_entry_set_icon_from_stock(GTK_ENTRY(mte->edit_widget),
822                                                         GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
823                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY(mte->edit_widget),
824                                                         GTK_ENTRY_ICON_SECONDARY, "Clear");
825                 g_signal_connect(GTK_ENTRY(mte->edit_widget), "icon-press",
826                                                         G_CALLBACK(vf_marks_filter_on_icon_press), mte);
827
828                 gtk_widget_show(mte->edit_widget);
829                 gtk_widget_grab_focus(mte->edit_widget);
830                 gtk_widget_show(GTK_WIDGET(mte->gd->dialog));
831
832                 return TRUE;
833                 }
834
835         return FALSE;
836 }
837
838 static void vf_file_filter_save_cb(GtkWidget *widget, gpointer data)
839 {
840         ViewFile *vf = data;
841         gchar *entry_text;
842         gchar *remove_text = NULL;
843         gchar *index_text = NULL;
844         gboolean text_found = FALSE;
845         gint i;
846
847         entry_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)))));
848
849         if (entry_text[0] == '\0' && vf->file_filter.last_selected >= 0)
850                 {
851                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), vf->file_filter.last_selected);
852                 remove_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
853                 history_list_item_remove("file_filter", remove_text);
854                 gtk_combo_box_text_remove(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), vf->file_filter.last_selected);
855                 g_free(remove_text);
856
857                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), -1);
858                 vf->file_filter.last_selected = - 1;
859                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo))), "");
860                 vf->file_filter.count--;
861                 }
862         else
863                 {
864                 if (entry_text[0] != '\0')
865                         {
866                         for (i = 0; i < vf->file_filter.count; i++)
867                                 {
868                                 if (index_text)
869                                         {
870                                         g_free(index_text);
871                                         }
872                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), i);
873                                 index_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
874
875                                 if (g_strcmp0(index_text, entry_text) == 0)
876                                         {
877                                         text_found = TRUE;
878                                         break;
879                                         }
880                                 }
881
882                         g_free(index_text);
883                         if (!text_found)
884                                 {
885                                 history_list_add_to_key("file_filter", entry_text, 10);
886                                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), entry_text);
887                                 vf->file_filter.count++;
888                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), vf->file_filter.count - 1);
889                                 }
890                         }
891                 }
892         vf_refresh(vf);
893
894         g_free(entry_text);
895 }
896
897 static void vf_file_filter_cb(GtkWidget *widget, gpointer data)
898 {
899         ViewFile *vf = data;
900
901         vf_refresh(vf);
902 }
903
904 static gboolean vf_file_filter_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
905 {
906         ViewFile *vf = data;
907         vf->file_filter.last_selected = gtk_combo_box_get_active(GTK_COMBO_BOX(vf->file_filter.combo));
908
909         gtk_widget_grab_focus(widget);
910
911         return TRUE;
912 }
913
914 static GtkWidget *vf_marks_filter_init(ViewFile *vf)
915 {
916         GtkWidget *frame = gtk_frame_new(NULL);
917         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
918
919         gint i;
920
921         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
922                 {
923                 GtkWidget *check = gtk_check_button_new();
924                 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, FALSE, 0);
925                 g_signal_connect(G_OBJECT(check), "toggled",
926                          G_CALLBACK(vf_marks_filter_toggle_cb), vf);
927                 g_signal_connect(G_OBJECT(check), "button_press_event",
928                          G_CALLBACK(vf_marks_tooltip_cb), GINT_TO_POINTER(i));
929                 gtk_widget_set_tooltip_text(check, options->marks_tooltips[i]);
930
931                 gtk_widget_show(check);
932                 vf->filter_check[i] = check;
933                 }
934         gtk_container_add(GTK_CONTAINER(frame), hbox);
935         gtk_widget_show(hbox);
936         return frame;
937 }
938
939 void vf_file_filter_set(ViewFile *vf, gboolean enable)
940 {
941         if (enable)
942                 {
943                 gtk_widget_show(vf->file_filter.combo);
944                 gtk_widget_show(vf->file_filter.frame);
945                 }
946         else
947                 {
948                 gtk_widget_hide(vf->file_filter.combo);
949                 gtk_widget_hide(vf->file_filter.frame);
950                 }
951
952         vf_refresh(vf);
953 }
954
955 static GtkWidget *vf_file_filter_init(ViewFile *vf)
956 {
957         GtkWidget *frame = gtk_frame_new(NULL);
958         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
959         GList *work;
960         gint n = 0;
961         GtkWidget *combo_entry;
962
963         vf->file_filter.combo = gtk_combo_box_text_new_with_entry();
964         combo_entry = gtk_bin_get_child(GTK_BIN(vf->file_filter.combo));
965         gtk_widget_show(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)));
966         gtk_widget_show((GTK_WIDGET(vf->file_filter.combo)));
967         gtk_widget_set_tooltip_text(GTK_WIDGET(vf->file_filter.combo), "Use regular expressions");
968
969         work = history_list_get_by_key("file_filter");
970         while (work)
971                 {
972                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), (gchar *)work->data);
973                 work = work->next;
974                 n++;
975                 vf->file_filter.count = n;
976                 }
977         gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), 0);
978
979         g_signal_connect(G_OBJECT(combo_entry), "activate",
980                 G_CALLBACK(vf_file_filter_save_cb), vf);
981                 
982         g_signal_connect(G_OBJECT(vf->file_filter.combo), "changed",
983                 G_CALLBACK(vf_file_filter_cb), vf);
984
985         g_signal_connect(G_OBJECT(combo_entry), "button_press_event",
986                          G_CALLBACK(vf_file_filter_press_cb), vf);
987
988         gtk_box_pack_start(GTK_BOX(hbox), vf->file_filter.combo, FALSE, FALSE, 0);
989         gtk_widget_show(vf->file_filter.combo);
990         gtk_container_add(GTK_CONTAINER(frame), hbox);
991         gtk_widget_show(hbox);
992
993         return frame;
994 }
995
996 void vf_mark_filter_toggle(ViewFile *vf, gint mark)
997 {
998         gint n = mark - 1;
999         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vf->filter_check[n]),
1000                                      !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[n])));
1001 }
1002
1003 ViewFile *vf_new(FileViewType type, FileData *dir_fd)
1004 {
1005         ViewFile *vf;
1006
1007         vf = g_new0(ViewFile, 1);
1008
1009         vf->type = type;
1010         vf->sort_method = SORT_NAME;
1011         vf->sort_ascend = TRUE;
1012         vf->read_metadata_in_idle_id = 0;
1013
1014         vf->scrolled = gtk_scrolled_window_new(NULL, NULL);
1015         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vf->scrolled), GTK_SHADOW_IN);
1016         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vf->scrolled),
1017                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1018
1019         vf->filter = vf_marks_filter_init(vf);
1020         vf->file_filter.frame = vf_file_filter_init(vf);
1021
1022         vf->widget = gtk_vbox_new(FALSE, 0);
1023         gtk_box_pack_start(GTK_BOX(vf->widget), vf->filter, FALSE, FALSE, 0);
1024         gtk_box_pack_start(GTK_BOX(vf->widget), vf->file_filter.frame, FALSE, FALSE, 0);
1025         gtk_box_pack_start(GTK_BOX(vf->widget), vf->scrolled, TRUE, TRUE, 0);
1026         gtk_widget_show(vf->scrolled);
1027
1028         g_signal_connect(G_OBJECT(vf->widget), "destroy",
1029                          G_CALLBACK(vf_destroy_cb), vf);
1030
1031         switch (type)
1032         {
1033         case FILEVIEW_LIST: vf = vflist_new(vf, dir_fd); break;
1034         case FILEVIEW_ICON: vf = vficon_new(vf, dir_fd); break;
1035         }
1036
1037         vf_dnd_init(vf);
1038
1039         g_signal_connect(G_OBJECT(vf->listview), "key_press_event",
1040                          G_CALLBACK(vf_press_key_cb), vf);
1041         g_signal_connect(G_OBJECT(vf->listview), "button_press_event",
1042                          G_CALLBACK(vf_press_cb), vf);
1043         g_signal_connect(G_OBJECT(vf->listview), "button_release_event",
1044                          G_CALLBACK(vf_release_cb), vf);
1045
1046         gtk_container_add(GTK_CONTAINER(vf->scrolled), vf->listview);
1047         gtk_widget_show(vf->listview);
1048
1049         if (dir_fd) vf_set_fd(vf, dir_fd);
1050
1051         return vf;
1052 }
1053
1054 void vf_set_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gpointer data), gpointer data)
1055 {
1056         vf->func_status = func;
1057         vf->data_status = data;
1058 }
1059
1060 void vf_set_thumb_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gdouble val, const gchar *text, gpointer data), gpointer data)
1061 {
1062         vf->func_thumb_status = func;
1063         vf->data_thumb_status = data;
1064 }
1065
1066 void vf_thumb_set(ViewFile *vf, gboolean enable)
1067 {
1068         switch (vf->type)
1069         {
1070         case FILEVIEW_LIST: vflist_thumb_set(vf, enable); break;
1071         case FILEVIEW_ICON: /*vficon_thumb_set(vf, enable);*/ break;
1072         }
1073 }
1074
1075
1076 static gboolean vf_thumb_next(ViewFile *vf);
1077
1078 static gdouble vf_thumb_progress(ViewFile *vf)
1079 {
1080         gint count = 0;
1081         gint done = 0;
1082
1083         switch (vf->type)
1084         {
1085         case FILEVIEW_LIST: vflist_thumb_progress_count(vf->list, &count, &done); break;
1086         case FILEVIEW_ICON: vficon_thumb_progress_count(vf->list, &count, &done); break;
1087         }
1088
1089         DEBUG_1("thumb progress: %d of %d", done, count);
1090         return (gdouble)done / count;
1091 }
1092
1093 static gdouble vf_read_metadata_in_idle_progress(ViewFile *vf)
1094 {
1095         gint count = 0;
1096         gint done = 0;
1097
1098         switch (vf->type)
1099                 {
1100                 case FILEVIEW_LIST: vflist_read_metadata_progress_count(vf->list, &count, &done); break;
1101                 case FILEVIEW_ICON: vficon_read_metadata_progress_count(vf->list, &count, &done); break;
1102                 }
1103
1104         return (gdouble)done / count;
1105 }
1106
1107 static void vf_set_thumb_fd(ViewFile *vf, FileData *fd)
1108 {
1109         switch (vf->type)
1110         {
1111         case FILEVIEW_LIST: vflist_set_thumb_fd(vf, fd); break;
1112         case FILEVIEW_ICON: vficon_set_thumb_fd(vf, fd); break;
1113         }
1114 }
1115
1116 static void vf_thumb_status(ViewFile *vf, gdouble val, const gchar *text)
1117 {
1118         if (vf->func_thumb_status)
1119                 {
1120                 vf->func_thumb_status(vf, val, text, vf->data_thumb_status);
1121                 }
1122 }
1123
1124 static void vf_thumb_do(ViewFile *vf, FileData *fd)
1125 {
1126         if (!fd) return;
1127
1128         vf_set_thumb_fd(vf, fd);
1129         vf_thumb_status(vf, vf_thumb_progress(vf), _("Loading thumbs..."));
1130 }
1131
1132 void vf_thumb_cleanup(ViewFile *vf)
1133 {
1134         vf_thumb_status(vf, 0.0, NULL);
1135
1136         vf->thumbs_running = FALSE;
1137
1138         thumb_loader_free(vf->thumbs_loader);
1139         vf->thumbs_loader = NULL;
1140
1141         vf->thumbs_filedata = NULL;
1142 }
1143
1144 void vf_thumb_stop(ViewFile *vf)
1145 {
1146         if (vf->thumbs_running) vf_thumb_cleanup(vf);
1147 }
1148
1149 static void vf_thumb_common_cb(ThumbLoader *tl, gpointer data)
1150 {
1151         ViewFile *vf = data;
1152
1153         if (vf->thumbs_filedata && vf->thumbs_loader == tl)
1154                 {
1155                 vf_thumb_do(vf, vf->thumbs_filedata);
1156                 }
1157
1158         while (vf_thumb_next(vf));
1159 }
1160
1161 static void vf_thumb_error_cb(ThumbLoader *tl, gpointer data)
1162 {
1163         vf_thumb_common_cb(tl, data);
1164 }
1165
1166 static void vf_thumb_done_cb(ThumbLoader *tl, gpointer data)
1167 {
1168         vf_thumb_common_cb(tl, data);
1169 }
1170
1171 static gboolean vf_thumb_next(ViewFile *vf)
1172 {
1173         FileData *fd = NULL;
1174
1175         if (!gtk_widget_get_realized(vf->listview))
1176                 {
1177                 vf_thumb_status(vf, 0.0, NULL);
1178                 return FALSE;
1179                 }
1180
1181         switch (vf->type)
1182         {
1183         case FILEVIEW_LIST: fd = vflist_thumb_next_fd(vf); break;
1184         case FILEVIEW_ICON: fd = vficon_thumb_next_fd(vf); break;
1185         }
1186
1187         if (!fd)
1188                 {
1189                 /* done */
1190                 vf_thumb_cleanup(vf);
1191                 return FALSE;
1192                 }
1193
1194         vf->thumbs_filedata = fd;
1195
1196         thumb_loader_free(vf->thumbs_loader);
1197
1198         vf->thumbs_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
1199         thumb_loader_set_callbacks(vf->thumbs_loader,
1200                                    vf_thumb_done_cb,
1201                                    vf_thumb_error_cb,
1202                                    NULL,
1203                                    vf);
1204
1205         if (!thumb_loader_start(vf->thumbs_loader, fd))
1206                 {
1207                 /* set icon to unknown, continue */
1208                 DEBUG_1("thumb loader start failed %s", fd->path);
1209                 vf_thumb_do(vf, fd);
1210
1211                 return TRUE;
1212                 }
1213
1214         return FALSE;
1215 }
1216
1217 static void vf_thumb_reset_all(ViewFile *vf)
1218 {
1219         GList *work;
1220
1221         for (work = vf->list; work; work = work->next)
1222                 {
1223                 FileData *fd = work->data;
1224                 if (fd->thumb_pixbuf)
1225                         {
1226                         g_object_unref(fd->thumb_pixbuf);
1227                         fd->thumb_pixbuf = NULL;
1228                         }
1229                 }
1230 }
1231
1232 void vf_thumb_update(ViewFile *vf)
1233 {
1234         vf_thumb_stop(vf);
1235
1236         if (vf->type == FILEVIEW_LIST && !VFLIST(vf)->thumbs_enabled) return;
1237
1238         vf_thumb_status(vf, 0.0, _("Loading thumbs..."));
1239         vf->thumbs_running = TRUE;
1240
1241         if (thumb_format_changed)
1242                 {
1243                 vf_thumb_reset_all(vf);
1244                 thumb_format_changed = FALSE;
1245                 }
1246
1247         while (vf_thumb_next(vf));
1248 }
1249
1250
1251 void vf_marks_set(ViewFile *vf, gboolean enable)
1252 {
1253         if (vf->marks_enabled == enable) return;
1254
1255         vf->marks_enabled = enable;
1256
1257         switch (vf->type)
1258         {
1259         case FILEVIEW_LIST: vflist_marks_set(vf, enable); break;
1260         case FILEVIEW_ICON: vficon_marks_set(vf, enable); break;
1261         }
1262         if (enable)
1263                 gtk_widget_show(vf->filter);
1264         else
1265                 gtk_widget_hide(vf->filter);
1266
1267         vf_refresh_idle(vf);
1268 }
1269
1270 void vf_star_rating_set(ViewFile *vf, gboolean enable)
1271 {
1272         if (options->show_star_rating == enable) return;
1273         options->show_star_rating = enable;
1274
1275         switch (vf->type)
1276                 {
1277                 case FILEVIEW_LIST: vflist_star_rating_set(vf, enable); break;
1278                 case FILEVIEW_ICON: vficon_star_rating_set(vf, enable); break;
1279                 }
1280         vf_refresh_idle(vf);
1281 }
1282
1283 guint vf_marks_get_filter(ViewFile *vf)
1284 {
1285         guint ret = 0;
1286         gint i;
1287         if (!vf->marks_enabled) return 0;
1288
1289         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
1290                 {
1291                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[i])))
1292                         {
1293                         ret |= 1 << i;
1294                         }
1295                 }
1296         return ret;
1297 }
1298
1299 GRegex *vf_file_filter_get_filter(ViewFile *vf)
1300 {
1301         GRegex *ret = NULL;
1302         GError *error = NULL;
1303         gchar *file_filter_text = NULL;
1304
1305         if (!gtk_widget_get_visible(vf->file_filter.combo))
1306                 {
1307                 return g_regex_new("", 0, 0, NULL);
1308                 }
1309
1310         file_filter_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
1311
1312         if (file_filter_text[0] != '\0')
1313                 {
1314                 ret = g_regex_new(file_filter_text, 0, 0, &error);
1315                 if (error)
1316                         {
1317                         log_printf("Error: could not compile regular expression %s\n%s\n", file_filter_text, error->message);
1318                         g_error_free(error);
1319                         error = NULL;
1320                         ret = g_regex_new("", 0, 0, NULL);
1321                         }
1322                 g_free(file_filter_text);
1323                 }
1324         else
1325                 {
1326                 ret = g_regex_new("", 0, 0, NULL);
1327                 }
1328
1329         return ret;
1330 }
1331
1332 void vf_set_layout(ViewFile *vf, LayoutWindow *layout)
1333 {
1334         vf->layout = layout;
1335 }
1336
1337
1338 /*
1339  *-----------------------------------------------------------------------------
1340  * maintenance (for rename, move, remove)
1341  *-----------------------------------------------------------------------------
1342  */
1343
1344 static gboolean vf_refresh_idle_cb(gpointer data)
1345 {
1346         ViewFile *vf = data;
1347
1348         vf_refresh(vf);
1349         vf->refresh_idle_id = 0;
1350         return FALSE;
1351 }
1352
1353 void vf_refresh_idle_cancel(ViewFile *vf)
1354 {
1355         if (vf->refresh_idle_id)
1356                 {
1357                 g_source_remove(vf->refresh_idle_id);
1358                 vf->refresh_idle_id = 0;
1359                 }
1360 }
1361
1362
1363 void vf_refresh_idle(ViewFile *vf)
1364 {
1365         if (!vf->refresh_idle_id)
1366                 {
1367                 vf->time_refresh_set = time(NULL);
1368                 /* file operations run with G_PRIORITY_DEFAULT_IDLE */
1369                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE + 50, vf_refresh_idle_cb, vf, NULL);
1370                 }
1371         else if (time(NULL) - vf->time_refresh_set > 1)
1372                 {
1373                 /* more than 1 sec since last update - increase priority */
1374                 vf_refresh_idle_cancel(vf);
1375                 vf->time_refresh_set = time(NULL);
1376                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 50, vf_refresh_idle_cb, vf, NULL);
1377                 }
1378 }
1379
1380 void vf_notify_cb(FileData *fd, NotifyType type, gpointer data)
1381 {
1382         ViewFile *vf = data;
1383         gboolean refresh;
1384
1385         NotifyType interested = NOTIFY_CHANGE | NOTIFY_REREAD | NOTIFY_GROUPING;
1386         if (vf->marks_enabled) interested |= NOTIFY_MARKS | NOTIFY_METADATA;
1387         /* FIXME: NOTIFY_METADATA should be checked by the keyword-to-mark functions and converted to NOTIFY_MARKS only if there was a change */
1388
1389         if (!(type & interested) || vf->refresh_idle_id || !vf->dir_fd) return;
1390
1391         refresh = (fd == vf->dir_fd);
1392
1393         if (!refresh)
1394                 {
1395                 gchar *base = remove_level_from_path(fd->path);
1396                 refresh = (g_strcmp0(base, vf->dir_fd->path) == 0);
1397                 g_free(base);
1398                 }
1399
1400         if ((type & NOTIFY_CHANGE) && fd->change)
1401                 {
1402                 if (!refresh && fd->change->dest)
1403                         {
1404                         gchar *dest_base = remove_level_from_path(fd->change->dest);
1405                         refresh = (g_strcmp0(dest_base, vf->dir_fd->path) == 0);
1406                         g_free(dest_base);
1407                         }
1408
1409                 if (!refresh && fd->change->source)
1410                         {
1411                         gchar *source_base = remove_level_from_path(fd->change->source);
1412                         refresh = (g_strcmp0(source_base, vf->dir_fd->path) == 0);
1413                         g_free(source_base);
1414                         }
1415                 }
1416
1417         if (refresh)
1418                 {
1419                 DEBUG_1("Notify vf: %s %04x", fd->path, type);
1420                 vf_refresh_idle(vf);
1421                 }
1422 }
1423
1424 static gboolean vf_read_metadata_in_idle_cb(gpointer data)
1425 {
1426         FileData *fd;
1427         ViewFile *vf = data;
1428         GList *list_entry;
1429         GList *work;
1430
1431         vf_thumb_status(vf, vf_read_metadata_in_idle_progress(vf), _("Loading meta..."));
1432
1433         work = vf->list;
1434
1435         while (work)
1436                 {
1437                 fd = work->data;
1438
1439                 if (fd && !fd->metadata_in_idle_loaded)
1440                         {
1441                         if (!fd->exifdate)
1442                                 {
1443                                 read_exif_time_data(fd);
1444                                 }
1445                         if (!fd->exifdate_digitized)
1446                                 {
1447                                 read_exif_time_digitized_data(fd);
1448                                 }
1449                         if (fd->rating == STAR_RATING_NOT_READ)
1450                                 {
1451                                 read_rating_data(fd);
1452                                 }
1453                         fd->metadata_in_idle_loaded = TRUE;
1454                         return TRUE;
1455                         }
1456                 work = work->next;
1457                 }
1458
1459         vf_thumb_status(vf, 0.0, NULL);
1460         vf->read_metadata_in_idle_id = 0;
1461         vf_refresh(vf);
1462         return FALSE;
1463 }
1464
1465 static void vf_read_metadata_in_idle_finished_cb(gpointer data)
1466 {
1467         ViewFile *vf = data;
1468
1469         vf_thumb_status(vf, 0.0, "Loading meta...");
1470         vf->read_metadata_in_idle_id = 0;
1471 }
1472
1473 void vf_read_metadata_in_idle(ViewFile *vf)
1474 {
1475         GList *work;
1476         FileData *fd;
1477
1478         if (!vf) return;
1479
1480         if (vf->read_metadata_in_idle_id)
1481                 {
1482                 g_idle_remove_by_data(vf);
1483                 }
1484         vf->read_metadata_in_idle_id = 0;
1485
1486         if (vf->list)
1487                 {
1488                 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);
1489                 }
1490
1491         return;
1492 }
1493
1494 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */