summaryrefslogtreecommitdiffstats
path: root/lib/gs-plugin-job-list-distro-upgrades.c
blob: 618bb2abe1391cf54761bdece52b75e985436a7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 * vi:set noexpandtab tabstop=8 shiftwidth=8:
 *
 * Copyright (C) 2022 Endless OS Foundation LLC
 *
 * Author: Philip Withnall <pwithnall@endlessos.org>
 *
 * SPDX-License-Identifier: GPL-2.0+
 */

/**
 * SECTION:gs-plugin-job-list-distro-upgrades
 * @short_description: A plugin job to list distro upgrades
 *
 * #GsPluginJobListDistroUpgrades is a #GsPluginJob representing an operation to
 * list available upgrades for the distro, from all #GsPlugins.
 *
 * Upgrades for the distro are large upgrades, such as from Fedora 34 to
 * Fedora 35. They are not small package updates.
 *
 * This job will list the available upgrades, but will not download them or
 * install them. Due to the typical size of an upgrade, these should not be
 * downloaded until the user has explicitly requested it.
 *
 * The known properties on the set of apps returned by this operation can be
 * controlled with the #GsPluginJobListDistroUpgrades:refine-flags property. All
 * results will be refined using %GS_PLUGIN_REFINE_FLAGS_REQUIRE_SETUP_ACTION
 * plus the given set of refine flags. See #GsPluginJobRefine.
 *
 * This class is a wrapper around #GsPluginClass.list_distro_upgrades_async,
 * calling it for all loaded plugins, with some additional filtering
 * done on the results and #GsPluginJobRefine used to refine them.
 *
 * Retrieve the resulting #GsAppList using
 * gs_plugin_job_list_distro_upgrades_get_result_list(). Components in the list
 * are expected to be of type %AS_COMPONENT_KIND_OPERATING_SYSTEM.
 *
 * See also: #GsPluginClass.list_distro_upgrades_async
 * Since: 42
 */

#include "config.h"

#include <glib.h>
#include <glib-object.h>
#include <glib/gi18n.h>

#include "gs-app.h"
#include "gs-app-list-private.h"
#include "gs-enums.h"
#include "gs-plugin-job-private.h"
#include "gs-plugin-job-list-distro-upgrades.h"
#include "gs-plugin-job-refine.h"
#include "gs-plugin-private.h"
#include "gs-plugin-types.h"
#include "gs-utils.h"

struct _GsPluginJobListDistroUpgrades
{
	GsPluginJob parent;

	/* Input arguments. */
	GsPluginListDistroUpgradesFlags flags;
	GsPluginRefineFlags refine_flags;

	/* In-progress data. */
	GsAppList *merged_list;  /* (owned) (nullable) */
	GError *saved_error;  /* (owned) (nullable) */
	guint n_pending_ops;

	/* Results. */
	GsAppList *result_list;  /* (owned) (nullable) */
};

G_DEFINE_TYPE (GsPluginJobListDistroUpgrades, gs_plugin_job_list_distro_upgrades, GS_TYPE_PLUGIN_JOB)

typedef enum {
	PROP_REFINE_FLAGS = 1,
	PROP_FLAGS,
} GsPluginJobListDistroUpgradesProperty;

static GParamSpec *props[PROP_FLAGS + 1] = { NULL, };

static void
gs_plugin_job_list_distro_upgrades_dispose (GObject *object)
{
	GsPluginJobListDistroUpgrades *self = GS_PLUGIN_JOB_LIST_DISTRO_UPGRADES (object);

	g_assert (self->merged_list == NULL);
	g_assert (self->saved_error == NULL);
	g_assert (self->n_pending_ops == 0);

	g_clear_object (&self->result_list);

	G_OBJECT_CLASS (gs_plugin_job_list_distro_upgrades_parent_class)->dispose (object);
}

static void
gs_plugin_job_list_distro_upgrades_get_property (GObject    *object,
                                                 guint       prop_id,
                                                 GValue     *value,
                                                 GParamSpec *pspec)
{
	GsPluginJobListDistroUpgrades *self = GS_PLUGIN_JOB_LIST_DISTRO_UPGRADES (object);

	switch ((GsPluginJobListDistroUpgradesProperty) prop_id) {
	case PROP_REFINE_FLAGS:
		g_value_set_flags (value, self->refine_flags);
		break;
	case PROP_FLAGS:
		g_value_set_flags (value, self->flags);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
gs_plugin_job_list_distro_upgrades_set_property (GObject      *object,
                                                 guint         prop_id,
                                                 const GValue *value,
                                                 GParamSpec   *pspec)
{
	GsPluginJobListDistroUpgrades *self = GS_PLUGIN_JOB_LIST_DISTRO_UPGRADES (object);

	switch ((GsPluginJobListDistroUpgradesProperty) prop_id) {
	case PROP_REFINE_FLAGS:
		/* Construct only. */
		g_assert (self->refine_flags == 0);
		self->refine_flags = g_value_get_flags (value);
		g_object_notify_by_pspec (object, props[prop_id]);
		break;
	case PROP_FLAGS:
		/* Construct only. */
		g_assert (self->flags == 0);
		self->flags = g_value_get_flags (value);
		g_object_notify_by_pspec (object, props[prop_id]);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static gint
app_sort_version_cb (GsApp    *app1,
                     GsApp    *app2,
                     gpointer  user_data)
{
	return as_vercmp_simple (gs_app_get_version (app1),
				 gs_app_get_version (app2));
}

static void plugin_list_distro_upgrades_cb (GObject      *source_object,
                                            GAsyncResult *result,
                                            gpointer      user_data);
static void finish_op (GTask  *task,
                       GError *error);
static void refine_cb (GObject      *source_object,
                       GAsyncResult *result,
                       gpointer      user_data);
static void finish_task (GTask     *task,
                         GsAppList *merged_list);

static void
gs_plugin_job_list_distro_upgrades_run_async (GsPluginJob         *job,
                                              GsPluginLoader      *plugin_loader,
                                              GCancellable        *cancellable,
                                              GAsyncReadyCallback  callback,
                                              gpointer             user_data)
{
	GsPluginJobListDistroUpgrades *self = GS_PLUGIN_JOB_LIST_DISTRO_UPGRADES (job);
	g_autoptr(GTask) task = NULL;
	GPtrArray *plugins;  /* (element-type GsPlugin) */
	gboolean anything_ran = FALSE;

	/* check required args */
	task = g_task_new (job, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_job_list_distro_upgrades_run_async);
	g_task_set_task_data (task, g_object_ref (plugin_loader), (GDestroyNotify) g_object_unref);

	/* run each plugin, keeping a counter of pending operations which is
	 * initialised to 1 until all the operations are started */
	self->n_pending_ops = 1;
	self->merged_list = gs_app_list_new ();
	plugins = gs_plugin_loader_get_plugins (plugin_loader);

	for (guint i = 0; i < plugins->len; i++) {
		GsPlugin *plugin = g_ptr_array_index (plugins, i);
		GsPluginClass *plugin_class = GS_PLUGIN_GET_CLASS (plugin);

		if (!gs_plugin_get_enabled (plugin))
			continue;
		if (plugin_class->list_distro_upgrades_async == NULL)
			continue;

		/* at least one plugin supports this vfunc */
		anything_ran = TRUE;

		/* run the plugin */
		self->n_pending_ops++;
		plugin_class->list_distro_upgrades_async (plugin, self->flags, cancellable, plugin_list_distro_upgrades_cb, g_object_ref (task));
	}

	if (!anything_ran)
		g_debug ("no plugin could handle listing distro upgrades");

	finish_op (task, NULL);
}

static void
plugin_list_distro_upgrades_cb (GObject      *source_object,
                                GAsyncResult *result,
                                gpointer      user_data)
{
	GsPlugin *plugin = GS_PLUGIN (source_object);
	GsPluginClass *plugin_class = GS_PLUGIN_GET_CLASS (plugin);
	g_autoptr(GTask) task = G_TASK (user_data);
	GsPluginJobListDistroUpgrades *self = g_task_get_source_object (task);
	g_autoptr(GsAppList) plugin_apps = NULL;
	g_autoptr(GError) local_error = NULL;

	plugin_apps = plugin_class->list_distro_upgrades_finish (plugin, result, &local_error);
	gs_plugin_status_update (plugin, NULL, GS_PLUGIN_STATUS_FINISHED);

	if (plugin_apps != NULL)
		gs_app_list_add_list (self->merged_list, plugin_apps);

	finish_op (task, g_steal_pointer (&local_error));
}

/* @error is (transfer full) if non-%NULL */
static void
finish_op (GTask  *task,
           GError *error)
{
	GsPluginJobListDistroUpgrades *self = g_task_get_source_object (task);
	GCancellable *cancellable = g_task_get_cancellable (task);
	GsPluginLoader *plugin_loader = g_task_get_task_data (task);
	g_autoptr(GsAppList) merged_list = NULL;
	g_autoptr(GError) error_owned = g_steal_pointer (&error);

	if (error_owned != NULL && self->saved_error == NULL)
		self->saved_error = g_steal_pointer (&error_owned);
	else if (error_owned != NULL)
		g_debug ("Additional error while listing distro upgrades: %s", error_owned->message);

	g_assert (self->n_pending_ops > 0);
	self->n_pending_ops--;

	if (self->n_pending_ops > 0)
		return;

	/* Get the results of the parallel ops. */
	merged_list = g_steal_pointer (&self->merged_list);

	if (self->saved_error != NULL) {
		g_task_return_error (task, g_steal_pointer (&self->saved_error));
		return;
	}

	/* run refine() on each one if required */
	if (merged_list != NULL &&
	    gs_app_list_length (merged_list) > 0) {
		g_autoptr(GsPluginJob) refine_job = NULL;

		/* Always specify REQUIRE_SETUP_ACTION, as that requires enough
		 * information to be able to install the upgrade later if
		 * requested. */
		refine_job = gs_plugin_job_refine_new (merged_list,
						       self->refine_flags |
						       GS_PLUGIN_REFINE_FLAGS_REQUIRE_SETUP_ACTION |
						       GS_PLUGIN_REFINE_FLAGS_DISABLE_FILTERING);
		gs_plugin_loader_job_process_async (plugin_loader, refine_job,
						    cancellable,
						    refine_cb,
						    g_object_ref (task));
	} else {
		g_debug ("No distro upgrades to refine");
		finish_task (task, merged_list);
	}
}

static void
refine_cb (GObject      *source_object,
           GAsyncResult *result,
           gpointer      user_data)
{
	GsPluginLoader *plugin_loader = GS_PLUGIN_LOADER (source_object);
	g_autoptr(GTask) task = G_TASK (user_data);
	g_autoptr(GsAppList) new_list = NULL;
	g_autoptr(GError) local_error = NULL;

	new_list = gs_plugin_loader_job_process_finish (plugin_loader, result, &local_error);
	if (new_list == NULL) {
		gs_utils_error_convert_gio (&local_error);
		g_task_return_error (task, g_steal_pointer (&local_error));
		return;
	}

	finish_task (task, new_list);
}

static void
finish_task (GTask     *task,
             GsAppList *merged_list)
{
	GsPluginJobListDistroUpgrades *self = g_task_get_source_object (task);
	g_autofree gchar *job_debug = NULL;

	/* Sort the results. The refine may have added useful metadata. */
	gs_app_list_sort (merged_list, app_sort_version_cb, NULL);

	/* show elapsed time */
	job_debug = gs_plugin_job_to_string (GS_PLUGIN_JOB (self));
	g_debug ("%s", job_debug);

	/* Check the intermediate working values are all cleared. */
	g_assert (self->merged_list == NULL);
	g_assert (self->saved_error == NULL);
	g_assert (self->n_pending_ops == 0);

	/* success */
	g_set_object (&self->result_list, merged_list);
	g_task_return_boolean (task, TRUE);
}

static gboolean
gs_plugin_job_list_distro_upgrades_run_finish (GsPluginJob   *self,
                                               GAsyncResult  *result,
                                               GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

static void
gs_plugin_job_list_distro_upgrades_class_init (GsPluginJobListDistroUpgradesClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	GsPluginJobClass *job_class = GS_PLUGIN_JOB_CLASS (klass);

	object_class->dispose = gs_plugin_job_list_distro_upgrades_dispose;
	object_class->get_property = gs_plugin_job_list_distro_upgrades_get_property;
	object_class->set_property = gs_plugin_job_list_distro_upgrades_set_property;

	job_class->run_async = gs_plugin_job_list_distro_upgrades_run_async;
	job_class->run_finish = gs_plugin_job_list_distro_upgrades_run_finish;

	/**
	 * GsPluginJobListDistroUpgrades:refine-flags:
	 *
	 * Flags to specify how to refine the returned apps.
	 *
	 * %GS_PLUGIN_REFINE_FLAGS_REQUIRE_SETUP_ACTION will always be used.
	 *
	 * Since: 42
	 */
	props[PROP_REFINE_FLAGS] =
		g_param_spec_flags ("refine-flags", "Refine Flags",
				    "Flags to specify how to refine the returned apps.",
				    GS_TYPE_PLUGIN_REFINE_FLAGS, GS_PLUGIN_REFINE_FLAGS_NONE,
				    G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
				    G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	/**
	 * GsPluginJobListDistroUpgrades:flags:
	 *
	 * Flags to specify how the operation should run.
	 *
	 * Since: 42
	 */
	props[PROP_FLAGS] =
		g_param_spec_flags ("flags", "Flags",
				    "Flags to specify how the operation should run.",
				    GS_TYPE_PLUGIN_LIST_DISTRO_UPGRADES_FLAGS,
				    GS_PLUGIN_LIST_DISTRO_UPGRADES_FLAGS_NONE,
				    G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
				    G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);

	g_object_class_install_properties (object_class, G_N_ELEMENTS (props), props);
}

static void
gs_plugin_job_list_distro_upgrades_init (GsPluginJobListDistroUpgrades *self)
{
}

/**
 * gs_plugin_job_list_distro_upgrades_new:
 * @flags: flags affecting how the operation runs
 * @refine_flags: flags to affect how the results are refined
 *
 * Create a new #GsPluginJobListDistroUpgrades for listing the available distro
 * upgrades.
 *
 * Returns: (transfer full): a new #GsPluginJobListDistroUpgrades
 * Since: 42
 */
GsPluginJob *
gs_plugin_job_list_distro_upgrades_new (GsPluginListDistroUpgradesFlags flags,
                                        GsPluginRefineFlags             refine_flags)
{
	return g_object_new (GS_TYPE_PLUGIN_JOB_LIST_DISTRO_UPGRADES,
			     "refine-flags", refine_flags,
			     "flags", flags,
			     NULL);
}

/**
 * gs_plugin_job_list_distro_upgrades_get_result_list:
 * @self: a #GsPluginJobListDistroUpgrades
 *
 * Get the full list of available distro upgrades.
 *
 * If this is called before the job is complete, %NULL will be returned.
 *
 * Returns: (transfer none) (nullable): the job results, or %NULL on error
 *   or if called before the job has completed
 * Since: 42
 */
GsAppList *
gs_plugin_job_list_distro_upgrades_get_result_list (GsPluginJobListDistroUpgrades *self)
{
	g_return_val_if_fail (GS_IS_PLUGIN_JOB_LIST_DISTRO_UPGRADES (self), NULL);

	return self->result_list;
}