[nautilus/antoniof/file-name-widget-patches: 3/5] file-name-widget: Clarify logic in name_is_valid()



commit 5198b5e2104f7f7a538c6bd03a50f17561ec2ef7
Author: António Fernandes <antoniof gnome org>
Date:   Tue Jul 31 18:27:28 2018 +0100

    file-name-widget: Clarify logic in name_is_valid()
    
    We rely on the presence of an error_message to decide whether the
    name is valid.
    
    However, this is misleading, because we can have an invalid name with
    no error_message (the empty name case), and we can have a valid name
    with an error_message (the dotfile case), requiring 2 early returns.
    
    Instead, use an explicit return variable.

 src/nautilus-compress-dialog-controller.c     | 19 +++++++++++++------
 src/nautilus-file-name-widget-controller.c    | 18 ++++++++++++------
 src/nautilus-new-folder-dialog-controller.c   | 17 ++++++++++++-----
 src/nautilus-rename-file-popover-controller.c | 18 ++++++++++++------
 4 files changed, 49 insertions(+), 23 deletions(-)
---
diff --git a/src/nautilus-compress-dialog-controller.c b/src/nautilus-compress-dialog-controller.c
index fc8264d26..d551c37bc 100644
--- a/src/nautilus-compress-dialog-controller.c
+++ b/src/nautilus-compress-dialog-controller.c
@@ -49,29 +49,36 @@ nautilus_compress_dialog_controller_name_is_valid (NautilusFileNameWidgetControl
                                                    gchar                             *name,
                                                    gchar                            **error_message)
 {
+    gboolean is_valid;
+
+    is_valid = TRUE;
     if (strlen (name) == 0)
     {
-        return FALSE;
+        is_valid = FALSE;
     }
-
-    if (strstr (name, "/") != NULL)
+    else if (strstr (name, "/") != NULL)
     {
+        is_valid = FALSE;
         *error_message = _("Archive names cannot contain “/”.");
     }
     else if (strcmp (name, ".") == 0)
     {
+        is_valid = FALSE;
         *error_message = _("An archive cannot be called “.”.");
     }
     else if (strcmp (name, "..") == 0)
     {
+        is_valid = FALSE;
         *error_message = _("An archive cannot be called “..”.");
     }
-    else if (g_str_has_prefix (name, "."))
+
+    if (is_valid && g_str_has_prefix (name, "."))
     {
+        /* We must warn about the side effect */
         *error_message = _("Archives with “.” at the beginning of their name are hidden.");
-        return TRUE;
     }
-    return *error_message == NULL;
+
+    return is_valid;
 }
 
 static gchar *
diff --git a/src/nautilus-file-name-widget-controller.c b/src/nautilus-file-name-widget-controller.c
index 6d04ce1ed..eb1007613 100644
--- a/src/nautilus-file-name-widget-controller.c
+++ b/src/nautilus-file-name-widget-controller.c
@@ -105,30 +105,36 @@ real_name_is_valid (NautilusFileNameWidgetController  *self,
                     gchar                             *name,
                     gchar                            **error_message)
 {
+    gboolean is_valid;
+
+    is_valid = TRUE;
     if (strlen (name) == 0)
     {
-        return FALSE;
+        is_valid = FALSE;
     }
-
-    if (strstr (name, "/") != NULL)
+    else if (strstr (name, "/") != NULL)
     {
+        is_valid = FALSE;
         *error_message = _("File names cannot contain “/”.");
     }
     else if (strcmp (name, ".") == 0)
     {
+        is_valid = FALSE;
         *error_message = _("A file cannot be called “.”.");
     }
     else if (strcmp (name, "..") == 0)
     {
+        is_valid = FALSE;
         *error_message = _("A file cannot be called “..”.");
     }
-    else if (g_str_has_prefix (name, "."))
+
+    if (is_valid && g_str_has_prefix (name, "."))
     {
+        /* We must warn about the side effect */
         *error_message = _("Files with “.” at the beginning of their name are hidden.");
-        return TRUE;
     }
 
-    return *error_message == NULL;
+    return is_valid;
 }
 
 static gboolean
diff --git a/src/nautilus-new-folder-dialog-controller.c b/src/nautilus-new-folder-dialog-controller.c
index 14d46a5ec..52b6e217b 100644
--- a/src/nautilus-new-folder-dialog-controller.c
+++ b/src/nautilus-new-folder-dialog-controller.c
@@ -42,30 +42,37 @@ nautilus_new_folder_dialog_controller_name_is_valid (NautilusFileNameWidgetContr
                                                      gchar                             *name,
                                                      gchar                            **error_message)
 {
+    gboolean is_valid;
+
+    is_valid = TRUE;
     if (strlen (name) == 0)
     {
-        return FALSE;
+        is_valid = FALSE;
     }
-
-    if (strstr (name, "/") != NULL)
+    else if (strstr (name, "/") != NULL)
     {
+        is_valid = FALSE;
         *error_message = _("Folder names cannot contain “/”.");
     }
     else if (strcmp (name, ".") == 0)
     {
+        is_valid = FALSE;
         *error_message = _("A folder cannot be called “.”.");
     }
     else if (strcmp (name, "..") == 0)
     {
+        is_valid = FALSE;
         *error_message = _("A folder cannot be called “..”.");
     }
-    else if (g_str_has_prefix (name, "."))
+
+    if (is_valid && g_str_has_prefix (name, "."))
     {
+        /* We must warn about the side effect */
         *error_message = _("Folders with “.” at the beginning of their name are hidden.");
         return TRUE;
     }
 
-    return *error_message == NULL;
+    return is_valid;
 }
 
 static void
diff --git a/src/nautilus-rename-file-popover-controller.c b/src/nautilus-rename-file-popover-controller.c
index f93421c51..e4fd9aa17 100644
--- a/src/nautilus-rename-file-popover-controller.c
+++ b/src/nautilus-rename-file-popover-controller.c
@@ -106,19 +106,21 @@ nautilus_rename_file_popover_controller_name_is_valid (NautilusFileNameWidgetCon
     NautilusDirectory *directory;
     glong max_name_length;
     size_t name_length;
+    gboolean is_valid;
 
     self = NAUTILUS_RENAME_FILE_POPOVER_CONTROLLER (controller);
     directory = nautilus_file_get_directory (self->target_file);
     name_length = strlen (name);
     max_name_length = nautilus_directory_get_max_child_name_length (directory);
 
+    is_valid = TRUE;
     if (name_length == 0)
     {
-        return FALSE;
+        is_valid = FALSE;
     }
-
-    if (strstr (name, "/") != NULL)
+    else if (strstr (name, "/") != NULL)
     {
+        is_valid = FALSE;
         if (self->target_is_folder)
         {
             *error_message = _("Folder names cannot contain “/”.");
@@ -130,6 +132,7 @@ nautilus_rename_file_popover_controller_name_is_valid (NautilusFileNameWidgetCon
     }
     else if (strcmp (name, ".") == 0)
     {
+        is_valid = FALSE;
         if (self->target_is_folder)
         {
             *error_message = _("A folder cannot be called “.”.");
@@ -141,6 +144,7 @@ nautilus_rename_file_popover_controller_name_is_valid (NautilusFileNameWidgetCon
     }
     else if (strcmp (name, "..") == 0)
     {
+        is_valid = FALSE;
         if (self->target_is_folder)
         {
             *error_message = _("A folder cannot be called “..”.");
@@ -152,6 +156,7 @@ nautilus_rename_file_popover_controller_name_is_valid (NautilusFileNameWidgetCon
     }
     else if (name_length > max_name_length + 1 && max_name_length != -1)
     {
+        is_valid = FALSE;
         if (self->target_is_folder)
         {
             *error_message = _("Folder name is too long.");
@@ -161,8 +166,10 @@ nautilus_rename_file_popover_controller_name_is_valid (NautilusFileNameWidgetCon
             *error_message = _("File name is too long.");
         }
     }
-    else if (g_str_has_prefix (name, "."))
+
+    if (is_valid && g_str_has_prefix (name, "."))
     {
+        /* We must warn about the side effect */
         if (self->target_is_folder)
         {
             *error_message = _("Folders with “.” at the beginning of their name are hidden.");
@@ -171,10 +178,9 @@ nautilus_rename_file_popover_controller_name_is_valid (NautilusFileNameWidgetCon
         {
             *error_message = _("Files with “.” at the beginning of their name are hidden.");
         }
-      return TRUE;
     }
 
-    return *error_message == NULL;
+    return is_valid;
 }
 
 static gboolean


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