From 8c005786ca7d08828b2b132ddac520a1866f5071 Mon Sep 17 00:00:00 2001
From: Ondrej Holy <oholy@redhat.com>
Date: Wed, 14 Jun 2023 16:32:03 +0200
Subject: [PATCH] Force reload current location when it reappears

Resolves: rhbz#2100426
---
 ...revent-passing-NULL-to-g_object_unre.patch | 42 ++++++++++++++++++
 nautilus.spec                                 |  9 +++-
 ...ing-when-current-location-disappears.patch | 16 +++++++
 ...conditions-to-restore-selection-when.patch | 37 ++++++++++++++++
 ...e-reload-current-location-when-it-re.patch | 43 +++++++++++++++++++
 5 files changed, 146 insertions(+), 1 deletion(-)
 create mode 100644 file-utilities-Prevent-passing-NULL-to-g_object_unre.patch
 create mode 100644 pathbar-Do-nothing-when-current-location-disappears.patch
 create mode 100644 window-slot-Fix-conditions-to-restore-selection-when.patch
 create mode 100644 window-slot-Force-reload-current-location-when-it-re.patch

diff --git a/file-utilities-Prevent-passing-NULL-to-g_object_unre.patch b/file-utilities-Prevent-passing-NULL-to-g_object_unre.patch
new file mode 100644
index 0000000..3bce483
--- /dev/null
+++ b/file-utilities-Prevent-passing-NULL-to-g_object_unre.patch
@@ -0,0 +1,42 @@
+From e03d731e3dcb8d0f52ffbc6faa188802b742d1e9 Mon Sep 17 00:00:00 2001
+From: Ondrej Holy <oholy@redhat.com>
+Date: Thu, 26 Jan 2023 13:20:51 +0100
+Subject: [PATCH] file-utilities: Prevent passing NULL to g_object_unref
+
+The `nautilus_find_existing_uri_in_hierarchy` function calls the
+`g_object_unref` with a `NULL` pointer when `g_file_query_info` fails.
+This leads to a crash e.g. when parent directory of the currently
+opened location is removed. Let's port the code to use `g_autoptr` to
+avoid that.
+---
+ src/nautilus-file-utilities.c | 4 ++--
+ 1 file changed, 2 insertions(+), 2 deletions(-)
+
+diff --git a/src/nautilus-file-utilities.c b/src/nautilus-file-utilities.c
+index e8f1ca2fb..1c913dbad 100644
+--- a/src/nautilus-file-utilities.c
++++ b/src/nautilus-file-utilities.c
+@@ -598,7 +598,6 @@ nautilus_generate_unique_file_in_directory (GFile      *directory,
+ GFile *
+ nautilus_find_existing_uri_in_hierarchy (GFile *location)
+ {
+-    GFileInfo *info;
+     GFile *tmp;
+ 
+     g_assert (location != NULL);
+@@ -606,10 +605,11 @@ nautilus_find_existing_uri_in_hierarchy (GFile *location)
+     location = g_object_ref (location);
+     while (location != NULL)
+     {
++        g_autoptr (GFileInfo) info = NULL;
++
+         info = g_file_query_info (location,
+                                   G_FILE_ATTRIBUTE_STANDARD_NAME,
+                                   0, NULL, NULL);
+-        g_object_unref (info);
+         if (info != NULL)
+         {
+             return location;
+-- 
+2.40.0
+
diff --git a/nautilus.spec b/nautilus.spec
index 40a6674..c83312b 100644
--- a/nautilus.spec
+++ b/nautilus.spec
@@ -8,7 +8,7 @@
 
 Name:           nautilus
 Version:        3.28.1
-Release:        24%{?dist}
+Release:        25%{?dist}
 Summary:        File manager for GNOME
 
 License:        GPLv3+
@@ -91,6 +91,10 @@ Patch44:        freedesktop-dbus-Try-to-own-the-name-until-after-exp.patch
 
 # https://bugzilla.redhat.com/show_bug.cgi?id=2100426
 Patch45:        window-slot-Try-current-location-even-if-it-is-marke.patch
+Patch46:        window-slot-Force-reload-current-location-when-it-re.patch
+Patch47:        pathbar-Do-nothing-when-current-location-disappears.patch
+Patch48:        file-utilities-Prevent-passing-NULL-to-g_object_unre.patch
+Patch49:        window-slot-Fix-conditions-to-restore-selection-when.patch
 
 BuildRequires:  gtk-doc
 BuildRequires:  meson
@@ -204,6 +208,9 @@ desktop-file-validate $RPM_BUILD_ROOT%{_datadir}/applications/*.desktop
 %{_datadir}/gir-1.0/*.gir
 
 %changelog
+* Wed Jun 14 2023 Ondrej Holy <oholy@redhat.com> - 3.28.1-25
+- Force reload current location when it reappears (#2100426)
+
 * Wed Apr 05 2023 Ondrej Holy <oholy@redhat.com> - 3.28.1-24
 - Try current location even if it is marked as gone (#2100426)
 
diff --git a/pathbar-Do-nothing-when-current-location-disappears.patch b/pathbar-Do-nothing-when-current-location-disappears.patch
new file mode 100644
index 0000000..8c79fd3
--- /dev/null
+++ b/pathbar-Do-nothing-when-current-location-disappears.patch
@@ -0,0 +1,16 @@
+diff --git a/src/nautilus-pathbar.c b/src/nautilus-pathbar.c
+index 630b8ed33..2fe88fe1d 100644
+--- a/src/nautilus-pathbar.c
++++ b/src/nautilus-pathbar.c
+@@ -2033,7 +2033,7 @@ button_data_file_changed (NautilusFile *file,
+         if (g_file_has_prefix (current_location, location) ||
+             g_file_equal (current_location, location))
+         {
+-            nautilus_path_bar_clear_buttons (self);
++            /* Do nothing here, the view will set new path if needed. */
+         }
+         else if (g_file_has_prefix (location, current_location))
+         {
+-- 
+2.40.0
+
diff --git a/window-slot-Fix-conditions-to-restore-selection-when.patch b/window-slot-Fix-conditions-to-restore-selection-when.patch
new file mode 100644
index 0000000..d902d64
--- /dev/null
+++ b/window-slot-Fix-conditions-to-restore-selection-when.patch
@@ -0,0 +1,37 @@
+From 87d2f2cfd61baf813aee204be570172b78159281 Mon Sep 17 00:00:00 2001
+From: Ondrej Holy <oholy@redhat.com>
+Date: Wed, 14 Jun 2023 12:52:02 +0200
+Subject: [PATCH] window-slot: Fix conditions to restore selection when
+ reloading
+
+Currently, the `nautilus_window_slot_force_reload` function doesn't
+preserve selection or position. However, there is a code that should do
+it. The code is executed only when `new_content_view != NULL`, but it
+is based on the `content_view` property. This seems to be a regression
+caused by the commit 9806d70e. Let's fix that condition in order to
+ensure that the selection and position are restored.
+---
+ src/nautilus-window-slot.c | 4 ++--
+ 1 file changed, 2 insertions(+), 2 deletions(-)
+
+diff --git a/src/nautilus-window-slot.c b/src/nautilus-window-slot.c
+index 2bfb3f3ce..8ceac9562 100644
+--- a/src/nautilus-window-slot.c
++++ b/src/nautilus-window-slot.c
+@@ -2376,11 +2376,11 @@ nautilus_window_slot_force_reload (NautilusWindowSlot *self)
+     g_object_ref (location);
+     current_pos = NULL;
+ 
+-    if (priv->new_content_view)
++    if (priv->content_view)
+     {
+         selection = nautilus_view_get_selection (priv->content_view);
+ 
+-        if (NAUTILUS_IS_FILES_VIEW (priv->new_content_view))
++        if (NAUTILUS_IS_FILES_VIEW (priv->content_view))
+         {
+             current_pos = nautilus_files_view_get_first_visible_file (NAUTILUS_FILES_VIEW (priv->content_view));
+         }
+-- 
+2.40.0
+
diff --git a/window-slot-Force-reload-current-location-when-it-re.patch b/window-slot-Force-reload-current-location-when-it-re.patch
new file mode 100644
index 0000000..8e3780c
--- /dev/null
+++ b/window-slot-Force-reload-current-location-when-it-re.patch
@@ -0,0 +1,43 @@
+From f68481d2d8393f1ba1a9b0a86a1b28b6ac303a63 Mon Sep 17 00:00:00 2001
+From: Ondrej Holy <oholy@redhat.com>
+Date: Wed, 24 May 2023 13:09:35 +0200
+Subject: [PATCH] window-slot: Force reload current location when it reappears
+
+When the currently opened location disappears, nautilus tries to
+reopen it thanks to the commit b0e28bc1. However, the
+`nautilus_window_slot_open_location_full` function doesn't begin the
+location change when the old location is the same as the new one. This
+is a problem because the old `NautilusFile` object is marked as gone
+and thus monitoring and perhaps some other stuff won't work. Let's use
+the `nautilus_window_slot_force_reload` function instead, in this case,
+to ensure that the underlying `NautilusFile` is updated.
+---
+ src/nautilus-window-slot.c | 12 +++++++++++-
+ 1 file changed, 11 insertions(+), 1 deletion(-)
+
+diff --git a/src/nautilus-window-slot.c b/src/nautilus-window-slot.c
+index 811152a21..561b34f12 100644
+--- a/src/nautilus-window-slot.c
++++ b/src/nautilus-window-slot.c
+@@ -1458,7 +1458,17 @@ viewed_file_changed_callback (NautilusFile       *file,
+                 go_to_file = g_file_new_for_path (g_get_home_dir ());
+             }
+ 
+-            nautilus_window_slot_open_location_full (self, go_to_file, 0, NULL);
++            if (g_file_equal (location, go_to_file))
++            {
++                /* Path gone by time out may have been remounted by
++                 * `nautilus_find_existing_uri_in_hierarchy()`.
++                 */
++                nautilus_window_slot_force_reload (self);
++            }
++            else
++            {
++                nautilus_window_slot_open_location_full (self, go_to_file, 0, NULL);
++            }
+ 
+             g_object_unref (go_to_file);
+             g_object_unref (location);
+-- 
+2.40.0
+