2 * Copyright (C) 2007 Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
3 * Copyright (C) 2007 OpenedHand Ltd
5 * Authors: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
6 * Jorn Baayen <jorn@openedhand.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 * SECTION:gupnp-last-change-parser
26 * @short_description: A/V LastChange event XML parser
28 * #GUPnPLastChangeParser parses XML strings from LastChange events that are
29 * generated by AVTransport and RenderingControl services.
34 #include <gobject/gvaluecollector.h>
36 #include "gupnp-last-change-parser.h"
37 #include "gvalue-util.h"
40 G_DEFINE_TYPE (GUPnPLastChangeParser,
41 gupnp_last_change_parser,
45 gupnp_last_change_parser_init (GUPnPLastChangeParser *parser)
50 gupnp_last_change_parser_dispose (GObject *object)
52 GObjectClass *gobject_class;
54 gobject_class = G_OBJECT_CLASS (gupnp_last_change_parser_parent_class);
55 gobject_class->dispose (object);
59 gupnp_last_change_parser_class_init (GUPnPLastChangeParserClass *klass)
61 GObjectClass *object_class;
63 object_class = G_OBJECT_CLASS (klass);
65 object_class->dispose = gupnp_last_change_parser_dispose;
68 /* Reads a value of state variable @variable_name to an initialised GValue pair
69 * from the InstanceID node of a LastChange xml doc */
71 read_state_variable (const char *variable_name,
73 xmlNode *instance_node)
75 xmlNode *variable_node;
78 variable_node = xml_util_get_element (instance_node,
84 val_str = xml_util_get_attribute_content (variable_node, "val");
86 g_warning ("No value provided for variable \"%s\" in "
93 gvalue_util_set_value_from_string (value, val_str);
99 get_instance_node (xmlDoc *doc,
104 if (doc->children == NULL)
107 for (node = doc->children->children;
110 if (node->type != XML_ELEMENT_NODE)
113 if (!xmlStrcmp (node->name, BAD_CAST ("InstanceID")) &&
114 xml_util_get_uint_attribute (node, "val", 0) == instance_id)
122 * gupnp_last_change_parser_new:
124 * Return value: A new #GUPnPLastChangeParser
126 GUPnPLastChangeParser *
127 gupnp_last_change_parser_new (void)
129 return g_object_new (GUPNP_TYPE_LAST_CHANGE_PARSER,
134 * gupnp_last_change_parser_parse_last_change_valist:
135 * @parser: A #GUPnPLastChangeParser
136 * @instance_id: The ID of the AV instance caller is interested in
137 * @last_change_xml: The xml from the "LastChange" event to parse
138 * @error: The location where to store any error, or NULL
139 * @var_args: A va_list of tuples of state variable name, state variable type,
140 * and state variable value location, terminated with NULL. The state variable
141 * values should be freed after use
143 * See gupnp_last_change_parser_parse_last_change(); this version takes a
144 * va_list for use by language bindings.
146 * Return value: TRUE on success.
149 gupnp_last_change_parser_parse_last_change_valist
150 (GUPnPLastChangeParser *parser,
152 const char *last_change_xml,
156 const char *variable_name;
158 xmlNode *instance_node;
160 g_return_val_if_fail (last_change_xml, FALSE);
162 doc = xmlParseDoc ((const xmlChar *) last_change_xml);
166 GUPNP_XML_ERROR_PARSE,
167 "Could not parse LastChange xml");
172 instance_node = get_instance_node (doc, instance_id);
173 if (instance_node == NULL) {
174 /* This is not an error since the caller of this function
175 * doesn't (need to) know if the instance of his interest is
176 * part of the LastChange event received.
184 variable_name = va_arg (var_args, const char *);
185 while (variable_name) {
187 GValue value = { 0, };
188 char *copy_error = NULL;
190 variable_type = va_arg (var_args, GType);
192 g_value_init (&value, variable_type);
194 if (read_state_variable (variable_name,
197 G_VALUE_LCOPY (&value, var_args, 0, ©_error);
199 va_arg (var_args, gpointer);
202 g_value_unset (&value);
205 g_warning ("Error copying value: %s", copy_error);
210 variable_name = va_arg (var_args, const char *);
220 * gupnp_last_change_parser_parse_last_change:
221 * @parser: A #GUPnPLastChangeParser
222 * @instance_id: The ID of the AV instance caller is interested in
223 * @last_change_xml: The xml from the "LastChange" event to parse
224 * @error: The location where to store any error, or NULL
225 * @Varargs: tuples of state variable name, state variable type, and state
226 * variable value location, terminated with NULL. The state variable values
227 * should be freed after use.
229 * Parses the xml fragment from a LastChange event.
231 * Return value: TRUE on success.
234 gupnp_last_change_parser_parse_last_change
235 (GUPnPLastChangeParser *parser,
237 const char *last_change_xml,
244 va_start (var_args, error);
245 ret = gupnp_last_change_parser_parse_last_change_valist