2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2009, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "vikgototool.h"
28 #include "curl_download.h"
33 #include <glib/gi18n.h>
34 #include <glib/gstdio.h>
36 static void goto_tool_class_init ( VikGotoToolClass *klass );
37 static void goto_tool_init ( VikGotoTool *vlp );
39 static GObjectClass *parent_class;
41 static void goto_tool_finalize ( GObject *gob );
42 static gchar *goto_tool_get_label ( VikGotoTool *vw );
43 static DownloadOptions *goto_tool_get_download_options ( VikGotoTool *self );
45 typedef struct _VikGotoToolPrivate VikGotoToolPrivate;
47 struct _VikGotoToolPrivate
53 #define GOTO_TOOL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
57 GType vik_goto_tool_get_type()
59 static GType w_type = 0;
63 static const GTypeInfo w_info =
65 sizeof (VikGotoToolClass),
67 NULL, /* base_finalize */
68 (GClassInitFunc) goto_tool_class_init,
69 NULL, /* class_finalize */
70 NULL, /* class_data */
73 (GInstanceInitFunc) goto_tool_init,
75 w_type = g_type_register_static ( G_TYPE_OBJECT, "VikGotoTool", &w_info, G_TYPE_FLAG_ABSTRACT );
90 goto_tool_set_property (GObject *object,
95 VikGotoTool *self = VIK_GOTO_TOOL (object);
96 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
101 priv->id = g_value_get_uint (value);
102 g_debug ("VikGotoTool.id: %d", priv->id);
106 g_free (priv->label);
107 priv->label = g_value_dup_string (value);
108 g_debug ("VikGotoTool.label: %s", priv->label);
112 /* We don't have any other property... */
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
119 goto_tool_get_property (GObject *object,
124 VikGotoTool *self = VIK_GOTO_TOOL (object);
125 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
130 g_value_set_uint (value, priv->id);
134 g_value_set_string (value, priv->label);
138 /* We don't have any other property... */
139 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
144 static void goto_tool_class_init ( VikGotoToolClass *klass )
146 GObjectClass *gobject_class;
149 gobject_class = G_OBJECT_CLASS (klass);
150 gobject_class->finalize = goto_tool_finalize;
151 gobject_class->set_property = goto_tool_set_property;
152 gobject_class->get_property = goto_tool_get_property;
154 pspec = g_param_spec_string ("label",
157 "<no-set>" /* default value */,
158 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
159 g_object_class_install_property (gobject_class,
163 pspec = g_param_spec_uint ("id",
166 0 /* minimum value */,
167 G_MAXUINT16 /* maximum value */,
168 0 /* default value */,
169 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
170 g_object_class_install_property (gobject_class,
174 klass->get_label = goto_tool_get_label;
175 klass->get_download_options = goto_tool_get_download_options;
177 parent_class = g_type_class_peek_parent (klass);
179 g_type_class_add_private (klass, sizeof (VikGotoToolPrivate));
182 VikGotoTool *vik_goto_tool_new ()
184 return VIK_GOTO_TOOL ( g_object_new ( VIK_GOTO_TOOL_TYPE, NULL ) );
187 static void goto_tool_init ( VikGotoTool *self )
189 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
193 static void goto_tool_finalize ( GObject *gob )
195 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE ( gob );
196 g_free ( priv->label ); priv->label = NULL;
197 G_OBJECT_CLASS(parent_class)->finalize(gob);
200 static gchar *goto_tool_get_label ( VikGotoTool *self )
202 VikGotoToolPrivate *priv = NULL;
203 priv = GOTO_TOOL_GET_PRIVATE (self);
204 return g_strdup ( priv->label );
207 static DownloadOptions *goto_tool_get_download_options ( VikGotoTool *self )
209 // Default: return NULL
213 gchar *vik_goto_tool_get_label ( VikGotoTool *self )
215 return VIK_GOTO_TOOL_GET_CLASS( self )->get_label( self );
218 gchar *vik_goto_tool_get_url_format ( VikGotoTool *self )
220 return VIK_GOTO_TOOL_GET_CLASS( self )->get_url_format( self );
223 DownloadOptions *vik_goto_tool_get_download_options ( VikGotoTool *self )
225 return VIK_GOTO_TOOL_GET_CLASS( self )->get_download_options( self );
228 gboolean vik_goto_tool_parse_file_for_latlon (VikGotoTool *self, gchar *filename, struct LatLon *ll)
230 return VIK_GOTO_TOOL_GET_CLASS( self )->parse_file_for_latlon( self, filename, ll );
233 int vik_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord )
239 gchar *escaped_srch_str;
240 int ret = 0; /* OK */
243 g_debug("%s: raw goto: %s", __FUNCTION__, srch_str);
245 escaped_srch_str = uri_escape(srch_str);
247 g_debug("%s: escaped goto: %s", __FUNCTION__, escaped_srch_str);
249 if ((tmp_fd = g_file_open_tmp ("vikgoto.XXXXXX", &tmpname, NULL)) == -1) {
250 g_critical(_("couldn't open temp file"));
254 tmp_file = fdopen(tmp_fd, "r+");
255 uri = g_strdup_printf(vik_goto_tool_get_url_format(self), escaped_srch_str);
257 /* TODO: curl may not be available */
258 if (curl_download_uri(uri, tmp_file, vik_goto_tool_get_download_options(self), 0, NULL)) { /* error */
267 g_debug("%s: %s", __FILE__, tmpname);
268 if (!vik_goto_tool_parse_file_for_latlon(self, tmpname, &ll)) {
272 vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll );
275 g_free(escaped_srch_str);