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