Fri Nov 24 21:37:01 2006 John Ellis <johne@verizon.net>
[geeqie.git] / src / view_file_list.c
1 /*
2  * GQview
3  * (C) 2004 John Ellis
4  *
5  * Author: John Ellis
6  *
7  * This software is released under the GNU General Public License (GNU GPL).
8  * Please read the included file COPYING for more information.
9  * This software comes with no warranty of any kind, use at your own risk!
10  */
11
12 #include "gqview.h"
13 #include "view_file_list.h"
14
15 #include "cache_maint.h"
16 #include "dnd.h"
17 #include "editors.h"
18 #include "img-view.h"
19 #include "info.h"
20 #include "layout.h"
21 #include "layout_image.h"
22 #include "menu.h"
23 #include "thumb.h"
24 #include "utilops.h"
25 #include "ui_bookmark.h"
26 #include "ui_fileops.h"
27 #include "ui_menu.h"
28 #include "ui_tree_edit.h"
29
30 #include <gdk/gdkkeysyms.h> /* for keyboard values */
31
32
33 enum {
34         FILE_COLUMN_POINTER = 0,
35         FILE_COLUMN_THUMB,
36         FILE_COLUMN_NAME,
37         FILE_COLUMN_SIZE,
38         FILE_COLUMN_DATE,
39         FILE_COLUMN_COLOR,
40         FILE_COLUMN_COUNT
41 };
42
43
44 static gint vflist_row_is_selected(ViewFileList *vfl, FileData *fd);
45 static gint vflist_row_rename_cb(TreeEditData *td, const gchar *old, const gchar *new, gpointer data);
46 static void vflist_populate_view(ViewFileList *vfl);
47
48 /*
49  *-----------------------------------------------------------------------------
50  * signals
51  *-----------------------------------------------------------------------------
52  */
53
54 static void vflist_send_update(ViewFileList *vfl)
55 {
56         if (vfl->func_status) vfl->func_status(vfl, vfl->data_status);
57 }
58
59 /*
60  *-----------------------------------------------------------------------------
61  * misc
62  *-----------------------------------------------------------------------------
63  */
64
65 static gint vflist_find_row(ViewFileList *vfl, FileData *fd, GtkTreeIter *iter)
66 {
67         GtkTreeModel *store;
68         gint valid;
69         gint row = 0;
70
71         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview));
72         valid = gtk_tree_model_get_iter_first(store, iter);
73         while (valid)
74                 {
75                 FileData *fd_n;
76                 gtk_tree_model_get(GTK_TREE_MODEL(store), iter, FILE_COLUMN_POINTER, &fd_n, -1);
77                 if (fd_n == fd) return row;
78
79                 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), iter);
80                 row++;
81                 }
82
83         return -1;
84 }
85
86 static void vflist_color_set(ViewFileList *vfl, FileData *fd, gint color_set)
87 {
88         GtkTreeModel *store;
89         GtkTreeIter iter;
90
91         if (vflist_find_row(vfl, fd, &iter) < 0) return;
92         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview));
93         gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_COLOR, color_set, -1);
94 }
95
96 static void vflist_move_cursor(ViewFileList *vfl, GtkTreeIter *iter)
97 {
98         GtkTreeModel *store;
99         GtkTreePath *tpath;
100
101         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview));
102
103         tpath = gtk_tree_model_get_path(store, iter);
104         gtk_tree_view_set_cursor(GTK_TREE_VIEW(vfl->listview), tpath, NULL, FALSE);
105         gtk_tree_path_free(tpath);
106 }
107
108 /*
109  *-----------------------------------------------------------------------------
110  * dnd
111  *-----------------------------------------------------------------------------
112  */
113
114 static void vflist_dnd_get(GtkWidget *widget, GdkDragContext *context,
115                            GtkSelectionData *selection_data, guint info,
116                            guint time, gpointer data)
117 {
118         ViewFileList *vfl = data;
119         GList *list = NULL;
120         gchar *uri_text = NULL;
121         gint total;
122
123         if (!vfl->click_fd) return;
124
125         if (vflist_row_is_selected(vfl, vfl->click_fd))
126                 {
127                 list = vflist_selection_get_list(vfl);
128                 }
129         else
130                 {
131                 list = g_list_append(NULL, g_strdup(vfl->click_fd->path));
132                 }
133
134         if (!list) return;
135
136         uri_text = uri_text_from_list(list, &total, (info == TARGET_TEXT_PLAIN));
137         path_list_free(list);
138
139         if (debug) printf(uri_text);
140
141         gtk_selection_data_set(selection_data, selection_data->target,
142                                8, (guchar *)uri_text, total);
143         g_free(uri_text);
144 }
145
146 static void vflist_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
147 {
148         ViewFileList *vfl = data;
149
150         vflist_color_set(vfl, vfl->click_fd, TRUE);
151
152         if (vfl->thumbs_enabled &&
153             vfl->click_fd && vfl->click_fd->pixbuf)
154                 {
155                 gint items;
156
157                 if (vflist_row_is_selected(vfl, vfl->click_fd))
158                         items = vflist_selection_count(vfl, NULL);
159                 else
160                         items = 1;
161
162                 dnd_set_drag_icon(widget, context, vfl->click_fd->pixbuf, items);
163                 }
164 }
165
166 static void vflist_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
167 {
168         ViewFileList *vfl = data;
169
170         vflist_color_set(vfl, vfl->click_fd, FALSE);
171
172         if (context->action == GDK_ACTION_MOVE)
173                 {
174                 vflist_refresh(vfl);
175                 }
176 }
177
178 static void vflist_dnd_init(ViewFileList *vfl)
179 {
180         gtk_drag_source_set(vfl->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
181                             dnd_file_drag_types, dnd_file_drag_types_count,
182                             GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
183         g_signal_connect(G_OBJECT(vfl->listview), "drag_data_get",
184                          G_CALLBACK(vflist_dnd_get), vfl);
185         g_signal_connect(G_OBJECT(vfl->listview), "drag_begin",
186                          G_CALLBACK(vflist_dnd_begin), vfl);
187         g_signal_connect(G_OBJECT(vfl->listview), "drag_end",
188                          G_CALLBACK(vflist_dnd_end), vfl);
189 }
190
191 /*
192  *-----------------------------------------------------------------------------
193  * pop-up menu
194  *-----------------------------------------------------------------------------
195  */
196
197 static GList *vflist_pop_menu_file_list(ViewFileList *vfl)
198 {
199         if (!vfl->click_fd) return NULL;
200
201         if (vflist_row_is_selected(vfl, vfl->click_fd))
202                 {
203                 return vflist_selection_get_list(vfl);
204                 }
205
206         return g_list_append(NULL, g_strdup(vfl->click_fd->path));
207 }
208
209 static void vflist_pop_menu_edit_cb(GtkWidget *widget, gpointer data)
210 {
211         ViewFileList *vfl;
212         gint n;
213         GList *list;
214
215         vfl = submenu_item_get_data(widget);
216         n = GPOINTER_TO_INT(data);
217
218         if (!vfl) return;
219
220         list = vflist_pop_menu_file_list(vfl);
221         start_editor_from_path_list(n, list);
222         path_list_free(list);
223 }
224
225 static void vflist_pop_menu_info_cb(GtkWidget *widget, gpointer data)
226 {
227         ViewFileList *vfl = data;
228
229         info_window_new(NULL, vflist_pop_menu_file_list(vfl));
230 }
231
232 static void vflist_pop_menu_view_cb(GtkWidget *widget, gpointer data)
233 {
234         ViewFileList *vfl = data;
235
236         if (vflist_row_is_selected(vfl, vfl->click_fd))
237                 {
238                 GList *list;
239
240                 list = vflist_selection_get_list(vfl);
241                 view_window_new_from_list(list);
242                 path_list_free(list);
243                 }
244         else
245                 {
246                 const gchar *path;
247                 
248                 path = vfl->click_fd->path;
249                 view_window_new(path);
250                 }
251 }
252
253 static void vflist_pop_menu_copy_cb(GtkWidget *widget, gpointer data)
254 {
255         ViewFileList *vfl = data;
256
257         file_util_copy(NULL, vflist_pop_menu_file_list(vfl), NULL, vfl->listview);
258 }
259
260 static void vflist_pop_menu_move_cb(GtkWidget *widget, gpointer data)
261 {
262         ViewFileList *vfl = data;
263
264         file_util_move(NULL, vflist_pop_menu_file_list(vfl), NULL, vfl->listview);
265 }
266
267 static void vflist_pop_menu_rename_cb(GtkWidget *widget, gpointer data)
268 {
269         ViewFileList *vfl = data;
270         GList *list;
271
272         list = vflist_pop_menu_file_list(vfl);
273         if (enable_in_place_rename &&
274             list && !list->next && vfl->click_fd)
275                 {
276                 GtkTreeModel *store;
277                 GtkTreeIter iter;
278
279                 path_list_free(list);
280
281                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview));
282                 if (vflist_find_row(vfl, vfl->click_fd, &iter) >= 0)
283                         {
284                         GtkTreePath *tpath;
285
286                         tpath = gtk_tree_model_get_path(store, &iter);
287                         tree_edit_by_path(GTK_TREE_VIEW(vfl->listview), tpath,
288                                           FILE_COLUMN_NAME -1, vfl->click_fd->name,
289                                           vflist_row_rename_cb, vfl);
290                         gtk_tree_path_free(tpath);
291                         }
292                 return;
293                 }
294
295         file_util_rename(NULL, list, vfl->listview);
296 }
297
298 static void vflist_pop_menu_delete_cb(GtkWidget *widget, gpointer data)
299 {
300         ViewFileList *vfl = data;
301
302         file_util_delete(NULL, vflist_pop_menu_file_list(vfl), vfl->listview);
303 }
304
305 static void vflist_pop_menu_sort_cb(GtkWidget *widget, gpointer data)
306 {
307         ViewFileList *vfl;
308         SortType type;
309
310         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
311         
312         vfl = submenu_item_get_data(widget);
313         if (!vfl) return;
314
315         type = (SortType)GPOINTER_TO_INT(data);
316
317         if (vfl->layout)
318                 {
319                 layout_sort_set(vfl->layout, type, vfl->sort_ascend);
320                 }
321         else
322                 {
323                 vflist_sort_set(vfl, type, vfl->sort_ascend);
324                 }
325 }
326
327 static void vflist_pop_menu_sort_ascend_cb(GtkWidget *widget, gpointer data)
328 {
329         ViewFileList *vfl = data;
330
331         if (vfl->layout)
332                 {
333                 layout_sort_set(vfl->layout, vfl->sort_method, !vfl->sort_ascend);
334                 }
335         else
336                 {
337                 vflist_sort_set(vfl, vfl->sort_method, !vfl->sort_ascend);
338                 }
339 }
340
341 static void vflist_pop_menu_icons_cb(GtkWidget *widget, gpointer data)
342 {
343         ViewFileList *vfl = data;
344
345         if (vfl->layout) layout_views_set(vfl->layout, vfl->layout->tree_view, TRUE);
346 }
347
348 static void vflist_pop_menu_thumbs_cb(GtkWidget *widget, gpointer data)
349 {
350         ViewFileList *vfl = data;
351
352         vflist_color_set(vfl, vfl->click_fd, FALSE);
353         if (vfl->layout)
354                 {
355                 layout_thumb_set(vfl->layout, !vfl->thumbs_enabled);
356                 }
357         else
358                 {
359                 vflist_thumb_set(vfl, !vfl->thumbs_enabled);
360                 }
361 }
362
363 static void vflist_pop_menu_refresh_cb(GtkWidget *widget, gpointer data)
364 {
365         ViewFileList *vfl = data;
366
367         vflist_color_set(vfl, vfl->click_fd, FALSE);
368         vflist_refresh(vfl);
369 }
370
371 static void vflist_popup_destroy_cb(GtkWidget *widget, gpointer data)
372 {
373         ViewFileList *vfl = data;
374         vflist_color_set(vfl, vfl->click_fd, FALSE);
375         vfl->click_fd = NULL;
376         vfl->popup = NULL;
377 }
378
379 static GtkWidget *vflist_pop_menu(ViewFileList *vfl, FileData *fd)
380 {
381         GtkWidget *menu;
382         GtkWidget *item;
383         GtkWidget *submenu;
384         gint active;
385
386         vflist_color_set(vfl, fd, TRUE);
387         active = (fd != NULL);
388
389         menu = popup_menu_short_lived();
390         g_signal_connect(G_OBJECT(menu), "destroy",
391                          G_CALLBACK(vflist_popup_destroy_cb), vfl);
392
393         submenu_add_edit(menu, &item, G_CALLBACK(vflist_pop_menu_edit_cb), vfl);
394         gtk_widget_set_sensitive(item, active);
395
396         menu_item_add_stock_sensitive(menu, _("_Properties"), GTK_STOCK_PROPERTIES, active,
397                                       G_CALLBACK(vflist_pop_menu_info_cb), vfl);
398         menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, active,
399                                       G_CALLBACK(vflist_pop_menu_view_cb), vfl);
400
401         menu_item_add_divider(menu);
402         menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, active,
403                                       G_CALLBACK(vflist_pop_menu_copy_cb), vfl);
404         menu_item_add_sensitive(menu, _("_Move..."), active,
405                                 G_CALLBACK(vflist_pop_menu_move_cb), vfl);
406         menu_item_add_sensitive(menu, _("_Rename..."), active,
407                                 G_CALLBACK(vflist_pop_menu_rename_cb), vfl);
408         menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, active,
409                                 G_CALLBACK(vflist_pop_menu_delete_cb), vfl);
410
411         menu_item_add_divider(menu);
412
413         submenu = submenu_add_sort(NULL, G_CALLBACK(vflist_pop_menu_sort_cb), vfl,
414                                    FALSE, FALSE, TRUE, vfl->sort_method);
415         menu_item_add_divider(submenu);
416         menu_item_add_check(submenu, _("Ascending"), vfl->sort_ascend,
417                             G_CALLBACK(vflist_pop_menu_sort_ascend_cb), vfl);
418
419         item = menu_item_add(menu, _("_Sort"), NULL, NULL);
420         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
421
422         menu_item_add_check(menu, _("View as _icons"), FALSE,
423                             G_CALLBACK(vflist_pop_menu_icons_cb), vfl);
424         menu_item_add_check(menu, _("Show _thumbnails"), vfl->thumbs_enabled,
425                             G_CALLBACK(vflist_pop_menu_thumbs_cb), vfl);
426         menu_item_add_stock(menu, _("Re_fresh"), GTK_STOCK_REFRESH, G_CALLBACK(vflist_pop_menu_refresh_cb), vfl);
427
428         return menu;
429 }
430
431 /*
432  *-----------------------------------------------------------------------------
433  * callbacks
434  *-----------------------------------------------------------------------------
435  */
436
437 static gint vflist_row_rename_cb(TreeEditData *td, const gchar *old, const gchar *new, gpointer data)
438 {
439         ViewFileList *vfl = data;
440         gchar *old_path;
441         gchar *new_path;
442
443         if (strlen(new) == 0) return FALSE;
444
445         old_path = concat_dir_and_file(vfl->path, old);
446         new_path = concat_dir_and_file(vfl->path, new);
447
448         if (strchr(new, '/') != NULL)
449                 {
450                 gchar *text = g_strdup_printf(_("Invalid file name:\n%s"), new);
451                 file_util_warning_dialog(_("Error renaming file"), text, GTK_STOCK_DIALOG_ERROR, vfl->listview);
452                 g_free(text);
453                 }
454         else if (isfile(new_path))
455                 {
456                 gchar *text = g_strdup_printf(_("A file with name %s already exists."), new);
457                 file_util_warning_dialog(_("Error renaming file"), text, GTK_STOCK_DIALOG_ERROR, vfl->listview);
458                 g_free(text);
459                 }
460         else if (!rename_file(old_path, new_path))
461                 {
462                 gchar *text = g_strdup_printf(_("Unable to rename file:\n%s\nto:\n%s"), old, new);
463                 file_util_warning_dialog(_("Error renaming file"), text, GTK_STOCK_DIALOG_ERROR, vfl->listview);
464                 g_free(text);
465                 }
466         else
467                 {
468                 file_maint_renamed(old_path, new_path);
469                 }
470
471         g_free(old_path);
472         g_free(new_path);
473
474         return FALSE;
475 }
476
477 static void vflist_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
478 {
479         ViewFileList *vfl = data;
480         GtkTreeModel *store;
481         GtkTreeIter iter;
482         GtkTreePath *tpath;
483         gint cw, ch;
484
485         if (vflist_find_row(vfl, vfl->click_fd, &iter) < 0) return;
486         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview));
487         tpath = gtk_tree_model_get_path(store, &iter);
488         tree_view_get_cell_clamped(GTK_TREE_VIEW(vfl->listview), tpath, FILE_COLUMN_NAME - 1, TRUE, x, y, &cw, &ch);
489         gtk_tree_path_free(tpath);
490         *y += ch;
491         popup_menu_position_clamp(menu, x, y, 0);
492 }
493
494 static gint vflist_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
495 {
496         ViewFileList *vfl = data;
497         GtkTreePath *tpath;
498
499         if (event->keyval != GDK_Menu) return FALSE;
500
501         gtk_tree_view_get_cursor(GTK_TREE_VIEW(vfl->listview), &tpath, NULL);
502         if (tpath)
503                 {
504                 GtkTreeModel *store;
505                 GtkTreeIter iter;
506
507                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
508                 gtk_tree_model_get_iter(store, &iter, tpath);
509                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &vfl->click_fd, -1);
510                 gtk_tree_path_free(tpath);
511                 }
512         else
513                 {
514                 vfl->click_fd = NULL;
515                 }
516
517         vfl->popup = vflist_pop_menu(vfl, vfl->click_fd);
518         gtk_menu_popup(GTK_MENU(vfl->popup), NULL, NULL, vflist_menu_position_cb, vfl, 0, GDK_CURRENT_TIME);
519
520         return TRUE;
521 }
522
523 static gint vflist_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
524 {
525         ViewFileList *vfl = data;
526         GtkTreePath *tpath;
527         GtkTreeIter iter;
528         FileData *fd = NULL;
529
530         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
531                                           &tpath, NULL, NULL, NULL))
532                 {
533                 GtkTreeModel *store;
534
535                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
536                 gtk_tree_model_get_iter(store, &iter, tpath);
537                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1);
538 #if 0
539                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, NULL, FALSE);
540 #endif
541                 gtk_tree_path_free(tpath);
542                 }
543
544         vfl->click_fd = fd;
545
546         if (bevent->button == 3)
547                 {
548                 vfl->popup = vflist_pop_menu(vfl, vfl->click_fd);
549                 gtk_menu_popup(GTK_MENU(vfl->popup), NULL, NULL, NULL, NULL,
550                                 bevent->button, bevent->time);
551                 return TRUE;
552                 }
553
554         if (!fd) return FALSE;
555
556         if (bevent->button == 2)
557                 {
558                 if (!vflist_row_is_selected(vfl, fd))
559                         {
560                         vflist_color_set(vfl, fd, TRUE);
561                         }
562                 return TRUE;
563                 }
564
565
566         if (bevent->button == 1 && bevent->type == GDK_BUTTON_PRESS &&
567             !(bevent->state & GDK_SHIFT_MASK ) &&
568             !(bevent->state & GDK_CONTROL_MASK ) &&
569             vflist_row_is_selected(vfl, fd))
570                 {
571                 gtk_widget_grab_focus(widget);
572                 return TRUE;
573                 }
574
575 #if 0
576         if (bevent->button == 1 && bevent->type == GDK_2BUTTON_PRESS)
577                 {
578                 if (vfl->layout) layout_image_full_screen_start(vfl->layout);
579                 }
580 #endif
581
582         return FALSE;
583 }
584
585 static gint vflist_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
586 {
587         ViewFileList *vfl = data;
588         GtkTreePath *tpath;
589         GtkTreeIter iter;
590         FileData *fd = NULL;
591
592         if (bevent->button == 2)
593                 {
594                 vflist_color_set(vfl, vfl->click_fd, FALSE);
595                 }
596
597         if (bevent->button != 1 && bevent->button != 2)
598                 {
599                 return TRUE;
600                 }
601
602         if ((bevent->x != 0 || bevent->y != 0) &&
603             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
604                                           &tpath, NULL, NULL, NULL))
605                 {
606                 GtkTreeModel *store;
607
608                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
609                 gtk_tree_model_get_iter(store, &iter, tpath);
610                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1);
611                 gtk_tree_path_free(tpath);
612                 }
613
614         if (bevent->button == 2)
615                 {
616                 if (fd && vfl->click_fd == fd)
617                         {
618                         GtkTreeSelection *selection;
619
620                         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
621                         if (vflist_row_is_selected(vfl, fd))
622                                 {
623                                 gtk_tree_selection_unselect_iter(selection, &iter);
624                                 }
625                         else
626                                 {
627                                 gtk_tree_selection_select_iter(selection, &iter);
628                                 }
629                         }
630                 return TRUE;
631                 }
632
633         if (fd && vfl->click_fd == fd &&
634             !(bevent->state & GDK_SHIFT_MASK ) &&
635             !(bevent->state & GDK_CONTROL_MASK ) &&
636             vflist_row_is_selected(vfl, fd))
637                 {
638                 GtkTreeSelection *selection;
639
640                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget));
641                 gtk_tree_selection_unselect_all(selection);
642                 gtk_tree_selection_select_iter(selection, &iter);
643                 vflist_move_cursor(vfl, &iter);
644                 return TRUE;
645                 }
646
647         return FALSE;
648 }
649
650 static void vflist_select_image(ViewFileList *vfl, gint row)
651 {
652         const gchar *path;
653         const gchar *read_ahead_path = NULL;
654
655         path = vflist_index_get_path(vfl, row);
656         if (!path) return;
657
658         if (path && enable_read_ahead)
659                 {
660                 FileData *fd;
661                 if (row > vflist_index_by_path(vfl, layout_image_get_path(vfl->layout)) &&
662                     row + 1 < vflist_count(vfl, NULL))
663                         {
664                         fd = vflist_index_get_data(vfl, row + 1);
665                         }
666                 else if (row > 0)
667                         {
668                         fd = vflist_index_get_data(vfl, row - 1);
669                         }
670                 else
671                         {
672                         fd = NULL;
673                         }
674                 if (fd) read_ahead_path = fd->path;
675                 }
676
677         layout_image_set_with_ahead(vfl->layout, path, read_ahead_path);
678 }
679
680 static gint vflist_select_idle_cb(gpointer data)
681 {
682         ViewFileList *vfl = data;
683
684         if (!vfl->layout)
685                 {
686                 vfl->select_idle_id = -1;
687                 return FALSE;
688                 }
689
690         vflist_send_update(vfl);
691
692         if (vfl->select_fd)
693                 {
694                 vflist_select_image(vfl, g_list_index(vfl->list, vfl->select_fd));
695                 vfl->select_fd = NULL;
696                 }
697
698         vfl->select_idle_id = -1;
699         return FALSE;
700 }
701
702 static void vflist_select_idle_cancel(ViewFileList *vfl)
703 {
704         if (vfl->select_idle_id != -1) g_source_remove(vfl->select_idle_id);
705         vfl->select_idle_id = -1;
706 }
707
708 static gboolean vflist_select_cb(GtkTreeSelection *selection, GtkTreeModel *store, GtkTreePath *tpath,
709                                  gboolean path_currently_selected, gpointer data)
710 {
711         ViewFileList *vfl = data;
712         GtkTreeIter iter;
713
714         if (!path_currently_selected &&
715             gtk_tree_model_get_iter(store, &iter, tpath))
716                 {
717                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &vfl->select_fd, -1);
718                 }
719         else
720                 {
721                 vfl->select_fd = NULL;
722                 }
723
724         if (vfl->layout &&
725             vfl->select_idle_id == -1)
726                 {
727                 vfl->select_idle_id = g_idle_add(vflist_select_idle_cb, vfl);
728                 }
729
730         return TRUE;
731 }
732
733 /*
734  *-----------------------------------------------------------------------------
735  * misc
736  *-----------------------------------------------------------------------------
737  */
738
739 static gboolean vflist_dummy_select_cb(GtkTreeSelection *selection, GtkTreeModel *store, GtkTreePath *tpath,
740                                         gboolean path_currently_selected, gpointer data)
741 {
742         return TRUE;
743 }
744
745 void vflist_sort_set(ViewFileList *vfl, SortType type, gint ascend)
746 {
747         GtkTreeModel *model;
748         GtkListStore *store;
749         GList *work;
750         GtkTreeSelection *selection;
751         GtkTreePath *tpath;
752         GtkTreeIter iter;
753         GList *select_list;
754         FileData *cursor_fd = NULL;
755         gint single_select;
756
757         if (vfl->sort_method == type && vfl->sort_ascend == ascend) return;
758
759         vfl->sort_method = type;
760         vfl->sort_ascend = ascend;
761
762         if (!vfl->list) return;
763
764         vfl->list = filelist_sort(vfl->list, vfl->sort_method, vfl->sort_ascend);
765
766         /* now reorder the treeview, maintaining current selection */
767
768 #if 0
769         /* this is simpler, but much slower */
770         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)));
771
772         work = g_list_last(vfl->list);
773         while (work)
774                 {
775                 FileData *fd;
776                 GtkTreeIter iter;
777
778                 fd = work->data;
779                 if (vflist_find_row(vfl, fd, &iter) >= 0)
780                         {
781                         gtk_list_store_move_after(store, &iter, NULL);
782                         }
783
784                 work = work->prev;
785                 }
786 #endif
787
788         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview));
789
790         gtk_tree_selection_set_select_function(selection, vflist_dummy_select_cb, vfl, NULL);
791
792         select_list = gtk_tree_selection_get_selected_rows(selection, &model);
793         work = select_list;
794         while (work)
795                 {
796                 FileData *fd;
797
798                 tpath = work->data;
799                 gtk_tree_model_get_iter(model, &iter, tpath);
800                 gtk_tree_model_get(model, &iter, FILE_COLUMN_POINTER, &fd, -1);
801                 gtk_tree_path_free(tpath);
802
803                 work->data = fd;
804                 work = work->next;
805                 }
806
807         select_list = filelist_sort(select_list, vfl->sort_method, vfl->sort_ascend);
808
809         gtk_tree_view_get_cursor(GTK_TREE_VIEW(vfl->listview), &tpath, NULL);
810         if (tpath)
811                 {
812                 if (gtk_tree_model_get_iter(model, &iter, tpath))
813                         {
814                         gtk_tree_model_get(model, &iter, FILE_COLUMN_POINTER, &cursor_fd, -1);
815                         }
816                 gtk_tree_path_free(tpath);
817                 }
818
819         single_select = (select_list && !select_list->next);
820         if (single_select) cursor_fd = select_list->data;
821
822         store = GTK_LIST_STORE(model);
823         gtk_list_store_clear(store);
824
825         work = vfl->list;
826         while (work)
827                 {
828                 FileData *fd;
829                 gchar *size;
830
831                 fd = work->data;
832                 size = text_from_size(fd->size);
833                 gtk_list_store_append(store, &iter);
834                 gtk_list_store_set(store, &iter, FILE_COLUMN_POINTER, fd,
835                                                  FILE_COLUMN_THUMB, (vfl->thumbs_enabled) ? fd->pixbuf : NULL,
836                                                  FILE_COLUMN_NAME, fd->name,
837                                                  FILE_COLUMN_SIZE, size,
838                                                  FILE_COLUMN_DATE, text_from_time(fd->date),
839                                                  FILE_COLUMN_COLOR, FALSE, -1);
840                 g_free(size);
841
842                 if (select_list && select_list->data == fd)
843                         {
844                         select_list = g_list_remove(select_list, fd);
845                         gtk_tree_selection_select_iter(selection, &iter);
846                         }
847
848                 work = work->next;
849                 }
850
851         g_list_free(select_list);
852
853         if (cursor_fd &&
854             vflist_find_row(vfl, cursor_fd, &iter) >= 0)
855                 {
856                 if (single_select)
857                         {
858                         vflist_move_cursor(vfl, &iter);
859                         }
860                 else
861                         {
862                         tree_view_row_make_visible(GTK_TREE_VIEW(vfl->listview), &iter, TRUE);
863                         }
864                 }
865
866         gtk_tree_selection_set_select_function(selection, vflist_select_cb, vfl, NULL);
867 }
868
869 /*
870  *-----------------------------------------------------------------------------
871  * thumb updates
872  *-----------------------------------------------------------------------------
873  */
874
875 static gint vflist_thumb_next(ViewFileList *vfl);
876
877 static void vflist_thumb_status(ViewFileList *vfl, gdouble val, const gchar *text)
878 {
879         if (vfl->func_thumb_status)
880                 {
881                 vfl->func_thumb_status(vfl, val, text, vfl->data_thumb_status);
882                 }
883 }
884
885 static void vflist_thumb_cleanup(ViewFileList *vfl)
886 {
887         vflist_thumb_status(vfl, 0.0, NULL);
888
889         vfl->thumbs_count = 0;
890         vfl->thumbs_running = FALSE;
891
892         thumb_loader_free(vfl->thumbs_loader);
893         vfl->thumbs_loader = NULL;
894
895         vfl->thumbs_filedata = NULL;
896 }
897
898 static void vflist_thumb_stop(ViewFileList *vfl)
899 {
900         if (vfl->thumbs_running) vflist_thumb_cleanup(vfl);
901 }
902
903 static void vflist_thumb_do(ViewFileList *vfl, ThumbLoader *tl, FileData *fd)
904 {
905         GtkListStore *store;
906         GtkTreeIter iter;
907
908         if (!fd || vflist_find_row(vfl, fd, &iter) < 0) return;
909
910         if (fd->pixbuf) g_object_unref(fd->pixbuf);
911         fd->pixbuf = thumb_loader_get_pixbuf(tl, TRUE);
912
913         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)));
914         gtk_list_store_set(store, &iter, FILE_COLUMN_THUMB, fd->pixbuf, -1);
915
916         vflist_thumb_status(vfl, (gdouble)(vfl->thumbs_count) / g_list_length(vfl->list), _("Loading thumbs..."));
917 }
918
919 static void vflist_thumb_error_cb(ThumbLoader *tl, gpointer data)
920 {
921         ViewFileList *vfl = data;
922
923         if (vfl->thumbs_filedata && vfl->thumbs_loader == tl)
924                 {
925                 vflist_thumb_do(vfl, tl, vfl->thumbs_filedata);
926                 }
927
928         while (vflist_thumb_next(vfl));
929 }
930
931 static void vflist_thumb_done_cb(ThumbLoader *tl, gpointer data)
932 {
933         ViewFileList *vfl = data;
934
935         if (vfl->thumbs_filedata && vfl->thumbs_loader == tl)
936                 {
937                 vflist_thumb_do(vfl, tl, vfl->thumbs_filedata);
938                 }
939
940         while (vflist_thumb_next(vfl));
941 }
942
943 static gint vflist_thumb_next(ViewFileList *vfl)
944 {
945         GtkTreePath *tpath;
946         FileData *fd = NULL;
947
948         /* first check the visible files */
949
950         if (GTK_WIDGET_REALIZED(vfl->listview) &&
951             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vfl->listview), 0, 0, &tpath, NULL, NULL, NULL))
952                 {
953                 GtkTreeModel *store;
954                 GtkTreeIter iter;
955                 gint valid = TRUE;
956
957                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview));
958                 gtk_tree_model_get_iter(store, &iter, tpath);
959                 gtk_tree_path_free(tpath);
960
961                 while (!fd && valid && tree_view_row_get_visibility(GTK_TREE_VIEW(vfl->listview), &iter, FALSE) == 0)
962                         {
963                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1);
964                         if (fd->pixbuf) fd = NULL;
965
966                         valid = gtk_tree_model_iter_next(store, &iter);
967                         }
968                 }
969
970         /* then find first undone */
971
972         if (!fd)
973                 {
974                 GList *work = vfl->list;
975                 while (work && !fd)
976                         {
977                         FileData *fd_p = work->data;
978                         work = work->next;
979
980                         if (!fd_p->pixbuf) fd = fd_p;
981                         }
982                 }
983
984         if (!fd)
985                 {
986                 /* done */
987                 vflist_thumb_cleanup(vfl);
988                 return FALSE;
989                 }
990
991         vfl->thumbs_count++;
992
993         vfl->thumbs_filedata = fd;
994
995         thumb_loader_free(vfl->thumbs_loader);
996
997         vfl->thumbs_loader = thumb_loader_new(thumb_max_width, thumb_max_height);
998         thumb_loader_set_callbacks(vfl->thumbs_loader,
999                                    vflist_thumb_done_cb,
1000                                    vflist_thumb_error_cb,
1001                                    NULL,
1002                                    vfl);
1003
1004         if (!thumb_loader_start(vfl->thumbs_loader, fd->path))
1005                 {
1006                 /* set icon to unknown, continue */
1007                 if (debug) printf("thumb loader start failed %s\n", vfl->thumbs_loader->path);
1008                 vflist_thumb_do(vfl, vfl->thumbs_loader, fd);
1009
1010                 return TRUE;
1011                 }
1012
1013         return FALSE;
1014 }
1015
1016 static void vflist_thumb_update(ViewFileList *vfl)
1017 {
1018         vflist_thumb_stop(vfl);
1019         if (!vfl->thumbs_enabled) return;
1020
1021         vflist_thumb_status(vfl, 0.0, _("Loading thumbs..."));
1022         vfl->thumbs_running = TRUE;
1023
1024         while (vflist_thumb_next(vfl));
1025 }
1026
1027 /*
1028  *-----------------------------------------------------------------------------
1029  * row stuff
1030  *-----------------------------------------------------------------------------
1031  */
1032
1033 FileData *vflist_index_get_data(ViewFileList *vfl, gint row)
1034 {
1035         return g_list_nth_data(vfl->list, row);
1036 }
1037
1038 gchar *vflist_index_get_path(ViewFileList *vfl, gint row)
1039 {
1040         FileData *fd;
1041
1042         fd = g_list_nth_data(vfl->list, row);
1043
1044         return (fd ? fd->path : NULL);
1045 }
1046
1047 static gint vflist_row_by_path(ViewFileList *vfl, const gchar *path, FileData **fd)
1048 {
1049         gint p = 0;
1050         GList *work;
1051
1052         if (!path) return -1;
1053
1054         work = vfl->list;
1055         while (work)
1056                 {
1057                 FileData *fd_n = work->data;
1058                 if (strcmp(path, fd_n->path) == 0)
1059                         {
1060                         if (fd) *fd = fd_n;
1061                         return p;
1062                         }
1063                 work = work->next;
1064                 p++;
1065                 }
1066
1067         if (fd) *fd = NULL;
1068         return -1;
1069 }
1070
1071 gint vflist_index_by_path(ViewFileList *vfl, const gchar *path)
1072 {
1073         return vflist_row_by_path(vfl, path, NULL);
1074 }
1075
1076 gint vflist_count(ViewFileList *vfl, gint64 *bytes)
1077 {
1078         if (bytes)
1079                 {
1080                 gint64 b = 0;
1081                 GList *work;
1082
1083                 work = vfl->list;
1084                 while (work)
1085                         {
1086                         FileData *fd = work->data;
1087                         work = work->next;
1088                         b += fd->size;
1089                         }
1090
1091                 *bytes = b;
1092                 }
1093
1094         return g_list_length(vfl->list);
1095 }
1096
1097 GList *vflist_get_list(ViewFileList *vfl)
1098 {
1099         GList *list = NULL;
1100         GList *work;
1101
1102         work = vfl->list;
1103         while (work)
1104                 {
1105                 FileData *fd = work->data;
1106                 work = work->next;
1107
1108                 list = g_list_prepend(list, g_strdup(fd->path));
1109                 }
1110
1111         return g_list_reverse(list);
1112 }
1113
1114 /*
1115  *-----------------------------------------------------------------------------
1116  * selections
1117  *-----------------------------------------------------------------------------
1118  */
1119
1120 static gint vflist_row_is_selected(ViewFileList *vfl, FileData *fd)
1121 {
1122         GtkTreeModel *store;
1123         GtkTreeSelection *selection;
1124         GList *slist;
1125         GList *work;
1126         gint found = FALSE;
1127
1128         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview));
1129         slist = gtk_tree_selection_get_selected_rows(selection, &store);
1130         work = slist;
1131         while (!found && work)
1132                 {
1133                 GtkTreePath *tpath = work->data;
1134                 FileData *fd_n;
1135                 GtkTreeIter iter;
1136
1137                 gtk_tree_model_get_iter(store, &iter, tpath);
1138                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd_n, -1);
1139                 if (fd_n == fd) found = TRUE;
1140                 work = work->next;
1141                 }
1142         g_list_foreach(slist, (GFunc)gtk_tree_path_free, NULL);
1143         g_list_free(slist);
1144
1145         return found;
1146 }
1147
1148 gint vflist_index_is_selected(ViewFileList *vfl, gint row)
1149 {
1150         FileData *fd;
1151
1152         fd = vflist_index_get_data(vfl, row);
1153         return vflist_row_is_selected(vfl, fd);
1154 }
1155
1156 gint vflist_selection_count(ViewFileList *vfl, gint64 *bytes)
1157 {
1158         GtkTreeModel *store;
1159         GtkTreeSelection *selection;
1160         GList *slist;
1161         gint count;
1162
1163         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview));
1164         slist = gtk_tree_selection_get_selected_rows(selection, &store);
1165
1166         if (bytes)
1167                 {
1168                 gint64 b = 0;
1169                 GList *work;
1170
1171                 work = slist;
1172                 while (work)
1173                         {
1174                         GtkTreePath *tpath = work->data;
1175                         GtkTreeIter iter;
1176                         FileData *fd;
1177
1178                         gtk_tree_model_get_iter(store, &iter, tpath);
1179                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1);
1180                         b += fd->size;
1181
1182                         work = work->next;
1183                         }
1184
1185                 *bytes = b;
1186                 }
1187
1188         count = g_list_length(slist);
1189         g_list_foreach(slist, (GFunc)gtk_tree_path_free, NULL);
1190         g_list_free(slist);
1191
1192         return count;
1193 }
1194
1195 GList *vflist_selection_get_list(ViewFileList *vfl)
1196 {
1197         GtkTreeModel *store;
1198         GtkTreeSelection *selection;
1199         GList *slist;
1200         GList *list = NULL;
1201         GList *work;
1202
1203         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview));
1204         slist = gtk_tree_selection_get_selected_rows(selection, &store);
1205         work = slist;
1206         while (work)
1207                 {
1208                 GtkTreePath *tpath = work->data;
1209                 FileData *fd;
1210                 GtkTreeIter iter;
1211
1212                 gtk_tree_model_get_iter(store, &iter, tpath);
1213                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1);
1214
1215                 list = g_list_prepend(list, g_strdup(fd->path));
1216
1217                 work = work->next;
1218                 }
1219         g_list_foreach(slist, (GFunc)gtk_tree_path_free, NULL);
1220         g_list_free(slist);
1221
1222         return g_list_reverse(list);
1223 }
1224
1225 GList *vflist_selection_get_list_by_index(ViewFileList *vfl)
1226 {
1227         GtkTreeModel *store;
1228         GtkTreeSelection *selection;
1229         GList *slist;
1230         GList *list = NULL;
1231         GList *work;
1232
1233         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview));
1234         slist = gtk_tree_selection_get_selected_rows(selection, &store);
1235         work = slist;
1236         while (work)
1237                 {
1238                 GtkTreePath *tpath = work->data;
1239                 FileData *fd;
1240                 GtkTreeIter iter;
1241
1242                 gtk_tree_model_get_iter(store, &iter, tpath);
1243                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1);
1244
1245                 list = g_list_prepend(list, GINT_TO_POINTER(g_list_index(vfl->list, fd)));
1246
1247                 work = work->next;
1248                 }
1249         g_list_foreach(slist, (GFunc)gtk_tree_path_free, NULL);
1250         g_list_free(slist);
1251
1252         return g_list_reverse(list);
1253 }
1254
1255 void vflist_select_all(ViewFileList *vfl)
1256 {
1257         GtkTreeSelection *selection;
1258
1259         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview));
1260         gtk_tree_selection_select_all(selection);
1261
1262         vfl->select_fd = NULL;
1263 }
1264
1265 void vflist_select_none(ViewFileList *vfl)
1266 {
1267         GtkTreeSelection *selection;
1268
1269         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview));
1270         gtk_tree_selection_unselect_all(selection);
1271 }
1272
1273 void vflist_select_by_path(ViewFileList *vfl, const gchar *path)
1274 {
1275         FileData *fd;
1276         GtkTreeIter iter;
1277
1278         if (vflist_row_by_path(vfl, path, &fd) < 0) return;
1279         if (vflist_find_row(vfl, fd, &iter) < 0) return;
1280
1281         tree_view_row_make_visible(GTK_TREE_VIEW(vfl->listview), &iter, TRUE);
1282
1283         if (!vflist_row_is_selected(vfl, fd))
1284                 {
1285                 GtkTreeSelection *selection;
1286                 GtkTreeModel *store;
1287                 GtkTreePath *tpath;
1288
1289                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview));
1290                 gtk_tree_selection_unselect_all(selection);
1291                 gtk_tree_selection_select_iter(selection, &iter);
1292                 vflist_move_cursor(vfl, &iter);
1293
1294                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview));
1295                 tpath = gtk_tree_model_get_path(store, &iter);
1296                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vfl->listview), tpath, NULL, FALSE);
1297                 gtk_tree_path_free(tpath);
1298                 }
1299 }
1300
1301 /*
1302  *-----------------------------------------------------------------------------
1303  * core (population)
1304  *-----------------------------------------------------------------------------
1305  */
1306
1307 static void vflist_listview_set_height(GtkWidget *listview, gint thumb)
1308 {
1309         GtkTreeViewColumn *column;
1310         GtkCellRenderer *cell;
1311         GList *list;
1312
1313         column = gtk_tree_view_get_column(GTK_TREE_VIEW(listview), FILE_COLUMN_THUMB - 1);
1314         if (!column) return;
1315
1316         gtk_tree_view_column_set_fixed_width(column, (thumb) ? thumb_max_width : 4);
1317
1318         list = gtk_tree_view_column_get_cell_renderers(column);
1319         if (!list) return;
1320         cell = list->data;
1321         g_list_free(list);
1322
1323         g_object_set(G_OBJECT(cell), "height", (thumb) ? thumb_max_height : -1, NULL);
1324         gtk_tree_view_columns_autosize(GTK_TREE_VIEW(listview));
1325 }
1326
1327 static void vflist_populate_view(ViewFileList *vfl)
1328 {
1329         GtkListStore *store;
1330         GtkTreeIter iter;
1331         gint thumbs;
1332         GList *work;
1333         GtkTreeRowReference *visible_row = NULL;
1334         GtkTreePath *tpath;
1335         gint valid;
1336
1337         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)));
1338         thumbs = vfl->thumbs_enabled;
1339
1340         vflist_thumb_stop(vfl);
1341
1342         if (!vfl->list)
1343                 {
1344                 gtk_list_store_clear(store);
1345                 vflist_send_update(vfl);
1346                 return;
1347                 }
1348
1349         if (GTK_WIDGET_REALIZED(vfl->listview) &&
1350             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vfl->listview), 0, 0, &tpath, NULL, NULL, NULL))
1351                 {
1352                 visible_row = gtk_tree_row_reference_new(GTK_TREE_MODEL(store), tpath);
1353                 gtk_tree_path_free(tpath);
1354                 }
1355
1356         vflist_listview_set_height(vfl->listview, thumbs);
1357
1358         valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
1359
1360         work = vfl->list;
1361         while (work)
1362                 {
1363                 gint match;
1364                 FileData *fd = work->data;
1365                 gint done = FALSE;
1366
1367                 while (!done)
1368                         {
1369                         FileData *old_fd = NULL;
1370
1371                         if (valid)
1372                                 {
1373                                 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, FILE_COLUMN_POINTER, &old_fd, -1);
1374                                 match = CASE_SORT(fd->name, old_fd->name);
1375                                 }
1376                         else
1377                                 {
1378                                 match = -1;
1379                                 }
1380
1381                         if (match < 0)
1382                                 {
1383                                 GtkTreeIter new;
1384                                 gchar *size;
1385
1386                                 size = text_from_size(fd->size);
1387                                 if (valid)
1388                                         {
1389                                         gtk_list_store_insert_before(store, &new, &iter);
1390                                         }
1391                                 else
1392                                         {
1393                                         gtk_list_store_append(store, &new);
1394                                         }
1395                                 gtk_list_store_set(store, &new, FILE_COLUMN_POINTER, fd,
1396                                                                 FILE_COLUMN_THUMB, (thumbs) ? fd->pixbuf : NULL,
1397                                                                 FILE_COLUMN_NAME, fd->name,
1398                                                                 FILE_COLUMN_SIZE, size,
1399                                                                 FILE_COLUMN_DATE, text_from_time(fd->date),
1400                                                                 FILE_COLUMN_COLOR, FALSE, -1);
1401                                 g_free(size);
1402
1403                                 done = TRUE;
1404                                 }
1405                         else if (match > 0)
1406                                 {
1407                                 valid = gtk_list_store_remove(store, &iter);
1408                                 }
1409                         else
1410                                 {
1411                                 gtk_list_store_set(store, &iter, FILE_COLUMN_POINTER, fd, -1);
1412                                 if (fd->date != old_fd->date)
1413                                         {
1414                                         gchar *size;
1415
1416                                         /* update, file changed */
1417                                         size = text_from_size(fd->size);
1418                                         gtk_list_store_set(store, &iter, FILE_COLUMN_SIZE, size,
1419                                                                          FILE_COLUMN_DATE, text_from_time(fd->date), -1);
1420                                         g_free(size);
1421                                         }
1422                                 else if (fd != old_fd)
1423                                         {
1424                                         /* preserve thumbnail */
1425                                         if (fd->pixbuf) g_object_unref(fd->pixbuf);
1426                                         fd->pixbuf = old_fd->pixbuf;
1427                                         if (fd->pixbuf) g_object_ref(fd->pixbuf);
1428                                         }
1429
1430                                 gtk_list_store_set(store, &iter, FILE_COLUMN_THUMB, (thumbs) ? fd->pixbuf : NULL, -1);
1431
1432                                 if (vfl->select_fd == old_fd) vfl->select_fd = fd;
1433
1434                                 if (valid) valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
1435
1436                                 done = TRUE;
1437                                 }
1438                         }
1439                 work = work->next;
1440                 }
1441
1442         while (valid)
1443                 {
1444                 valid = gtk_list_store_remove(store, &iter);
1445                 }
1446
1447         if (visible_row)
1448                 {
1449                 if (gtk_tree_row_reference_valid(visible_row))
1450                         {
1451                         tpath = gtk_tree_row_reference_get_path(visible_row);
1452                         gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(vfl->listview), tpath, NULL, TRUE, 0.0, 0.0);
1453                         gtk_tree_path_free(tpath);
1454                         }
1455                 gtk_tree_row_reference_free(visible_row);
1456                 }
1457
1458         vflist_send_update(vfl);
1459         vflist_thumb_update(vfl);
1460 }
1461
1462 gint vflist_refresh(ViewFileList *vfl)
1463 {
1464         GList *old_list;
1465         gint ret = TRUE;
1466
1467         old_list = vfl->list;
1468         vfl->list = NULL;
1469
1470         if (vfl->path)
1471                 {
1472                 ret = filelist_read(vfl->path, &vfl->list, NULL);
1473                 }
1474
1475         vfl->list = filelist_sort(vfl->list, vfl->sort_method, vfl->sort_ascend);
1476         vflist_populate_view(vfl);
1477
1478         filelist_free(old_list);
1479
1480         return ret;
1481 }
1482
1483 /* this overrides the low default of a GtkCellRenderer from 100 to CELL_HEIGHT_OVERRIDE, something sane for our purposes */
1484
1485 #define CELL_HEIGHT_OVERRIDE 512
1486
1487 static void cell_renderer_height_override(GtkCellRenderer *renderer)
1488 {
1489         GParamSpec *spec;
1490
1491         spec = g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(renderer)), "height");
1492         if (spec && G_IS_PARAM_SPEC_INT(spec))
1493                 {
1494                 GParamSpecInt *spec_int;
1495
1496                 spec_int = G_PARAM_SPEC_INT(spec);
1497                 if (spec_int->maximum < CELL_HEIGHT_OVERRIDE) spec_int->maximum = CELL_HEIGHT_OVERRIDE;
1498                 }
1499 }
1500
1501 static GdkColor *vflist_listview_color_shifted(GtkWidget *widget)
1502 {
1503         static GdkColor color;
1504         static GtkWidget *done = NULL;
1505
1506         if (done != widget)
1507                 {
1508                 GtkStyle *style;
1509
1510                 style = gtk_widget_get_style(widget);
1511                 memcpy(&color, &style->base[GTK_STATE_NORMAL], sizeof(color));
1512                 shift_color(&color, -1, 0);
1513                 done = widget;
1514                 }
1515
1516         return &color;
1517 }
1518
1519 static void vflist_listview_color_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
1520                                      GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
1521 {
1522         ViewFileList *vfl = data;
1523         gboolean set;
1524
1525         gtk_tree_model_get(tree_model, iter, FILE_COLUMN_COLOR, &set, -1);
1526         g_object_set(G_OBJECT(cell),
1527                      "cell-background-gdk", vflist_listview_color_shifted(vfl->listview),
1528                      "cell-background-set", set, NULL);
1529 }
1530
1531 static void vflist_listview_add_column(ViewFileList *vfl, gint n, const gchar *title, gint image, gint right_justify)
1532 {
1533         GtkTreeViewColumn *column;
1534         GtkCellRenderer *renderer;
1535
1536         column = gtk_tree_view_column_new();
1537         gtk_tree_view_column_set_title(column, title);
1538         gtk_tree_view_column_set_min_width(column, 4);
1539
1540         if (!image)
1541                 {
1542                 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
1543                 renderer = gtk_cell_renderer_text_new();
1544                 if (right_justify)
1545                         {
1546                         g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
1547                         }
1548                 gtk_tree_view_column_pack_start(column, renderer, TRUE);
1549                 gtk_tree_view_column_add_attribute(column, renderer, "text", n);
1550                 }
1551         else
1552                 {
1553                 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
1554                 renderer = gtk_cell_renderer_pixbuf_new();
1555                 cell_renderer_height_override(renderer);
1556                 gtk_tree_view_column_pack_start(column, renderer, TRUE);
1557                 gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", n);
1558                 }
1559
1560         gtk_tree_view_column_set_cell_data_func(column, renderer, vflist_listview_color_cb, vfl, NULL);
1561
1562         gtk_tree_view_append_column(GTK_TREE_VIEW(vfl->listview), column);
1563 }
1564
1565 /*
1566  *-----------------------------------------------------------------------------
1567  * base
1568  *-----------------------------------------------------------------------------
1569  */
1570
1571 gint vflist_set_path(ViewFileList *vfl, const gchar *path)
1572 {
1573         GtkListStore *store;
1574
1575         if (!path) return FALSE;
1576         if (vfl->path && strcmp(path, vfl->path) == 0) return TRUE;
1577
1578         g_free(vfl->path);
1579         vfl->path = g_strdup(path);
1580
1581         /* force complete reload */
1582         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)));
1583         gtk_list_store_clear(store);
1584
1585         filelist_free(vfl->list);
1586         vfl->list = NULL;
1587
1588         return vflist_refresh(vfl);
1589 }
1590
1591 static void vflist_destroy_cb(GtkWidget *widget, gpointer data)
1592 {
1593         ViewFileList *vfl = data;
1594
1595         if (vfl->popup)
1596                 {
1597                 g_signal_handlers_disconnect_matched(G_OBJECT(vfl->popup), G_SIGNAL_MATCH_DATA,
1598                                                      0, 0, 0, NULL, vfl);
1599                 gtk_widget_destroy(vfl->popup);
1600                 }
1601
1602         vflist_select_idle_cancel(vfl);
1603         vflist_thumb_stop(vfl);
1604
1605         g_free(vfl->path);
1606         filelist_free(vfl->list);
1607         g_free(vfl);
1608 }
1609
1610 ViewFileList *vflist_new(const gchar *path, gint thumbs)
1611 {
1612         ViewFileList *vfl;
1613         GtkListStore *store;
1614         GtkTreeSelection *selection;
1615
1616         vfl = g_new0(ViewFileList, 1);
1617
1618         vfl->path = NULL;
1619         vfl->list = NULL;
1620         vfl->click_fd = NULL;
1621         vfl->select_fd = NULL;
1622         vfl->sort_method = SORT_NAME;
1623         vfl->sort_ascend = TRUE;
1624         vfl->thumbs_enabled = thumbs;
1625
1626         vfl->thumbs_running = FALSE;
1627         vfl->thumbs_count = 0;
1628         vfl->thumbs_loader = NULL;
1629         vfl->thumbs_filedata = NULL;
1630
1631         vfl->select_idle_id = -1;
1632
1633         vfl->popup = NULL;
1634
1635         vfl->widget = gtk_scrolled_window_new(NULL, NULL);
1636         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vfl->widget), GTK_SHADOW_IN);
1637         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vfl->widget),
1638                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
1639         g_signal_connect(G_OBJECT(vfl->widget), "destroy",
1640                          G_CALLBACK(vflist_destroy_cb), vfl);
1641
1642         store = gtk_list_store_new(6, G_TYPE_POINTER, GDK_TYPE_PIXBUF, G_TYPE_STRING,
1643                                    G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN);
1644         vfl->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
1645         g_object_unref(store);
1646
1647         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview));
1648         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
1649         gtk_tree_selection_set_select_function(selection, vflist_select_cb, vfl, NULL);
1650
1651         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vfl->listview), FALSE);
1652         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vfl->listview), FALSE);
1653
1654         vflist_listview_add_column(vfl, FILE_COLUMN_THUMB, "", TRUE, FALSE);
1655         vflist_listview_add_column(vfl, FILE_COLUMN_NAME, _("Name"), FALSE, FALSE);
1656         vflist_listview_add_column(vfl, FILE_COLUMN_SIZE, _("Size"), FALSE, TRUE);
1657         vflist_listview_add_column(vfl, FILE_COLUMN_DATE, _("Date"), FALSE, TRUE);
1658
1659         g_signal_connect(G_OBJECT(vfl->listview), "key_press_event",
1660                          G_CALLBACK(vflist_press_key_cb), vfl);
1661
1662         gtk_container_add (GTK_CONTAINER(vfl->widget), vfl->listview);
1663         gtk_widget_show(vfl->listview);
1664
1665         vflist_dnd_init(vfl);
1666
1667         g_signal_connect(G_OBJECT(vfl->listview), "button_press_event",
1668                          G_CALLBACK(vflist_press_cb), vfl);
1669         g_signal_connect(G_OBJECT(vfl->listview), "button_release_event",
1670                          G_CALLBACK(vflist_release_cb), vfl);
1671
1672         if (path) vflist_set_path(vfl, path);
1673
1674         return vfl;
1675 }
1676
1677 void vflist_set_status_func(ViewFileList *vfl,
1678                             void (*func)(ViewFileList *vfl, gpointer data), gpointer data)
1679 {
1680         vfl->func_status = func;
1681         vfl->data_status = data;
1682 }
1683
1684 void vflist_set_thumb_status_func(ViewFileList *vfl,
1685                                   void (*func)(ViewFileList *vfl, gdouble val, const gchar *text, gpointer data),
1686                                   gpointer data)
1687 {
1688         vfl->func_thumb_status = func;
1689         vfl->data_thumb_status = data;
1690 }
1691
1692 void vflist_thumb_set(ViewFileList *vfl, gint enable)
1693 {
1694         if (vfl->thumbs_enabled == enable) return;
1695
1696         vfl->thumbs_enabled = enable;
1697         vflist_refresh(vfl);
1698 }
1699
1700 void vflist_set_layout(ViewFileList *vfl, LayoutWindow *layout)
1701 {
1702         vfl->layout = layout;
1703 }
1704
1705 /*
1706  *-----------------------------------------------------------------------------
1707  * maintenance (for rename, move, remove)
1708  *-----------------------------------------------------------------------------
1709  */
1710
1711 static gint vflist_maint_find_closest(ViewFileList *vfl, gint row, gint count, GList *ignore_list)
1712 {
1713         GList *list = NULL;
1714         GList *work;
1715         gint rev = row - 1;
1716         row ++;
1717
1718         work = ignore_list;
1719         while (work)
1720                 {
1721                 gint f = vflist_index_by_path(vfl, work->data);
1722                 if (f >= 0) list = g_list_prepend(list, GINT_TO_POINTER(f));
1723                 work = work->next;
1724                 }
1725
1726         while (list)
1727                 {
1728                 gint c = TRUE;
1729                 work = list;
1730                 while (work && c)
1731                         {
1732                         gpointer p = work->data;
1733                         work = work->next;
1734                         if (row == GPOINTER_TO_INT(p))
1735                                 {
1736                                 row++;
1737                                 c = FALSE;
1738                                 }
1739                         if (rev == GPOINTER_TO_INT(p))
1740                                 {
1741                                 rev--;
1742                                 c = FALSE;
1743                                 }
1744                         if (!c) list = g_list_remove(list, p);
1745                         }
1746                 if (c && list)
1747                         {
1748                         g_list_free(list);
1749                         list = NULL;
1750                         }
1751                 }
1752         if (row > count - 1)
1753                 {
1754                 if (rev < 0)
1755                         return -1;
1756                 else
1757                         return rev;
1758                 }
1759         else
1760                 {
1761                 return row;
1762                 }
1763 }
1764
1765 gint vflist_maint_renamed(ViewFileList *vfl, const gchar *source, const gchar *dest)
1766 {
1767         gint ret = FALSE;
1768         gint row;
1769         gchar *source_base;
1770         gchar *dest_base;
1771         GList *work;
1772         FileData *fd;
1773
1774         row = vflist_index_by_path(vfl, source);
1775         if (row < 0) return FALSE;
1776
1777         source_base = remove_level_from_path(source);
1778         dest_base = remove_level_from_path(dest);
1779
1780         work = g_list_nth(vfl->list, row);
1781         fd = work->data;
1782
1783         if (strcmp(source_base, dest_base) == 0)
1784                 {
1785                 GtkListStore *store;
1786                 GtkTreeIter iter;
1787                 GtkTreeIter position;
1788                 gint old_row;
1789                 gint n;
1790
1791                 old_row = g_list_index(vfl->list, fd);
1792
1793                 vfl->list = g_list_remove(vfl->list, fd);
1794                 g_free(fd->path);
1795
1796                 fd->path = g_strdup(dest);
1797                 fd->name = filename_from_path(fd->path);
1798
1799                 vfl->list = filelist_insert_sort(vfl->list, fd, vfl->sort_method, vfl->sort_ascend);
1800                 n = g_list_index(vfl->list, fd);
1801
1802                 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)));
1803                 row = vflist_find_row(vfl, fd, &iter);
1804                 if (vflist_find_row(vfl, fd, &iter) >= 0 &&
1805                     gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store), &position, NULL, n))
1806                         {
1807                         if (old_row >= n)
1808                                 {
1809                                 gtk_list_store_move_before(store, &iter, &position);
1810                                 }
1811                         else
1812                                 {
1813                                 gtk_list_store_move_after(store, &iter, &position);
1814                                 }
1815                         }
1816                 gtk_list_store_set(store, &iter, FILE_COLUMN_NAME, fd->name, -1);
1817
1818                 ret = TRUE;
1819                 }
1820         else
1821                 {
1822                 ret = vflist_maint_removed(vfl, source, NULL);
1823                 }
1824
1825         g_free(source_base);
1826         g_free(dest_base);
1827
1828         return ret;
1829 }
1830
1831 gint vflist_maint_removed(ViewFileList *vfl, const gchar *path, GList *ignore_list)
1832 {
1833         GtkTreeIter iter;
1834         GList *list;
1835         FileData *fd;
1836         gint row;
1837         gint new_row = -1;
1838
1839         row = vflist_index_by_path(vfl, path);
1840         if (row < 0) return FALSE;
1841
1842         if (vflist_index_is_selected(vfl, row) &&
1843             layout_image_get_collection(vfl->layout, NULL) == NULL)
1844                 {
1845                 gint n;
1846
1847                 n = vflist_count(vfl, NULL);
1848                 if (ignore_list)
1849                         {
1850                         new_row = vflist_maint_find_closest(vfl, row, n, ignore_list);
1851                         if (debug) printf("row = %d, closest is %d\n", row, new_row);
1852                         }
1853                 else
1854                         {
1855                         if (row + 1 < n)
1856                                 {
1857                                 new_row = row + 1;
1858                                 }
1859                         else if (row > 0)
1860                                 {
1861                                 new_row = row - 1;
1862                                 }
1863                         }
1864                 vflist_select_none(vfl);
1865                 if (new_row >= 0)
1866                         {
1867                         fd = vflist_index_get_data(vfl, new_row);
1868                         if (vflist_find_row(vfl, fd, &iter) >= 0)
1869                                 {
1870                                 GtkTreeSelection *selection;
1871
1872                                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview));
1873                                 gtk_tree_selection_select_iter(selection, &iter);
1874                                 vflist_move_cursor(vfl, &iter);
1875                                 }
1876                         }
1877                 }
1878
1879         fd = vflist_index_get_data(vfl, row);
1880         if (vflist_find_row(vfl, fd, &iter) >= 0)
1881                 {
1882                 GtkListStore *store;
1883                 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)));
1884                 gtk_list_store_remove(store, &iter);
1885                 }
1886         list = g_list_nth(vfl->list, row);
1887         fd = list->data;
1888
1889         /* thumbnail loader check */
1890         if (fd == vfl->thumbs_filedata) vfl->thumbs_filedata = NULL;
1891         if (vfl->thumbs_count > 0) vfl->thumbs_count--;
1892
1893         vfl->list = g_list_remove(vfl->list, fd);
1894         file_data_free(fd);
1895
1896         vflist_send_update(vfl);
1897
1898         return TRUE;
1899 }
1900
1901 gint vflist_maint_moved(ViewFileList *vfl, const gchar *source, const gchar *dest, GList *ignore_list)
1902 {
1903         gint ret = FALSE;
1904         gchar *buf;
1905
1906         if (!source || !vfl->path) return FALSE;
1907
1908         buf = remove_level_from_path(source);
1909
1910         if (strcmp(buf, vfl->path) == 0)
1911                 {
1912                 ret = vflist_maint_removed(vfl, source, ignore_list);
1913                 }
1914
1915         g_free(buf);
1916
1917         return ret;
1918 }
1919