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"
40 static void vik_goto_xml_tool_finalize ( GObject *gob );
42 static gchar *vik_goto_xml_tool_get_url_format ( VikGotoTool *self );
43 static gboolean vik_goto_xml_tool_parse_file_for_latlon(VikGotoTool *self, gchar *filename, struct LatLon *ll);
45 typedef struct _VikGotoXmlToolPrivate VikGotoXmlToolPrivate;
47 struct _VikGotoXmlToolPrivate
58 #define GOTO_XML_TOOL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
59 VIK_GOTO_XML_TOOL_TYPE, \
60 VikGotoXmlToolPrivate))
62 G_DEFINE_TYPE (VikGotoXmlTool, vik_goto_xml_tool, VIK_GOTO_TOOL_TYPE)
76 vik_goto_xml_tool_set_property (GObject *object,
81 VikGotoXmlTool *self = VIK_GOTO_XML_TOOL (object);
82 VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self);
83 gchar **splitted = NULL;
88 g_free (priv->url_format);
89 priv->url_format = g_value_dup_string (value);
93 splitted = g_strsplit (g_value_get_string (value), "@", 2);
94 g_free (priv->lat_path);
95 priv->lat_path = splitted[0];
98 g_object_set (object, "lat-attr", splitted[1], NULL);
101 /* only free the tab, not the strings */
107 /* Avoid to overwrite XPATH value */
108 /* NB: This disable future overwriting,
109 but as property is CONSTRUCT_ONLY there is no matter */
110 if (!priv->lat_attr || g_value_get_string (value))
112 g_free (priv->lat_attr);
113 priv->lat_attr = g_value_dup_string (value);
118 splitted = g_strsplit (g_value_get_string (value), "@", 2);
119 g_free (priv->lon_path);
120 priv->lon_path = splitted[0];
123 g_object_set (object, "lon-attr", splitted[1], NULL);
124 g_free (splitted[1]);
126 /* only free the tab, not the strings */
132 /* Avoid to overwrite XPATH value */
133 /* NB: This disable future overwriting,
134 but as property is CONSTRUCT_ONLY there is no matter */
135 if (!priv->lon_attr || g_value_get_string (value))
137 g_free (priv->lon_attr);
138 priv->lon_attr = g_value_dup_string (value);
143 /* We don't have any other property... */
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
150 vik_goto_xml_tool_get_property (GObject *object,
155 VikGotoXmlTool *self = VIK_GOTO_XML_TOOL (object);
156 VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self);
160 case PROP_URL_FORMAT:
161 g_value_set_string (value, priv->url_format);
165 g_value_set_string (value, priv->lat_path);
169 g_value_set_string (value, priv->lat_attr);
173 g_value_set_string (value, priv->lon_path);
177 g_value_set_string (value, priv->lon_attr);
181 /* We don't have any other property... */
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
188 vik_goto_xml_tool_class_init ( VikGotoXmlToolClass *klass )
190 GObjectClass *object_class;
191 VikGotoToolClass *parent_class;
194 object_class = G_OBJECT_CLASS (klass);
196 object_class->finalize = vik_goto_xml_tool_finalize;
197 object_class->set_property = vik_goto_xml_tool_set_property;
198 object_class->get_property = vik_goto_xml_tool_get_property;
201 pspec = g_param_spec_string ("url-format",
203 "The format of the URL",
204 "<no-set>" /* default value */,
205 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
206 g_object_class_install_property (object_class,
210 pspec = g_param_spec_string ("lat-path",
212 "XPath of the latitude",
213 "<no-set>" /* default value */,
214 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
215 g_object_class_install_property (object_class,
219 pspec = g_param_spec_string ("lat-attr",
220 "Latitude attribute",
221 "XML attribute of the latitude",
222 NULL /* default value */,
223 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
224 g_object_class_install_property (object_class,
228 pspec = g_param_spec_string ("lon-path",
230 "XPath of the longitude",
231 "<no-set>" /* default value */,
232 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
233 g_object_class_install_property (object_class,
237 pspec = g_param_spec_string ("lon-attr",
238 "Longitude attribute",
239 "XML attribute of the longitude",
240 NULL /* default value */,
241 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
242 g_object_class_install_property (object_class,
246 parent_class = VIK_GOTO_TOOL_CLASS (klass);
248 parent_class->get_url_format = vik_goto_xml_tool_get_url_format;
249 parent_class->parse_file_for_latlon = vik_goto_xml_tool_parse_file_for_latlon;
251 g_type_class_add_private (klass, sizeof (VikGotoXmlToolPrivate));
255 vik_goto_xml_tool_new ()
257 return VIK_GOTO_XML_TOOL ( g_object_new ( VIK_GOTO_XML_TOOL_TYPE, "label", "Google", NULL ) );
261 vik_goto_xml_tool_init ( VikGotoXmlTool *self )
263 VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self);
264 priv->url_format = NULL;
265 priv->lat_path = NULL;
266 priv->lat_attr = NULL;
267 priv->lon_path = NULL;
268 priv->lon_attr = NULL;
275 vik_goto_xml_tool_finalize ( GObject *gob )
277 G_OBJECT_GET_CLASS(gob)->finalize(gob);
281 stack_is_path (const GSList *stack,
284 gboolean equal = TRUE;
285 int stack_len = g_list_length((GList *)stack);
288 while (equal == TRUE && i >= 0)
294 const gchar *current = g_list_nth_data((GList *)stack, i);
295 size_t len = strlen(current);
296 if (strncmp(path, current, len) != 0 )
309 /* Called for open tags <foo bar="baz"> */
311 _start_element (GMarkupParseContext *context,
312 const gchar *element_name,
313 const gchar **attribute_names,
314 const gchar **attribute_values,
318 VikGotoXmlTool *self = VIK_GOTO_XML_TOOL (user_data);
319 VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self);
320 const GSList *stack = g_markup_parse_context_get_element_stack (context);
322 if (priv->lon_attr != NULL && isnan(priv->ll.lon) && stack_is_path (stack, priv->lon_path))
325 while (attribute_names[i] != NULL)
327 if (strcmp (attribute_names[i], priv->lon_attr) == 0)
329 priv->ll.lon = g_ascii_strtod(attribute_values[i], NULL);
335 if (priv->lat_attr != NULL && isnan(priv->ll.lat) && stack_is_path (stack, priv->lat_path))
338 while (attribute_names[i] != NULL)
340 if (strcmp (attribute_names[i], priv->lat_attr) == 0)
342 priv->ll.lat = g_ascii_strtod(attribute_values[i], NULL);
349 /* Called for character data */
350 /* text is not nul-terminated */
352 _text (GMarkupParseContext *context,
358 VikGotoXmlTool *self = VIK_GOTO_XML_TOOL (user_data);
359 VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self);
360 const GSList *stack = g_markup_parse_context_get_element_stack (context);
361 gchar *textl = g_strndup(text, text_len);
362 /* Store only first result */
363 if (priv->lat_attr == NULL && isnan(priv->ll.lat) && stack_is_path (stack, priv->lat_path))
365 priv->ll.lat = g_ascii_strtod(textl, NULL);
367 if (priv->lon_attr == NULL && isnan(priv->ll.lon) && stack_is_path (stack, priv->lon_path))
369 priv->ll.lon = g_ascii_strtod(textl, NULL);
375 vik_goto_xml_tool_parse_file_for_latlon(VikGotoTool *self, gchar *filename, struct LatLon *ll)
377 GMarkupParser xml_parser;
378 GMarkupParseContext *xml_context = NULL;
379 GError *error = NULL;
380 VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self);
381 g_return_val_if_fail(priv != NULL, FALSE);
383 g_debug ("%s: %s@%s, %s@%s",
385 priv->lat_path, priv->lat_attr,
386 priv->lon_path, priv->lon_attr);
388 FILE *file = g_fopen (filename, "r");
390 /* TODO emit warning */
393 /* setup context parse (ie callbacks) */
394 if (priv->lat_attr != NULL || priv->lon_attr != NULL)
395 // At least one coordinate uses an attribute
396 xml_parser.start_element = &_start_element;
398 xml_parser.start_element = NULL;
399 xml_parser.end_element = NULL;
400 if (priv->lat_attr == NULL || priv->lon_attr == NULL)
401 // At least one coordinate uses a raw element
402 xml_parser.text = &_text;
404 xml_parser.text = NULL;
405 xml_parser.passthrough = NULL;
406 xml_parser.error = NULL;
408 xml_context = g_markup_parse_context_new(&xml_parser, 0, self, NULL);
416 while (xml_context &&
417 (nb = fread (buff, sizeof(gchar), BUFSIZ, file)) > 0)
419 if (!g_markup_parse_context_parse(xml_context, buff, nb, &error))
421 fprintf(stderr, "%s: parsing error: %s.\n",
422 __FUNCTION__, error->message);
423 g_markup_parse_context_free(xml_context);
426 g_clear_error (&error);
430 !g_markup_parse_context_end_parse(xml_context, &error))
431 fprintf(stderr, "%s: errors occurred while reading file: %s.\n",
432 __FUNCTION__, error->message);
433 g_clear_error (&error);
436 g_markup_parse_context_free(xml_context);
445 if (isnan(priv->ll.lat) || isnan(priv->ll.lat))
446 /* At least one coordinate not found */
453 vik_goto_xml_tool_get_url_format ( VikGotoTool *self )
455 VikGotoXmlToolPrivate *priv = GOTO_XML_TOOL_GET_PRIVATE (self);
456 g_return_val_if_fail(priv != NULL, NULL);
457 return priv->url_format;