[nautilus/wip/antoniof/metadata-lists-optimization: 2/3] file: Set metadata list as array




commit c52f6a8cc8acdc62da8e1ad4408d90069ffef34a
Author: António Fernandes <antoniof gnome org>
Date:   Mon Jul 26 14:33:27 2021 +0100

    file: Set metadata list as array
    
    Same rationale as previous commit, this time for the setter.
    
    This saves a lot of futile array-->list-->array conversions.

 src/nautilus-file.c      | 18 ++-------------
 src/nautilus-file.h      |  2 +-
 src/nautilus-list-view.c | 60 +++++++++++++++---------------------------------
 3 files changed, 22 insertions(+), 58 deletions(-)
---
diff --git a/src/nautilus-file.c b/src/nautilus-file.c
index b6394f569..10ef736d5 100644
--- a/src/nautilus-file.c
+++ b/src/nautilus-file.c
@@ -4188,27 +4188,13 @@ nautilus_file_set_metadata (NautilusFile *file,
 void
 nautilus_file_set_metadata_list (NautilusFile *file,
                                  const char   *key,
-                                 GList        *list)
+                                 gchar       **list)
 {
-    char **val;
-    int len, i;
-    GList *l;
-
     g_return_if_fail (NAUTILUS_IS_FILE (file));
     g_return_if_fail (key != NULL);
     g_return_if_fail (key[0] != '\0');
 
-    len = g_list_length (list);
-    val = g_new (char *, len + 1);
-    for (l = list, i = 0; l != NULL; l = l->next, i++)
-    {
-        val[i] = l->data;
-    }
-    val[i] = NULL;
-
-    NAUTILUS_FILE_CLASS (G_OBJECT_GET_CLASS (file))->set_metadata_as_list (file, key, val);
-
-    g_free (val);
+    NAUTILUS_FILE_CLASS (G_OBJECT_GET_CLASS (file))->set_metadata_as_list (file, key, list);
 }
 
 gboolean
diff --git a/src/nautilus-file.h b/src/nautilus-file.h
index 939033683..27a5ed458 100644
--- a/src/nautilus-file.h
+++ b/src/nautilus-file.h
@@ -380,7 +380,7 @@ void                    nautilus_file_set_metadata                      (Nautilu
                                                                         const char                     
*metadata);
 void                    nautilus_file_set_metadata_list                 (NautilusFile                   
*file,
                                                                         const char                     *key,
-                                                                        GList                          
*list);
+                                                                        gchar                         
**list);
 
 /* Covers for common data types. */
 gboolean                nautilus_file_get_boolean_metadata              (NautilusFile                   
*file,
diff --git a/src/nautilus-list-view.c b/src/nautilus-list-view.c
index e342ee176..8e146f2cd 100644
--- a/src/nautilus-list-view.c
+++ b/src/nautilus-list-view.c
@@ -1419,49 +1419,44 @@ column_header_menu_toggled (GtkCheckMenuItem *menu_item,
     char **visible_columns;
     char **column_order;
     const char *column;
-    GList *list = NULL;
-    GList *l;
+    gboolean active;
+    GPtrArray *ptr_array;
     int i;
 
     file = nautilus_files_view_get_directory_as_file (NAUTILUS_FILES_VIEW (list_view));
     visible_columns = get_visible_columns (list_view);
     column_order = get_column_order (list_view);
     column = g_object_get_data (G_OBJECT (menu_item), "column-name");
+    active = gtk_check_menu_item_get_active (menu_item);
 
+    /* Rebuild visible_columns to add or remove the toggled column. */
+    ptr_array = g_ptr_array_sized_new (g_strv_length (visible_columns));
     for (i = 0; visible_columns[i] != NULL; ++i)
     {
-        list = g_list_prepend (list, visible_columns[i]);
-    }
-
-    if (gtk_check_menu_item_get_active (menu_item))
-    {
-        list = g_list_prepend (list, g_strdup (column));
+        if (!active && g_strcmp0 (visible_columns[i], column))
+        {
+            continue;
+        }
+        g_ptr_array_add (ptr_array, visible_columns[i]);
     }
-    else
+    if (active)
     {
-        l = g_list_find_custom (list, column, (GCompareFunc) g_strcmp0);
-        list = g_list_delete_link (list, l);
+        g_ptr_array_add (ptr_array, g_strdup (column));
     }
-
-    list = g_list_reverse (list);
-    nautilus_file_set_metadata_list (file,
-                                     NAUTILUS_METADATA_KEY_LIST_VIEW_VISIBLE_COLUMNS,
-                                     list);
+    g_ptr_array_add (ptr_array, NULL);
 
     g_free (visible_columns);
+    visible_columns = (gchar **) g_ptr_array_free (ptr_array, FALSE);
 
-    visible_columns = g_new0 (char *, g_list_length (list) + 1);
-    for (i = 0, l = list; l != NULL; ++i, l = l->next)
-    {
-        visible_columns[i] = l->data;
-    }
+    nautilus_file_set_metadata_list (file,
+                                     NAUTILUS_METADATA_KEY_LIST_VIEW_VISIBLE_COLUMNS,
+                                     visible_columns);
 
     /* set view values ourselves, as new metadata could not have been
      * updated yet.
      */
     apply_columns_settings (list_view, column_order, visible_columns);
 
-    g_list_free (list);
     g_strfreev (column_order);
     g_strfreev (visible_columns);
 }
@@ -3263,8 +3258,6 @@ column_chooser_changed_callback (NautilusColumnChooser *chooser,
     NautilusFile *file;
     char **visible_columns;
     char **column_order;
-    GList *list;
-    int i;
 
     file = nautilus_files_view_get_directory_as_file (NAUTILUS_FILES_VIEW (view));
 
@@ -3272,27 +3265,12 @@ column_chooser_changed_callback (NautilusColumnChooser *chooser,
                                           &visible_columns,
                                           &column_order);
 
-    list = NULL;
-    for (i = 0; visible_columns[i] != NULL; ++i)
-    {
-        list = g_list_prepend (list, visible_columns[i]);
-    }
-    list = g_list_reverse (list);
     nautilus_file_set_metadata_list (file,
                                      NAUTILUS_METADATA_KEY_LIST_VIEW_VISIBLE_COLUMNS,
-                                     list);
-    g_list_free (list);
-
-    list = NULL;
-    for (i = 0; column_order[i] != NULL; ++i)
-    {
-        list = g_list_prepend (list, column_order[i]);
-    }
-    list = g_list_reverse (list);
+                                     visible_columns);
     nautilus_file_set_metadata_list (file,
                                      NAUTILUS_METADATA_KEY_LIST_VIEW_COLUMN_ORDER,
-                                     list);
-    g_list_free (list);
+                                     column_order);
 
     apply_columns_settings (view, column_order, visible_columns);
 


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