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"
32 #include <glib/gi18n.h>
33 #include <glib/gstdio.h>
35 static GObjectClass *parent_class;
37 static void goto_tool_finalize ( GObject *gob );
38 static gchar *goto_tool_get_label ( VikGotoTool *vw );
39 static DownloadFileOptions *goto_tool_get_download_options ( VikGotoTool *self );
41 typedef struct _VikGotoToolPrivate VikGotoToolPrivate;
43 struct _VikGotoToolPrivate
49 #define GOTO_TOOL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
53 G_DEFINE_ABSTRACT_TYPE (VikGotoTool, vik_goto_tool, G_TYPE_OBJECT)
64 goto_tool_set_property (GObject *object,
69 VikGotoTool *self = VIK_GOTO_TOOL (object);
70 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
75 priv->id = g_value_get_uint (value);
76 g_debug ("VikGotoTool.id: %d", priv->id);
81 priv->label = g_value_dup_string (value);
82 g_debug ("VikGotoTool.label: %s", priv->label);
86 /* We don't have any other property... */
87 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
93 goto_tool_get_property (GObject *object,
98 VikGotoTool *self = VIK_GOTO_TOOL (object);
99 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
104 g_value_set_uint (value, priv->id);
108 g_value_set_string (value, priv->label);
112 /* We don't have any other property... */
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
118 static void vik_goto_tool_class_init ( VikGotoToolClass *klass )
120 GObjectClass *gobject_class;
123 gobject_class = G_OBJECT_CLASS (klass);
124 gobject_class->finalize = goto_tool_finalize;
125 gobject_class->set_property = goto_tool_set_property;
126 gobject_class->get_property = goto_tool_get_property;
128 pspec = g_param_spec_string ("label",
131 "<no-set>" /* default value */,
132 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
133 g_object_class_install_property (gobject_class,
137 pspec = g_param_spec_uint ("id",
140 0 /* minimum value */,
141 G_MAXUINT16 /* maximum value */,
142 0 /* default value */,
143 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
144 g_object_class_install_property (gobject_class,
148 klass->get_label = goto_tool_get_label;
149 klass->get_download_options = goto_tool_get_download_options;
151 parent_class = g_type_class_peek_parent (klass);
153 g_type_class_add_private (klass, sizeof (VikGotoToolPrivate));
156 VikGotoTool *vik_goto_tool_new ()
158 return VIK_GOTO_TOOL ( g_object_new ( VIK_GOTO_TOOL_TYPE, NULL ) );
161 static void vik_goto_tool_init ( VikGotoTool *self )
163 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
167 static void goto_tool_finalize ( GObject *gob )
169 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE ( gob );
170 g_free ( priv->label ); priv->label = NULL;
171 G_OBJECT_CLASS(parent_class)->finalize(gob);
174 static gchar *goto_tool_get_label ( VikGotoTool *self )
176 VikGotoToolPrivate *priv = NULL;
177 priv = GOTO_TOOL_GET_PRIVATE (self);
178 return g_strdup ( priv->label );
181 static DownloadFileOptions *goto_tool_get_download_options ( VikGotoTool *self )
183 // Default: return NULL
187 gchar *vik_goto_tool_get_label ( VikGotoTool *self )
189 return VIK_GOTO_TOOL_GET_CLASS( self )->get_label( self );
192 gchar *vik_goto_tool_get_url_format ( VikGotoTool *self )
194 return VIK_GOTO_TOOL_GET_CLASS( self )->get_url_format( self );
197 DownloadFileOptions *vik_goto_tool_get_download_options ( VikGotoTool *self )
199 return VIK_GOTO_TOOL_GET_CLASS( self )->get_download_options( self );
202 gboolean vik_goto_tool_parse_file_for_latlon (VikGotoTool *self, gchar *filename, struct LatLon *ll)
204 return VIK_GOTO_TOOL_GET_CLASS( self )->parse_file_for_latlon( self, filename, ll );
208 * vik_goto_tool_get_coord:
210 * @self: The #VikGotoTool
211 * @vvp: The #VikViewport
212 * @srch_str: The string to search with
213 * @coord: Returns the top match position for a successful search
215 * Returns: An integer value indicating:
216 * 0 = search found something
217 * -1 = search place not found by the #VikGotoTool
218 * 1 = search unavailable in the #VikGotoTool due to communication issue
221 int vik_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord )
225 gchar *escaped_srch_str;
226 int ret = 0; /* OK */
229 g_debug("%s: raw goto: %s", __FUNCTION__, srch_str);
231 escaped_srch_str = uri_escape(srch_str);
233 g_debug("%s: escaped goto: %s", __FUNCTION__, escaped_srch_str);
235 uri = g_strdup_printf(vik_goto_tool_get_url_format(self), escaped_srch_str);
237 tmpname = a_download_uri_to_tmp_file ( uri, vik_goto_tool_get_download_options(self) );
240 // Some kind of download error, so no tmp file
245 g_debug("%s: %s", __FILE__, tmpname);
246 if (!vik_goto_tool_parse_file_for_latlon(self, tmpname, &ll)) {
250 vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll );
253 (void)util_remove(tmpname);
256 g_free(escaped_srch_str);