summaryrefslogtreecommitdiff
path: root/libappstream-glib/as-bundle.c
blob: e3eb2475bbf8083d3e6564448f98d9392340a6c7 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2014-2015 Richard Hughes <richard@hughsie.com>
 *
 * SPDX-License-Identifier: LGPL-2.1+
 */

/**
 * SECTION:as-bundle
 * @short_description: Object representing a single bundle used in a screenshot.
 * @include: appstream-glib.h
 * @stability: Stable
 *
 * Screenshot may have multiple versions of an bundle in different resolutions
 * or aspect ratios. This object allows access to the location and size of a
 * single bundle.
 *
 * See also: #AsScreenshot
 */

#include "config.h"

#include "as-bundle-private.h"
#include "as-node-private.h"
#include "as-ref-string.h"
#include "as-utils-private.h"
#include "as-yaml.h"

typedef struct
{
	AsBundleKind		 kind;
	AsRefString		*id;
	AsRefString		*runtime;
	AsRefString		*sdk;
} AsBundlePrivate;

G_DEFINE_TYPE_WITH_PRIVATE (AsBundle, as_bundle, G_TYPE_OBJECT)

#define GET_PRIVATE(o) (as_bundle_get_instance_private (o))

static void
as_bundle_finalize (GObject *object)
{
	AsBundle *bundle = AS_BUNDLE (object);
	AsBundlePrivate *priv = GET_PRIVATE (bundle);

	if (priv->id != NULL)
		as_ref_string_unref (priv->id);
	if (priv->runtime != NULL)
		as_ref_string_unref (priv->runtime);
	if (priv->sdk != NULL)
		as_ref_string_unref (priv->sdk);

	G_OBJECT_CLASS (as_bundle_parent_class)->finalize (object);
}

static void
as_bundle_init (AsBundle *bundle)
{
}

static void
as_bundle_class_init (AsBundleClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = as_bundle_finalize;
}


/**
 * as_bundle_kind_from_string:
 * @kind: the string.
 *
 * Converts the text representation to an enumerated value.
 *
 * Returns: (transfer full): a #AsBundleKind, or %AS_BUNDLE_KIND_UNKNOWN for unknown.
 *
 * Since: 0.3.5
 **/
AsBundleKind
as_bundle_kind_from_string (const gchar *kind)
{
	if (g_strcmp0 (kind, "limba") == 0)
		return AS_BUNDLE_KIND_LIMBA;
	if (g_strcmp0 (kind, "xdg-app") == 0) /* backwards compat */
		return AS_BUNDLE_KIND_FLATPAK;
	if (g_strcmp0 (kind, "flatpak") == 0)
		return AS_BUNDLE_KIND_FLATPAK;
	if (g_strcmp0 (kind, "snap") == 0)
		return AS_BUNDLE_KIND_SNAP;
	if (g_strcmp0 (kind, "package") == 0)
		return AS_BUNDLE_KIND_PACKAGE;
	if (g_strcmp0 (kind, "cabinet") == 0)
		return AS_BUNDLE_KIND_CABINET;
	if (g_strcmp0 (kind, "appimage") == 0)
		return AS_BUNDLE_KIND_APPIMAGE;
	return AS_BUNDLE_KIND_UNKNOWN;
}

/**
 * as_bundle_kind_to_string:
 * @kind: the #AsBundleKind.
 *
 * Converts the enumerated value to an text representation.
 *
 * Returns: string version of @kind
 *
 * Since: 0.3.5
 **/
const gchar *
as_bundle_kind_to_string (AsBundleKind kind)
{
	if (kind == AS_BUNDLE_KIND_LIMBA)
		return "limba";
	if (kind == AS_BUNDLE_KIND_FLATPAK)
		return "flatpak";
	if (kind == AS_BUNDLE_KIND_SNAP)
		return "snap";
	if (kind == AS_BUNDLE_KIND_PACKAGE)
		return "package";
	if (kind == AS_BUNDLE_KIND_CABINET)
		return "cabinet";
	if (kind == AS_BUNDLE_KIND_APPIMAGE)
		return "appimage";
	return NULL;
}

/**
 * as_bundle_get_id:
 * @bundle: a #AsBundle instance.
 *
 * Gets the ID for this bundle.
 *
 * Returns: ID, e.g. "foobar-1.0.2"
 *
 * Since: 0.3.5
 **/
const gchar *
as_bundle_get_id (AsBundle *bundle)
{
	AsBundlePrivate *priv = GET_PRIVATE (bundle);
	g_return_val_if_fail (AS_IS_BUNDLE (bundle), NULL);
	return priv->id;
}

/**
 * as_bundle_get_runtime:
 * @bundle: a #AsBundle instance.
 *
 * Gets the runtime required for this bundle.
 *
 * Returns: Runtime identifier, e.g. "org.gnome.Platform/i386/master"
 *
 * Since: 0.5.10
 **/
const gchar *
as_bundle_get_runtime (AsBundle *bundle)
{
	AsBundlePrivate *priv = GET_PRIVATE (bundle);
	g_return_val_if_fail (AS_IS_BUNDLE (bundle), NULL);
	return priv->runtime;
}

/**
 * as_bundle_get_sdk:
 * @bundle: a #AsBundle instance.
 *
 * Gets the SDK for this bundle.
 *
 * Returns: SDK identifier, e.g. "org.gnome.Sdk/i386/master"
 *
 * Since: 0.5.10
 **/
const gchar *
as_bundle_get_sdk (AsBundle *bundle)
{
	AsBundlePrivate *priv = GET_PRIVATE (bundle);
	g_return_val_if_fail (AS_IS_BUNDLE (bundle), NULL);
	return priv->sdk;
}

/**
 * as_bundle_get_kind:
 * @bundle: a #AsBundle instance.
 *
 * Gets the bundle kind.
 *
 * Returns: the #AsBundleKind
 *
 * Since: 0.3.5
 **/
AsBundleKind
as_bundle_get_kind (AsBundle *bundle)
{
	AsBundlePrivate *priv = GET_PRIVATE (bundle);
	g_return_val_if_fail (AS_IS_BUNDLE (bundle), AS_BUNDLE_KIND_UNKNOWN);
	return priv->kind;
}

/**
 * as_bundle_set_id:
 * @bundle: a #AsBundle instance.
 * @id: the URL.
 *
 * Sets the ID for this bundle.
 *
 * Since: 0.3.5
 **/
void
as_bundle_set_id (AsBundle *bundle, const gchar *id)
{
	AsBundlePrivate *priv = GET_PRIVATE (bundle);
	g_return_if_fail (AS_IS_BUNDLE (bundle));
	as_ref_string_assign_safe (&priv->id, id);
}

/**
 * as_bundle_set_runtime:
 * @bundle: a #AsBundle instance.
 * @runtime: the URL.
 *
 * Sets the runtime required for this bundle.
 *
 * Since: 0.5.10
 **/
void
as_bundle_set_runtime (AsBundle *bundle, const gchar *runtime)
{
	AsBundlePrivate *priv = GET_PRIVATE (bundle);
	g_return_if_fail (AS_IS_BUNDLE (bundle));
	as_ref_string_assign_safe (&priv->runtime, runtime);
}

/**
 * as_bundle_set_sdk:
 * @bundle: a #AsBundle instance.
 * @sdk: the URL.
 *
 * Sets the SDK for this bundle.
 *
 * Since: 0.5.10
 **/
void
as_bundle_set_sdk (AsBundle *bundle, const gchar *sdk)
{
	AsBundlePrivate *priv = GET_PRIVATE (bundle);
	g_return_if_fail (AS_IS_BUNDLE (bundle));
	as_ref_string_assign_safe (&priv->sdk, sdk);
}

/**
 * as_bundle_set_kind:
 * @bundle: a #AsBundle instance.
 * @kind: the #AsBundleKind, e.g. %AS_BUNDLE_KIND_FLATPAK.
 *
 * Sets the bundle kind.
 *
 * Since: 0.3.5
 **/
void
as_bundle_set_kind (AsBundle *bundle, AsBundleKind kind)
{
	AsBundlePrivate *priv = GET_PRIVATE (bundle);
	g_return_if_fail (AS_IS_BUNDLE (bundle));
	priv->kind = kind;
}

/**
 * as_bundle_node_insert: (skip)
 * @bundle: a #AsBundle instance.
 * @parent: the parent #GNode to use..
 * @ctx: the #AsNodeContext
 *
 * Inserts the bundle into the DOM tree.
 *
 * Returns: (transfer none): A populated #GNode
 *
 * Since: 0.3.5
 **/
GNode *
as_bundle_node_insert (AsBundle *bundle, GNode *parent, AsNodeContext *ctx)
{
	AsBundlePrivate *priv = GET_PRIVATE (bundle);
	GNode *n;

	g_return_val_if_fail (AS_IS_BUNDLE (bundle), NULL);

	n = as_node_insert (parent, "bundle", priv->id,
			    AS_NODE_INSERT_FLAG_NONE,
			    "type", as_bundle_kind_to_string (priv->kind),
			    NULL);
	if (priv->runtime != NULL)
		as_node_add_attribute (n, "runtime", priv->runtime);
	if (priv->sdk != NULL)
		as_node_add_attribute (n, "sdk", priv->sdk);

	return n;
}

/**
 * as_bundle_node_parse:
 * @bundle: a #AsBundle instance.
 * @node: a #GNode.
 * @ctx: a #AsNodeContext.
 * @error: A #GError or %NULL.
 *
 * Populates the object from a DOM node.
 *
 * Returns: %TRUE for success
 *
 * Since: 0.3.5
 **/
gboolean
as_bundle_node_parse (AsBundle *bundle, GNode *node,
		      AsNodeContext *ctx, GError **error)
{
	AsBundlePrivate *priv = GET_PRIVATE (bundle);
	const gchar *tmp;

	g_return_val_if_fail (AS_IS_BUNDLE (bundle), FALSE);

	tmp = as_node_get_attribute (node, "type");
	as_bundle_set_kind (bundle, as_bundle_kind_from_string (tmp));

	as_ref_string_assign (&priv->id, as_node_get_data_as_refstr (node));

	/* optional */
	as_ref_string_assign (&priv->runtime, as_node_get_attribute_as_refstr (node, "runtime"));
	as_ref_string_assign (&priv->sdk, as_node_get_attribute_as_refstr (node, "sdk"));

	return TRUE;
}

/**
 * as_bundle_node_parse_dep11:
 * @bundle: a #AsBundle instance.
 * @node: a #GNode.
 * @ctx: a #AsNodeContext.
 * @error: A #GError or %NULL.
 *
 * Populates the object from a DEP-11 node.
 *
 * Returns: %TRUE for success
 *
 * Since: 0.3.5
 **/
gboolean
as_bundle_node_parse_dep11 (AsBundle *im, GNode *node,
			    AsNodeContext *ctx, GError **error)
{
	GNode *n;
	const gchar *tmp;

	for (n = node->children; n != NULL; n = n->next) {
		tmp = as_yaml_node_get_key (n);
		if (g_strcmp0 (tmp, "id") == 0)
			as_bundle_set_id (im, as_yaml_node_get_value (n));
	}
	return TRUE;
}

/**
 * as_bundle_new:
 *
 * Creates a new #AsBundle.
 *
 * Returns: (transfer full): a #AsBundle
 *
 * Since: 0.3.5
 **/
AsBundle *
as_bundle_new (void)
{
	AsBundle *bundle;
	bundle = g_object_new (AS_TYPE_BUNDLE, NULL);
	return AS_BUNDLE (bundle);
}