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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2018 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1+
*/
/**
* SECTION:as-agreement
* @short_description: Object representing a privacy policy
* @include: appstream-glib.h
* @stability: Unstable
*
* Agreements can be used by components to specify GDPR, EULA or other warnings.
*
* See also: #AsAgreementDetail, #AsAgreementSection
*/
#include "config.h"
#include "as-node-private.h"
#include "as-agreement-private.h"
#include "as-agreement-section-private.h"
#include "as-ref-string.h"
#include "as-tag.h"
typedef struct {
AsAgreementKind kind;
AsRefString *version_id;
GPtrArray *sections;
} AsAgreementPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (AsAgreement, as_agreement, G_TYPE_OBJECT)
#define GET_PRIVATE(o) (as_agreement_get_instance_private (o))
static void
as_agreement_finalize (GObject *object)
{
AsAgreement *agreement = AS_AGREEMENT (object);
AsAgreementPrivate *priv = GET_PRIVATE (agreement);
if (priv->version_id != NULL)
as_ref_string_unref (priv->version_id);
g_ptr_array_unref (priv->sections);
G_OBJECT_CLASS (as_agreement_parent_class)->finalize (object);
}
static void
as_agreement_init (AsAgreement *agreement)
{
AsAgreementPrivate *priv = GET_PRIVATE (agreement);
priv->sections = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
}
static void
as_agreement_class_init (AsAgreementClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = as_agreement_finalize;
}
/**
* as_agreement_kind_to_string:
* @value: the #AsAgreementKind.
*
* Converts the enumerated value to an text representation.
*
* Returns: string version of @value
*
* Since: 0.7.8
**/
const gchar *
as_agreement_kind_to_string (AsAgreementKind value)
{
if (value == AS_AGREEMENT_KIND_GENERIC)
return "generic";
if (value == AS_AGREEMENT_KIND_EULA)
return "eula";
if (value == AS_AGREEMENT_KIND_PRIVACY)
return "privacy";
return "unknown";
}
/**
* as_agreement_kind_from_string:
* @value: the string.
*
* Converts the text representation to an enumerated value.
*
* Returns: a #AsAgreementKind or %AS_AGREEMENT_KIND_UNKNOWN for unknown
*
* Since: 0.7.8
**/
AsAgreementKind
as_agreement_kind_from_string (const gchar *value)
{
if (g_strcmp0 (value, "generic") == 0)
return AS_AGREEMENT_KIND_GENERIC;
if (g_strcmp0 (value, "eula") == 0)
return AS_AGREEMENT_KIND_EULA;
if (g_strcmp0 (value, "privacy") == 0)
return AS_AGREEMENT_KIND_PRIVACY;
return AS_AGREEMENT_KIND_UNKNOWN;
}
/**
* as_agreement_get_kind:
* @agreement: a #AsAgreement instance.
*
* Gets the agreement kind.
*
* Returns: a string, e.g. %AS_AGREEMENT_KIND_EULA
*
* Since: 0.7.8
**/
AsAgreementKind
as_agreement_get_kind (AsAgreement *agreement)
{
AsAgreementPrivate *priv = GET_PRIVATE (agreement);
g_return_val_if_fail (AS_IS_AGREEMENT (agreement), AS_AGREEMENT_KIND_UNKNOWN);
return priv->kind;
}
/**
* as_agreement_set_kind:
* @agreement: a #AsAgreement instance.
* @kind: the agreement kind, e.g. %AS_AGREEMENT_KIND_EULA
*
* Sets the agreement kind.
*
* Since: 0.7.8
**/
void
as_agreement_set_kind (AsAgreement *agreement, AsAgreementKind kind)
{
AsAgreementPrivate *priv = GET_PRIVATE (agreement);
g_return_if_fail (AS_IS_AGREEMENT (agreement));
priv->kind = kind;
}
/**
* as_agreement_get_version_id:
* @agreement: a #AsAgreement instance.
*
* Gets the agreement version_id.
*
* Returns: a string, e.g. "1.4a", or NULL
*
* Since: 0.7.8
**/
const gchar *
as_agreement_get_version_id (AsAgreement *agreement)
{
AsAgreementPrivate *priv = GET_PRIVATE (agreement);
g_return_val_if_fail (AS_IS_AGREEMENT (agreement), NULL);
return priv->version_id;
}
/**
* as_agreement_set_version_id:
* @agreement: a #AsAgreement instance.
* @version_id: the agreement version ID, e.g. "1.4a"
*
* Sets the agreement version identifier.
*
* Since: 0.7.8
**/
void
as_agreement_set_version_id (AsAgreement *agreement, const gchar *version_id)
{
AsAgreementPrivate *priv = GET_PRIVATE (agreement);
g_return_if_fail (AS_IS_AGREEMENT (agreement));
as_ref_string_assign_safe (&priv->version_id, version_id);
}
/**
* as_agreement_get_sections:
* @agreement: a #AsAgreement instance.
*
* Gets all the sections in the agreement.
*
* Returns: (transfer container) (element-type AsAgreementSection): array
*
* Since: 0.7.8
**/
GPtrArray *
as_agreement_get_sections (AsAgreement *agreement)
{
AsAgreementPrivate *priv = GET_PRIVATE (agreement);
g_return_val_if_fail (AS_IS_AGREEMENT (agreement), NULL);
return priv->sections;
}
/**
* as_agreement_get_section_default:
* @agreement: a #AsAgreement instance.
*
* Gets the first section in the agreement.
*
* Returns: (transfer none): agreement section, or %NULL
*
* Since: 0.7.8
**/
AsAgreementSection *
as_agreement_get_section_default (AsAgreement *agreement)
{
AsAgreementPrivate *priv = GET_PRIVATE (agreement);
g_return_val_if_fail (AS_IS_AGREEMENT (agreement), NULL);
if (priv->sections->len == 0)
return NULL;
return AS_AGREEMENT_SECTION (g_ptr_array_index (priv->sections, 0));
}
/**
* as_agreement_add_detail:
* @agreement: a #AsAgreement instance.
* @agreement_section: a #AsAgreementSection instance.
*
* Adds a section to the agreement.
*
* Since: 0.7.8
**/
void
as_agreement_add_section (AsAgreement *agreement, AsAgreementSection *agreement_section)
{
AsAgreementPrivate *priv = GET_PRIVATE (agreement);
g_return_if_fail (AS_IS_AGREEMENT (agreement));
g_ptr_array_add (priv->sections, g_object_ref (agreement_section));
}
/**
* as_agreement_node_insert: (skip)
* @agreement: a #AsAgreement instance.
* @parent: the parent #GNode to use..
* @ctx: the #AsNodeContext
*
* Inserts the agreement into the DOM tree.
*
* Returns: (transfer none): A populated #GNode, or %NULL
*
* Since: 0.7.8
**/
GNode *
as_agreement_node_insert (AsAgreement *agreement,
GNode *parent,
AsNodeContext *ctx)
{
AsAgreementPrivate *priv = GET_PRIVATE (agreement);
g_return_val_if_fail (AS_IS_AGREEMENT (agreement), NULL);
GNode *n = as_node_insert (parent, "agreement", NULL,
AS_NODE_INSERT_FLAG_NONE,
NULL);
if (priv->kind != AS_AGREEMENT_KIND_UNKNOWN) {
as_node_add_attribute (n, "type",
as_agreement_kind_to_string (priv->kind));
}
if (priv->version_id != NULL)
as_node_add_attribute (n, "version_id", priv->version_id);
for (guint i = 0; i < priv->sections->len; i++) {
AsAgreementSection *ps = g_ptr_array_index (priv->sections, i);
as_agreement_section_node_insert (ps, n, ctx);
}
return n;
}
/**
* as_agreement_node_parse:
* @agreement: a #AsAgreement 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.7.8
**/
gboolean
as_agreement_node_parse (AsAgreement *agreement, GNode *node,
AsNodeContext *ctx, GError **error)
{
const gchar *tmp;
/* get ID */
tmp = as_node_get_attribute (node, "type");
if (tmp != NULL)
as_agreement_set_kind (agreement, as_agreement_kind_from_string (tmp));
tmp = as_node_get_attribute (node, "version_id");
if (tmp != NULL)
as_agreement_set_version_id (agreement, tmp);
/* get sections and details */
for (GNode *c = node->children; c != NULL; c = c->next) {
if (as_node_get_tag (c) == AS_TAG_AGREEMENT_SECTION) {
g_autoptr(AsAgreementSection) ps = as_agreement_section_new ();
if (!as_agreement_section_node_parse (ps, c, ctx, error))
return FALSE;
as_agreement_add_section (agreement, ps);
continue;
}
}
return TRUE;
}
/**
* as_agreement_new:
*
* Creates a new #AsAgreement.
*
* Returns: (transfer full): a #AsAgreement
*
* Since: 0.7.8
**/
AsAgreement *
as_agreement_new (void)
{
AsAgreement *agreement;
agreement = g_object_new (AS_TYPE_AGREEMENT, NULL);
return AS_AGREEMENT (agreement);
}
|