[nautilus/ellipsization-truncation-a-united-nation] error-reporting: truncate messages used in dialogs



commit d3e5f27de17e8a126031381ecf8953193bc91d08
Author: Ernestas Kulik <ernestask gnome org>
Date:   Mon Feb 19 21:33:16 2018 +0200

    error-reporting: truncate messages used in dialogs
    
    This is most notably a problem with paths that are approaching PATH_MAX
    in length. Cairo surface creation fails due to its internal size limit
    being reached and GTK+ promptly crashes after trying to dereference a
    null pointer. Besides, the window can get ridiculously long even if
    Nautilus doesn’t crash.

 src/nautilus-error-reporting.c | 147 +++++++++++++++++++++++------------------
 1 file changed, 82 insertions(+), 65 deletions(-)
---
diff --git a/src/nautilus-error-reporting.c b/src/nautilus-error-reporting.c
index e2e4cfe8b..a2426d28b 100644
--- a/src/nautilus-error-reporting.c
+++ b/src/nautilus-error-reporting.c
@@ -35,18 +35,31 @@
 
 #define NEW_NAME_TAG "Nautilus: new name"
 #define MAXIMUM_DISPLAYED_FILE_NAME_LENGTH      50
+#define MAXIMUM_DISPLAYED_ERROR_MESSAGE_LENGTH  (MAXIMUM_DISPLAYED_FILE_NAME_LENGTH * 2)
 
 static void finish_rename (NautilusFile *file,
                            gboolean      stop_timer,
                            GError       *error);
 
+static char *
+get_truncated_name_for_file (NautilusFile *file)
+{
+    g_autofree char *file_name = NULL;
+
+    g_assert (NAUTILUS_IS_FILE (file));
+
+    file_name = nautilus_file_get_display_name (file);
+
+    return eel_str_middle_truncate (file_name, MAXIMUM_DISPLAYED_FILE_NAME_LENGTH);
+}
+
 void
 nautilus_report_error_loading_directory (NautilusFile *file,
                                          GError       *error,
                                          GtkWindow    *parent_window)
 {
-    char *file_name;
-    char *message;
+    g_autofree char *truncated_name = NULL;
+    g_autofree char *message = NULL;
 
     if (error == NULL ||
         error->message == NULL)
@@ -61,7 +74,7 @@ nautilus_report_error_loading_directory (NautilusFile *file,
         return;
     }
 
-    file_name = nautilus_file_get_display_name (file);
+    truncated_name = get_truncated_name_for_file (file);
 
     if (error->domain == G_IO_ERROR)
     {
@@ -70,20 +83,28 @@ nautilus_report_error_loading_directory (NautilusFile *file,
             case G_IO_ERROR_PERMISSION_DENIED:
             {
                 message = g_strdup_printf (_("You do not have the permissions necessary to view the contents 
of “%s”."),
-                                           file_name);
+                                           truncated_name);
             }
             break;
 
             case G_IO_ERROR_NOT_FOUND:
             {
                 message = g_strdup_printf (_("“%s” could not be found. Perhaps it has recently been 
deleted."),
-                                           file_name);
+                                           truncated_name);
             }
             break;
 
             default:
-                message = g_strdup_printf (_("Sorry, could not display all the contents of “%s”: %s"), 
file_name,
-                                           error->message);
+            {
+                g_autofree char *truncated_error_message = NULL;
+
+                truncated_error_message = eel_str_end_truncate (error->message,
+                                                                MAXIMUM_DISPLAYED_ERROR_MESSAGE_LENGTH);
+
+                message = g_strdup_printf (_("Sorry, could not display all the contents of “%s”: %s"), 
truncated_name,
+                                           truncated_error_message);
+            }
+            break;
         }
     }
     else
@@ -92,9 +113,6 @@ nautilus_report_error_loading_directory (NautilusFile *file,
     }
 
     eel_show_error_dialog (_("This location could not be displayed."), message, parent_window);
-
-    g_free (file_name);
-    g_free (message);
 }
 
 void
@@ -102,17 +120,16 @@ nautilus_report_error_setting_group (NautilusFile *file,
                                      GError       *error,
                                      GtkWindow    *parent_window)
 {
-    char *file_name;
-    char *message;
+    g_autofree char *truncated_name = NULL;
+    g_autofree char *message = NULL;
 
     if (error == NULL)
     {
         return;
     }
 
-    file_name = nautilus_file_get_display_name (file);
+    truncated_name = get_truncated_name_for_file (file);
 
-    message = NULL;
     if (error->domain == G_IO_ERROR)
     {
         switch (error->code)
@@ -120,7 +137,7 @@ nautilus_report_error_setting_group (NautilusFile *file,
             case G_IO_ERROR_PERMISSION_DENIED:
             {
                 message = g_strdup_printf (_("You do not have the permissions necessary to change the group 
of “%s”."),
-                                           file_name);
+                                           truncated_name);
             }
             break;
 
@@ -133,19 +150,21 @@ nautilus_report_error_setting_group (NautilusFile *file,
 
     if (message == NULL)
     {
+        g_autofree char *truncated_error_message = NULL;
+
+        truncated_error_message = eel_str_end_truncate (error->message,
+                                                        MAXIMUM_DISPLAYED_ERROR_MESSAGE_LENGTH);
+
         /* We should invent decent error messages for every case we actually experience. */
         g_warning ("Hit unhandled case %s:%d in nautilus_report_error_setting_group",
                    g_quark_to_string (error->domain), error->code);
         /* fall through */
-        message = g_strdup_printf (_("Sorry, could not change the group of “%s”: %s"), file_name,
-                                   error->message);
+        message = g_strdup_printf (_("Sorry, could not change the group of “%s”: %s"), truncated_name,
+                                   truncated_error_message);
     }
 
 
     eel_show_error_dialog (_("The group could not be changed."), message, parent_window);
-
-    g_free (file_name);
-    g_free (message);
 }
 
 void
@@ -153,22 +172,23 @@ nautilus_report_error_setting_owner (NautilusFile *file,
                                      GError       *error,
                                      GtkWindow    *parent_window)
 {
-    char *file_name;
-    char *message;
+    g_autofree char *truncated_name = NULL;
+    g_autofree char *truncated_error_message = NULL;
+    g_autofree char *message = NULL;
 
     if (error == NULL)
     {
         return;
     }
 
-    file_name = nautilus_file_get_display_name (file);
+    truncated_name = get_truncated_name_for_file (file);
 
-    message = g_strdup_printf (_("Sorry, could not change the owner of “%s”: %s"), file_name, 
error->message);
+    truncated_error_message = eel_str_end_truncate (error->message,
+                                                    MAXIMUM_DISPLAYED_ERROR_MESSAGE_LENGTH);
+    message = g_strdup_printf (_("Sorry, could not change the owner of “%s”: %s"),
+                               truncated_name, truncated_error_message);
 
     eel_show_error_dialog (_("The owner could not be changed."), message, parent_window);
-
-    g_free (file_name);
-    g_free (message);
 }
 
 void
@@ -176,22 +196,23 @@ nautilus_report_error_setting_permissions (NautilusFile *file,
                                            GError       *error,
                                            GtkWindow    *parent_window)
 {
-    char *file_name;
-    char *message;
+    g_autofree char *truncated_name = NULL;
+    g_autofree char *truncated_error_message = NULL;
+    g_autofree char *message = NULL;
 
     if (error == NULL)
     {
         return;
     }
 
-    file_name = nautilus_file_get_display_name (file);
+    truncated_name = get_truncated_name_for_file (file);
 
-    message = g_strdup_printf (_("Sorry, could not change the permissions of “%s”: %s"), file_name, 
error->message);
+    truncated_error_message = eel_str_end_truncate (error->message,
+                                                    MAXIMUM_DISPLAYED_ERROR_MESSAGE_LENGTH);
+    message = g_strdup_printf (_("Sorry, could not change the permissions of “%s”: %s"),
+                               truncated_name, truncated_error_message);
 
     eel_show_error_dialog (_("The permissions could not be changed."), message, parent_window);
-
-    g_free (file_name);
-    g_free (message);
 }
 
 typedef struct _NautilusRenameData
@@ -207,20 +228,16 @@ nautilus_report_error_renaming_file (NautilusFile *file,
                                      GError       *error,
                                      GtkWindow    *parent_window)
 {
-    char *original_name, *original_name_truncated;
-    char *new_name_truncated;
-    char *message;
+    g_autofree char *truncated_old_name = NULL;
+    g_autofree char *truncated_new_name = NULL;
+    g_autofree char *message = NULL;
 
     /* Truncate names for display since very long file names with no spaces
      * in them won't get wrapped, and can create insanely wide dialog boxes.
      */
-    original_name = nautilus_file_get_display_name (file);
-    original_name_truncated = eel_str_middle_truncate (original_name, MAXIMUM_DISPLAYED_FILE_NAME_LENGTH);
-    g_free (original_name);
-
-    new_name_truncated = eel_str_middle_truncate (new_name, MAXIMUM_DISPLAYED_FILE_NAME_LENGTH);
+    truncated_old_name = get_truncated_name_for_file (file);
+    truncated_new_name = eel_str_middle_truncate (new_name, MAXIMUM_DISPLAYED_FILE_NAME_LENGTH);
 
-    message = NULL;
     if (error->domain == G_IO_ERROR)
     {
         switch (error->code)
@@ -229,7 +246,7 @@ nautilus_report_error_renaming_file (NautilusFile *file,
             {
                 message = g_strdup_printf (_("The name “%s” is already used in this location. "
                                              "Please use a different name."),
-                                           new_name_truncated);
+                                           truncated_new_name);
             }
             break;
 
@@ -237,14 +254,14 @@ nautilus_report_error_renaming_file (NautilusFile *file,
             {
                 message = g_strdup_printf (_("There is no “%s” in this location. "
                                              "Perhaps it was just moved or deleted?"),
-                                           original_name_truncated);
+                                           truncated_old_name);
             }
             break;
 
             case G_IO_ERROR_PERMISSION_DENIED:
             {
                 message = g_strdup_printf (_("You do not have the permissions necessary to rename “%s”."),
-                                           original_name_truncated);
+                                           truncated_old_name);
             }
             break;
 
@@ -254,13 +271,13 @@ nautilus_report_error_renaming_file (NautilusFile *file,
                 {
                     message = g_strdup_printf (_("The name “%s” is not valid because it contains the 
character “/”. "
                                                  "Please use a different name."),
-                                               new_name_truncated);
+                                               truncated_new_name);
                 }
                 else
                 {
                     message = g_strdup_printf (_("The name “%s” is not valid. "
                                                  "Please use a different name."),
-                                               new_name_truncated);
+                                               truncated_new_name);
                 }
             }
             break;
@@ -269,7 +286,7 @@ nautilus_report_error_renaming_file (NautilusFile *file,
             {
                 message = g_strdup_printf (_("The name “%s” is too long. "
                                              "Please use a different name."),
-                                           new_name_truncated);
+                                           truncated_new_name);
             }
             break;
 
@@ -282,20 +299,21 @@ nautilus_report_error_renaming_file (NautilusFile *file,
 
     if (message == NULL)
     {
+        g_autofree char *truncated_error_message = NULL;
+
+        truncated_error_message = eel_str_end_truncate (error->message,
+                                                        MAXIMUM_DISPLAYED_ERROR_MESSAGE_LENGTH);
+
         /* We should invent decent error messages for every case we actually experience. */
         g_warning ("Hit unhandled case %s:%d in nautilus_report_error_renaming_file",
                    g_quark_to_string (error->domain), error->code);
         /* fall through */
         message = g_strdup_printf (_("Sorry, could not rename “%s” to “%s”: %s"),
-                                   original_name_truncated, new_name_truncated,
-                                   error->message);
+                                   truncated_old_name, truncated_new_name,
+                                   truncated_error_message);
     }
 
-    g_free (original_name_truncated);
-    g_free (new_name_truncated);
-
     eel_show_error_dialog (_("The item could not be renamed."), message, parent_window);
-    g_free (message);
 }
 
 static void
@@ -377,10 +395,12 @@ nautilus_rename_file (NautilusFile                  *file,
                       NautilusFileOperationCallback  callback,
                       gpointer                       callback_data)
 {
-    char *old_name, *wait_message;
+    g_autoptr (GError) error = NULL;
     NautilusRenameData *data;
-    char *uri;
-    GError *error;
+    g_autofree char *truncated_old_name = NULL;
+    g_autofree char *truncated_new_name = NULL;
+    g_autofree char *wait_message = NULL;
+    g_autofree char *uri = NULL;
 
     g_return_if_fail (NAUTILUS_IS_FILE (file));
     g_return_if_fail (new_name != NULL);
@@ -388,7 +408,6 @@ nautilus_rename_file (NautilusFile                  *file,
     /* Stop any earlier rename that's already in progress. */
     error = g_error_new (G_IO_ERROR, G_IO_ERROR_CANCELLED, "Cancelled");
     finish_rename (file, TRUE, error);
-    g_error_free (error);
 
     data = g_new0 (NautilusRenameData, 1);
     data->name = g_strdup (new_name);
@@ -401,18 +420,16 @@ nautilus_rename_file (NautilusFile                  *file,
                             data, (GDestroyNotify) nautilus_rename_data_free);
 
     /* Start the timed wait to cancel the rename. */
-    old_name = nautilus_file_get_display_name (file);
+    truncated_old_name = get_truncated_name_for_file (file);
+    truncated_new_name = eel_str_middle_truncate (new_name, MAXIMUM_DISPLAYED_FILE_NAME_LENGTH);
     wait_message = g_strdup_printf (_("Renaming “%s” to “%s”."),
-                                    old_name,
-                                    new_name);
-    g_free (old_name);
+                                    truncated_old_name,
+                                    truncated_new_name);
     eel_timed_wait_start (cancel_rename_callback, file, wait_message,
                           NULL);     /* FIXME bugzilla.gnome.org 42395: Parent this? */
-    g_free (wait_message);
 
     uri = nautilus_file_get_uri (file);
     DEBUG ("Renaming file %s to %s", uri, new_name);
-    g_free (uri);
 
     /* Start the rename. */
     nautilus_file_rename (file, new_name,


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