summaryrefslogtreecommitdiff
path: root/gdata/gdata-batch-feed.c
blob: d29c41d50b71b6062e13159368c81fd08d6393e8 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * GData Client
 * Copyright (C) Philip Withnall 2010 <philip@tecnocode.co.uk>
 *
 * GData Client is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * GData Client 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with GData Client.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * SECTION:gdata-batch-feed
 * @short_description: GData batch feed helper object
 * @stability: Unstable
 * @include: gdata/gdata-batch-feed.h
 *
 * Helper class to parse the feed returned from a batch operation and instantiate different types of #GDataEntry according to the batch operation
 * associated with each one. It's tightly coupled with #GDataBatchOperation, and isn't exposed publicly.
 *
 * For more information, see the <ulink type="http" url="http://code.google.com/apis/gdata/docs/batch.html">online documentation</ulink>.
 *
 * Since: 0.7.0
 */

#include <glib.h>
#include <gxml.h>

#include "gdata-batch-feed.h"
#include "gdata-private.h"
#include "gdata-batch-private.h"

static gboolean parse_xml (GDataParsable *parsable, GXmlDomDocument *doc, GXmlDomXNode *root_node, gpointer user_data, GError **error);

G_DEFINE_TYPE (GDataBatchFeed, gdata_batch_feed, GDATA_TYPE_FEED)

static void
gdata_batch_feed_class_init (GDataBatchFeedClass *klass)
{
	GDataParsableClass *parsable_class = GDATA_PARSABLE_CLASS (klass);
	parsable_class->parse_xml = parse_xml;
}

static void
gdata_batch_feed_init (GDataBatchFeed *self)
{
	/* Nothing to see here */
}

static gboolean
parse_xml (GDataParsable *parsable, GXmlDomDocument *doc, GXmlDomXNode *node, gpointer user_data, GError **error)
{
	GDataBatchOperation *operation = GDATA_BATCH_OPERATION (user_data);

	if (g_strcmp0 (gxml_dom_xnode_get_node_name (node), "entry") == 0) {
		GDataEntry *entry = NULL;
		gchar *status_response = g_strdup ("");
		gchar *status_response_new = NULL;
		gchar *status_reason = NULL;
		guint id = 0, status_code = 0;
		GXmlDomXNode *entry_node;
		BatchOperation *op;
		const gchar *entry_node_name;

		/* Parse the child nodes of the <entry> to get the batch namespace elements containing information about this operation */
		for (entry_node = gxml_dom_xnode_get_first_child (node); entry_node != NULL; entry_node = gxml_dom_xnode_get_next_sibling (entry_node)) {
			/* We have to be careful about namespaces here, and we can skip text nodes (since none of the nodes we're looking for
			 * are text nodes) */
			if (gxml_dom_xnode_get_node_type (entry_node) == GXML_DOM_NODE_TYPE_TEXT ||  // < TODO:GXML: consider using GXML_DOM_IS_TEXT, but then subclasses (CDATASection) would match? want that?
			    gdata_parser_is_namespace (entry_node, "http://schemas.google.com/gdata/batch") == FALSE)
				continue;

			entry_node_name = gxml_dom_xnode_get_node_name (entry_node);

			if (g_strcmp0 (entry_node_name, "id") == 0) {
				/* batch:id */
				gchar *id_string = gxml_dom_node_list_to_string (gxml_dom_xnode_get_child_nodes (entry_node), TRUE);
				id = strtoul ((char*) id_string, NULL, 10);
				g_free (id_string); // TODO:GXML: do we want to be freeing this?
			} else if (g_strcmp0 (entry_node_name, "status") == 0) {
				/* batch:status */
				gchar *status_code_string;
				GXmlDomXNode *child_node;

				status_code_string = gxml_dom_element_get_attribute (GXML_DOM_ELEMENT (entry_node), "code");
				status_code = strtoul ((char*) status_code_string, NULL, 10);
				g_free (status_code_string); // TODO:GXML: do we want to free this?

				status_reason = gxml_dom_element_get_attribute (GXML_DOM_ELEMENT (entry_node), "reason");

				/* Dump the content of the status node, since it's service-specific, and could be anything from plain text to XML */

				for (child_node = gxml_dom_xnode_get_first_child (entry_node); child_node != NULL; child_node = gxml_dom_xnode_get_next_sibling (child_node)) {
					status_response_new = g_strconcat (status_response, gxml_dom_xnode_to_string (child_node, 0, 0), NULL); // TODO:GXML: want to use a string building thing like gstring
					g_free (status_response);
					status_response = status_response_new;
				}
			}

			if (id != 0 && status_code != 0)
				break;
		}

		/* Check we've got all the required data */
		if (id == 0) {
			gdata_parser_error_required_element_missing ("batch:id", "entry", error);
			goto error;
		} else if (status_code == 0) {
			gdata_parser_error_required_element_missing ("batch:status", "entry", error);
			goto error;
		}

		op = _gdata_batch_operation_get_operation (operation, id);

		/* Check for errors */
		if (SOUP_STATUS_IS_SUCCESSFUL (status_code) == FALSE) {
			/* Handle the error */
			GDataService *service = gdata_batch_operation_get_service (operation);
			GDataServiceClass *klass = GDATA_SERVICE_GET_CLASS (service);
			GError *child_error = NULL;

			/* Parse the error (it's returned in a service-specific format */
			g_assert (klass->parse_error_response != NULL);
			klass->parse_error_response (service, op->type, status_code, status_reason, status_response,
			                             strlen (status_response), &child_error);

			/* Run the operation's callback. This takes ownership of @child_error. */
			_gdata_batch_operation_run_callback (operation, op, NULL, child_error);

			g_free (status_reason);
			g_free (status_response);

			/* We return TRUE because we parsed the XML successfully; despite it being an error that we parsed */
			return TRUE;
		}

		/* If there wasn't an error, parse the resulting GDataEntry and run the operation's callback */
		if (op->type == GDATA_BATCH_OPERATION_QUERY)
			entry = GDATA_ENTRY (_gdata_parsable_new_from_xml_node (op->entry_type, doc, node, NULL, error));
		else if (op->type != GDATA_BATCH_OPERATION_DELETION)
			entry = GDATA_ENTRY (_gdata_parsable_new_from_xml_node (G_OBJECT_TYPE (op->entry), doc, node, NULL, error));

		if (op->type != GDATA_BATCH_OPERATION_DELETION && entry == NULL)
			goto error;

		_gdata_batch_operation_run_callback (operation, op, entry, NULL);

		if (entry != NULL)
			g_object_unref (entry);

		g_free (status_reason);
		g_free (status_response);

		return TRUE;

error:
		g_free (status_reason);
		g_free (status_response);

		return FALSE;
	} else if (GDATA_PARSABLE_CLASS (gdata_batch_feed_parent_class)->parse_xml (parsable, doc, node, user_data, error) == FALSE) {
		/* Error! */
		return FALSE;
	}

	return TRUE;
}