1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2  *
3  * Copyright (C) 2016 Richard Hughes <richard@hughsie.com>
4  *
5  * SPDX-License-Identifier: LGPL-2.1+
6  */
7 
8 /**
9  * SECTION:as-translation
10  * @short_description: Object representing a single translation.
11  * @include: appstream-glib.h
12  * @stability: Stable
13  *
14  * Translation systems such as gettext install the translated files in a
15  * specific location.
16  *
17  * This object represents translation data for an application.
18  *
19  * See also: #AsApp
20  */
21 
22 #include "config.h"
23 
24 #include "as-translation-private.h"
25 #include "as-node-private.h"
26 #include "as-ref-string.h"
27 #include "as-utils-private.h"
28 #include "as-yaml.h"
29 
30 typedef struct
31 {
32 	AsTranslationKind	 kind;
33 	AsRefString		*id;
34 } AsTranslationPrivate;
35 
G_DEFINE_TYPE_WITH_PRIVATE(AsTranslation,as_translation,G_TYPE_OBJECT)36 G_DEFINE_TYPE_WITH_PRIVATE (AsTranslation, as_translation, G_TYPE_OBJECT)
37 
38 #define GET_PRIVATE(o) (as_translation_get_instance_private (o))
39 
40 static void
41 as_translation_finalize (GObject *object)
42 {
43 	AsTranslation *translation = AS_TRANSLATION (object);
44 	AsTranslationPrivate *priv = GET_PRIVATE (translation);
45 
46 	if (priv->id != NULL)
47 		as_ref_string_unref (priv->id);
48 
49 	G_OBJECT_CLASS (as_translation_parent_class)->finalize (object);
50 }
51 
52 static void
as_translation_init(AsTranslation * translation)53 as_translation_init (AsTranslation *translation)
54 {
55 }
56 
57 static void
as_translation_class_init(AsTranslationClass * klass)58 as_translation_class_init (AsTranslationClass *klass)
59 {
60 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
61 	object_class->finalize = as_translation_finalize;
62 }
63 
64 
65 /**
66  * as_translation_kind_from_string:
67  * @kind: the string.
68  *
69  * Converts the text representation to an enumerated value.
70  *
71  * Returns: (transfer full): a #AsTranslationKind, or %AS_TRANSLATION_KIND_UNKNOWN for unknown.
72  *
73  * Since: 0.5.8
74  **/
75 AsTranslationKind
as_translation_kind_from_string(const gchar * kind)76 as_translation_kind_from_string (const gchar *kind)
77 {
78 	if (g_strcmp0 (kind, "gettext") == 0)
79 		return AS_TRANSLATION_KIND_GETTEXT;
80 	if (g_strcmp0 (kind, "qt") == 0)
81 		return AS_TRANSLATION_KIND_QT;
82 	return AS_TRANSLATION_KIND_UNKNOWN;
83 }
84 
85 /**
86  * as_translation_kind_to_string:
87  * @kind: the #AsTranslationKind.
88  *
89  * Converts the enumerated value to an text representation.
90  *
91  * Returns: string version of @kind
92  *
93  * Since: 0.5.8
94  **/
95 const gchar *
as_translation_kind_to_string(AsTranslationKind kind)96 as_translation_kind_to_string (AsTranslationKind kind)
97 {
98 	if (kind == AS_TRANSLATION_KIND_GETTEXT)
99 		return "gettext";
100 	if (kind == AS_TRANSLATION_KIND_QT)
101 		return "qt";
102 	return NULL;
103 }
104 
105 /**
106  * as_translation_get_id:
107  * @translation: a #AsTranslation instance.
108  *
109  * Gets the ID for this translation.
110  *
111  * Returns: ID, e.g. "foobar-1.0.2"
112  *
113  * Since: 0.5.8
114  **/
115 const gchar *
as_translation_get_id(AsTranslation * translation)116 as_translation_get_id (AsTranslation *translation)
117 {
118 	AsTranslationPrivate *priv = GET_PRIVATE (translation);
119 	g_return_val_if_fail (AS_IS_TRANSLATION (translation), NULL);
120 	return priv->id;
121 }
122 
123 /**
124  * as_translation_get_kind:
125  * @translation: a #AsTranslation instance.
126  *
127  * Gets the translation kind.
128  *
129  * Returns: the #AsTranslationKind
130  *
131  * Since: 0.5.8
132  **/
133 AsTranslationKind
as_translation_get_kind(AsTranslation * translation)134 as_translation_get_kind (AsTranslation *translation)
135 {
136 	AsTranslationPrivate *priv = GET_PRIVATE (translation);
137 	g_return_val_if_fail (AS_IS_TRANSLATION (translation), AS_TRANSLATION_KIND_UNKNOWN);
138 	return priv->kind;
139 }
140 
141 /**
142  * as_translation_set_id:
143  * @translation: a #AsTranslation instance.
144  * @id: the URL.
145  *
146  * Sets the ID for this translation.
147  *
148  * Since: 0.5.8
149  **/
150 void
as_translation_set_id(AsTranslation * translation,const gchar * id)151 as_translation_set_id (AsTranslation *translation, const gchar *id)
152 {
153 	AsTranslationPrivate *priv = GET_PRIVATE (translation);
154 	g_return_if_fail (AS_IS_TRANSLATION (translation));
155 	as_ref_string_assign_safe (&priv->id, id);
156 }
157 
158 /**
159  * as_translation_set_kind:
160  * @translation: a #AsTranslation instance.
161  * @kind: the #AsTranslationKind, e.g. %AS_TRANSLATION_KIND_THUMBNAIL.
162  *
163  * Sets the translation kind.
164  *
165  * Since: 0.5.8
166  **/
167 void
as_translation_set_kind(AsTranslation * translation,AsTranslationKind kind)168 as_translation_set_kind (AsTranslation *translation, AsTranslationKind kind)
169 {
170 	AsTranslationPrivate *priv = GET_PRIVATE (translation);
171 	g_return_if_fail (AS_IS_TRANSLATION (translation));
172 	priv->kind = kind;
173 }
174 
175 /**
176  * as_translation_node_insert: (skip)
177  * @translation: a #AsTranslation instance.
178  * @parent: the parent #GNode to use..
179  * @ctx: the #AsNodeContext
180  *
181  * Inserts the translation into the DOM tree.
182  *
183  * Returns: (transfer none): A populated #GNode
184  *
185  * Since: 0.5.8
186  **/
187 GNode *
as_translation_node_insert(AsTranslation * translation,GNode * parent,AsNodeContext * ctx)188 as_translation_node_insert (AsTranslation *translation, GNode *parent, AsNodeContext *ctx)
189 {
190 	AsTranslationPrivate *priv = GET_PRIVATE (translation);
191 	GNode *n;
192 
193 	g_return_val_if_fail (AS_IS_TRANSLATION (translation), NULL);
194 
195 	/* invalid */
196 	if (priv->kind == AS_TRANSLATION_KIND_UNKNOWN)
197 		return NULL;
198 
199 	n = as_node_insert (parent, "translation", priv->id,
200 			    AS_NODE_INSERT_FLAG_NONE,
201 			    "type", as_translation_kind_to_string (priv->kind),
202 			    NULL);
203 	return n;
204 }
205 
206 /**
207  * as_translation_node_parse:
208  * @translation: a #AsTranslation instance.
209  * @node: a #GNode.
210  * @ctx: a #AsNodeContext.
211  * @error: A #GError or %NULL.
212  *
213  * Populates the object from a DOM node.
214  *
215  * Returns: %TRUE for success
216  *
217  * Since: 0.5.8
218  **/
219 gboolean
as_translation_node_parse(AsTranslation * translation,GNode * node,AsNodeContext * ctx,GError ** error)220 as_translation_node_parse (AsTranslation *translation, GNode *node,
221 			   AsNodeContext *ctx, GError **error)
222 {
223 	AsTranslationPrivate *priv = GET_PRIVATE (translation);
224 	const gchar *tmp;
225 
226 	g_return_val_if_fail (AS_IS_TRANSLATION (translation), FALSE);
227 
228 	tmp = as_node_get_attribute (node, "type");
229 	as_translation_set_kind (translation, as_translation_kind_from_string (tmp));
230 	as_ref_string_assign (&priv->id, as_node_get_data_as_refstr (node));
231 	return TRUE;
232 }
233 
234 /**
235  * as_translation_node_parse_dep11:
236  * @translation: a #AsTranslation instance.
237  * @node: a #GNode.
238  * @ctx: a #AsNodeContext.
239  * @error: A #GError or %NULL.
240  *
241  * Populates the object from a DEP-11 node.
242  *
243  * Returns: %TRUE for success
244  *
245  * Since: 0.5.8
246  **/
247 gboolean
as_translation_node_parse_dep11(AsTranslation * translation,GNode * node,AsNodeContext * ctx,GError ** error)248 as_translation_node_parse_dep11 (AsTranslation *translation, GNode *node,
249 				 AsNodeContext *ctx, GError **error)
250 {
251 	GNode *n;
252 	const gchar *tmp;
253 
254 	g_return_val_if_fail (AS_IS_TRANSLATION (translation), FALSE);
255 
256 	for (n = node->children; n != NULL; n = n->next) {
257 		tmp = as_yaml_node_get_key (n);
258 		if (g_strcmp0 (tmp, "id") == 0)
259 			as_translation_set_id (translation, as_yaml_node_get_value (n));
260 	}
261 	return TRUE;
262 }
263 
264 /**
265  * as_translation_new:
266  *
267  * Creates a new #AsTranslation.
268  *
269  * Returns: (transfer full): a #AsTranslation
270  *
271  * Since: 0.5.8
272  **/
273 AsTranslation *
as_translation_new(void)274 as_translation_new (void)
275 {
276 	AsTranslation *translation;
277 	translation = g_object_new (AS_TYPE_TRANSLATION, NULL);
278 	return AS_TRANSLATION (translation);
279 }
280