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 GObjectClass *parent_class;
38 static void goto_tool_finalize ( GObject *gob );
39 static gchar *goto_tool_get_label ( VikGotoTool *vw );
40 static DownloadMapOptions *goto_tool_get_download_options ( VikGotoTool *self );
42 typedef struct _VikGotoToolPrivate VikGotoToolPrivate;
44 struct _VikGotoToolPrivate
50 #define GOTO_TOOL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
54 G_DEFINE_ABSTRACT_TYPE (VikGotoTool, vik_goto_tool, G_TYPE_OBJECT)
65 goto_tool_set_property (GObject *object,
70 VikGotoTool *self = VIK_GOTO_TOOL (object);
71 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
76 priv->id = g_value_get_uint (value);
77 g_debug ("VikGotoTool.id: %d", priv->id);
82 priv->label = g_value_dup_string (value);
83 g_debug ("VikGotoTool.label: %s", priv->label);
87 /* We don't have any other property... */
88 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
94 goto_tool_get_property (GObject *object,
99 VikGotoTool *self = VIK_GOTO_TOOL (object);
100 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
105 g_value_set_uint (value, priv->id);
109 g_value_set_string (value, priv->label);
113 /* We don't have any other property... */
114 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
119 static void vik_goto_tool_class_init ( VikGotoToolClass *klass )
121 GObjectClass *gobject_class;
124 gobject_class = G_OBJECT_CLASS (klass);
125 gobject_class->finalize = goto_tool_finalize;
126 gobject_class->set_property = goto_tool_set_property;
127 gobject_class->get_property = goto_tool_get_property;
129 pspec = g_param_spec_string ("label",
132 "<no-set>" /* default value */,
133 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
134 g_object_class_install_property (gobject_class,
138 pspec = g_param_spec_uint ("id",
141 0 /* minimum value */,
142 G_MAXUINT16 /* maximum value */,
143 0 /* default value */,
144 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
145 g_object_class_install_property (gobject_class,
149 klass->get_label = goto_tool_get_label;
150 klass->get_download_options = goto_tool_get_download_options;
152 parent_class = g_type_class_peek_parent (klass);
154 g_type_class_add_private (klass, sizeof (VikGotoToolPrivate));
157 VikGotoTool *vik_goto_tool_new ()
159 return VIK_GOTO_TOOL ( g_object_new ( VIK_GOTO_TOOL_TYPE, NULL ) );
162 static void vik_goto_tool_init ( VikGotoTool *self )
164 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
168 static void goto_tool_finalize ( GObject *gob )
170 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE ( gob );
171 g_free ( priv->label ); priv->label = NULL;
172 G_OBJECT_CLASS(parent_class)->finalize(gob);
175 static gchar *goto_tool_get_label ( VikGotoTool *self )
177 VikGotoToolPrivate *priv = NULL;
178 priv = GOTO_TOOL_GET_PRIVATE (self);
179 return g_strdup ( priv->label );
182 static DownloadMapOptions *goto_tool_get_download_options ( VikGotoTool *self )
184 // Default: return NULL
188 gchar *vik_goto_tool_get_label ( VikGotoTool *self )
190 return VIK_GOTO_TOOL_GET_CLASS( self )->get_label( self );
193 gchar *vik_goto_tool_get_url_format ( VikGotoTool *self )
195 return VIK_GOTO_TOOL_GET_CLASS( self )->get_url_format( self );
198 DownloadMapOptions *vik_goto_tool_get_download_options ( VikGotoTool *self )
200 return VIK_GOTO_TOOL_GET_CLASS( self )->get_download_options( self );
203 gboolean vik_goto_tool_parse_file_for_latlon (VikGotoTool *self, gchar *filename, struct LatLon *ll)
205 return VIK_GOTO_TOOL_GET_CLASS( self )->parse_file_for_latlon( self, filename, ll );
208 int vik_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord )
214 gchar *escaped_srch_str;
215 int ret = 0; /* OK */
218 g_debug("%s: raw goto: %s", __FUNCTION__, srch_str);
220 escaped_srch_str = uri_escape(srch_str);
222 g_debug("%s: escaped goto: %s", __FUNCTION__, escaped_srch_str);
224 if ((tmp_fd = g_file_open_tmp ("vikgoto.XXXXXX", &tmpname, NULL)) == -1) {
225 g_critical(_("couldn't open temp file"));
229 tmp_file = fdopen(tmp_fd, "r+");
230 uri = g_strdup_printf(vik_goto_tool_get_url_format(self), escaped_srch_str);
232 /* TODO: curl may not be available */
233 if (curl_download_uri(uri, tmp_file, vik_goto_tool_get_download_options(self), 0, NULL)) { /* error */
242 g_debug("%s: %s", __FILE__, tmpname);
243 if (!vik_goto_tool_parse_file_for_latlon(self, tmpname, &ll)) {
247 vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll );
250 g_free(escaped_srch_str);