summaryrefslogtreecommitdiff
path: root/gdata/georss/gdata-georss-where.c
blob: 8673c2610a0e11d59bdd95fe8ec254bcaa882ce9 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * GData Client
 * Copyright (C) Philip Withnall 2009 <philip@tecnocode.co.uk>
 * Copyright (C) Richard Schwarting 2009 <aquarichy@gmail.com>
 *
 * 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-georss-where
 * @short_description: GeoRSS where element
 * @stability: Stable
 * @include: gdata/georss/gdata-georss-where.h
 *
 * #GDataGeoRSSWhere represents a "where" element from the
 * <ulink type="http" url="http://www.georss.org/georss">GeoRSS specification</ulink>.
 * with PicasaWeb usage defined at
 * <ulink type="http" url="http://code.google.com/apis/picasaweb/docs/2.0/reference.html#georss_reference">PicasaWeb API reference</ulink>.
 *
 * It is private API, since implementing classes are likely to proxy the properties and functions
 * of #GDataGeoRSSWhere as appropriate; most entry types which implement #GDataGeoRSSWhere have no use
 * for most of its properties, and it would be unnecessary and confusing to expose #GDataGeoRSSWhere itself.
 *
 * Since: 0.5.0
 */

#include <glib.h>
#include <libxml/parser.h>
#include <string.h>

#include "gdata-georss-where.h"
#include "gdata-parsable.h"
#include "gdata-parser.h"
#include "gdata-private.h"

static gboolean parse_xml (GDataParsable *parsable, xmlDoc *doc, xmlNode *node, gpointer user_data, GError **error);
static void get_namespaces (GDataParsable *parsable, GHashTable *namespaces);
static void get_xml (GDataParsable *parsable, GString *xml_string);

struct _GDataGeoRSSWherePrivate {
	gdouble latitude;
	gdouble longitude;
};

G_DEFINE_TYPE (GDataGeoRSSWhere, gdata_georss_where, GDATA_TYPE_PARSABLE)

static void
gdata_georss_where_class_init (GDataGeoRSSWhereClass *klass)
{
	GDataParsableClass *parsable_class = GDATA_PARSABLE_CLASS (klass);

	g_type_class_add_private (klass, sizeof (GDataGeoRSSWherePrivate));

	parsable_class->get_xml = get_xml;
	parsable_class->parse_xml = parse_xml;
	parsable_class->get_namespaces = get_namespaces;
	parsable_class->element_name = "where";
	parsable_class->element_namespace = "georss";
}

static void
gdata_georss_where_init (GDataGeoRSSWhere *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GDATA_TYPE_GEORSS_WHERE, GDataGeoRSSWherePrivate);

	self->priv->latitude = G_MAXDOUBLE;
	self->priv->longitude = G_MAXDOUBLE;
}

static gboolean
parse_xml (GDataParsable *parsable, xmlDoc *doc, xmlNode *node, gpointer user_data, GError **error)
{
	GDataGeoRSSWhere *self = GDATA_GEORSS_WHERE (parsable);

	if (gdata_parser_is_namespace (node, "http://www.opengis.net/gml") == TRUE &&
	    xmlStrcmp (node->name, (xmlChar*) "Point") == 0) {
		/* gml:Point */
		gboolean found_pos = FALSE;
		xmlNode *child;

		for (child = node->children; child != NULL; child = child->next) {
			if (xmlStrcmp (child->name, (xmlChar*) "pos") == 0) {
				xmlChar *pos = xmlNodeListGetString (doc, child->children, TRUE);
				gchar *endptr;

				self->priv->latitude = g_ascii_strtod ((gchar*) pos, &endptr);
				self->priv->longitude = g_ascii_strtod (endptr, NULL);

				xmlFree (pos);
				found_pos = TRUE;
			} else {
				/* TODO: this logic copied from gdata-parsable.c.  Re-evaluate this at some point in the future.
				   If GeoRSS and GML support were to be used more widely, it might due to implement GML objects. */
				xmlBuffer *buffer;

				/* Unhandled XML */
				buffer = xmlBufferCreate ();
				xmlNodeDump (buffer, doc, child, 0, 0);
				g_debug ("Unhandled XML in <gml:Point>: %s", (gchar*) xmlBufferContent (buffer));
				xmlBufferFree (buffer);
			}
		}

		if (found_pos == FALSE)
			return gdata_parser_error_required_element_missing ("pos", "gml:Point", error);
		return TRUE;
	}

	return GDATA_PARSABLE_CLASS (gdata_georss_where_parent_class)->parse_xml (parsable, doc, node, user_data, error);
}

static void
get_xml (GDataParsable *parsable, GString *xml_string)
{
	GDataGeoRSSWherePrivate *priv = GDATA_GEORSS_WHERE (parsable)->priv;
	gchar latitude_str[G_ASCII_DTOSTR_BUF_SIZE];
	gchar longitude_str[G_ASCII_DTOSTR_BUF_SIZE];

	if (priv->latitude != G_MAXDOUBLE && priv->longitude != G_MAXDOUBLE) {
		g_string_append_printf (xml_string, "<gml:Point><gml:pos>%s %s</gml:pos></gml:Point>",
		                        g_ascii_dtostr (latitude_str, sizeof (latitude_str), priv->latitude),
		                        g_ascii_dtostr (longitude_str, sizeof (longitude_str), priv->longitude));
	}
}

static void
get_namespaces (GDataParsable *parsable, GHashTable *namespaces)
{
	g_hash_table_insert (namespaces, (gchar*) "georss", (gchar*) "http://www.georss.org/georss");
	g_hash_table_insert (namespaces, (gchar*) "gml", (gchar*) "http://www.opengis.net/gml");
}

/**
 * gdata_georss_where_get_latitude:
 * @self: a #GDataGeoRSSWhere
 *
 * Gets the #GDataGeoRSSWhere:latitude property.
 *
 * Return value: the latitude of this position, or %G_MAXDOUBLE if unknown
 *
 * Since: 0.5.0
 */
gdouble
gdata_georss_where_get_latitude (GDataGeoRSSWhere *self)
{
	g_return_val_if_fail (GDATA_IS_GEORSS_WHERE (self), G_MAXDOUBLE);
	return self->priv->latitude;
}

/**
 * gdata_georss_where_get_longitude:
 * @self: a #GDataGeoRSSWhere
 *
 * Gets the #GDataGeoRSSWhere:longitude property.
 *
 * Return value: the longitude of this position, or %G_MAXDOUBLE if unknown
 *
 * Since: 0.5.0
 */
gdouble
gdata_georss_where_get_longitude (GDataGeoRSSWhere *self)
{
	g_return_val_if_fail (GDATA_IS_GEORSS_WHERE (self), G_MAXDOUBLE);
	return self->priv->longitude;
}

/**
 * gdata_georss_where_set_latitude:
 * @self: a #GDataGeoRSSWhere
 * @latitude: the new latitude coordinate, or %G_MAXDOUBLE
 *
 * Sets the #GDataGeoRSSWhere:latitude property to @latitude.
 *
 * Valid values range from <code class="literal">-90.0</code> to <code class="literal">90.0</code> inclusive.
 * Set @latitude to %G_MAXDOUBLE to unset it.
 *
 * Since: 0.5.0
 */
void
gdata_georss_where_set_latitude (GDataGeoRSSWhere *self, gdouble latitude)
{
	g_return_if_fail (GDATA_IS_GEORSS_WHERE (self));

	if (latitude < -90.0 || latitude > 90.0)
		self->priv->latitude = G_MAXDOUBLE;
	else
		self->priv->latitude = latitude;
}

/**
 * gdata_georss_where_set_longitude:
 * @self: a #GDataGeoRSSWhere
 * @longitude: the new longitude coordinate, or %G_MAXDOUBLE
 *
 * Sets the #GDataGeoRSSWhere:longitude property to @longitude.
 *
 * Valid values range from <code class="literal">-180.0</code> to <code class="literal">180.0</code> inclusive.
 * Set @longitude to %G_MAXDOUBLE to unset it.
 *
 * Since: 0.5.0
 */
void
gdata_georss_where_set_longitude (GDataGeoRSSWhere *self, gdouble longitude)
{
	g_return_if_fail (GDATA_IS_GEORSS_WHERE (self));

	if (longitude < -180.0 || longitude > 180.0)
		self->priv->longitude = G_MAXDOUBLE;
	else
		self->priv->longitude = longitude;
}