[gnome-software: 16/24] flatpak: Make listing recently updated apps asynchronous




commit adfc7e6277415190152ac6f47fe9255db3cc8fd4
Author: Philip Withnall <pwithnall endlessos org>
Date:   Wed Apr 13 22:54:28 2022 +0100

    flatpak: Make listing recently updated apps asynchronous
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>
    
    Helps: #1472

 plugins/flatpak/gs-plugin-flatpak.c | 82 ++++++++++++++++++++++++++++++++-----
 1 file changed, 72 insertions(+), 10 deletions(-)
---
diff --git a/plugins/flatpak/gs-plugin-flatpak.c b/plugins/flatpak/gs-plugin-flatpak.c
index 05849482a..c8c994b6c 100644
--- a/plugins/flatpak/gs-plugin-flatpak.c
+++ b/plugins/flatpak/gs-plugin-flatpak.c
@@ -1913,22 +1913,82 @@ gs_plugin_add_featured (GsPlugin *plugin,
        return TRUE;
 }
 
-gboolean
-gs_plugin_add_recent (GsPlugin *plugin,
-                     GsAppList *list,
-                     guint64 age,
-                     GCancellable *cancellable,
-                     GError **error)
+static void list_apps_thread_cb (GTask        *task,
+                                 gpointer      source_object,
+                                 gpointer      task_data,
+                                 GCancellable *cancellable);
+
+static void
+gs_plugin_flatpak_list_apps_async (GsPlugin              *plugin,
+                                   GsAppQuery            *query,
+                                   GsPluginListAppsFlags  flags,
+                                   GCancellable          *cancellable,
+                                   GAsyncReadyCallback    callback,
+                                   gpointer               user_data)
 {
        GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
-       gboolean interactive = gs_plugin_has_flags (plugin, GS_PLUGIN_FLAGS_INTERACTIVE);
+       g_autoptr(GTask) task = NULL;
+       gboolean interactive = (flags & GS_PLUGIN_LIST_APPS_FLAGS_INTERACTIVE);
+
+
+       task = gs_plugin_list_apps_data_new_task (plugin, query, flags,
+                                                 cancellable, callback, user_data);
+       g_task_set_source_tag (task, gs_plugin_flatpak_list_apps_async);
+
+       /* Queue a job to get the apps. */
+       gs_worker_thread_queue (self->worker, get_priority_for_interactivity (interactive),
+                               list_apps_thread_cb, g_steal_pointer (&task));
+}
+
+/* Run in @worker. */
+static void
+list_apps_thread_cb (GTask        *task,
+                     gpointer      source_object,
+                     gpointer      task_data,
+                     GCancellable *cancellable)
+{
+       GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
+       g_autoptr(GsAppList) list = gs_app_list_new ();
+       GsPluginListAppsData *data = task_data;
+       gboolean interactive = (data->flags & GS_PLUGIN_LIST_APPS_FLAGS_INTERACTIVE);
+       GDateTime *released_since = NULL;
+       guint64 age_secs = 0;
+       g_autoptr(GError) local_error = NULL;
+
+       assert_in_worker (self);
+
+       if (data->query != NULL)
+               released_since = gs_app_query_get_released_since (data->query);
+       if (released_since != NULL) {
+               g_autoptr(GDateTime) now = g_date_time_new_now_local ();
+               age_secs = g_date_time_difference (now, released_since) / G_TIME_SPAN_SECOND;
+       }
+
+       /* Currently only support released-since queries. */
+       if (released_since == NULL) {
+               g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
+                                        "Unsupported query");
+               return;
+       }
 
        for (guint i = 0; i < self->installations->len; i++) {
                GsFlatpak *flatpak = g_ptr_array_index (self->installations, i);
-               if (!gs_flatpak_add_recent (flatpak, list, age, interactive, cancellable, error))
-                       return FALSE;
+
+               if (!gs_flatpak_add_recent (flatpak, list, age_secs, interactive, cancellable, &local_error)) 
{
+                       g_task_return_error (task, g_steal_pointer (&local_error));
+                       return;
+               }
        }
-       return TRUE;
+
+       g_task_return_pointer (task, g_steal_pointer (&list), g_object_unref);
+}
+
+static GsAppList *
+gs_plugin_flatpak_list_apps_finish (GsPlugin      *plugin,
+                                    GAsyncResult  *result,
+                                    GError       **error)
+{
+       return g_task_propagate_pointer (G_TASK (result), error);
 }
 
 gboolean
@@ -2054,6 +2114,8 @@ gs_plugin_flatpak_class_init (GsPluginFlatpakClass *klass)
        plugin_class->refine_finish = gs_plugin_flatpak_refine_finish;
        plugin_class->list_installed_apps_async = gs_plugin_flatpak_list_installed_apps_async;
        plugin_class->list_installed_apps_finish = gs_plugin_flatpak_list_installed_apps_finish;
+       plugin_class->list_apps_async = gs_plugin_flatpak_list_apps_async;
+       plugin_class->list_apps_finish = gs_plugin_flatpak_list_apps_finish;
        plugin_class->refresh_metadata_async = gs_plugin_flatpak_refresh_metadata_async;
        plugin_class->refresh_metadata_finish = gs_plugin_flatpak_refresh_metadata_finish;
 }


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