summaryrefslogtreecommitdiff
path: root/libappstream-glib/as-agreement-section.c
blob: f911858e964eb402251d24875d458d745b396f0f (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2018 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-agreement-section
 * @short_description: Object representing a agreement section
 * @include: appstream-glib.h
 * @stability: Unstable
 *
 * Agreements are typically split up into sections.
 *
 * See also: #AsAgreement, #AsAgreementDetail
 */

#include "config.h"

#include "as-node-private.h"
#include "as-agreement-section-private.h"
#include "as-ref-string.h"
#include "as-tag.h"
#include "as-utils-private.h"

typedef struct {
	AsRefString		*kind;
	GHashTable		*names;		/* of AsRefString:AsRefString */
	GHashTable		*descriptions;	/* of AsRefString:AsRefString */
} AsAgreementSectionPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (AsAgreementSection, as_agreement_section, G_TYPE_OBJECT)

#define GET_PRIVATE(o) (as_agreement_section_get_instance_private (o))

static void
as_agreement_section_finalize (GObject *object)
{
	AsAgreementSection *agreement_section = AS_AGREEMENT_SECTION (object);
	AsAgreementSectionPrivate *priv = GET_PRIVATE (agreement_section);

	if (priv->kind != NULL)
		as_ref_string_unref (priv->kind);
	g_hash_table_unref (priv->names);
	g_hash_table_unref (priv->descriptions);

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

static void
as_agreement_section_init (AsAgreementSection *agreement_section)
{
	AsAgreementSectionPrivate *priv = GET_PRIVATE (agreement_section);
	priv->names = g_hash_table_new_full (g_str_hash, g_str_equal,
					     (GDestroyNotify) as_ref_string_unref,
					     (GDestroyNotify) as_ref_string_unref);
	priv->descriptions = g_hash_table_new_full (g_str_hash, g_str_equal,
						    (GDestroyNotify) as_ref_string_unref,
						    (GDestroyNotify) as_ref_string_unref);
}

static void
as_agreement_section_class_init (AsAgreementSectionClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = as_agreement_section_finalize;
}

/**
 * as_agreement_section_get_kind:
 * @agreement_section: a #AsAgreementSection instance.
 *
 * Gets the agreement section kind.
 *
 * Returns: a string, e.g. "GDPR", or NULL
 *
 * Since: 0.7.8
 **/
const gchar *
as_agreement_section_get_kind (AsAgreementSection *agreement_section)
{
	AsAgreementSectionPrivate *priv = GET_PRIVATE (agreement_section);
	g_return_val_if_fail (AS_IS_AGREEMENT_SECTION (agreement_section), NULL);
	return priv->kind;
}

/**
 * as_agreement_section_set_kind:
 * @agreement_section: a #AsAgreementSection instance.
 * @kind: the rating kind, e.g. "GDPR"
 *
 * Sets the agreement section kind.
 *
 * Since: 0.7.8
 **/
void
as_agreement_section_set_kind (AsAgreementSection *agreement_section, const gchar *kind)
{
	AsAgreementSectionPrivate *priv = GET_PRIVATE (agreement_section);
	g_return_if_fail (AS_IS_AGREEMENT_SECTION (agreement_section));
	as_ref_string_assign_safe (&priv->kind, kind);
}

/**
 * as_agreement_section_get_name:
 * @agreement_section: a #AsAgreementSection instance.
 * @locale: (nullable): the locale. e.g. "en_GB"
 *
 * Gets the agreement section name.
 *
 * Returns: a string, e.g. "GDPR", or NULL
 *
 * Since: 0.7.8
 **/
const gchar *
as_agreement_section_get_name (AsAgreementSection *agreement_section, const gchar *locale)
{
	AsAgreementSectionPrivate *priv = GET_PRIVATE (agreement_section);
	g_return_val_if_fail (AS_IS_AGREEMENT_SECTION (agreement_section), NULL);
	return as_hash_lookup_by_locale (priv->names, locale);
}

/**
 * as_agreement_section_set_name:
 * @agreement_section: a #AsAgreementSection instance.
 * @locale: (nullable): the locale. e.g. "en_GB"
 * @name: the rating name, e.g. "GDPR"
 *
 * Sets the agreement section name.
 *
 * Since: 0.7.8
 **/
void
as_agreement_section_set_name (AsAgreementSection *agreement_section,
			       const gchar *locale, const gchar *name)
{
	AsAgreementSectionPrivate *priv = GET_PRIVATE (agreement_section);
	g_autoptr(AsRefString) locale_fixed = NULL;

	g_return_if_fail (AS_IS_AGREEMENT_SECTION (agreement_section));
	g_return_if_fail (name != NULL);

	/* get fixed locale */
	locale_fixed = as_node_fix_locale (locale);
	if (locale_fixed == NULL)
		return;
	g_hash_table_insert (priv->names,
			     as_ref_string_ref (locale_fixed),
			     as_ref_string_new (name));
}

/**
 * as_agreement_section_get_description:
 * @agreement_section: a #AsAgreementSection instance.
 * @locale: (nullable): the locale. e.g. "en_GB"
 *
 * Gets the agreement section desc.
 *
 * Returns: a string, e.g. "GDPR", or NULL
 *
 * Since: 0.7.8
 **/
const gchar *
as_agreement_section_get_description (AsAgreementSection *agreement_section,
				      const gchar *locale)
{
	AsAgreementSectionPrivate *priv = GET_PRIVATE (agreement_section);
	g_return_val_if_fail (AS_IS_AGREEMENT_SECTION (agreement_section), NULL);
	return as_hash_lookup_by_locale (priv->descriptions, locale);
}

/**
 * as_agreement_section_set_description:
 * @agreement_section: a #AsAgreementSection instance.
 * @locale: (nullable): the locale. e.g. "en_GB"
 * @desc: the rating desc, e.g. "GDPR"
 *
 * Sets the agreement section description.
 *
 * Since: 0.7.8
 **/
void
as_agreement_section_set_description (AsAgreementSection *agreement_section,
				      const gchar *locale, const gchar *desc)
{
	AsAgreementSectionPrivate *priv = GET_PRIVATE (agreement_section);
	g_autoptr(AsRefString) locale_fixed = NULL;

	g_return_if_fail (AS_IS_AGREEMENT_SECTION (agreement_section));
	g_return_if_fail (desc != NULL);

	/* get fixed locale */
	locale_fixed = as_node_fix_locale (locale);
	if (locale_fixed == NULL)
		return;
	g_hash_table_insert (priv->descriptions,
			     as_ref_string_ref (locale_fixed),
			     as_ref_string_new (desc));
}

/**
 * as_agreement_section_node_insert: (skip)
 * @agreement_section: a #AsAgreementSection instance.
 * @parent: the parent #GNode to use..
 * @ctx: the #AsNodeContext
 *
 * Inserts the agreement_section into the DOM tree.
 *
 * Returns: (transfer none): A populated #GNode, or %NULL
 *
 * Since: 0.7.8
 **/
GNode *
as_agreement_section_node_insert (AsAgreementSection *agreement_section,
				  GNode *parent,
				  AsNodeContext *ctx)
{
	AsAgreementSectionPrivate *priv = GET_PRIVATE (agreement_section);
	g_return_val_if_fail (AS_IS_AGREEMENT_SECTION (agreement_section), NULL);
	GNode *n = as_node_insert (parent, "agreement_section", NULL,
				   AS_NODE_INSERT_FLAG_NONE,
				   NULL);
	if (priv->kind != NULL)
		as_node_add_attribute (n, "type", priv->kind);
	as_node_insert_localized (n, "name",
				  priv->names,
				  AS_NODE_INSERT_FLAG_DEDUPE_LANG);
	as_node_insert_localized (n, "description",
				  priv->descriptions,
				  AS_NODE_INSERT_FLAG_PRE_ESCAPED |
				  AS_NODE_INSERT_FLAG_DEDUPE_LANG);

	return n;
}

static void
as_agreement_section_copy_dict (GHashTable *dest, GHashTable *src)
{
	g_autoptr(GList) keys = g_hash_table_get_keys (src);
	for (GList *l = keys; l != NULL; l = l->next) {
		AsRefString *key = l->data;
		AsRefString *value = g_hash_table_lookup (src, key);
		g_hash_table_insert (dest, as_ref_string_ref (key), as_ref_string_ref (value));
	}
}

/**
 * as_agreement_section_node_parse:
 * @agreement_section: a #AsAgreementSection 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_section_node_parse (AsAgreementSection *agreement_section, GNode *n,
				 AsNodeContext *ctx, GError **error)
{
	AsAgreementSectionPrivate *priv = GET_PRIVATE (agreement_section);
	const gchar *tmp;
	AsRefString *str;

	/* get ID */
	tmp = as_node_get_attribute (n, "type");
	if (tmp != NULL)
		as_agreement_section_set_kind (agreement_section, tmp);

	/* get sections and details */
	for (GNode *c = n->children; c != NULL; c = c->next) {
		if (as_node_get_tag (c) == AS_TAG_NAME) {
			g_autoptr(AsRefString) xml_lang = NULL;
			xml_lang = as_node_fix_locale_full (n, as_node_get_attribute (n, "xml:lang"));
			if (xml_lang == NULL)
				break;
			str = as_node_get_data_as_refstr (n);
			if (str != NULL) {
				g_hash_table_insert (priv->names,
						     as_ref_string_ref (xml_lang),
						     as_ref_string_ref (str));
			}
			continue;
		}
		if (as_node_get_tag (c) == AS_TAG_DESCRIPTION) {
			g_autoptr(GHashTable) desc = NULL;
			desc = as_node_get_localized_unwrap (c, error);
			if (desc == NULL)
				return FALSE;
			as_agreement_section_copy_dict (priv->descriptions, desc);
			continue;
		}
	}
	return TRUE;
}

/**
 * as_agreement_section_new:
 *
 * Creates a new #AsAgreementSection.
 *
 * Returns: (transfer full): a #AsAgreementSection
 *
 * Since: 0.7.8
 **/
AsAgreementSection *
as_agreement_section_new (void)
{
	AsAgreementSection *agreement_section;
	agreement_section = g_object_new (AS_TYPE_AGREEMENT_SECTION, NULL);
	return AS_AGREEMENT_SECTION (agreement_section);
}