[gnome-software/1296-calculate-app-space-usage-for-download-context-tile: 38/43] gs-app: Split download/installed size for the app and the dependencies




commit b753b1549ff1f99f0d527d88b876b9da8f344155
Author: Milan Crha <mcrha redhat com>
Date:   Wed Jul 28 22:08:14 2021 +0200

    gs-app: Split download/installed size for the app and the dependencies
    
    This helps to show sizes separately for the application itself and
    for its dependencies/addons.

 lib/gs-app.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++-------------
 lib/gs-app.h |   4 ++
 2 files changed, 123 insertions(+), 32 deletions(-)
---
diff --git a/lib/gs-app.c b/lib/gs-app.c
index c70676f4b..b7c75818a 100644
--- a/lib/gs-app.c
+++ b/lib/gs-app.c
@@ -158,7 +158,9 @@ typedef enum {
        PROP_LICENSE,
        PROP_SIZE_CACHE_DATA,
        PROP_SIZE_DOWNLOAD,
+       PROP_SIZE_DOWNLOAD_DEPENDENCIES,
        PROP_SIZE_INSTALLED,
+       PROP_SIZE_INSTALLED_DEPENDENCIES,
        PROP_SIZE_USER_DATA,
        PROP_PERMISSIONS,
        PROP_RELATIONS,
@@ -678,8 +680,12 @@ gs_app_to_string_append (GsApp *app, GString *str)
        }
        if (priv->size_installed != 0)
                gs_app_kv_size (str, "size-installed", priv->size_installed);
+       if (gs_app_get_size_installed_dependencies (app) != 0)
+               gs_app_kv_size (str, "size-installed-dependencies", gs_app_get_size_installed_dependencies 
(app));
        if (priv->size_download != 0)
                gs_app_kv_size (str, "size-download", gs_app_get_size_download (app));
+       if (gs_app_get_size_download_dependencies (app) != 0)
+               gs_app_kv_size (str, "size-download-dependencies", gs_app_get_size_download_dependencies 
(app));
        if (priv->size_cache_data != GS_APP_SIZE_UNKNOWABLE)
                gs_app_kv_size (str, "size-cache-data", gs_app_get_size_cache_data (app));
        if (priv->size_user_data != GS_APP_SIZE_UNKNOWABLE)
@@ -3440,26 +3446,10 @@ guint64
 gs_app_get_size_download (GsApp *app)
 {
        GsAppPrivate *priv = gs_app_get_instance_private (app);
-       guint64 sz;
 
        g_return_val_if_fail (GS_IS_APP (app), G_MAXUINT64);
 
-       /* this app */
-       sz = priv->size_download;
-
-       /* add the runtime if this is not installed */
-       if (priv->runtime != NULL) {
-               if (gs_app_get_state (priv->runtime) == GS_APP_STATE_AVAILABLE)
-                       sz += gs_app_get_size_download (priv->runtime);
-       }
-
-       /* add related apps */
-       for (guint i = 0; i < gs_app_list_length (priv->related); i++) {
-               GsApp *app_related = gs_app_list_index (priv->related, i);
-               sz += gs_app_get_size_download (app_related);
-       }
-
-       return sz;
+       return priv->size_download;
 }
 
 /**
@@ -3484,35 +3474,60 @@ gs_app_set_size_download (GsApp *app, guint64 size_download)
 }
 
 /**
- * gs_app_get_size_installed:
- * @app: a #GsApp
+ * gs_app_get_size_download_dependencies:
+ * @app: A #GsApp
  *
- * Get the value of #GsApp:size-installed.
+ * Get the value of #GsApp:size-download-dependencies.
  *
- * Returns: size in bytes, `0` for unknown, or %GS_APP_SIZE_UNKNOWABLE for invalid.
+ * Returns: number of bytes, `0` for unknown
  *
- * Since: 3.22
+ * Since: 41
  **/
 guint64
-gs_app_get_size_installed (GsApp *app)
+gs_app_get_size_download_dependencies (GsApp *app)
 {
        GsAppPrivate *priv = gs_app_get_instance_private (app);
-       guint64 sz;
+       guint64 sz = 0;
 
        g_return_val_if_fail (GS_IS_APP (app), G_MAXUINT64);
 
-       /* this app */
-       sz = priv->size_installed;
+       /* add the runtime if this is not installed */
+       if (priv->runtime != NULL) {
+               if (gs_app_get_state (priv->runtime) == GS_APP_STATE_AVAILABLE)
+                       sz += gs_app_get_size_download (priv->runtime) +
+                             gs_app_get_size_download_dependencies (priv->runtime);
+       }
 
        /* add related apps */
        for (guint i = 0; i < gs_app_list_length (priv->related); i++) {
                GsApp *app_related = gs_app_list_index (priv->related, i);
-               sz += gs_app_get_size_installed (app_related);
+               sz += gs_app_get_size_download (app_related) +
+                     gs_app_get_size_download_dependencies (app_related);
        }
 
        return sz;
 }
 
+/**
+ * gs_app_get_size_installed:
+ * @app: a #GsApp
+ *
+ * Get the value of #GsApp:size-installed.
+ *
+ * Returns: size in bytes, `0` for unknown, or %GS_APP_SIZE_UNKNOWABLE for invalid.
+ *
+ * Since: 3.22
+ **/
+guint64
+gs_app_get_size_installed (GsApp *app)
+{
+       GsAppPrivate *priv = gs_app_get_instance_private (app);
+
+       g_return_val_if_fail (GS_IS_APP (app), G_MAXUINT64);
+
+       return priv->size_installed;
+}
+
 /**
  * gs_app_set_size_installed:
  * @app: a #GsApp
@@ -3533,6 +3548,34 @@ gs_app_set_size_installed (GsApp *app, guint64 size_installed)
        gs_app_queue_notify (app, obj_props[PROP_SIZE_INSTALLED]);
 }
 
+/**
+ * gs_app_get_size_installed_dependencies:
+ * @app: a #GsApp
+ *
+ * Get the value of #GsApp:size-installed-dependencies.
+ *
+ * Returns: size in bytes, `0` for unknown.
+ *
+ * Since: 41
+ **/
+guint64
+gs_app_get_size_installed_dependencies (GsApp *app)
+{
+       GsAppPrivate *priv = gs_app_get_instance_private (app);
+       guint64 sz = 0;
+
+       g_return_val_if_fail (GS_IS_APP (app), G_MAXUINT64);
+
+       /* add related apps */
+       for (guint i = 0; i < gs_app_list_length (priv->related); i++) {
+               GsApp *app_related = gs_app_list_index (priv->related, i);
+               sz += gs_app_get_size_installed (app_related) +
+                     gs_app_get_size_installed_dependencies (app_related);
+       }
+
+       return sz;
+}
+
 /**
  * gs_app_get_size_user_data:
  * @app: A #GsApp
@@ -3842,8 +3885,8 @@ gs_app_add_related (GsApp *app, GsApp *app2)
        gs_app_list_add (priv->related, app2);
 
        /* The related apps add to the main app’s sizes. */
-       gs_app_queue_notify (app, obj_props[PROP_SIZE_DOWNLOAD]);
-       gs_app_queue_notify (app, obj_props[PROP_SIZE_INSTALLED]);
+       gs_app_queue_notify (app, obj_props[PROP_SIZE_DOWNLOAD_DEPENDENCIES]);
+       gs_app_queue_notify (app, obj_props[PROP_SIZE_INSTALLED_DEPENDENCIES]);
 }
 
 /**
@@ -4791,9 +4834,15 @@ gs_app_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *
        case PROP_SIZE_DOWNLOAD:
                g_value_set_uint64 (value, gs_app_get_size_download (app));
                break;
+       case PROP_SIZE_DOWNLOAD_DEPENDENCIES:
+               g_value_set_uint64 (value, gs_app_get_size_download_dependencies (app));
+               break;
        case PROP_SIZE_INSTALLED:
                g_value_set_uint64 (value, gs_app_get_size_installed (app));
                break;
+       case PROP_SIZE_INSTALLED_DEPENDENCIES:
+               g_value_set_uint64 (value, gs_app_get_size_installed_dependencies (app));
+               break;
        case PROP_SIZE_USER_DATA:
                g_value_set_uint64 (value, gs_app_get_size_user_data (app));
                break;
@@ -4889,9 +4938,15 @@ gs_app_set_property (GObject *object, guint prop_id, const GValue *value, GParam
        case PROP_SIZE_DOWNLOAD:
                gs_app_set_size_download (app, g_value_get_uint64 (value));
                break;
+       case PROP_SIZE_DOWNLOAD_DEPENDENCIES:
+               /* Read-only */
+               g_assert_not_reached ();
        case PROP_SIZE_INSTALLED:
                gs_app_set_size_installed (app, g_value_get_uint64 (value));
                break;
+       case PROP_SIZE_INSTALLED_DEPENDENCIES:
+               /* Read-only */
+               g_assert_not_reached ();
        case PROP_SIZE_USER_DATA:
                gs_app_set_size_user_data (app, g_value_get_uint64 (value));
                break;
@@ -5191,7 +5246,8 @@ gs_app_class_init (GsAppClass *klass)
         * This is `0` if the download size is unknown, and
         * %GS_APP_SIZE_UNKNOWABLE if it’s not possible to know.
         *
-        * If there is a runtime not yet installed then this is also added.
+        * To get the runtime or other dependencies download size,
+        * use #GsApp:size-download-dependencies.
         *
         * Since: 41
         */
@@ -5200,6 +5256,22 @@ gs_app_class_init (GsAppClass *klass)
                                     0, G_MAXUINT64, 0,
                                     G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
 
+       /**
+        * GsApp:size-download-dependencies
+        *
+        * The size of the total download needed to either install or update
+        * this application's dependencies, in bytes. If the dependencies are partially
+        * downloaded, this is the number of bytes remaining to download.
+        *
+        * This is `0` if the download size is unknown.
+        *
+        * Since: 41
+        */
+       obj_props[PROP_SIZE_DOWNLOAD_DEPENDENCIES] =
+               g_param_spec_uint64 ("size-download-dependencies", NULL, NULL,
+                                    0, G_MAXUINT64, 0,
+                                    G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
+
        /**
         * GsApp:size-installed
         *
@@ -5209,8 +5281,8 @@ gs_app_class_init (GsAppClass *klass)
         * This is `0` if the download size is unknown, and
         * %GS_APP_SIZE_UNKNOWABLE if it’s not possible to know.
         *
-        * If the application has a runtime or extensions, their size
-        * requirements are also added.
+        * To get the application runtime or extensions installed sizes,
+        * use #GsApp:size-installed-dependencies.
         *
         * Since: 41
         */
@@ -5219,6 +5291,21 @@ gs_app_class_init (GsAppClass *klass)
                                     0, G_MAXUINT64, 0,
                                     G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
 
+       /**
+        * GsApp:size-installed-dependencies
+        *
+        * The size of the application's dependencies on disk, in bytes. If the dependencies are
+        * not yet installed, this is the size it would need, once installed.
+        *
+        * This is `0` if the download size is unknown.
+        *
+        * Since: 41
+        */
+       obj_props[PROP_SIZE_INSTALLED_DEPENDENCIES] =
+               g_param_spec_uint64 ("size-installed-dependencies", NULL, NULL,
+                                    0, G_MAXUINT64, 0,
+                                    G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
+
        /**
         * GsApp:size-user-data
         *
diff --git a/lib/gs-app.h b/lib/gs-app.h
index 46987f13a..8068b85f7 100644
--- a/lib/gs-app.h
+++ b/lib/gs-app.h
@@ -407,6 +407,8 @@ void                 gs_app_add_provided_item       (GsApp          *app,
 guint64                 gs_app_get_size_installed      (GsApp          *app);
 void            gs_app_set_size_installed      (GsApp          *app,
                                                 guint64         size_installed);
+guint64                 gs_app_get_size_installed_dependencies
+                                               (GsApp          *app);
 guint64                 gs_app_get_size_user_data      (GsApp          *app);
 void            gs_app_set_size_user_data      (GsApp          *app,
                                                 guint64         size_user_data);
@@ -416,6 +418,8 @@ void                 gs_app_set_size_cache_data     (GsApp          *app,
 guint64                 gs_app_get_size_download       (GsApp          *app);
 void            gs_app_set_size_download       (GsApp          *app,
                                                 guint64         size_download);
+guint64                 gs_app_get_size_download_dependencies
+                                               (GsApp          *app);
 void            gs_app_add_related             (GsApp          *app,
                                                 GsApp          *app2);
 void            gs_app_add_addon               (GsApp          *app,


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