]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkfilechooserdefault.c
Display an error when we come to the root.
[~andy/gtk] / gtk / gtkfilechooserdefault.c
index 03f740df583c476f624e0968f1ebbb0be6e8a97f..c5338b9b69ef938d9d21ae5188e07bc943c09c88 100644 (file)
@@ -544,8 +544,7 @@ _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
                                GDK_l, GDK_CONTROL_MASK,
                                "location-popup",
                                1, G_TYPE_STRING, "");
-  /* FMQ: remove this? */
-#if 0
+
   gtk_binding_entry_add_signal (binding_set,
                                GDK_slash, 0,
                                "location-popup",
@@ -560,7 +559,6 @@ _gtk_file_chooser_default_class_init (GtkFileChooserDefaultClass *class)
                                GDK_asciitilde, 0,
                                "location-popup",
                                1, G_TYPE_STRING, "~");
-#endif
 #endif
 
   gtk_binding_entry_add_signal (binding_set,
@@ -1907,7 +1905,11 @@ shortcuts_add_current_folder (GtkFileChooserDefault *impl)
          shortcuts_insert_path (impl, pos, TRUE, volume, NULL, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
        }
       else
-       shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
+        {
+         shortcuts_insert_path (impl, pos, FALSE, NULL, impl->current_folder, NULL, FALSE, SHORTCUTS_CURRENT_FOLDER);
+         if (volume)
+           gtk_file_system_volume_free (impl->file_system, volume);
+       }
 
       if (base_path)
        gtk_file_path_free (base_path);
@@ -1967,7 +1969,7 @@ shortcuts_model_create (GtkFileChooserDefault *impl)
                                              G_TYPE_BOOLEAN,   /* is the previous column a volume? */
                                              G_TYPE_BOOLEAN,   /* removable */
                                              G_TYPE_BOOLEAN,   /* pixbuf cell visibility */
-                                             G_TYPE_OBJECT);   /* GtkFileSystemHandle */
+                                             G_TYPE_POINTER);   /* GtkFileSystemHandle */
 
   if (impl->file_system)
     {
@@ -3160,8 +3162,6 @@ tree_view_keybinding_cb (GtkWidget             *tree_view,
                         GdkEventKey           *event,
                         GtkFileChooserDefault *impl)
 {
-  /* FMQ: remove this? */
-#if 0
   if ((event->keyval == GDK_slash
        || event->keyval == GDK_KP_Divide
 #ifdef G_OS_UNIX
@@ -3172,7 +3172,7 @@ tree_view_keybinding_cb (GtkWidget             *tree_view,
       location_popup_handler (impl, event->string);
       return TRUE;
     }
-#endif
+
   return FALSE;
 }
 
@@ -3544,8 +3544,6 @@ trap_activate_cb (GtkWidget   *widget,
 
   modifiers = gtk_accelerator_get_default_mod_mask ();
 
-  /* FMQ: remove this? */
-#if 0
   if ((event->keyval == GDK_slash
        || event->keyval == GDK_KP_Divide
 #ifdef G_OS_UNIX
@@ -3556,7 +3554,6 @@ trap_activate_cb (GtkWidget   *widget,
       location_popup_handler (impl, event->string);
       return TRUE;
     }
-#endif
 
   if ((event->keyval == GDK_Return
        || event->keyval == GDK_ISO_Enter
@@ -5644,7 +5641,7 @@ load_timeout_cb (gpointer data)
   return FALSE;
 }
 
-/* Sets up a new load timer for the model and switches to the LOAD_LOADING state */
+/* Sets up a new load timer for the model and switches to the LOAD_PRELOAD state */
 static void
 load_setup_timer (GtkFileChooserDefault *impl)
 {
@@ -5790,6 +5787,7 @@ show_and_select_paths_finished_loading (GtkFileFolder *folder,
 
   browse_files_center_selected_row (data->impl);
 
+  g_object_unref (data->impl);
   gtk_file_paths_free (data->paths);
   g_free (data);
 }
@@ -6140,6 +6138,8 @@ struct UpdateCurrentFolderData
   GtkFileChooserDefault *impl;
   GtkFilePath *path;
   gboolean keep_trail;
+  GtkFilePath *original_path;
+  GError *original_error;
 };
 
 static void
@@ -6165,8 +6165,52 @@ update_current_folder_get_info_cb (GtkFileSystemHandle *handle,
 
   if (error)
     {
-      error_changing_folder_dialog (impl, data->path, g_error_copy (error));
-      goto out;
+      GtkFilePath *parent_path;
+
+      if (!data->original_path)
+        {
+         data->original_path = gtk_file_path_copy (data->path);
+         data->original_error = g_error_copy (error);
+       }
+
+      /* get parent path and try to change the folder to that */
+      if (gtk_file_system_get_parent (impl->file_system, data->path, &parent_path, NULL) &&
+         parent_path != NULL)
+        {
+         gtk_file_path_free (data->path);
+         data->path = parent_path;
+
+         g_object_unref (handle);
+
+         /* restart the update current folder operation */
+         impl->reload_state = RELOAD_HAS_FOLDER;
+
+         impl->update_current_folder_handle =
+           gtk_file_system_get_info (impl->file_system, data->path,
+                                     GTK_FILE_INFO_IS_FOLDER,
+                                     update_current_folder_get_info_cb,
+                                     data);
+
+         set_busy_cursor (impl, TRUE);
+
+         return;
+       }
+      else
+        {
+         /* error and bail out */
+         error_changing_folder_dialog (impl, data->original_path, data->original_error);
+
+         gtk_file_path_free (data->original_path);
+
+         goto out;
+       }
+    }
+
+  if (data->original_path)
+    {
+      error_changing_folder_dialog (impl, data->original_path, data->original_error);
+
+      gtk_file_path_free (data->original_path);
     }
 
   if (!gtk_file_info_get_is_folder (info))
@@ -7329,7 +7373,7 @@ out:
   g_object_unref (handle);
 }
 
-struct SaveEntryData
+struct FileExistsData
 {
   GtkFileChooserDefault *impl;
   gboolean file_exists_and_is_not_folder;
@@ -7345,7 +7389,7 @@ save_entry_get_info_cb (GtkFileSystemHandle *handle,
 {
   gboolean parent_is_folder;
   gboolean cancelled = handle->cancelled;
-  struct SaveEntryData *data = user_data;
+  struct FileExistsData *data = user_data;
 
   if (handle != data->impl->should_respond_get_info_handle)
     goto out;
@@ -7408,6 +7452,72 @@ out:
   g_object_unref (handle);
 }
 
+static void
+file_exists_get_info_cb (GtkFileSystemHandle *handle,
+                        const GtkFileInfo   *info,
+                        const GError        *error,
+                        gpointer             user_data)
+{
+  gboolean data_ownership_taken = FALSE;
+  gboolean cancelled = handle->cancelled;
+  gboolean file_exists_and_is_not_folder;
+  struct FileExistsData *data = user_data;
+
+  if (handle != data->impl->file_exists_get_info_handle)
+    goto out;
+
+  data->impl->file_exists_get_info_handle = NULL;
+
+  set_busy_cursor (data->impl, FALSE);
+
+  if (cancelled)
+    goto out;
+
+  file_exists_and_is_not_folder = info && !gtk_file_info_get_is_folder (info);
+
+  if (data->impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
+    /* user typed a filename; we are done */
+    g_signal_emit_by_name (data->impl, "response-requested");
+  else if (data->impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
+          && file_exists_and_is_not_folder)
+    {
+      /* Oops, the user typed the name of an existing path which is not
+       * a folder
+       */
+      error_creating_folder_over_existing_file_dialog (data->impl, data->path,
+                                                      g_error_copy (error));
+    }
+  else
+    {
+      /* check that everything up to the last component exists */
+
+      data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
+      data_ownership_taken = TRUE;
+
+      if (data->impl->should_respond_get_info_handle)
+       gtk_file_system_cancel_operation (data->impl->should_respond_get_info_handle);
+
+      data->impl->should_respond_get_info_handle =
+       gtk_file_system_get_info (data->impl->file_system,
+                                 data->parent_path,
+                                 GTK_FILE_INFO_IS_FOLDER,
+                                 save_entry_get_info_cb,
+                                 data);
+      set_busy_cursor (data->impl, TRUE);
+    }
+
+out:
+  if (!data_ownership_taken)
+    {
+      g_object_unref (data->impl);
+      gtk_file_path_free (data->path);
+      gtk_file_path_free (data->parent_path);
+      g_free (data);
+    }
+
+  g_object_unref (handle);
+}
+
 static void
 paste_text_received (GtkClipboard          *clipboard,
                     const gchar           *text,
@@ -7595,46 +7705,26 @@ gtk_file_chooser_default_should_respond (GtkFileChooserEmbed *chooser_embed)
        }
       else
        {
-         gboolean file_exists_and_is_not_folder;
-
-         file_exists_and_is_not_folder = g_error_matches (error, GTK_FILE_SYSTEM_ERROR, GTK_FILE_SYSTEM_ERROR_NOT_FOLDER);
-
-         if (impl->action == GTK_FILE_CHOOSER_ACTION_OPEN)
-           retval = TRUE; /* user typed a filename; we are done */
-         else if (impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER && file_exists_and_is_not_folder)
-           {
-             /* Oops, the user typed the name of an existing path which is not a folder */
-             error_creating_folder_over_existing_file_dialog (impl, path, error);
-             error = NULL; /* as it will be freed below for the general case */
-             retval = FALSE;
-           }
-         else
-           {
-             GtkFilePath *parent_path;
-             struct SaveEntryData *data;
+         struct FileExistsData *data;
 
-             /* check that everything up to the last component exists */
+         /* We need to check whether path exists and is not a folder */
 
-             parent_path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
+         data = g_new0 (struct FileExistsData, 1);
+         data->impl = g_object_ref (impl);
+         data->path = gtk_file_path_copy (path);
+         data->parent_path = gtk_file_path_copy (_gtk_file_chooser_entry_get_current_folder (entry));
 
-             data = g_new0 (struct SaveEntryData, 1);
-             data->impl = g_object_ref (impl);
-             data->file_exists_and_is_not_folder = file_exists_and_is_not_folder;
-             data->parent_path = parent_path; /* Takes ownership */
-             data->path = gtk_file_path_copy (path);
+         if (impl->file_exists_get_info_handle)
+           gtk_file_system_cancel_operation (impl->file_exists_get_info_handle);
 
-             if (impl->should_respond_get_info_handle)
-               gtk_file_system_cancel_operation (impl->should_respond_get_info_handle);
+         impl->file_exists_get_info_handle =
+           gtk_file_system_get_info (impl->file_system, path,
+                                     GTK_FILE_INFO_IS_FOLDER,
+                                     file_exists_get_info_cb,
+                                     data);
 
-             impl->should_respond_get_info_handle =
-               gtk_file_system_get_info (impl->file_system, parent_path,
-                                         GTK_FILE_INFO_IS_FOLDER,
-                                         save_entry_get_info_cb,
-                                         data);
-             set_busy_cursor (impl, TRUE);
-
-             retval = FALSE;
-           }
+         set_busy_cursor (impl, TRUE);
+         retval = FALSE;
 
          if (error != NULL)
            g_error_free (error);
@@ -8381,6 +8471,14 @@ _gtk_file_chooser_default_new (const char *file_system)
                        NULL);
 }
 
+static void
+location_set_user_text (GtkFileChooserDefault *impl,
+                       const gchar           *path)
+{
+  _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->location_entry), path);
+  gtk_editable_set_position (GTK_EDITABLE (impl->location_entry), -1);
+}
+
 static void
 location_popup_handler (GtkFileChooserDefault *impl,
                        const gchar           *path)
@@ -8390,7 +8488,12 @@ location_popup_handler (GtkFileChooserDefault *impl,
     {
       LocationMode new_mode;
 
-      if (impl->location_mode == LOCATION_MODE_PATH_BAR)
+      if (path != NULL)
+       {
+         /* since the user typed something, we unconditionally want to turn on the entry */
+         new_mode = LOCATION_MODE_FILENAME_ENTRY;
+       }
+      else if (impl->location_mode == LOCATION_MODE_PATH_BAR)
        new_mode = LOCATION_MODE_FILENAME_ENTRY;
       else if (impl->location_mode == LOCATION_MODE_FILENAME_ENTRY)
        new_mode = LOCATION_MODE_PATH_BAR;
@@ -8403,13 +8506,22 @@ location_popup_handler (GtkFileChooserDefault *impl,
       location_mode_set (impl, new_mode, TRUE);
       if (new_mode == LOCATION_MODE_FILENAME_ENTRY)
        {
-         location_entry_set_initial_text (impl);
-         gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
+         if (path != NULL)
+           location_set_user_text (impl, path);
+         else
+           {
+             location_entry_set_initial_text (impl);
+             gtk_editable_select_region (GTK_EDITABLE (impl->location_entry), 0, -1);
+           }
        }
     }
   else if (impl->action == GTK_FILE_CHOOSER_ACTION_SAVE
           || impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
-    gtk_widget_grab_focus (impl->location_entry);
+    {
+      gtk_widget_grab_focus (impl->location_entry);
+      if (path != NULL)
+       location_set_user_text (impl, path);
+    }
   else
     g_assert_not_reached ();
 }
@@ -8559,6 +8671,3 @@ shortcuts_model_filter_new (GtkFileChooserDefault *impl,
 
   return GTK_TREE_MODEL (model);
 }
-
-#define __GTK_FILE_CHOOSER_DEFAULT_C__
-#include "gtkaliasdef.c"