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



commit eb606728c18a5ff8d9d468fe8bfab6ed4c18c343
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 | 121 ++++++++++++++++++++++++++---------------
 1 file changed, 76 insertions(+), 45 deletions(-)
---
diff --git a/src/nautilus-error-reporting.c b/src/nautilus-error-reporting.c
index e2e4cfe8b..2ba498395 100644
--- a/src/nautilus-error-reporting.c
+++ b/src/nautilus-error-reporting.c
@@ -35,6 +35,7 @@
 
 #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,
@@ -45,8 +46,8 @@ 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 +62,12 @@ nautilus_report_error_loading_directory (NautilusFile *file,
         return;
     }
 
-    file_name = nautilus_file_get_display_name (file);
+    {
+        g_autofree char *file_name = NULL;
+
+        file_name = nautilus_file_get_display_name (file);
+        truncated_name = eel_str_middle_truncate (file_name, MAXIMUM_DISPLAYED_FILE_NAME_LENGTH);
+    }
 
     if (error->domain == G_IO_ERROR)
     {
@@ -70,20 +76,27 @@ 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 *error_message = NULL;
+
+                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,
+                                           error_message);
+            }
+            break;
         }
     }
     else
@@ -92,9 +105,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 +112,21 @@ 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);
+    {
+        g_autofree char *file_name = NULL;
+
+        file_name = nautilus_file_get_display_name (file);
+        truncated_name = eel_str_middle_truncate (file_name, MAXIMUM_DISPLAYED_FILE_NAME_LENGTH);
+    }
 
-    message = NULL;
     if (error->domain == G_IO_ERROR)
     {
         switch (error->code)
@@ -120,7 +134,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 +147,20 @@ nautilus_report_error_setting_group (NautilusFile *file,
 
     if (message == NULL)
     {
+        g_autofree char *error_message = NULL;
+
+        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,
+                                   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 +168,26 @@ 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 *error_message = NULL;
+    g_autofree char *message = NULL;
 
     if (error == NULL)
     {
         return;
     }
 
-    file_name = nautilus_file_get_display_name (file);
+    {
+        g_autofree char *file_name = NULL;
 
-    message = g_strdup_printf (_("Sorry, could not change the owner of “%s”: %s"), file_name, 
error->message);
+        file_name = nautilus_file_get_display_name (file);
+        truncated_name = eel_str_middle_truncate (file_name, MAXIMUM_DISPLAYED_FILE_NAME_LENGTH);
+    }
 
-    eel_show_error_dialog (_("The owner could not be changed."), message, parent_window);
+    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, 
error_message);
 
-    g_free (file_name);
-    g_free (message);
+    eel_show_error_dialog (_("The owner could not be changed."), message, parent_window);
 }
 
 void
@@ -176,22 +195,26 @@ 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 *error_message = NULL;
+    g_autofree char *message = NULL;
 
     if (error == NULL)
     {
         return;
     }
 
-    file_name = nautilus_file_get_display_name (file);
+    {
+        g_autofree char *file_name = NULL;
+
+        file_name = nautilus_file_get_display_name (file);
+        truncated_name = eel_str_middle_truncate (file_name, MAXIMUM_DISPLAYED_FILE_NAME_LENGTH);
+    }
 
-    message = g_strdup_printf (_("Sorry, could not change the permissions of “%s”: %s"), file_name, 
error->message);
+    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, 
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
@@ -282,13 +305,17 @@ nautilus_report_error_renaming_file (NautilusFile *file,
 
     if (message == NULL)
     {
+        g_autofree char *error_message = NULL;
+
+        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);
+                                   error_message);
     }
 
     g_free (original_name_truncated);
@@ -377,10 +404,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 +417,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 +429,21 @@ 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);
+    {
+        g_autofree char *old_name = NULL;
+
+        old_name = nautilus_file_get_display_name (file);
+        truncated_old_name = eel_str_middle_truncate (old_name, MAXIMUM_DISPLAYED_FILE_NAME_LENGTH);
+    }
+    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]