2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 * Copyright (C) 2009, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <glib/gstdio.h>
33 #include <glib/gprintf.h>
34 #include <glib/gi18n.h>
38 #include "vikgotoxmltool.h"
41 static void _goto_xml_tool_class_init ( VikGotoXmlToolClass *klass );
42 static void _goto_xml_tool_init ( VikGotoXmlTool *self );
44 static void _goto_xml_tool_finalize ( GObject *gob );
46 static gchar *_goto_xml_tool_get_url_format ( VikGotoTool *self );
47 static gboolean _goto_xml_tool_parse_file_for_latlon(VikGotoTool *self, gchar *filename, struct LatLon *ll);
49 typedef struct _VikGotoXmlToolPrivate VikGotoXmlToolPrivate;
51 struct _VikGotoXmlToolPrivate
62 #define GOTO_XML_TOOL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
63 VIK_GOTO_XML_TOOL_TYPE, \
64 VikGotoXmlToolPrivate))
66 GType vik_goto_xml_tool_get_type()
68 static GType w_type = 0;
72 static const GTypeInfo w_info =
74 sizeof (VikGotoXmlToolClass),
76 NULL, /* base_finalize */
77 (GClassInitFunc) _goto_xml_tool_class_init,
78 NULL, /* class_finalize */
79 NULL, /* class_data */
80 sizeof (VikGotoXmlTool),
82 (GInstanceInitFunc) _goto_xml_tool_init,
84 w_type = g_type_register_static ( VIK_GOTO_TOOL_TYPE, "VikGotoXmlTool", &w_info, 0 );
102 _goto_xml_tool_set_property (GObject *object,
107 VikGotoXmlTool *self = VIK_GOTO_XML_TOOL (object);
108 VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self);
109 gchar **splitted = NULL;
113 case PROP_URL_FORMAT:
114 g_free (priv->url_format);
115 priv->url_format = g_value_dup_string (value);
119 splitted = g_strsplit (g_value_get_string (value), "@", 2);
120 g_free (priv->lat_path);
121 priv->lat_path = splitted[0];
124 g_object_set (object, "lat-attr", splitted[1], NULL);
125 g_free (splitted[1]);
127 /* only free the tab, not the strings */
133 /* Avoid to overwrite XPATH value */
134 /* NB: This disable future overwriting,
135 but as property is CONSTRUCT_ONLY there is no matter */
136 if (!priv->lat_attr || g_value_get_string (value))
138 g_free (priv->lat_attr);
139 priv->lat_attr = g_value_dup_string (value);
144 splitted = g_strsplit (g_value_get_string (value), "@", 2);
145 g_free (priv->lon_path);
146 priv->lon_path = splitted[0];
149 g_object_set (object, "lon-attr", splitted[1], NULL);
150 g_free (splitted[1]);
152 /* only free the tab, not the strings */
158 /* Avoid to overwrite XPATH value */
159 /* NB: This disable future overwriting,
160 but as property is CONSTRUCT_ONLY there is no matter */
161 if (!priv->lon_attr || g_value_get_string (value))
163 g_free (priv->lon_attr);
164 priv->lon_attr = g_value_dup_string (value);
169 /* We don't have any other property... */
170 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
176 _goto_xml_tool_get_property (GObject *object,
181 VikGotoXmlTool *self = VIK_GOTO_XML_TOOL (object);
182 VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self);
186 case PROP_URL_FORMAT:
187 g_value_set_string (value, priv->url_format);
191 g_value_set_string (value, priv->lat_path);
195 g_value_set_string (value, priv->lat_attr);
199 g_value_set_string (value, priv->lon_path);
203 g_value_set_string (value, priv->lon_attr);
207 /* We don't have any other property... */
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
213 static void _goto_xml_tool_class_init ( VikGotoXmlToolClass *klass )
215 GObjectClass *object_class;
216 VikGotoToolClass *parent_class;
219 object_class = G_OBJECT_CLASS (klass);
221 object_class->finalize = _goto_xml_tool_finalize;
222 object_class->set_property = _goto_xml_tool_set_property;
223 object_class->get_property = _goto_xml_tool_get_property;
226 pspec = g_param_spec_string ("url-format",
228 "The format of the URL",
229 "<no-set>" /* default value */,
230 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
231 g_object_class_install_property (object_class,
235 pspec = g_param_spec_string ("lat-path",
237 "XPath of the latitude",
238 "<no-set>" /* default value */,
239 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
240 g_object_class_install_property (object_class,
244 pspec = g_param_spec_string ("lat-attr",
245 "Latitude attribute",
246 "XML attribute of the latitude",
247 NULL /* default value */,
248 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
249 g_object_class_install_property (object_class,
253 pspec = g_param_spec_string ("lon-path",
255 "XPath of the longitude",
256 "<no-set>" /* default value */,
257 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
258 g_object_class_install_property (object_class,
262 pspec = g_param_spec_string ("lon-attr",
263 "Longitude attribute",
264 "XML attribute of the longitude",
265 NULL /* default value */,
266 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
267 g_object_class_install_property (object_class,
271 parent_class = VIK_GOTO_TOOL_CLASS (klass);
273 parent_class->get_url_format = _goto_xml_tool_get_url_format;
274 parent_class->parse_file_for_latlon = _goto_xml_tool_parse_file_for_latlon;
276 g_type_class_add_private (klass, sizeof (VikGotoXmlToolPrivate));
279 VikGotoXmlTool *vik_goto_xml_tool_new ()
281 return VIK_GOTO_XML_TOOL ( g_object_new ( VIK_GOTO_XML_TOOL_TYPE, "label", "Google", NULL ) );
284 static void _goto_xml_tool_init ( VikGotoXmlTool *self )
286 VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self);
287 priv->url_format = NULL;
288 priv->lat_path = NULL;
289 priv->lat_attr = NULL;
290 priv->lon_path = NULL;
291 priv->lon_attr = NULL;
297 static void _goto_xml_tool_finalize ( GObject *gob )
299 G_OBJECT_GET_CLASS(gob)->finalize(gob);
303 stack_is_path (const GSList *stack,
306 gboolean equal = TRUE;
307 int stack_len = g_list_length((GList *)stack);
310 while (equal == TRUE && i >= 0)
316 const gchar *current = g_list_nth_data((GList *)stack, i);
317 size_t len = strlen(current);
318 if (strncmp(path, current, len) != 0 )
331 /* Called for open tags <foo bar="baz"> */
333 _start_element (GMarkupParseContext *context,
334 const gchar *element_name,
335 const gchar **attribute_names,
336 const gchar **attribute_values,
340 VikGotoXmlTool *self = VIK_GOTO_XML_TOOL (user_data);
341 VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self);
342 const GSList *stack = g_markup_parse_context_get_element_stack (context);
344 if (priv->lon_attr != NULL && isnan(priv->ll.lon) && stack_is_path (stack, priv->lon_path))
347 while (attribute_names[i] != NULL)
349 if (strcmp (attribute_names[i], priv->lon_attr) == 0)
351 priv->ll.lon = g_ascii_strtod(attribute_values[i], NULL);
357 if (priv->lat_attr != NULL && isnan(priv->ll.lat) && stack_is_path (stack, priv->lat_path))
360 while (attribute_names[i] != NULL)
362 if (strcmp (attribute_names[i], priv->lat_attr) == 0)
364 priv->ll.lat = g_ascii_strtod(attribute_values[i], NULL);
371 /* Called for character data */
372 /* text is not nul-terminated */
374 _text (GMarkupParseContext *context,
380 VikGotoXmlTool *self = VIK_GOTO_XML_TOOL (user_data);
381 VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self);
382 const GSList *stack = g_markup_parse_context_get_element_stack (context);
383 gchar *textl = g_strndup(text, text_len);
384 /* Store only first result */
385 if (priv->lat_attr == NULL && isnan(priv->ll.lat) && stack_is_path (stack, priv->lat_path))
387 priv->ll.lat = g_ascii_strtod(textl, NULL);
389 if (priv->lon_attr == NULL && isnan(priv->ll.lon) && stack_is_path (stack, priv->lon_path))
391 priv->ll.lon = g_ascii_strtod(textl, NULL);
397 _goto_xml_tool_parse_file_for_latlon(VikGotoTool *self, gchar *filename, struct LatLon *ll)
399 GMarkupParser xml_parser;
400 GMarkupParseContext *xml_context = NULL;
401 GError *error = NULL;
402 VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self);
403 g_return_val_if_fail(priv != NULL, FALSE);
405 g_debug ("%s: %s@%s, %s@%s",
407 priv->lat_path, priv->lat_attr,
408 priv->lon_path, priv->lon_attr);
410 FILE *file = g_fopen (filename, "r");
412 /* TODO emit warning */
415 /* setup context parse (ie callbacks) */
416 if (priv->lat_attr != NULL || priv->lon_attr != NULL)
417 // At least one coordinate uses an attribute
418 xml_parser.start_element = &_start_element;
420 xml_parser.start_element = NULL;
421 xml_parser.end_element = NULL;
422 if (priv->lat_attr == NULL || priv->lon_attr == NULL)
423 // At least one coordinate uses a raw element
424 xml_parser.text = &_text;
426 xml_parser.text = NULL;
427 xml_parser.passthrough = NULL;
428 xml_parser.error = NULL;
430 xml_context = g_markup_parse_context_new(&xml_parser, 0, self, NULL);
438 while (xml_context &&
439 (nb = fread (buff, sizeof(gchar), BUFSIZ, file)) > 0)
441 if (!g_markup_parse_context_parse(xml_context, buff, nb, &error))
443 fprintf(stderr, "%s: parsing error: %s.\n",
444 __FUNCTION__, error->message);
445 g_markup_parse_context_free(xml_context);
448 g_clear_error (&error);
452 !g_markup_parse_context_end_parse(xml_context, &error))
453 fprintf(stderr, "%s: errors occurred while reading file: %s.\n",
454 __FUNCTION__, error->message);
455 g_clear_error (&error);
458 g_markup_parse_context_free(xml_context);
467 if (isnan(priv->ll.lat) || isnan(priv->ll.lat))
468 /* At least one coordinate not found */
475 _goto_xml_tool_get_url_format ( VikGotoTool *self )
477 VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self);
478 g_return_val_if_fail(priv != NULL, NULL);
479 return priv->url_format;