[gnome-software: 2/72] lib: Add helper functions for storing vfunc call context
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-software: 2/72] lib: Add helper functions for storing vfunc call context
- Date: Wed, 15 Dec 2021 13:00:54 +0000 (UTC)
commit 9ab1e9f117fbc588f88b2649e9a506630f2ab96a
Author: Philip Withnall <pwithnall endlessos org>
Date: Fri Oct 22 14:53:50 2021 +0100
lib: Add helper functions for storing vfunc call context
These will be useful for passing messages for vfuncs to worker threads
in plugin implementations.
Signed-off-by: Philip Withnall <pwithnall endlessos org>
Helps: #1472
lib/gnome-software.h | 1 +
lib/gs-plugin-helpers.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++
lib/gs-plugin-helpers.h | 37 +++++++++++++++++++
lib/meson.build | 2 +
4 files changed, 137 insertions(+)
---
diff --git a/lib/gnome-software.h b/lib/gnome-software.h
index 32eb6a594..892abe55b 100644
--- a/lib/gnome-software.h
+++ b/lib/gnome-software.h
@@ -24,6 +24,7 @@
#include <gs-odrs-provider.h>
#include <gs-os-release.h>
#include <gs-plugin.h>
+#include <gs-plugin-helpers.h>
#include <gs-plugin-vfuncs.h>
#include <gs-remote-icon.h>
#include <gs-utils.h>
diff --git a/lib/gs-plugin-helpers.c b/lib/gs-plugin-helpers.c
new file mode 100644
index 000000000..e8817a68f
--- /dev/null
+++ b/lib/gs-plugin-helpers.c
@@ -0,0 +1,97 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ * vi:set noexpandtab tabstop=8 shiftwidth=8:
+ *
+ * Copyright (C) 2021 Endless OS Foundation LLC
+ *
+ * Author: Philip Withnall <pwithnall endlessos org>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+/**
+ * SECTION:gs-plugin-helpers
+ * @short_description: Helpers for storing call closures for #GsPlugin vfuncs
+ *
+ * The helpers in this file each create a context structure to store the
+ * arguments passed to a standard #GsPlugin vfunc.
+ *
+ * These are intended to be used by plugin implementations to easily create
+ * #GTasks for handling #GsPlugin vfunc calls, without all having to write the
+ * same code to create a structure to wrap the vfunc arguments.
+ *
+ * Since: 42
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <glib-object.h>
+
+#include "gs-plugin-helpers.h"
+
+/**
+ * gs_plugin_refine_data_new:
+ * @list: list of #GsApps to refine
+ * @flags: refine flags
+ *
+ * Context data for a call to #GsPluginClass.refine_async.
+ *
+ * Returns: (transfer full): context data structure
+ * Since: 42
+ */
+GsPluginRefineData *
+gs_plugin_refine_data_new (GsAppList *list,
+ GsPluginRefineFlags flags)
+{
+ g_autoptr(GsPluginRefineData) data = g_new0 (GsPluginRefineData, 1);
+ data->list = g_object_ref (list);
+ data->flags = flags;
+
+ return g_steal_pointer (&data);
+}
+
+/**
+ * gs_plugin_refine_data_new_task:
+ * @source_object: task source object
+ * @list: list of #GsApps to refine
+ * @flags: refine flags
+ * @cancellable: (nullable): a #GCancellable, or %NULL
+ * @callback: function to call once asynchronous operation is finished
+ * @user_data: data to pass to @callback
+ *
+ * Create a #GTask for a refine operation with the given arguments. The task
+ * data will be set to a #GsPluginRefineData containing the given context.
+ *
+ * This is essentially a combination of gs_plugin_refine_data_new(),
+ * g_task_new() and g_task_set_task_data().
+ *
+ * Returns: (transfer full): new #GTask with the given context data
+ * Since: 42
+ */
+GTask *
+gs_plugin_refine_data_new_task (gpointer source_object,
+ GsAppList *list,
+ GsPluginRefineFlags flags,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ g_autoptr(GTask) task = g_task_new (source_object, cancellable, callback, user_data);
+ g_task_set_task_data (task, gs_plugin_refine_data_new (list, flags), (GDestroyNotify)
gs_plugin_refine_data_free);
+ return g_steal_pointer (&task);
+}
+
+/**
+ * gs_plugin_refine_data_free:
+ * @data: (transfer full): a #GsPluginRefineData
+ *
+ * Free the given @data.
+ *
+ * Since: 42
+ */
+void
+gs_plugin_refine_data_free (GsPluginRefineData *data)
+{
+ g_clear_object (&data->list);
+ g_free (data);
+}
diff --git a/lib/gs-plugin-helpers.h b/lib/gs-plugin-helpers.h
new file mode 100644
index 000000000..3edab86c8
--- /dev/null
+++ b/lib/gs-plugin-helpers.h
@@ -0,0 +1,37 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ * vi:set noexpandtab tabstop=8 shiftwidth=8:
+ *
+ * Copyright (C) 2021 Endless OS Foundation LLC
+ *
+ * Author: Philip Withnall <pwithnall endlessos org>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#pragma once
+
+#include <gio/gio.h>
+#include <glib.h>
+#include <glib-object.h>
+
+#include <gnome-software.h>
+
+G_BEGIN_DECLS
+
+typedef struct {
+ GsAppList *list; /* (owned) (not nullable) */
+ GsPluginRefineFlags flags;
+} GsPluginRefineData;
+
+GsPluginRefineData *gs_plugin_refine_data_new (GsAppList *list,
+ GsPluginRefineFlags flags);
+GTask *gs_plugin_refine_data_new_task (gpointer source_object,
+ GsAppList *list,
+ GsPluginRefineFlags flags,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+void gs_plugin_refine_data_free (GsPluginRefineData *data);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (GsPluginRefineData, gs_plugin_refine_data_free)
+
+G_END_DECLS
diff --git a/lib/meson.build b/lib/meson.build
index a80da81e0..d55407685 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -19,6 +19,7 @@ libgnomesoftware_public_headers = [
'gs-os-release.h',
'gs-plugin.h',
'gs-plugin-event.h',
+ 'gs-plugin-helpers.h',
'gs-plugin-job.h',
'gs-plugin-loader.h',
'gs-plugin-loader-sync.h',
@@ -92,6 +93,7 @@ libgnomesoftware = library(
'gs-os-release.c',
'gs-plugin.c',
'gs-plugin-event.c',
+ 'gs-plugin-helpers.c',
'gs-plugin-job.c',
'gs-plugin-loader.c',
'gs-plugin-loader-sync.c',
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]