
2001-10-26 Christopher James Lahey <clahey@ximian.com> * gal/e-paned/e-hpaned.c, gal/e-paned/e-hpaned.h, gal/e-paned/e-paned.c, gal/e-paned/e-paned.h, gal/e-paned/e-vpaned.c, gal/e-paned/e-vpaned.h, gal/e-text/e-completion-match.c, gal/e-text/e-completion-match.h, gal/e-text/e-completion-test.c, gal/e-text/e-completion-view.c, gal/e-text/e-completion-view.h, gal/e-text/e-completion.c, gal/e-text/e-completion.h, gal/e-text/e-entry-test.c, gal/e-text/e-entry.c, gal/e-text/e-entry.h, gal/e-text/e-table-text-model.c, gal/e-text/e-table-text-model.h, gal/e-text/e-text-model-repos.c, gal/e-text/e-text-model-repos.h, gal/e-text/e-text-model-test.c, gal/e-text/e-text-model-uri.c, gal/e-text/e-text-model-uri.h, gal/e-text/e-text-model.c, gal/e-text/e-text-model.h, gal/e-text/e-text.c, gal/e-text/e-text.h, gal/util/e-bit-array.c, gal/util/e-bit-array.h, gal/util/e-cache.c, gal/util/e-cache.h, gal/util/e-iconv.c, gal/util/e-iconv.h, gal/util/e-sorter-array.c, gal/util/e-sorter-array.h, gal/util/e-sorter.c, gal/util/e-sorter.h, gal/util/e-text-event-processor-emacs-like.c, gal/util/e-text-event-processor-emacs-like.h, gal/util/e-text-event-processor-types.h, gal/util/e-text-event-processor.c, gal/util/e-text-event-processor.h, gal/util/e-util.c, gal/util/e-util.h, gal/util/e-xml-utils.c, gal/util/e-xml-utils.h, gal/widgets/color-group.c, gal/widgets/color-group.h, gal/widgets/color-palette.c, gal/widgets/color-palette.h, gal/widgets/e-canvas-utils.c, gal/widgets/e-canvas-utils.h, gal/widgets/e-canvas-vbox.c, gal/widgets/e-canvas-vbox.h, gal/widgets/e-canvas.c, gal/widgets/e-canvas.h, gal/widgets/e-categories-master-list-array.c, gal/widgets/e-categories-master-list-array.h, gal/widgets/e-categories-master-list-combo.c, gal/widgets/e-categories-master-list-combo.h, gal/widgets/e-categories-master-list-dialog-model.c, gal/widgets/e-categories-master-list-dialog-model.h, gal/widgets/e-categories-master-list-dialog.c, gal/widgets/e-categories-master-list-dialog.h, gal/widgets/e-categories-master-list.c, gal/widgets/e-categories-master-list.h, gal/widgets/e-categories.c, gal/widgets/e-categories.h, gal/widgets/e-colors.c, gal/widgets/e-colors.h, gal/widgets/e-cursors.c, gal/widgets/e-cursors.h, gal/widgets/e-font.c, gal/widgets/e-font.h, gal/widgets/e-gui-utils.c, gal/widgets/e-gui-utils.h, gal/widgets/e-hscrollbar.c, gal/widgets/e-hscrollbar.h, gal/widgets/e-popup-menu.c, gal/widgets/e-popup-menu.h, gal/widgets/e-printable.c, gal/widgets/e-printable.h, gal/widgets/e-reflow-model.c, gal/widgets/e-reflow-model.h, gal/widgets/e-reflow-sorted.c, gal/widgets/e-reflow-sorted.h, gal/widgets/e-reflow.c, gal/widgets/e-reflow.h, gal/widgets/e-scroll-frame.c, gal/widgets/e-scroll-frame.h, gal/widgets/e-selection-model-array.c, gal/widgets/e-selection-model-array.h, gal/widgets/e-selection-model-simple.c, gal/widgets/e-selection-model-simple.h, gal/widgets/e-selection-model.c, gal/widgets/e-selection-model.h, gal/widgets/e-unicode.c, gal/widgets/e-unicode.h, gal/widgets/e-vscrollbar.c, gal/widgets/e-vscrollbar.h, gal/widgets/gtk-combo-box.c, gal/widgets/gtk-combo-box.h, gal/widgets/gtk-combo-stack.c, gal/widgets/gtk-combo-stack.h, gal/widgets/gtk-combo-text.c, gal/widgets/gtk-combo-text.h, gal/widgets/test-color.c, gal/widgets/test-e-font.c, gal/widgets/test-e-font.h, gal/widgets/test-font-loading.c, gal/widgets/widget-color-combo.c, gal/widgets/widget-color-combo.h, gal/widgets/widget-pixmap-combo.c, gal/widgets/widget-pixmap-combo.h, src/e-table/e-table-sorted-variable.c, tests/test-define-views.c, tests/test-shortcut-bar.c, tests/test-table-1.c, tests/test-tree-1.c, tests/test-tree-2.c, tests/test-tree-3.c, tests/test-unicode.c: Changed the license announcement at the top of these files. svn path=/trunk/; revision=14158
422 lines
9.8 KiB
C
422 lines
9.8 KiB
C
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
|
|
/*
|
|
* e-xml-utils.c
|
|
* Copyright 2000, 2001, Ximian, Inc.
|
|
*
|
|
* Authors:
|
|
* Chris Lahey <clahey@ximian.com>
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Library General Public
|
|
* License, version 2, as published by the Free Software Foundation.
|
|
*
|
|
* 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
|
|
* Library General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Library General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
* 02111-1307, USA.
|
|
*/
|
|
|
|
#include <config.h>
|
|
|
|
#include "e-xml-utils.h"
|
|
|
|
#include <locale.h>
|
|
#include <math.h>
|
|
#include <string.h>
|
|
#include <gnome-xml/parser.h>
|
|
#include <gnome-xml/xmlmemory.h>
|
|
#include "gal/util/e-i18n.h"
|
|
#include "gal/util/e-util.h"
|
|
|
|
xmlNode *
|
|
e_xml_get_child_by_name (const xmlNode *parent, const xmlChar *child_name)
|
|
{
|
|
xmlNode *child;
|
|
|
|
g_return_val_if_fail (parent != NULL, NULL);
|
|
g_return_val_if_fail (child_name != NULL, NULL);
|
|
|
|
for (child = parent->xmlChildrenNode; child != NULL; child = child->next) {
|
|
if (xmlStrcmp (child->name, child_name) == 0) {
|
|
return child;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/* Returns the first child with the name child_name and the "lang"
|
|
* attribute that matches the current LC_MESSAGES, or else, the first
|
|
* child with the name child_name and no "lang" attribute.
|
|
*/
|
|
xmlNode *
|
|
e_xml_get_child_by_name_by_lang (const xmlNode *parent,
|
|
const xmlChar *child_name,
|
|
const gchar *lang)
|
|
{
|
|
xmlNode *child;
|
|
/* This is the default version of the string. */
|
|
xmlNode *C = NULL;
|
|
|
|
g_return_val_if_fail (parent != NULL, NULL);
|
|
g_return_val_if_fail (child_name != NULL, NULL);
|
|
|
|
if (lang == NULL) {
|
|
#ifdef HAVE_LC_MESSAGES
|
|
lang = setlocale (LC_MESSAGES, NULL);
|
|
#else
|
|
lang = setlocale (LC_CTYPE, NULL);
|
|
#endif
|
|
}
|
|
for (child = parent->xmlChildrenNode; child != NULL; child = child->next) {
|
|
if (xmlStrcmp (child->name, child_name) == 0) {
|
|
xmlChar *this_lang = xmlGetProp (child, "lang");
|
|
if (this_lang == NULL) {
|
|
C = child;
|
|
} else if (xmlStrcmp(this_lang, "lang") == 0) {
|
|
return child;
|
|
}
|
|
}
|
|
}
|
|
return C;
|
|
}
|
|
|
|
static xmlNode *
|
|
e_xml_get_child_by_name_by_lang_list_with_score (const xmlNode *parent,
|
|
const gchar *name,
|
|
GList *lang_list,
|
|
gint *best_lang_score)
|
|
{
|
|
xmlNodePtr best_node = NULL, node;
|
|
|
|
for (node = parent->xmlChildrenNode; node != NULL; node = node->next) {
|
|
xmlChar *lang;
|
|
|
|
if (node->name == NULL || strcmp (node->name, name) != 0) {
|
|
continue;
|
|
}
|
|
lang = xmlGetProp (node, "xml:lang");
|
|
if (lang != NULL) {
|
|
GList *l;
|
|
gint i;
|
|
|
|
for (l = lang_list, i = 0;
|
|
l != NULL && i < *best_lang_score;
|
|
l = l->next, i++) {
|
|
if (strcmp ((gchar *) l->data, lang) == 0) {
|
|
best_node = node;
|
|
*best_lang_score = i;
|
|
}
|
|
}
|
|
} else {
|
|
if (best_node == NULL) {
|
|
best_node = node;
|
|
}
|
|
}
|
|
xmlFree (lang);
|
|
if (*best_lang_score == 0) {
|
|
return best_node;
|
|
}
|
|
}
|
|
|
|
return best_node;
|
|
}
|
|
|
|
/*
|
|
* e_xml_get_child_by_name_by_lang_list:
|
|
*
|
|
*/
|
|
xmlNode *
|
|
e_xml_get_child_by_name_by_lang_list (const xmlNode *parent,
|
|
const gchar *name,
|
|
GList *lang_list)
|
|
{
|
|
gint best_lang_score = INT_MAX;
|
|
|
|
g_return_val_if_fail (parent != NULL, NULL);
|
|
g_return_val_if_fail (name != NULL, NULL);
|
|
|
|
if (lang_list == NULL) {
|
|
lang_list = gnome_i18n_get_language_list ("LC_MESSAGES");
|
|
}
|
|
return e_xml_get_child_by_name_by_lang_list_with_score
|
|
(parent,name,
|
|
lang_list,
|
|
&best_lang_score);
|
|
}
|
|
|
|
/*
|
|
* e_xml_get_child_by_name_no_lang
|
|
*
|
|
*/
|
|
xmlNode *
|
|
e_xml_get_child_by_name_no_lang (const xmlNode *parent, const gchar *name)
|
|
{
|
|
xmlNodePtr node;
|
|
|
|
g_return_val_if_fail (parent != NULL, NULL);
|
|
g_return_val_if_fail (name != NULL, NULL);
|
|
|
|
for (node = parent->xmlChildrenNode; node != NULL; node = node->next) {
|
|
xmlChar *lang;
|
|
|
|
if (node->name == NULL || strcmp (node->name, name) != 0) {
|
|
continue;
|
|
}
|
|
lang = xmlGetProp (node, "xml:lang");
|
|
if (lang == NULL) {
|
|
return node;
|
|
}
|
|
xmlFree (lang);
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
gint
|
|
e_xml_get_integer_prop_by_name (const xmlNode *parent, const xmlChar *prop_name)
|
|
{
|
|
g_return_val_if_fail (parent != NULL, 0);
|
|
g_return_val_if_fail (prop_name != NULL, 0);
|
|
|
|
return e_xml_get_integer_prop_by_name_with_default (parent, prop_name, 0);
|
|
}
|
|
|
|
gint
|
|
e_xml_get_integer_prop_by_name_with_default (const xmlNode *parent,
|
|
const xmlChar *prop_name,
|
|
gint def)
|
|
{
|
|
xmlChar *prop;
|
|
gint ret_val = def;
|
|
|
|
g_return_val_if_fail (parent != NULL, 0);
|
|
g_return_val_if_fail (prop_name != NULL, 0);
|
|
|
|
prop = xmlGetProp ((xmlNode *) parent, prop_name);
|
|
if (prop != NULL) {
|
|
(void) sscanf (prop, "%d", &ret_val);
|
|
xmlFree (prop);
|
|
}
|
|
return ret_val;
|
|
}
|
|
|
|
void
|
|
e_xml_set_integer_prop_by_name (xmlNode *parent,
|
|
const xmlChar *prop_name,
|
|
gint value)
|
|
{
|
|
gchar *valuestr;
|
|
|
|
g_return_if_fail (parent != NULL);
|
|
g_return_if_fail (prop_name != NULL);
|
|
|
|
valuestr = g_strdup_printf ("%d", value);
|
|
xmlSetProp (parent, prop_name, valuestr);
|
|
g_free (valuestr);
|
|
}
|
|
|
|
guint
|
|
e_xml_get_uint_prop_by_name (const xmlNode *parent, const xmlChar *prop_name)
|
|
{
|
|
g_return_val_if_fail (parent != NULL, 0);
|
|
g_return_val_if_fail (prop_name != NULL, 0);
|
|
|
|
return e_xml_get_uint_prop_by_name_with_default (parent, prop_name, 0);
|
|
}
|
|
|
|
guint
|
|
e_xml_get_uint_prop_by_name_with_default (const xmlNode *parent,
|
|
const xmlChar *prop_name,
|
|
guint def)
|
|
{
|
|
xmlChar *prop;
|
|
guint ret_val = def;
|
|
|
|
g_return_val_if_fail (parent != NULL, 0);
|
|
g_return_val_if_fail (prop_name != NULL, 0);
|
|
|
|
prop = xmlGetProp ((xmlNode *) parent, prop_name);
|
|
if (prop != NULL) {
|
|
(void) sscanf (prop, "%u", &ret_val);
|
|
xmlFree (prop);
|
|
}
|
|
return ret_val;
|
|
}
|
|
|
|
void
|
|
e_xml_set_uint_prop_by_name (xmlNode *parent,
|
|
const xmlChar *prop_name,
|
|
guint value)
|
|
{
|
|
gchar *valuestr;
|
|
|
|
g_return_if_fail (parent != NULL);
|
|
g_return_if_fail (prop_name != NULL);
|
|
|
|
valuestr = g_strdup_printf ("%u", value);
|
|
xmlSetProp (parent, prop_name, valuestr);
|
|
g_free (valuestr);
|
|
}
|
|
|
|
gboolean
|
|
e_xml_get_bool_prop_by_name (const xmlNode *parent,
|
|
const xmlChar *prop_name)
|
|
{
|
|
g_return_val_if_fail (parent != NULL, 0);
|
|
g_return_val_if_fail (prop_name != NULL, 0);
|
|
|
|
return e_xml_get_bool_prop_by_name_with_default (parent,
|
|
prop_name,
|
|
FALSE);
|
|
}
|
|
|
|
gboolean
|
|
e_xml_get_bool_prop_by_name_with_default(const xmlNode *parent,
|
|
const xmlChar *prop_name,
|
|
gboolean def)
|
|
{
|
|
xmlChar *prop;
|
|
gboolean ret_val = def;
|
|
|
|
g_return_val_if_fail (parent != NULL, 0);
|
|
g_return_val_if_fail (prop_name != NULL, 0);
|
|
|
|
prop = xmlGetProp ((xmlNode *) parent, prop_name);
|
|
if (prop != NULL) {
|
|
if (g_strcasecmp (prop, "true") == 0) {
|
|
ret_val = TRUE;
|
|
} else if (g_strcasecmp (prop, "false") == 0) {
|
|
ret_val = FALSE;
|
|
}
|
|
xmlFree(prop);
|
|
}
|
|
return ret_val;
|
|
}
|
|
|
|
void
|
|
e_xml_set_bool_prop_by_name (xmlNode *parent, const xmlChar *prop_name, gboolean value)
|
|
{
|
|
g_return_if_fail (parent != NULL);
|
|
g_return_if_fail (prop_name != NULL);
|
|
|
|
if (value) {
|
|
xmlSetProp (parent, prop_name, "true");
|
|
} else {
|
|
xmlSetProp (parent, prop_name, "false");
|
|
}
|
|
}
|
|
|
|
gdouble
|
|
e_xml_get_double_prop_by_name (const xmlNode *parent, const xmlChar *prop_name)
|
|
{
|
|
g_return_val_if_fail (parent != NULL, 0);
|
|
g_return_val_if_fail (prop_name != NULL, 0);
|
|
|
|
return e_xml_get_double_prop_by_name_with_default (parent, prop_name, 0.0);
|
|
}
|
|
|
|
gdouble
|
|
e_xml_get_double_prop_by_name_with_default (const xmlNode *parent, const xmlChar *prop_name, gdouble def)
|
|
{
|
|
xmlChar *prop;
|
|
gdouble ret_val = def;
|
|
|
|
g_return_val_if_fail (parent != NULL, 0);
|
|
g_return_val_if_fail (prop_name != NULL, 0);
|
|
|
|
prop = xmlGetProp ((xmlNode *) parent, prop_name);
|
|
if (prop != NULL) {
|
|
ret_val = e_flexible_strtod (prop, NULL);
|
|
xmlFree (prop);
|
|
}
|
|
return ret_val;
|
|
}
|
|
|
|
void
|
|
e_xml_set_double_prop_by_name(xmlNode *parent, const xmlChar *prop_name, gdouble value)
|
|
{
|
|
char buffer[E_ASCII_DTOSTR_BUF_SIZE];
|
|
|
|
g_return_if_fail (parent != NULL);
|
|
g_return_if_fail (prop_name != NULL);
|
|
|
|
if (fabs (value) < 1e9 && fabs (value) > 1e-5) {
|
|
e_ascii_dtostr (buffer, sizeof (buffer), "%.17f", value);
|
|
} else {
|
|
e_ascii_dtostr (buffer, sizeof (buffer), "%.17g", value);
|
|
}
|
|
xmlSetProp (parent, prop_name, buffer);
|
|
}
|
|
|
|
gchar *
|
|
e_xml_get_string_prop_by_name (const xmlNode *parent, const xmlChar *prop_name)
|
|
{
|
|
g_return_val_if_fail (parent != NULL, 0);
|
|
g_return_val_if_fail (prop_name != NULL, 0);
|
|
|
|
return e_xml_get_string_prop_by_name_with_default (parent, prop_name, NULL);
|
|
}
|
|
|
|
gchar *
|
|
e_xml_get_string_prop_by_name_with_default (const xmlNode *parent, const xmlChar *prop_name, const gchar *def)
|
|
{
|
|
xmlChar *prop;
|
|
gchar *ret_val;
|
|
|
|
g_return_val_if_fail (parent != NULL, 0);
|
|
g_return_val_if_fail (prop_name != NULL, 0);
|
|
|
|
prop = xmlGetProp ((xmlNode *) parent, prop_name);
|
|
if (prop != NULL) {
|
|
ret_val = g_strdup (prop);
|
|
xmlFree (prop);
|
|
} else {
|
|
ret_val = g_strdup (def);
|
|
}
|
|
return ret_val;
|
|
}
|
|
|
|
void
|
|
e_xml_set_string_prop_by_name (xmlNode *parent, const xmlChar *prop_name, const gchar *value)
|
|
{
|
|
g_return_if_fail (parent != NULL);
|
|
g_return_if_fail (prop_name != NULL);
|
|
|
|
if (value != NULL) {
|
|
xmlSetProp (parent, prop_name, value);
|
|
}
|
|
}
|
|
|
|
gchar *
|
|
e_xml_get_translated_string_prop_by_name (const xmlNode *parent, const xmlChar *prop_name)
|
|
{
|
|
xmlChar *prop;
|
|
gchar *ret_val = NULL;
|
|
gchar *combined_name;
|
|
|
|
g_return_val_if_fail (parent != NULL, 0);
|
|
g_return_val_if_fail (prop_name != NULL, 0);
|
|
|
|
prop = xmlGetProp ((xmlNode *) parent, prop_name);
|
|
if (prop != NULL) {
|
|
ret_val = g_strdup (prop);
|
|
xmlFree (prop);
|
|
return ret_val;
|
|
}
|
|
|
|
combined_name = g_strdup_printf("_%s", prop_name);
|
|
prop = xmlGetProp ((xmlNode *) parent, combined_name);
|
|
if (prop != NULL) {
|
|
ret_val = g_strdup (gettext(prop));
|
|
xmlFree (prop);
|
|
}
|
|
g_free(combined_name);
|
|
|
|
return ret_val;
|
|
}
|