Use FileData locks to avoid expensive reloads with marks enabled
[geeqie.git] / src / view_file_list.c
index c290587..8d2d80c 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Geeqie
  * (C) 2004 John Ellis
- * Copyright (C) 2008 - 2010 The Geeqie Team
+ * Copyright (C) 2008 - 2012 The Geeqie Team
  *
  * Author: John Ellis
  *
@@ -136,62 +136,37 @@ static FileData *vflist_find_data_by_coord(ViewFile *vf, gint x, gint y, GtkTree
        return NULL;
 }
 
-#if 0
-static gint vflist_find_sidecar_list_idx(GList *work, FileData *fd)
-{
-       gint i = 0;
-       while (work)
-               {
-               FileData *fd_p = work->data;
-               if (fd == fd_p) return i;
-
-               i++;
-
-               GList *work2 = fd_p->sidecar_files;
-               while (work2)
-                       {
-                       fd_p = work2->data;
-                       if (fd == fd_p) return i;
-
-                       i++;
-                       work2 = work2->next;
-                       }
-               work = work->next;
-               }
-       return -1;
-}
-
-static gint vflist_sidecar_list_count(GList *work)
-{
-       gint i = 0;
-       while (work)
-               {
-               FileData *fd = work->data;
-               i++;
-
-               GList *work2 = fd->sidecar_files;
-               while (work2)
-                       {
-                       i++;
-                       work2 = work2->next;
-                       }
-               work = work->next;
-               }
-       return i;
-}
-#endif
-
 static gboolean vflist_store_clear_cb(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
 {
        FileData *fd;
        gtk_tree_model_get(model, iter, FILE_COLUMN_POINTER, &fd, -1);
+
+       /* it seems that gtk_tree_store_clear may call some callbacks
+          that use the column. Set the pointer to NULL to be safe. */
+       gtk_tree_store_set(GTK_TREE_STORE(model), iter, FILE_COLUMN_POINTER, NULL, -1);
        file_data_unref(fd);
        return FALSE;
 }
 
-static void vflist_store_clear(ViewFile *vf)
+static void vflist_store_clear(ViewFile *vf, gboolean unlock_files)
 {
        GtkTreeModel *store;
+       GList *files = NULL;
+
+       if (unlock_files && vf->marks_enabled)
+               {
+               // unlock locked files in this directory
+               filelist_read(vf->dir_fd, &files, NULL);
+               while (files)
+                       {
+                       FileData *fd = files->data;
+                       files = files->next;
+                       file_data_unlock(fd);
+                       file_data_unref(fd);  // undo the ref that got added in filelist_read
+                       }
+               }
+
+       g_list_free(files);
        store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
        gtk_tree_model_foreach(store, vflist_store_clear_cb, NULL);
        gtk_tree_store_clear(GTK_TREE_STORE(store));
@@ -219,27 +194,6 @@ static void vflist_move_cursor(ViewFile *vf, GtkTreeIter *iter)
        gtk_tree_path_free(tpath);
 }
 
-#if 0
-static gint vflist_column_idx(ViewFile *vf, gint store_idx)
-{
-       GList *columns, *work;
-       gint i = 0;
-
-       columns = gtk_tree_view_get_columns(GTK_TREE_VIEW(vf->listview));
-       work = columns;
-       while (work)
-               {
-               GtkTreeViewColumn *column = work->data;
-               if (store_idx == GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_store_idx")))
-                       break;
-               work = work->next;
-               i++;
-               }
-
-       g_list_free(columns);
-       return i;
-}
-#endif
 
 /*
  *-----------------------------------------------------------------------------
@@ -253,8 +207,6 @@ static void vflist_dnd_get(GtkWidget *widget, GdkDragContext *context,
 {
        ViewFile *vf = data;
        GList *list = NULL;
-       gchar *uri_text = NULL;
-       gint total;
 
        if (!VFLIST(vf)->click_fd) return;
 
@@ -268,15 +220,8 @@ static void vflist_dnd_get(GtkWidget *widget, GdkDragContext *context,
                }
 
        if (!list) return;
-
-       uri_text = uri_text_from_filelist(list, &total, (info == TARGET_TEXT_PLAIN));
+       uri_selection_data_set_uris_from_filelist(selection_data, list);
        filelist_free(list);
-
-       DEBUG_1("%s", uri_text);
-
-       gtk_selection_data_set(selection_data, selection_data->target,
-                              8, (guchar *)uri_text, total);
-       g_free(uri_text);
 }
 
 static void vflist_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
@@ -305,7 +250,7 @@ static void vflist_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer
 
        vflist_color_set(vf, VFLIST(vf)->click_fd, FALSE);
 
-       if (context->action == GDK_ACTION_MOVE)
+       if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
                {
                vf_refresh(vf);
                }
@@ -322,18 +267,12 @@ static void vflist_drag_data_received(GtkWidget *entry_widget, GdkDragContext *c
 
                if (fd) {
                        /* Add keywords to file */
-                       gchar *str = g_strndup((gchar *)selection->data, selection->length);
+                       gchar *str = (gchar *) gtk_selection_data_get_text(selection);
                        GList *kw_list = string_to_keywords_list(str);
-                       
+
                        metadata_append_list(fd, KEYWORD_KEY, kw_list);
                        string_list_free(kw_list);
                        g_free(str);
-/*
-file notification should handle this automatically
-                       if (vf->layout && vf->layout->bar_info) {
-                               bar_set_fd(vf->layout->bar_info, fd);
-                       }
-*/
                }
        }
 }
@@ -372,7 +311,7 @@ GList *vflist_selection_get_one(ViewFile *vf, FileData *fd)
                /* check if the row is expanded */
                GtkTreeModel *store;
                GtkTreeIter iter;
-               
+
                store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
                if (vflist_find_row(vf, fd, &iter) >= 0)
                        {
@@ -521,7 +460,7 @@ static gboolean vflist_row_rename_cb(TreeEditData *td, const gchar *old, const g
                file_data_unref(fd);
                g_free(old_path);
                }
-       
+
        g_free(new_path);
 
        return FALSE;
@@ -549,7 +488,7 @@ gboolean vflist_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer dat
        ViewFile *vf = data;
        GtkTreePath *tpath;
 
-       if (event->keyval != GDK_Menu) return FALSE;
+       if (event->keyval != GDK_KEY_Menu) return FALSE;
 
        gtk_tree_view_get_cursor(GTK_TREE_VIEW(vf->listview), &tpath, NULL);
        if (tpath)
@@ -580,7 +519,7 @@ gboolean vflist_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer dat
        GtkTreeIter iter;
        FileData *fd = NULL;
        GtkTreeViewColumn *column;
-       
+
        vf->clicked_mark = 0;
 
        if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
@@ -600,9 +539,6 @@ gboolean vflist_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer dat
 
                gtk_tree_model_get_iter(store, &iter, tpath);
                gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1);
-#if 0
-               gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, NULL, FALSE);
-#endif
                gtk_tree_path_free(tpath);
                }
 
@@ -646,12 +582,10 @@ gboolean vflist_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer dat
                return (gtk_tree_selection_count_selected_rows(selection) > 1);
                }
 
-#if 1
        if (bevent->button == MOUSE_BUTTON_LEFT && bevent->type == GDK_2BUTTON_PRESS)
                {
                if (vf->layout) layout_image_full_screen_start(vf->layout);
                }
-#endif
 
        return FALSE;
 }
@@ -715,7 +649,6 @@ gboolean vflist_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer d
                gtk_tree_selection_unselect_all(selection);
                gtk_tree_selection_select_iter(selection, &iter);
                vflist_move_cursor(vf, &iter);
-//             return TRUE;// FIXME - expand
                }
 
        return FALSE;
@@ -825,13 +758,6 @@ static void vflist_collapse_cb(GtkTreeView *tree_view, GtkTreeIter *iter, GtkTre
  *-----------------------------------------------------------------------------
  */
 
-/*
-static gboolean vflist_dummy_select_cb(GtkTreeSelection *selection, GtkTreeModel *store, GtkTreePath *tpath,
-                                       gboolean path_currently_selected, gpointer data)
-{
-       return TRUE;
-}
-*/
 
 static gchar* vflist_get_formatted(ViewFile *vf, const gchar *name, const gchar *sidecars, const gchar *size, const gchar *time, gboolean expanded)
  {
@@ -888,7 +814,7 @@ static void vflist_setup_iter(ViewFile *vf, GtkTreeStore *store, GtkTreeIter *it
        const gchar *disabled_grouping;
        gchar *formatted;
        gboolean expanded = FALSE;
-       
+
        if (fd->sidecar_files) /* expanded has no effect on files without sidecars */
                {
                gtk_tree_model_get(GTK_TREE_MODEL(store), iter, FILE_COLUMN_EXPANDED, &expanded, -1);
@@ -899,9 +825,9 @@ static void vflist_setup_iter(ViewFile *vf, GtkTreeStore *store, GtkTreeIter *it
        disabled_grouping = fd->disable_grouping ? _(" [NO GROUPING]") : "";
        name = g_strdup_printf("%s%s%s", link, fd->name, disabled_grouping);
        size = text_from_size(fd->size);
-       
+
        formatted = vflist_get_formatted(vf, name, sidecars, size, time, expanded);
-       
+
        gtk_tree_store_set(store, iter, FILE_COLUMN_POINTER, fd,
                                        FILE_COLUMN_VERSION, fd->version,
                                        FILE_COLUMN_THUMB, fd->thumb_pixbuf,
@@ -980,7 +906,7 @@ static void vflist_setup_iter_recursive(ViewFile *vf, GtkTreeStore *store, GtkTr
 
                                        if (match == 0) g_warning("multiple fd for the same path");
                                        }
-                                       
+
                                }
                        else
                                {
@@ -1008,7 +934,7 @@ static void vflist_setup_iter_recursive(ViewFile *vf, GtkTreeStore *store, GtkTr
 
                                vflist_setup_iter(vf, store, &new, file_data_ref(fd));
                                vflist_setup_iter_recursive(vf, store, &new, fd->sidecar_files, selected, force);
-                               
+
                                if (g_list_find(selected, fd))
                                        {
                                        /* renamed files - the same fd appears at different position - select it again*/
@@ -1049,14 +975,14 @@ static void vflist_setup_iter_recursive(ViewFile *vf, GtkTreeStore *store, GtkTr
 
                valid = gtk_tree_store_remove(store, &iter);
                }
-               
+
        /* move the prepended entries to the correct position */
        if (num_prepended)
                {
                gint i;
                gint num_total = num_prepended + num_ordered;
                gint *new_order = g_malloc(num_total * sizeof(gint));
-               
+
                for (i = 0; i < num_total; i++)
                        {
                        if (i < num_ordered)
@@ -1131,7 +1057,7 @@ void vflist_thumb_progress_count(GList *list, gint *count, gint *done)
                work = work->next;
 
                if (fd->thumb_pixbuf) (*done)++;
-               
+
                if (fd->sidecar_files)
                        {
                        vflist_thumb_progress_count(fd->sidecar_files, count, done);
@@ -1163,7 +1089,7 @@ FileData *vflist_thumb_next_fd(ViewFile *vf)
                GtkTreeModel *store;
                GtkTreeIter iter;
                gboolean valid = TRUE;
-       
+
                store = gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview));
                gtk_tree_model_get_iter(store, &iter, tpath);
                gtk_tree_path_free(tpath);
@@ -1245,19 +1171,19 @@ gint vflist_index_by_fd(ViewFile *vf, FileData *fd)
                {
                FileData *list_fd = work->data;
                if (list_fd == fd) return p;
-               
+
                work2 = list_fd->sidecar_files;
                while (work2)
                        {
                        /* FIXME: return the same index also for sidecars
-                          it is sufficient for next/prev navigation but it should be rewritten 
+                          it is sufficient for next/prev navigation but it should be rewritten
                           without using indexes at all
                        */
                        FileData *sidecar_fd = work2->data;
                        if (sidecar_fd == fd) return p;
                        work2 = work2->next;
                        }
-               
+
                work = work->next;
                p++;
                }
@@ -1405,7 +1331,7 @@ GList *vflist_selection_get_list(ViewFile *vf)
                gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1);
 
                list = g_list_prepend(list, file_data_ref(fd));
-               
+
                if (!fd->parent && !gtk_tree_view_row_expanded(GTK_TREE_VIEW(vf->listview), tpath))
                        {
                        /* unexpanded - add whole group */
@@ -1497,13 +1423,13 @@ static gboolean tree_model_get_iter_last(GtkTreeModel *store, GtkTreeIter *iter)
        while (TRUE)
                {
                GtkTreeIter next = *iter;
-               
+
                if (gtk_tree_model_iter_next(store, &next))
                        *iter = next;
                else
                        break;
                }
-       
+
        return TRUE;
 }
 
@@ -1529,7 +1455,7 @@ void vflist_select_invert(ViewFile *vf)
                        gtk_tree_selection_unselect_iter(selection, &iter);
                else
                        gtk_tree_selection_select_iter(selection, &iter);
-                               
+
                valid = tree_model_iter_prev(store, &iter);
                }
 }
@@ -1564,10 +1490,10 @@ static void vflist_select_closest(ViewFile *vf, FileData *sel_fd)
 {
        GList *work;
        FileData *fd = NULL;
-       
+
        if (sel_fd->parent) sel_fd = sel_fd->parent;
        work = vf->list;
-       
+
        while (work)
                {
                gint match;
@@ -1575,7 +1501,7 @@ static void vflist_select_closest(ViewFile *vf, FileData *sel_fd)
                work = work->next;
 
                match = filelist_sort_compare_filedata_full(fd, sel_fd, vf->sort_method, vf->sort_ascend);
-               
+
                if (match >= 0) break;
                }
 
@@ -1662,7 +1588,7 @@ void vflist_selection_to_mark(ViewFile *vf, gint mark, SelectionToMarkMode mode)
                        case STM_MODE_TOGGLE: file_data_set_mark(fd, n, !file_data_get_mark(fd, n));
                                break;
                        }
-               
+
                if (!file_data_filter_marks(fd, vf_marks_get_filter(vf))) /* file no longer matches the filter -> remove it */
                        {
                        vf_refresh_idle(vf);
@@ -1675,7 +1601,7 @@ void vflist_selection_to_mark(ViewFile *vf, gint mark, SelectionToMarkMode mode)
                        vflist_setup_iter_recursive(vf, GTK_TREE_STORE(store), &iter, fd->sidecar_files, NULL, FALSE);
                        }
 
-               
+
                file_data_register_notify_func(vf_notify_cb, vf, NOTIFY_PRIORITY_MEDIUM);
 
                work = work->next;
@@ -1701,11 +1627,7 @@ static void vflist_listview_set_columns(GtkWidget *listview, gboolean thumb, gbo
 
        gtk_tree_view_column_set_fixed_width(column, options->thumbnails.max_width + 4);
 
-#if GTK_CHECK_VERSION(2,18,0)
        list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
-#else
-       list = gtk_tree_view_column_get_cell_renderers(column);
-#endif
        if (!list) return;
        cell = list->data;
        g_list_free(list);
@@ -1743,7 +1665,7 @@ static void vflist_populate_view(ViewFile *vf, gboolean force)
 
        if (!vf->list)
                {
-               vflist_store_clear(vf);
+               vflist_store_clear(vf, FALSE);
                vf_send_update(vf);
                return;
                }
@@ -1751,7 +1673,7 @@ static void vflist_populate_view(ViewFile *vf, gboolean force)
        vflist_listview_set_columns(vf->listview, VFLIST(vf)->thumbs_enabled, vflist_is_multiline(vf));
 
        selected = vflist_selection_get_list(vf);
-       
+
        vflist_setup_iter_recursive(vf, store, NULL, vf->list, selected, force);
 
        if (selected && vflist_selection_count(vf, NULL) == 0)
@@ -1761,7 +1683,7 @@ static void vflist_populate_view(ViewFile *vf, gboolean force)
                }
 
        filelist_free(selected);
-       
+
        vf_send_update(vf);
        vf_thumb_update(vf);
 }
@@ -1780,6 +1702,20 @@ gboolean vflist_refresh(ViewFile *vf)
                file_data_unregister_notify_func(vf_notify_cb, vf); /* we don't need the notification of changes detected by filelist_read */
 
                ret = filelist_read(vf->dir_fd, &vf->list, NULL);
+
+               if (vf->marks_enabled)
+                       {
+                       // When marks are enabled, lock FileDatas so that we don't end up re-parsing XML
+                       // each time a mark is changed.
+                       file_data_lock_list(vf->list);
+                       }
+               else
+                       {
+                       // FIXME: only do this when needed (aka when we just switched from
+                       // FIXME: marks-enabled to marks-disabled)
+                       file_data_unlock_list(vf->list);
+                       }
+
                vf->list = file_data_filter_marks_list(vf->list, vf_marks_get_filter(vf));
                file_data_register_notify_func(vf_notify_cb, vf, NOTIFY_PRIORITY_MEDIUM);
 
@@ -1791,6 +1727,8 @@ gboolean vflist_refresh(ViewFile *vf)
 
        vflist_populate_view(vf, FALSE);
 
+       DEBUG_1("%s vflist_refresh: free filelist", get_exec_time());
+
        filelist_free(old_list);
        DEBUG_1("%s vflist_refresh: done", get_exec_time());
 
@@ -1905,7 +1843,7 @@ static void vflist_listview_mark_toggled_cb(GtkCellRendererToggle *cell, gchar *
 
        gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, FILE_COLUMN_POINTER, &fd, col_idx, &marked, -1);
        marked = !marked;
-       
+
        /* the change has a very limited range and the standard notification would trigger
           complete re-read of the directory - try to do only minimal update instead */
        file_data_unregister_notify_func(vf_notify_cb, vf);
@@ -1930,10 +1868,6 @@ static void vflist_listview_add_column_toggle(ViewFile *vf, gint n, const gchar
 {
        GtkTreeViewColumn *column;
        GtkCellRenderer *renderer;
-       GtkTreeStore *store;
-       gint index;
-
-       store = GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vf->listview)));
 
        renderer = gtk_cell_renderer_toggle_new();
        column = gtk_tree_view_column_new_with_attributes(title, renderer, "active", n, NULL);
@@ -1942,7 +1876,7 @@ static void vflist_listview_add_column_toggle(ViewFile *vf, gint n, const gchar
        g_object_set_data(G_OBJECT(column), "column_store_idx", GUINT_TO_POINTER(n));
        g_object_set_data(G_OBJECT(renderer), "column_store_idx", GUINT_TO_POINTER(n));
 
-       index = gtk_tree_view_append_column(GTK_TREE_VIEW(vf->listview), column);
+       gtk_tree_view_append_column(GTK_TREE_VIEW(vf->listview), column);
        gtk_tree_view_column_set_fixed_width(column, 22);
        gtk_tree_view_column_set_visible(column, vf->marks_enabled);
 
@@ -1966,7 +1900,7 @@ gboolean vflist_set_fd(ViewFile *vf, FileData *dir_fd)
        vf->dir_fd = file_data_ref(dir_fd);
 
        /* force complete reload */
-       vflist_store_clear(vf);
+       vflist_store_clear(vf, TRUE);
 
        filelist_free(vf->list);
        vf->list = NULL;
@@ -1998,7 +1932,7 @@ ViewFile *vflist_new(ViewFile *vf, FileData *dir_fd)
        gint column;
 
        vf->info = g_new0(ViewFileInfoList, 1);
-       
+
        flist_types[FILE_COLUMN_POINTER] = G_TYPE_POINTER;
        flist_types[FILE_COLUMN_VERSION] = G_TYPE_INT;
        flist_types[FILE_COLUMN_THUMB] = GDK_TYPE_PIXBUF;
@@ -2042,7 +1976,7 @@ ViewFile *vflist_new(ViewFile *vf, FileData *dir_fd)
        vflist_listview_add_column(vf, FILE_COLUMN_THUMB, "", TRUE, FALSE, FALSE);
        g_assert(column == FILE_VIEW_COLUMN_THUMB);
        column++;
-       
+
        vflist_listview_add_column(vf, FILE_COLUMN_FORMATTED, _("Name"), FALSE, FALSE, TRUE);
        g_assert(column == FILE_VIEW_COLUMN_FORMATTED);
        column++;
@@ -2064,12 +1998,12 @@ void vflist_thumb_set(ViewFile *vf, gboolean enable)
        if (VFLIST(vf)->thumbs_enabled == enable) return;
 
        VFLIST(vf)->thumbs_enabled = enable;
-       
+
        /* vflist_populate_view is better than vf_refresh:
           - no need to re-read the directory
           - force update because the formatted string has changed
        */
-       if (vf->layout) 
+       if (vf->layout)
                {
                vflist_populate_view(vf, TRUE);
                gtk_tree_view_columns_autosize(GTK_TREE_VIEW(vf->listview));
@@ -2093,8 +2027,17 @@ void vflist_marks_set(ViewFile *vf, gboolean enable)
                        gtk_tree_view_column_set_visible(column, enable);
                }
 
+       if (enable)
+               {
+               // Previously disabled, which means that vf->list is complete
+               file_data_lock_list(vf->list);
+               }
+       else
+               {
+               // Previously enabled, which means that vf->list is incomplete
+               }
+
        g_list_free(columns);
-       //vf_refresh(vf);
 }
 
 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */