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

/**
 * SECTION:as-translation
 * @short_description: Object representing a single translation.
 * @include: appstream-glib.h
 * @stability: Stable
 *
 * Translation systems such as gettext install the translated files in a
 * specific location.
 *
 * This object represents translation data for an application.
 *
 * See also: #AsApp
 */

#include "config.h"

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

typedef struct
{
	AsTranslationKind	 kind;
	AsRefString		*id;
} AsTranslationPrivate;

G_DEFINE_TYPE_WITH_PRIVATE (AsTranslation, as_translation, G_TYPE_OBJECT)

#define GET_PRIVATE(o) (as_translation_get_instance_private (o))

static void
as_translation_finalize (GObject *object)
{
	AsTranslation *translation = AS_TRANSLATION (object);
	AsTranslationPrivate *priv = GET_PRIVATE (translation);

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

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

static void
as_translation_init (AsTranslation *translation)
{
}

static void
as_translation_class_init (AsTranslationClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = as_translation_finalize;
}


/**
 * as_translation_kind_from_string:
 * @kind: the string.
 *
 * Converts the text representation to an enumerated value.
 *
 * Returns: (transfer full): a #AsTranslationKind, or %AS_TRANSLATION_KIND_UNKNOWN for unknown.
 *
 * Since: 0.5.8
 **/
AsTranslationKind
as_translation_kind_from_string (const gchar *kind)
{
	if (g_strcmp0 (kind, "gettext") == 0)
		return AS_TRANSLATION_KIND_GETTEXT;
	if (g_strcmp0 (kind, "qt") == 0)
		return AS_TRANSLATION_KIND_QT;
	return AS_TRANSLATION_KIND_UNKNOWN;
}

/**
 * as_translation_kind_to_string:
 * @kind: the #AsTranslationKind.
 *
 * Converts the enumerated value to an text representation.
 *
 * Returns: string version of @kind
 *
 * Since: 0.5.8
 **/
const gchar *
as_translation_kind_to_string (AsTranslationKind kind)
{
	if (kind == AS_TRANSLATION_KIND_GETTEXT)
		return "gettext";
	if (kind == AS_TRANSLATION_KIND_QT)
		return "qt";
	return NULL;
}

/**
 * as_translation_get_id:
 * @translation: a #AsTranslation instance.
 *
 * Gets the ID for this translation.
 *
 * Returns: ID, e.g. "foobar-1.0.2"
 *
 * Since: 0.5.8
 **/
const gchar *
as_translation_get_id (AsTranslation *translation)
{
	AsTranslationPrivate *priv = GET_PRIVATE (translation);
	g_return_val_if_fail (AS_IS_TRANSLATION (translation), NULL);
	return priv->id;
}

/**
 * as_translation_get_kind:
 * @translation: a #AsTranslation instance.
 *
 * Gets the translation kind.
 *
 * Returns: the #AsTranslationKind
 *
 * Since: 0.5.8
 **/
AsTranslationKind
as_translation_get_kind (AsTranslation *translation)
{
	AsTranslationPrivate *priv = GET_PRIVATE (translation);
	g_return_val_if_fail (AS_IS_TRANSLATION (translation), AS_TRANSLATION_KIND_UNKNOWN);
	return priv->kind;
}

/**
 * as_translation_set_id:
 * @translation: a #AsTranslation instance.
 * @id: the URL.
 *
 * Sets the ID for this translation.
 *
 * Since: 0.5.8
 **/
void
as_translation_set_id (AsTranslation *translation, const gchar *id)
{
	AsTranslationPrivate *priv = GET_PRIVATE (translation);
	g_return_if_fail (AS_IS_TRANSLATION (translation));
	as_ref_string_assign_safe (&priv->id, id);
}

/**
 * as_translation_set_kind:
 * @translation: a #AsTranslation instance.
 * @kind: the #AsTranslationKind, e.g. %AS_TRANSLATION_KIND_THUMBNAIL.
 *
 * Sets the translation kind.
 *
 * Since: 0.5.8
 **/
void
as_translation_set_kind (AsTranslation *translation, AsTranslationKind kind)
{
	AsTranslationPrivate *priv = GET_PRIVATE (translation);
	g_return_if_fail (AS_IS_TRANSLATION (translation));
	priv->kind = kind;
}

/**
 * as_translation_node_insert: (skip)
 * @translation: a #AsTranslation instance.
 * @parent: the parent #GNode to use..
 * @ctx: the #AsNodeContext
 *
 * Inserts the translation into the DOM tree.
 *
 * Returns: (transfer none): A populated #GNode
 *
 * Since: 0.5.8
 **/
GNode *
as_translation_node_insert (AsTranslation *translation, GNode *parent, AsNodeContext *ctx)
{
	AsTranslationPrivate *priv = GET_PRIVATE (translation);
	GNode *n;

	g_return_val_if_fail (AS_IS_TRANSLATION (translation), NULL);

	/* invalid */
	if (priv->kind == AS_TRANSLATION_KIND_UNKNOWN)
		return NULL;

	n = as_node_insert (parent, "translation", priv->id,
			    AS_NODE_INSERT_FLAG_NONE,
			    "type", as_translation_kind_to_string (priv->kind),
			    NULL);
	return n;
}

/**
 * as_translation_node_parse:
 * @translation: a #AsTranslation 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.5.8
 **/
gboolean
as_translation_node_parse (AsTranslation *translation, GNode *node,
			   AsNodeContext *ctx, GError **error)
{
	AsTranslationPrivate *priv = GET_PRIVATE (translation);
	const gchar *tmp;

	g_return_val_if_fail (AS_IS_TRANSLATION (translation), FALSE);

	tmp = as_node_get_attribute (node, "type");
	as_translation_set_kind (translation, as_translation_kind_from_string (tmp));
	as_ref_string_assign (&priv->id, as_node_get_data_as_refstr (node));
	return TRUE;
}

/**
 * as_translation_node_parse_dep11:
 * @translation: a #AsTranslation 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.5.8
 **/
gboolean
as_translation_node_parse_dep11 (AsTranslation *translation, GNode *node,
				 AsNodeContext *ctx, GError **error)
{
	GNode *n;
	const gchar *tmp;

	g_return_val_if_fail (AS_IS_TRANSLATION (translation), FALSE);

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

/**
 * as_translation_new:
 *
 * Creates a new #AsTranslation.
 *
 * Returns: (transfer full): a #AsTranslation
 *
 * Since: 0.5.8
 **/
AsTranslation *
as_translation_new (void)
{
	AsTranslation *translation;
	translation = g_object_new (AS_TYPE_TRANSLATION, NULL);
	return AS_TRANSLATION (translation);
}