[gthumb] browser: do not load the parent when the current location is deleted



commit 6d47d644b961e2d182a0cc74c4c1bf6d19616bec
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Mon Dec 16 09:29:13 2019 +0100

    browser: do not load the parent when the current location is deleted
    
    Load the previous location in the folder tree.

 gthumb/gth-browser.c | 47 +++++++++++++++++++++++++++++++++++++++++++----
 gthumb/gtk-utils.c   | 36 ++++++++++++++++++++++++++++++++++++
 gthumb/gtk-utils.h   |  1 +
 3 files changed, 80 insertions(+), 4 deletions(-)
---
diff --git a/gthumb/gth-browser.c b/gthumb/gth-browser.c
index 6fce0d27..886eaa63 100644
--- a/gthumb/gth-browser.c
+++ b/gthumb/gth-browser.c
@@ -3090,6 +3090,14 @@ file_attributes_ready_cb (GthFileSource *file_source,
 }
 
 
+static gboolean
+_g_file_list_only_contains (GList *l,
+                           GFile *file)
+{
+       return (l->next == NULL) && _g_file_equal (file, G_FILE (l->data));
+}
+
+
 static GList *
 _g_file_list_find_file_or_ancestor (GList *l,
                                    GFile *file)
@@ -3141,12 +3149,43 @@ folder_changed_cb (GthMonitor      *monitor,
        if (browser->priv->location == NULL)
                return;
 
-       if ((event == GTH_MONITOR_EVENT_DELETED) && (_g_file_list_find_file_or_ancestor (list, 
browser->priv->location->file) != NULL))
-               _gth_browser_load (browser, parent, NULL, NULL, 0, GTH_ACTION_GO_TO, TRUE);
+       if ((event == GTH_MONITOR_EVENT_DELETED)
+               && _g_file_list_only_contains (list, browser->priv->location->file))
+       {
+               /* current location deleted -> load the previous location in
+                * the folder tree. */
+
+               GtkTreePath *location_path;
+
+               location_path = gth_folder_tree_get_path (GTH_FOLDER_TREE (browser->priv->folder_tree), 
browser->priv->location->file);
+               if (location_path != NULL) {
+                       GtkTreePath *prev_path;
 
-       if ((event == GTH_MONITOR_EVENT_CHANGED) && (_g_file_list_find_file_or_ancestor (list, 
browser->priv->location->file) != NULL)) {
+                       prev_path = _gtk_tree_path_get_previous_or_parent (location_path);
+                       if (prev_path != NULL) {
+                               GthFileData *prev_file;
+
+                               prev_file = gth_folder_tree_get_file (GTH_FOLDER_TREE 
(browser->priv->folder_tree), prev_path);
+                               if (prev_file != NULL) {
+                                       _gth_browser_load (browser, prev_file->file, NULL, NULL, 0, 
GTH_ACTION_GO_TO, TRUE);
+                                       _g_object_unref (prev_file);
+                               }
+
+                               gtk_tree_path_free (prev_path);
+                       }
+
+                       gtk_tree_path_free (location_path);
+               }
+       }
+       else if ((event == GTH_MONITOR_EVENT_DELETED)
+               && (_g_file_list_find_file_or_ancestor (list, browser->priv->location->file) != NULL))
+       {
+               _gth_browser_load (browser, parent, NULL, NULL, 0, GTH_ACTION_GO_TO, TRUE);
+       }
+       else if ((event == GTH_MONITOR_EVENT_CHANGED)
+               && (_g_file_list_find_file_or_ancestor (list, browser->priv->location->file) != NULL))
+       {
                _gth_browser_load (browser, browser->priv->location->file, NULL, NULL, 0, GTH_ACTION_GO_TO, 
TRUE);
-               return;
        }
 
 #if 0
diff --git a/gthumb/gtk-utils.c b/gthumb/gtk-utils.c
index 13e628e3..58f996dc 100644
--- a/gthumb/gtk-utils.c
+++ b/gthumb/gtk-utils.c
@@ -633,6 +633,42 @@ _gtk_tree_path_list_free (GList *list)
 }
 
 
+GtkTreePath *
+_gtk_tree_path_get_previous_or_parent (GtkTreePath *path)
+{
+       int         *indices;
+       int          depth;
+       int          last;
+       gboolean     return_parent;
+       int         *new_indices;
+       int          new_depth;
+       int          i;
+       GtkTreePath *new_path;
+
+       indices = gtk_tree_path_get_indices_with_depth (path, &depth);
+       if (indices == NULL)
+               return NULL;
+
+       last = depth - 1;
+       return_parent = indices[last] == 0;
+       new_depth = return_parent ? depth - 1 : depth;
+       if (new_depth == 0)
+               return NULL;
+
+       new_indices = g_new (int, new_depth);
+       for (i = 0; i < new_depth; i++)
+               new_indices[i] = indices[i];
+       if (! return_parent) /* return previous element */
+               new_indices[last] = indices[last] - 1;
+
+       new_path = gtk_tree_path_new_from_indicesv (new_indices, new_depth);
+
+       g_free (new_indices);
+
+       return new_path;
+}
+
+
 int
 _gtk_paned_get_position2 (GtkPaned *paned)
 {
diff --git a/gthumb/gtk-utils.h b/gthumb/gtk-utils.h
index 9482aa69..51b48799 100644
--- a/gthumb/gtk-utils.h
+++ b/gthumb/gtk-utils.h
@@ -146,6 +146,7 @@ void            _gtk_widget_set_margin                     (GtkWidget        *wi
                                                            int               bottom,
                                                            int               left);
 void            _gtk_tree_path_list_free                   (GList            *list);
+GtkTreePath *   _gtk_tree_path_get_previous_or_parent      (GtkTreePath      *path);
 int             _gtk_paned_get_position2                   (GtkPaned         *paned);
 void            _g_launch_command                          (GtkWidget        *parent,
                                                            const char       *command,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]