[gnome-software: 1/18] gs-app: Return NULL from get_icons() if no icons are set




commit ce5c8abb5a696734f9a384eec24ec076d53411f3
Author: Philip Withnall <pwithnall endlessos org>
Date:   Mon Mar 1 23:44:18 2021 +0000

    gs-app: Return NULL from get_icons() if no icons are set
    
    Rather than returning an empty array. This allows the array to be lazily
    created, which saves some memory for apps whose icons have not been
    refined.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>
    
    Helps: #1147

 lib/gs-app.c                          | 22 +++++++++++++++++-----
 lib/gs-plugin-loader.c                |  2 +-
 plugins/core/gs-appstream.c           |  2 +-
 plugins/core/gs-plugin-icons.c        |  2 +-
 plugins/dummy/gs-plugin-dummy.c       |  2 +-
 plugins/modalias/gs-plugin-modalias.c |  2 +-
 6 files changed, 22 insertions(+), 10 deletions(-)
---
diff --git a/lib/gs-app.c b/lib/gs-app.c
index ae0309af5..e8fd3197d 100644
--- a/lib/gs-app.c
+++ b/lib/gs-app.c
@@ -61,7 +61,7 @@ typedef struct
        gchar                   *name;
        gchar                   *renamed_from;
        GsAppQuality             name_quality;
-       GPtrArray               *icons;
+       GPtrArray               *icons;  /* (nullable) (owned) (element-type AsIcon) */
        GPtrArray               *sources;
        GPtrArray               *source_ids;
        gchar                   *project_group;
@@ -534,7 +534,7 @@ gs_app_to_string_append (GsApp *app, GString *str)
        }
        if (priv->action_screenshot != NULL)
                gs_app_kv_printf (str, "action-screenshot", "%p", priv->action_screenshot);
-       for (i = 0; i < priv->icons->len; i++) {
+       for (i = 0; priv->icons != NULL && i < priv->icons->len; i++) {
                AsIcon *icon = g_ptr_array_index (priv->icons, i);
                gs_app_kv_lpad (str, "icon-kind",
                                as_icon_kind_to_string (as_icon_get_kind (icon)));
@@ -1889,7 +1889,11 @@ gs_app_get_action_screenshot (GsApp *app)
  *
  * Gets the icons for the application.
  *
- * Returns: (transfer none) (element-type AsIcon): an array of icons
+ * This will never return an empty array; it will always return either %NULL or
+ * a non-empty array.
+ *
+ * Returns: (transfer none) (element-type AsIcon) (nullable): an array of icons,
+ *     or %NULL if there are no icons
  *
  * Since: 3.22
  **/
@@ -1898,6 +1902,10 @@ gs_app_get_icons (GsApp *app)
 {
        GsAppPrivate *priv = gs_app_get_instance_private (app);
        g_return_val_if_fail (GS_IS_APP (app), NULL);
+
+       if (priv->icons != NULL && priv->icons->len == 0)
+               return NULL;
+
        return priv->icons;
 }
 
@@ -1918,10 +1926,15 @@ gs_app_add_icon (GsApp *app, AsIcon *icon)
        g_autoptr(GMutexLocker) locker = NULL;
        g_return_if_fail (GS_IS_APP (app));
        locker = g_mutex_locker_new (&priv->mutex);
+
        if (icon == NULL) {
                g_ptr_array_set_size (priv->icons, 0);
                return;
        }
+
+       if (priv->icons == NULL)
+               priv->icons = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
+
        g_ptr_array_add (priv->icons, g_object_ref (icon));
 }
 
@@ -1945,7 +1958,7 @@ gs_app_get_use_drop_shadow (GsApp *app)
        g_return_val_if_fail (GS_IS_APP (app), FALSE);
 
        /* guess */
-       if (priv->icons->len == 0)
+       if (priv->icons == NULL || priv->icons->len == 0)
                return TRUE;
 
        /* stock, and symbolic */
@@ -4951,7 +4964,6 @@ gs_app_init (GsApp *app)
        priv->screenshots = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
        priv->reviews = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
        priv->provided = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
-       priv->icons = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
        priv->metadata = g_hash_table_new_full (g_str_hash,
                                                g_str_equal,
                                                g_free,
diff --git a/lib/gs-plugin-loader.c b/lib/gs-plugin-loader.c
index 358cb8439..7fac824c1 100644
--- a/lib/gs-plugin-loader.c
+++ b/lib/gs-plugin-loader.c
@@ -3048,7 +3048,7 @@ _gs_app_get_icon_by_kind (GsApp *app, AsIconKind kind)
 {
        GPtrArray *icons = gs_app_get_icons (app);
        guint i;
-       for (i = 0; i < icons->len; i++) {
+       for (i = 0; icons != NULL && i < icons->len; i++) {
                AsIcon *ic = g_ptr_array_index (icons, i);
                if (as_icon_get_kind (ic) == kind)
                        return ic;
diff --git a/plugins/core/gs-appstream.c b/plugins/core/gs-appstream.c
index 69dccef30..53648166f 100644
--- a/plugins/core/gs-appstream.c
+++ b/plugins/core/gs-appstream.c
@@ -950,7 +950,7 @@ gs_appstream_refine_app (GsPlugin *plugin,
 
        /* set icon */
        if ((refine_flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_ICON) > 0 &&
-           gs_app_get_icons(app)->len == 0)
+           gs_app_get_icons (app) == NULL)
                gs_appstream_refine_icon (plugin, app, component);
 
        /* set categories */
diff --git a/plugins/core/gs-plugin-icons.c b/plugins/core/gs-plugin-icons.c
index 539676e3d..d2e0bcf14 100644
--- a/plugins/core/gs-plugin-icons.c
+++ b/plugins/core/gs-plugin-icons.c
@@ -318,7 +318,7 @@ refine_app (GsPlugin             *plugin,
 
        /* process all icons */
        icons = gs_app_get_icons (app);
-       for (i = 0; i < icons->len; i++) {
+       for (i = 0; icons != NULL && i < icons->len; i++) {
                AsIcon *icon = g_ptr_array_index (icons, i);
                g_autoptr(GdkPixbuf) pixbuf = NULL;
                g_autoptr(GError) error_local = NULL;
diff --git a/plugins/dummy/gs-plugin-dummy.c b/plugins/dummy/gs-plugin-dummy.c
index a6f834f42..334fbac24 100644
--- a/plugins/dummy/gs-plugin-dummy.c
+++ b/plugins/dummy/gs-plugin-dummy.c
@@ -649,7 +649,7 @@ refine_app (GsPlugin *plugin,
                        gs_app_set_name (app, GS_APP_QUALITY_NORMAL, "tmp");
                if (gs_app_get_summary (app) == NULL)
                        gs_app_set_summary (app, GS_APP_QUALITY_NORMAL, "tmp");
-               if (gs_app_get_icons(app)->len == 0) {
+               if (gs_app_get_icons (app) == NULL) {
                        g_autoptr(AsIcon) ic = NULL;
                        ic = as_icon_new ();
                        as_icon_set_kind (ic, AS_ICON_KIND_STOCK);
diff --git a/plugins/modalias/gs-plugin-modalias.c b/plugins/modalias/gs-plugin-modalias.c
index 288ea96f3..87930b2c4 100644
--- a/plugins/modalias/gs-plugin-modalias.c
+++ b/plugins/modalias/gs-plugin-modalias.c
@@ -110,7 +110,7 @@ refine_app (GsPlugin             *plugin,
        guint i;
 
        /* not required */
-       if (gs_app_get_icons(app)->len > 0)
+       if (gs_app_get_icons (app) != NULL)
                return TRUE;
        if (gs_app_get_kind (app) != AS_COMPONENT_KIND_DRIVER)
                return TRUE;


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