summaryrefslogtreecommitdiff
path: root/libappstream-glib/as-provide.c
blob: 8189973741356a9f7023fed53b01e8c475a41f19 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2014 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */

/**
 * SECTION:as-provide
 * @short_description: Object representing a single object the application provides.
 * @include: appstream-glib.h
 * @stability: Stable
 *
 * Applications may provide different binary names, firmware files and that
 * kind of thing. This is the place to express those extra items.
 *
 * See also: #AsApp
 */

#include "config.h"

#include "as-node-private.h"
#include "as-provide-private.h"
#include "as-utils-private.h"
#include "as-yaml.h"

typedef struct
{
	AsProvideKind		 kind;
	gchar			*value;
} AsProvidePrivate;

G_DEFINE_TYPE_WITH_PRIVATE (AsProvide, as_provide, G_TYPE_OBJECT)

#define GET_PRIVATE(o) (as_provide_get_instance_private (o))

/**
 * as_provide_finalize:
 **/
static void
as_provide_finalize (GObject *object)
{
	AsProvide *provide = AS_PROVIDE (object);
	AsProvidePrivate *priv = GET_PRIVATE (provide);

	g_free (priv->value);

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

/**
 * as_provide_init:
 **/
static void
as_provide_init (AsProvide *provide)
{
}

/**
 * as_provide_class_init:
 **/
static void
as_provide_class_init (AsProvideClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = as_provide_finalize;
}


/**
 * as_provide_kind_from_string:
 * @kind: the string.
 *
 * Converts the text representation to an enumerated value.
 *
 * Returns: (transfer full): a #AsProvideKind, or %AS_PROVIDE_KIND_UNKNOWN for unknown.
 *
 * Since: 0.1.6
 **/
AsProvideKind
as_provide_kind_from_string (const gchar *kind)
{
	if (g_strcmp0 (kind, "library") == 0)
		return AS_PROVIDE_KIND_LIBRARY;
	if (g_strcmp0 (kind, "binary") == 0)
		return AS_PROVIDE_KIND_BINARY;
	if (g_strcmp0 (kind, "font") == 0)
		return AS_PROVIDE_KIND_FONT;
	if (g_strcmp0 (kind, "modalias") == 0)
		return AS_PROVIDE_KIND_MODALIAS;
	if (g_strcmp0 (kind, "firmware-runtime") == 0)
		return AS_PROVIDE_KIND_FIRMWARE_RUNTIME;
	if (g_strcmp0 (kind, "firmware-flashed") == 0)
		return AS_PROVIDE_KIND_FIRMWARE_FLASHED;
	if (g_strcmp0 (kind, "python2") == 0)
		return AS_PROVIDE_KIND_PYTHON2;
	if (g_strcmp0 (kind, "python3") == 0)
		return AS_PROVIDE_KIND_PYTHON3;
	if (g_strcmp0 (kind, "dbus-session") == 0)
		return AS_PROVIDE_KIND_DBUS_SESSION;
	if (g_strcmp0 (kind, "dbus-system") == 0)
		return AS_PROVIDE_KIND_DBUS_SYSTEM;
	return AS_PROVIDE_KIND_UNKNOWN;
}

/**
 * as_provide_kind_to_string:
 * @kind: the #AsProvideKind.
 *
 * Converts the enumerated value to an text representation.
 *
 * Returns: string version of @kind
 *
 * Since: 0.1.6
 **/
const gchar *
as_provide_kind_to_string (AsProvideKind kind)
{
	if (kind == AS_PROVIDE_KIND_LIBRARY)
		return "library";
	if (kind == AS_PROVIDE_KIND_BINARY)
		return "binary";
	if (kind == AS_PROVIDE_KIND_FONT)
		return "font";
	if (kind == AS_PROVIDE_KIND_MODALIAS)
		return "modalias";
	if (kind == AS_PROVIDE_KIND_FIRMWARE_RUNTIME)
		return "firmware-runtime";
	if (kind == AS_PROVIDE_KIND_FIRMWARE_FLASHED)
		return "firmware-flashed";
	if (kind == AS_PROVIDE_KIND_PYTHON2)
		return "python2";
	if (kind == AS_PROVIDE_KIND_PYTHON3)
		return "python3";
	if (kind == AS_PROVIDE_KIND_DBUS_SESSION)
		return "dbus";
	if (kind == AS_PROVIDE_KIND_DBUS_SYSTEM)
		return "dbus-system";
	return NULL;
}

/**
 * as_provide_get_value:
 * @provide: a #AsProvide instance.
 *
 * Gets the full qualified URL for the provide, usually pointing at some mirror.
 *
 * Returns: URL
 *
 * Since: 0.1.6
 **/
const gchar *
as_provide_get_value (AsProvide *provide)
{
	AsProvidePrivate *priv = GET_PRIVATE (provide);
	return priv->value;
}

/**
 * as_provide_get_kind:
 * @provide: a #AsProvide instance.
 *
 * Gets the provide kind.
 *
 * Returns: the #AsProvideKind
 *
 * Since: 0.1.6
 **/
AsProvideKind
as_provide_get_kind (AsProvide *provide)
{
	AsProvidePrivate *priv = GET_PRIVATE (provide);
	return priv->kind;
}

/**
 * as_provide_set_value:
 * @provide: a #AsProvide instance.
 * @value: the URL.
 *
 * Sets the fully-qualified mirror URL to use for the provide.
 *
 * Since: 0.1.6
 **/
void
as_provide_set_value (AsProvide *provide, const gchar *value)
{
	AsProvidePrivate *priv = GET_PRIVATE (provide);
	g_free (priv->value);
	priv->value = g_strdup (value);
}

/**
 * as_provide_set_kind:
 * @provide: a #AsProvide instance.
 * @kind: the #AsProvideKind, e.g. %AS_PROVIDE_KIND_LIBRARY.
 *
 * Sets the provide kind.
 *
 * Since: 0.1.6
 **/
void
as_provide_set_kind (AsProvide *provide, AsProvideKind kind)
{
	AsProvidePrivate *priv = GET_PRIVATE (provide);
	priv->kind = kind;
}

/**
 * as_provide_node_insert: (skip)
 * @provide: a #AsProvide instance.
 * @parent: the parent #GNode to use..
 * @ctx: the #AsNodeContext
 *
 * Inserts the provide into the DOM tree.
 *
 * Returns: (transfer none): A populated #GNode
 *
 * Since: 0.1.6
 **/
GNode *
as_provide_node_insert (AsProvide *provide, GNode *parent, AsNodeContext *ctx)
{
	AsProvidePrivate *priv = GET_PRIVATE (provide);
	GNode *n = NULL;

	switch (priv->kind) {
	case AS_PROVIDE_KIND_UNKNOWN:
		break;
	case AS_PROVIDE_KIND_DBUS_SESSION:
		n = as_node_insert (parent, "dbus",
				    priv->value,
				    AS_NODE_INSERT_FLAG_NONE,
				    "type", "session",
				    NULL);
		break;
	case AS_PROVIDE_KIND_DBUS_SYSTEM:
		n = as_node_insert (parent, "dbus",
				    priv->value,
				    AS_NODE_INSERT_FLAG_NONE,
				    "type", "system",
				    NULL);
		break;
	case AS_PROVIDE_KIND_FIRMWARE_FLASHED:
		n = as_node_insert (parent, "firmware",
				    priv->value,
				    AS_NODE_INSERT_FLAG_NONE,
				    "type", "flashed",
				    NULL);
		break;
	case AS_PROVIDE_KIND_FIRMWARE_RUNTIME:
		n = as_node_insert (parent, "firmware",
				    priv->value,
				    AS_NODE_INSERT_FLAG_NONE,
				    "type", "runtime",
				    NULL);
		break;
	default:
		n = as_node_insert (parent, as_provide_kind_to_string (priv->kind),
				    priv->value,
				    AS_NODE_INSERT_FLAG_NONE,
				    NULL);
		break;
	}
	return n;
}

/**
 * as_provide_node_parse_dep11:
 * @provide: a #AsProvide 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.0
 **/
gboolean
as_provide_node_parse_dep11 (AsProvide *provide, GNode *node,
			     AsNodeContext *ctx, GError **error)
{
	return TRUE;
}

/**
 * as_provide_node_parse:
 * @provide: a #AsProvide 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.1.6
 **/
gboolean
as_provide_node_parse (AsProvide *provide, GNode *node,
		       AsNodeContext *ctx, GError **error)
{
	AsProvidePrivate *priv = GET_PRIVATE (provide);
	const gchar *tmp;

	if (g_strcmp0 (as_node_get_name (node), "dbus") == 0) {
		tmp = as_node_get_attribute (node, "type");
		if (g_strcmp0 (tmp, "system") == 0)
			priv->kind = AS_PROVIDE_KIND_DBUS_SYSTEM;
		else
			priv->kind = AS_PROVIDE_KIND_DBUS_SESSION;
	} else if (g_strcmp0 (as_node_get_name (node), "firmware") == 0) {
		tmp = as_node_get_attribute (node, "type");
		if (g_strcmp0 (tmp, "flashed") == 0)
			priv->kind = AS_PROVIDE_KIND_FIRMWARE_FLASHED;
		else
			priv->kind = AS_PROVIDE_KIND_FIRMWARE_RUNTIME;
	} else {
		priv->kind = as_provide_kind_from_string (as_node_get_name (node));
	}
	g_free (priv->value);
	priv->value = as_node_take_data (node);
	return TRUE;
}

/**
 * as_provide_new:
 *
 * Creates a new #AsProvide.
 *
 * Returns: (transfer full): a #AsProvide
 *
 * Since: 0.1.6
 **/
AsProvide *
as_provide_new (void)
{
	AsProvide *provide;
	provide = g_object_new (AS_TYPE_PROVIDE, NULL);
	return AS_PROVIDE (provide);
}