[gtk/icontheme-api: 25/26] Make gtk_icon_theme_list_icons return a string array



commit c1032dfe5bea27ba4cb4845a67d54981361cd72c
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Feb 18 00:56:37 2020 -0500

    Make gtk_icon_theme_list_icons return a string array
    
    Update all callers.

 demos/gtk-demo/fishbowl.c | 22 ++--------------------
 gtk/gtkicontheme.c        | 36 ++++++++++++++++--------------------
 gtk/gtkicontheme.h        |  2 +-
 tests/testicontheme.c     | 15 +++++++--------
 testsuite/gtk/icontheme.c | 28 ++++++++++++++--------------
 5 files changed, 40 insertions(+), 63 deletions(-)
---
diff --git a/demos/gtk-demo/fishbowl.c b/demos/gtk-demo/fishbowl.c
index 111d6a199e..d870075be6 100644
--- a/demos/gtk-demo/fishbowl.c
+++ b/demos/gtk-demo/fishbowl.c
@@ -22,29 +22,11 @@ gsize n_icon_names = 0;
 static void
 init_icon_names (GtkIconTheme *theme)
 {
-  GPtrArray *icons;
-  GList *l, *icon_list;
-
   if (icon_names)
     return;
 
-  icon_list = gtk_icon_theme_list_icons (theme);
-  icons = g_ptr_array_new ();
-
-  for (l = icon_list; l; l = l->next)
-    {
-      if (g_str_has_suffix (l->data, "symbolic"))
-        continue;
-
-      g_ptr_array_add (icons, g_strdup (l->data));
-    }
-
-  n_icon_names = icons->len;
-  g_ptr_array_add (icons, NULL); /* NULL-terminate the array */
-  icon_names = (char **) g_ptr_array_free (icons, FALSE);
-
-  /* don't free strings, we assigned them to the array */
-  g_list_free_full (icon_list, g_free);
+  icon_names = gtk_icon_theme_list_icons (theme);
+  n_icon_names = g_strv_length (icon_names);
 }
 
 static const char *
diff --git a/gtk/gtkicontheme.c b/gtk/gtkicontheme.c
index 5c1f2dc22c..26a2f4e1c3 100644
--- a/gtk/gtkicontheme.c
+++ b/gtk/gtkicontheme.c
@@ -2462,32 +2462,25 @@ add_key_to_hash (gpointer key,
   g_hash_table_insert (hash, key, key);
 }
 
-static void
-add_key_to_list (gpointer key,
-                 gpointer value,
-                 gpointer user_data)
-{
-  GList **list = user_data;
-
-  *list = g_list_prepend (*list, g_strdup (key));
-}
-
 /**
  * gtk_icon_theme_list_icons:
  * @self: a #GtkIconTheme
  *
  * Lists the icons in the current icon theme.
  *
- * Returns: (element-type utf8) (transfer full): a #GList list
+ * Returns: (element-type utf8) (transfer full): a string array
  *     holding the names of all the icons in the theme. You must
- *     first free each element in the list with g_free(), then
- *     free the list itself with g_list_free().
+ *     free the array using g_strfreev().
  */
-GList *
+char **
 gtk_icon_theme_list_icons (GtkIconTheme *self)
 {
   GHashTable *icons;
-  GList *list, *l;
+  GHashTableIter iter;
+  char **names;
+  char *key;
+  int i;
+  GList *l;
 
   gtk_icon_theme_lock (self);
 
@@ -2507,17 +2500,20 @@ gtk_icon_theme_list_icons (GtkIconTheme *self)
                         add_key_to_hash,
                         icons);
 
-  list = NULL;
+  names = g_new (char *, g_hash_table_size (icons) + 1);
+
+  i = 0;
+  g_hash_table_iter_init (&iter, icons);
+  while (g_hash_table_iter_next (&iter, (gpointer *)&key, NULL))
+    names[i++] = g_strdup (key);
 
-  g_hash_table_foreach (icons,
-                        add_key_to_list,
-                        &list);
+  names[i] = NULL;
 
   g_hash_table_destroy (icons);
 
   gtk_icon_theme_unlock (self);
 
-  return list;
+  return names;
 }
 
 static gboolean
diff --git a/gtk/gtkicontheme.h b/gtk/gtkicontheme.h
index 86d22dc05c..f57865a39e 100644
--- a/gtk/gtkicontheme.h
+++ b/gtk/gtkicontheme.h
@@ -138,7 +138,7 @@ GtkIconPaintable *gtk_icon_paintable_new_for_file    (GFile
                                                       gint                         size,
                                                       gint                         scale);
 GDK_AVAILABLE_IN_ALL
-GList *       gtk_icon_theme_list_icons              (GtkIconTheme                *self);
+char **       gtk_icon_theme_list_icons              (GtkIconTheme                *self);
 
 GDK_AVAILABLE_IN_ALL
 GType                 gtk_icon_paintable_get_type         (void) G_GNUC_CONST;
diff --git a/tests/testicontheme.c b/tests/testicontheme.c
index cc9f988dca..16c25019f3 100644
--- a/tests/testicontheme.c
+++ b/tests/testicontheme.c
@@ -49,7 +49,6 @@ main (int argc, char *argv[])
   GtkIconTheme *icon_theme;
   GtkIconPaintable *icon;
   char *themename;
-  GList *list;
   int size = 48;
   int scale = 1;
   GtkIconLookupFlags flags;
@@ -117,13 +116,13 @@ main (int argc, char *argv[])
     }
   else if (strcmp (argv[1], "list") == 0)
     {
-      list = gtk_icon_theme_list_icons (icon_theme);
-      
-      while (list)
-       {
-         g_print ("%s\n", (char *)list->data);
-         list = list->next;
-       }
+      char **icons;
+      int i;
+
+      icons = gtk_icon_theme_list_icons (icon_theme);
+      for (i = 0; icons[i]; i++)
+       g_print ("%s\n", icons[i]);
+      g_strfreev (icons);
     }
   else if (strcmp (argv[1], "lookup") == 0)
     {
diff --git a/testsuite/gtk/icontheme.c b/testsuite/gtk/icontheme.c
index 3948828b93..0efed5dc38 100644
--- a/testsuite/gtk/icontheme.c
+++ b/testsuite/gtk/icontheme.c
@@ -656,23 +656,23 @@ static void
 test_list (void)
 {
   GtkIconTheme *theme;
-  GList *icons;
+  char **icons;
 
   theme = get_test_icontheme (TRUE);
   icons = gtk_icon_theme_list_icons (theme);
 
-  g_assert (g_list_find_custom (icons, "size-test", (GCompareFunc)g_strcmp0));
-  g_assert (g_list_find_custom (icons, "simple", (GCompareFunc)g_strcmp0));
-  g_assert (g_list_find_custom (icons, "twosize-fixed", (GCompareFunc)g_strcmp0));
-  g_assert (g_list_find_custom (icons, "twosize", (GCompareFunc)g_strcmp0));
-  g_assert (g_list_find_custom (icons, "only32-symbolic", (GCompareFunc)g_strcmp0));
-  g_assert (g_list_find_custom (icons, "everything", (GCompareFunc)g_strcmp0));
-  g_assert (g_list_find_custom (icons, "everything-rtl", (GCompareFunc)g_strcmp0));
-  g_assert (g_list_find_custom (icons, "everything-symbolic", (GCompareFunc)g_strcmp0));
-  g_assert (g_list_find_custom (icons, "everything-justregular", (GCompareFunc)g_strcmp0));
-  g_assert (g_list_find_custom (icons, "everything-justrtl-rtl", (GCompareFunc)g_strcmp0));
-  g_assert (g_list_find_custom (icons, "everything-symbolic-rtl", (GCompareFunc)g_strcmp0));
-  g_assert (g_list_find_custom (icons, "everything-justsymbolic-symbolic", (GCompareFunc)g_strcmp0));
+  g_assert (g_strv_contains ((const char * const *)icons, "size-test"));
+  g_assert (g_strv_contains ((const char * const *)icons, "simple"));
+  g_assert (g_strv_contains ((const char * const *)icons, "twosize-fixed"));
+  g_assert (g_strv_contains ((const char * const *)icons, "twosize"));
+  g_assert (g_strv_contains ((const char * const *)icons, "only32-symbolic"));
+  g_assert (g_strv_contains ((const char * const *)icons, "everything"));
+  g_assert (g_strv_contains ((const char * const *)icons, "everything-rtl"));
+  g_assert (g_strv_contains ((const char * const *)icons, "everything-symbolic"));
+  g_assert (g_strv_contains ((const char * const *)icons, "everything-justregular"));
+  g_assert (g_strv_contains ((const char * const *)icons, "everything-justrtl-rtl"));
+  g_assert (g_strv_contains ((const char * const *)icons, "everything-symbolic-rtl"));
+  g_assert (g_strv_contains ((const char * const *)icons, "everything-justsymbolic-symbolic"));
 
   g_assert (gtk_icon_theme_has_icon (theme, "size-test"));
   g_assert (gtk_icon_theme_has_icon (theme, "simple"));
@@ -687,7 +687,7 @@ test_list (void)
   g_assert (gtk_icon_theme_has_icon (theme, "everything-symbolic-rtl"));
   g_assert (gtk_icon_theme_has_icon (theme, "everything-justsymbolic-symbolic"));
 
-  g_list_free_full (icons, g_free);
+  g_strfreev (icons);
 }
 
 static void


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