filechooser: Do not look up parents of directories
authorBarnabás Pőcze <pobrn@protonmail.com>
Wed, 8 Mar 2023 19:20:35 +0000 (20:20 +0100)
committerBarnabás Pőcze <pobrn@protonmail.com>
Wed, 8 Mar 2023 19:46:17 +0000 (20:46 +0100)
If the `GtkRecentInfo` represents a directory, simply use it, and
do not try to find its parent in `_gtk_file_chooser_extract_recent_folders()`.

For example, there is an entry in my recently-used database
from the Amberol music player about the folder I have opened
with it, but the folder is not listed on the "Recent" tab of
the file chooser widget, only its parent. After this change,
the directory itself is shown.

gtk/gtkfilechooserwidget.c

index f2c76d56ee8b8fa206875826d739ed10b91dd1f8..3a0dc6e41b0c1c0ffdb578e88cdf1eecdf7eff7f 100644 (file)
@@ -637,8 +637,8 @@ _gtk_file_chooser_extract_recent_folders (GList *infos)
   for (l = infos; l; l = l->next)
     {
       GtkRecentInfo *info = l->data;
-      const char *uri;
-      GFile *parent;
+      const char *uri, *mime_type;
+      GFile *dir;
       GFile *file;
 
       if (!gtk_recent_info_is_local (info))
@@ -646,18 +646,27 @@ _gtk_file_chooser_extract_recent_folders (GList *infos)
 
       uri = gtk_recent_info_get_uri (info);
       file = g_file_new_for_uri (uri);
-      parent = g_file_get_parent (file);
-      g_object_unref (file);
 
-      if (parent)
+      mime_type = gtk_recent_info_get_mime_type (info);
+      if (strcmp (mime_type, "inode/directory") != 0)
+        {
+          dir = g_file_get_parent (file);
+          g_object_unref (file);
+        }
+      else
+        {
+          dir = file;
+        }
+
+      if (dir)
         {
-          if (!g_hash_table_lookup (folders, parent))
+          if (!g_hash_table_lookup (folders, dir))
             {
-              g_hash_table_insert (folders, parent, (gpointer) 1);
-              result = g_list_prepend (result, g_object_ref (parent));
+              g_hash_table_insert (folders, dir, (gpointer) 1);
+              result = g_list_prepend (result, g_object_ref (dir));
             }
 
-          g_object_unref (parent);
+          g_object_unref (dir);
         }
     }