2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU Lesser General Public License as published by
4 * the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful, but
7 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 * Chris Lahey <clahey@ximian.com>
18 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
26 #include "e-xml-utils.h"
31 #include <sys/types.h>
39 #include <glib/gi18n.h>
40 #include <glib/gstdio.h>
41 #include <libxml/parser.h>
42 #include <libxml/xmlmemory.h>
44 #include "e-misc-utils.h"
46 /* Returns the first child with the name child_name and the "lang"
47 * attribute that matches the current LC_MESSAGES, or else, the first
48 * child with the name child_name and no "lang" attribute.
51 e_xml_get_child_by_name_by_lang (const xmlNode
*parent
,
52 const xmlChar
*child_name
,
59 /* This is the default version of the string. */
62 g_return_val_if_fail (parent
!= NULL
, NULL
);
63 g_return_val_if_fail (child_name
!= NULL
, NULL
);
67 #ifdef HAVE_LC_MESSAGES
68 lang
= setlocale (LC_MESSAGES
, NULL
);
70 lang
= setlocale (LC_CTYPE
, NULL
);
73 lang
= freeme
= g_win32_getlocale ();
76 for (child
= parent
->xmlChildrenNode
; child
!= NULL
; child
= child
->next
) {
77 if (xmlStrcmp (child
->name
, child_name
) == 0) {
78 xmlChar
*this_lang
= xmlGetProp (
79 child
, (const guchar
*)"lang");
80 if (this_lang
== NULL
) {
82 } else if (xmlStrcmp (this_lang
, (xmlChar
*) lang
) == 0) {
97 e_xml_get_child_by_name_by_lang_list_with_score (const xmlNode
*parent
,
99 const GList
*lang_list
,
100 gint
*best_lang_score
)
102 xmlNodePtr best_node
= NULL
, node
;
104 for (node
= parent
->xmlChildrenNode
; node
!= NULL
; node
= node
->next
) {
107 if (node
->name
== NULL
|| strcmp ((gchar
*) node
->name
, name
) != 0) {
110 lang
= xmlGetProp (node
, (const guchar
*)"xml:lang");
115 for (l
= lang_list
, i
= 0;
116 l
!= NULL
&& i
< *best_lang_score
;
118 if (strcmp ((gchar
*) l
->data
, (gchar
*) lang
) == 0) {
120 *best_lang_score
= i
;
124 if (best_node
== NULL
) {
129 if (*best_lang_score
== 0) {
138 e_xml_get_child_by_name_by_lang_list (const xmlNode
*parent
,
140 const GList
*lang_list
)
142 gint best_lang_score
= INT_MAX
;
144 g_return_val_if_fail (parent
!= NULL
, NULL
);
145 g_return_val_if_fail (name
!= NULL
, NULL
);
147 if (lang_list
== NULL
) {
148 const gchar
* const *language_names
;
150 language_names
= g_get_language_names ();
151 while (*language_names
!= NULL
)
152 lang_list
= g_list_append (
153 (GList
*) lang_list
, (gchar
*) * language_names
++);
155 return e_xml_get_child_by_name_by_lang_list_with_score
162 e_xml_get_child_by_name_no_lang (const xmlNode
*parent
,
167 g_return_val_if_fail (parent
!= NULL
, NULL
);
168 g_return_val_if_fail (name
!= NULL
, NULL
);
170 for (node
= parent
->xmlChildrenNode
; node
!= NULL
; node
= node
->next
) {
173 if (node
->name
== NULL
|| strcmp ((gchar
*) node
->name
, name
) != 0) {
176 lang
= xmlGetProp (node
, (const guchar
*)"xml:lang");
187 e_xml_get_integer_prop_by_name (const xmlNode
*parent
,
188 const xmlChar
*prop_name
)
190 g_return_val_if_fail (parent
!= NULL
, 0);
191 g_return_val_if_fail (prop_name
!= NULL
, 0);
193 return e_xml_get_integer_prop_by_name_with_default (parent
, prop_name
, 0);
197 e_xml_get_integer_prop_by_name_with_default (const xmlNode
*parent
,
198 const xmlChar
*prop_name
,
204 g_return_val_if_fail (parent
!= NULL
, 0);
205 g_return_val_if_fail (prop_name
!= NULL
, 0);
207 prop
= xmlGetProp ((xmlNode
*) parent
, prop_name
);
209 (void) sscanf ((gchar
*) prop
, "%d", &ret_val
);
216 e_xml_set_integer_prop_by_name (xmlNode
*parent
,
217 const xmlChar
*prop_name
,
222 g_return_if_fail (parent
!= NULL
);
223 g_return_if_fail (prop_name
!= NULL
);
225 valuestr
= g_strdup_printf ("%d", value
);
226 xmlSetProp (parent
, prop_name
, (guchar
*) valuestr
);
231 e_xml_get_uint_prop_by_name (const xmlNode
*parent
,
232 const xmlChar
*prop_name
)
234 g_return_val_if_fail (parent
!= NULL
, 0);
235 g_return_val_if_fail (prop_name
!= NULL
, 0);
237 return e_xml_get_uint_prop_by_name_with_default (parent
, prop_name
, 0);
241 e_xml_get_uint_prop_by_name_with_default (const xmlNode
*parent
,
242 const xmlChar
*prop_name
,
248 g_return_val_if_fail (parent
!= NULL
, 0);
249 g_return_val_if_fail (prop_name
!= NULL
, 0);
251 prop
= xmlGetProp ((xmlNode
*) parent
, prop_name
);
253 (void) sscanf ((gchar
*) prop
, "%u", &ret_val
);
260 e_xml_set_uint_prop_by_name (xmlNode
*parent
,
261 const xmlChar
*prop_name
,
266 g_return_if_fail (parent
!= NULL
);
267 g_return_if_fail (prop_name
!= NULL
);
269 valuestr
= g_strdup_printf ("%u", value
);
270 xmlSetProp (parent
, prop_name
, (guchar
*) valuestr
);
275 e_xml_get_bool_prop_by_name (const xmlNode
*parent
,
276 const xmlChar
*prop_name
)
278 g_return_val_if_fail (parent
!= NULL
, 0);
279 g_return_val_if_fail (prop_name
!= NULL
, 0);
281 return e_xml_get_bool_prop_by_name_with_default (
282 parent
, prop_name
, FALSE
);
286 e_xml_get_bool_prop_by_name_with_default (const xmlNode
*parent
,
287 const xmlChar
*prop_name
,
291 gboolean ret_val
= def
;
293 g_return_val_if_fail (parent
!= NULL
, 0);
294 g_return_val_if_fail (prop_name
!= NULL
, 0);
296 prop
= xmlGetProp ((xmlNode
*) parent
, prop_name
);
298 if (g_ascii_strcasecmp ((gchar
*) prop
, "true") == 0) {
300 } else if (g_ascii_strcasecmp ((gchar
*) prop
, "false") == 0) {
309 e_xml_set_bool_prop_by_name (xmlNode
*parent
,
310 const xmlChar
*prop_name
,
313 g_return_if_fail (parent
!= NULL
);
314 g_return_if_fail (prop_name
!= NULL
);
317 xmlSetProp (parent
, prop_name
, (const guchar
*)"true");
319 xmlSetProp (parent
, prop_name
, (const guchar
*)"false");
324 e_xml_get_double_prop_by_name (const xmlNode
*parent
,
325 const xmlChar
*prop_name
)
327 g_return_val_if_fail (parent
!= NULL
, 0);
328 g_return_val_if_fail (prop_name
!= NULL
, 0);
330 return e_xml_get_double_prop_by_name_with_default (parent
, prop_name
, 0.0);
334 e_xml_get_double_prop_by_name_with_default (const xmlNode
*parent
,
335 const xmlChar
*prop_name
,
339 gdouble ret_val
= def
;
341 g_return_val_if_fail (parent
!= NULL
, 0);
342 g_return_val_if_fail (prop_name
!= NULL
, 0);
344 prop
= xmlGetProp ((xmlNode
*) parent
, prop_name
);
346 ret_val
= e_flexible_strtod ((gchar
*) prop
, NULL
);
353 e_xml_set_double_prop_by_name (xmlNode
*parent
,
354 const xmlChar
*prop_name
,
357 gchar buffer
[E_ASCII_DTOSTR_BUF_SIZE
];
360 g_return_if_fail (parent
!= NULL
);
361 g_return_if_fail (prop_name
!= NULL
);
363 if (fabs (value
) < 1e9
&& fabs (value
) > 1e-5) {
364 format
= g_strdup_printf ("%%.%df", DBL_DIG
);
366 format
= g_strdup_printf ("%%.%dg", DBL_DIG
);
368 e_ascii_dtostr (buffer
, sizeof (buffer
), format
, value
);
371 xmlSetProp (parent
, prop_name
, (const guchar
*) buffer
);
375 e_xml_get_string_prop_by_name (const xmlNode
*parent
,
376 const xmlChar
*prop_name
)
378 g_return_val_if_fail (parent
!= NULL
, NULL
);
379 g_return_val_if_fail (prop_name
!= NULL
, NULL
);
381 return e_xml_get_string_prop_by_name_with_default (parent
, prop_name
, NULL
);
385 e_xml_get_string_prop_by_name_with_default (const xmlNode
*parent
,
386 const xmlChar
*prop_name
,
392 g_return_val_if_fail (parent
!= NULL
, NULL
);
393 g_return_val_if_fail (prop_name
!= NULL
, NULL
);
395 prop
= xmlGetProp ((xmlNode
*) parent
, prop_name
);
397 ret_val
= g_strdup ((gchar
*) prop
);
400 ret_val
= g_strdup (def
);
406 e_xml_set_string_prop_by_name (xmlNode
*parent
,
407 const xmlChar
*prop_name
,
410 g_return_if_fail (parent
!= NULL
);
411 g_return_if_fail (prop_name
!= NULL
);
414 xmlSetProp (parent
, prop_name
, (guchar
*) value
);
419 e_xml_get_translated_string_prop_by_name (const xmlNode
*parent
,
420 const xmlChar
*prop_name
)
423 gchar
*ret_val
= NULL
;
424 gchar
*combined_name
;
426 g_return_val_if_fail (parent
!= NULL
, NULL
);
427 g_return_val_if_fail (prop_name
!= NULL
, NULL
);
429 prop
= xmlGetProp ((xmlNode
*) parent
, prop_name
);
431 ret_val
= g_strdup ((gchar
*) prop
);
436 combined_name
= g_strdup_printf ("_%s", prop_name
);
437 prop
= xmlGetProp ((xmlNode
*) parent
, (guchar
*) combined_name
);
439 ret_val
= g_strdup (gettext ((gchar
*) prop
));
442 g_free (combined_name
);