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