]> git.street.me.uk Git - andy/viking.git/blame - src/vikgototool.c
Some spelling fixes in a comment
[andy/viking.git] / src / vikgototool.c
CommitLineData
6571a7de
GB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2009, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25
34e71b99 26#include "vikgototool.h"
0c6b26d3 27#include "util.h"
6571a7de
GB
28
29#include <string.h>
30
0c6b26d3 31#include <glib.h>
6571a7de 32#include <glib/gi18n.h>
0c6b26d3 33#include <glib/gstdio.h>
6571a7de 34
6571a7de
GB
35static GObjectClass *parent_class;
36
34e71b99
GB
37static void goto_tool_finalize ( GObject *gob );
38static gchar *goto_tool_get_label ( VikGotoTool *vw );
686baff0 39static DownloadFileOptions *goto_tool_get_download_options ( VikGotoTool *self );
6571a7de 40
34e71b99 41typedef struct _VikGotoToolPrivate VikGotoToolPrivate;
6571a7de 42
34e71b99 43struct _VikGotoToolPrivate
6571a7de
GB
44{
45 gint id;
46 gchar *label;
47};
48
34e71b99
GB
49#define GOTO_TOOL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
50 VIK_GOTO_TOOL_TYPE, \
51 VikGotoToolPrivate))
6571a7de 52
07e08106 53G_DEFINE_ABSTRACT_TYPE (VikGotoTool, vik_goto_tool, G_TYPE_OBJECT)
6571a7de
GB
54
55enum
56{
57 PROP_0,
58
59 PROP_ID,
60 PROP_LABEL,
61};
62
63static void
34e71b99 64goto_tool_set_property (GObject *object,
6571a7de
GB
65 guint property_id,
66 const GValue *value,
67 GParamSpec *pspec)
68{
34e71b99
GB
69 VikGotoTool *self = VIK_GOTO_TOOL (object);
70 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
6571a7de
GB
71
72 switch (property_id)
73 {
74 case PROP_ID:
75 priv->id = g_value_get_uint (value);
34e71b99 76 g_debug ("VikGotoTool.id: %d", priv->id);
6571a7de
GB
77 break;
78
79 case PROP_LABEL:
80 g_free (priv->label);
81 priv->label = g_value_dup_string (value);
34e71b99 82 g_debug ("VikGotoTool.label: %s", priv->label);
6571a7de
GB
83 break;
84
85 default:
86 /* We don't have any other property... */
87 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
88 break;
89 }
90}
91
92static void
34e71b99 93goto_tool_get_property (GObject *object,
6571a7de
GB
94 guint property_id,
95 GValue *value,
96 GParamSpec *pspec)
97{
34e71b99
GB
98 VikGotoTool *self = VIK_GOTO_TOOL (object);
99 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
6571a7de
GB
100
101 switch (property_id)
102 {
103 case PROP_ID:
104 g_value_set_uint (value, priv->id);
105 break;
106
107 case PROP_LABEL:
108 g_value_set_string (value, priv->label);
109 break;
110
111 default:
112 /* We don't have any other property... */
113 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
114 break;
115 }
116}
117
07e08106 118static void vik_goto_tool_class_init ( VikGotoToolClass *klass )
6571a7de
GB
119{
120 GObjectClass *gobject_class;
121 GParamSpec *pspec;
122
123 gobject_class = G_OBJECT_CLASS (klass);
34e71b99
GB
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;
6571a7de
GB
127
128 pspec = g_param_spec_string ("label",
129 "Label",
130 "Set the label",
131 "<no-set>" /* default value */,
132 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
133 g_object_class_install_property (gobject_class,
134 PROP_LABEL,
135 pspec);
136
137 pspec = g_param_spec_uint ("id",
138 "Id of the tool",
139 "Set the 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,
145 PROP_ID,
146 pspec);
147
34e71b99 148 klass->get_label = goto_tool_get_label;
0c6b26d3 149 klass->get_download_options = goto_tool_get_download_options;
6571a7de
GB
150
151 parent_class = g_type_class_peek_parent (klass);
152
34e71b99 153 g_type_class_add_private (klass, sizeof (VikGotoToolPrivate));
6571a7de
GB
154}
155
34e71b99 156VikGotoTool *vik_goto_tool_new ()
6571a7de 157{
34e71b99 158 return VIK_GOTO_TOOL ( g_object_new ( VIK_GOTO_TOOL_TYPE, NULL ) );
6571a7de
GB
159}
160
07e08106 161static void vik_goto_tool_init ( VikGotoTool *self )
6571a7de 162{
34e71b99 163 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
6571a7de
GB
164 priv->label = NULL;
165}
166
34e71b99 167static void goto_tool_finalize ( GObject *gob )
6571a7de 168{
34e71b99 169 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE ( gob );
6571a7de
GB
170 g_free ( priv->label ); priv->label = NULL;
171 G_OBJECT_CLASS(parent_class)->finalize(gob);
172}
173
34e71b99 174static gchar *goto_tool_get_label ( VikGotoTool *self )
6571a7de 175{
34e71b99
GB
176 VikGotoToolPrivate *priv = NULL;
177 priv = GOTO_TOOL_GET_PRIVATE (self);
6571a7de
GB
178 return g_strdup ( priv->label );
179}
180
686baff0 181static DownloadFileOptions *goto_tool_get_download_options ( VikGotoTool *self )
6571a7de 182{
0c6b26d3
GB
183 // Default: return NULL
184 return NULL;
185}
186
187gchar *vik_goto_tool_get_label ( VikGotoTool *self )
188{
189 return VIK_GOTO_TOOL_GET_CLASS( self )->get_label( self );
190}
191
192gchar *vik_goto_tool_get_url_format ( VikGotoTool *self )
193{
194 return VIK_GOTO_TOOL_GET_CLASS( self )->get_url_format( self );
195}
196
686baff0 197DownloadFileOptions *vik_goto_tool_get_download_options ( VikGotoTool *self )
0c6b26d3
GB
198{
199 return VIK_GOTO_TOOL_GET_CLASS( self )->get_download_options( self );
200}
201
202gboolean vik_goto_tool_parse_file_for_latlon (VikGotoTool *self, gchar *filename, struct LatLon *ll)
203{
204 return VIK_GOTO_TOOL_GET_CLASS( self )->parse_file_for_latlon( self, filename, ll );
6571a7de
GB
205}
206
79f34e27
RN
207/**
208 * vik_goto_tool_get_coord:
209 *
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
214 *
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
219 *
220 */
34e71b99 221int vik_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord )
6571a7de 222{
0c6b26d3
GB
223 gchar *tmpname;
224 gchar *uri;
225 gchar *escaped_srch_str;
226 int ret = 0; /* OK */
227 struct LatLon ll;
228
229 g_debug("%s: raw goto: %s", __FUNCTION__, srch_str);
230
b5bac32d 231 escaped_srch_str = g_uri_escape_string(srch_str, NULL, TRUE);
0c6b26d3
GB
232
233 g_debug("%s: escaped goto: %s", __FUNCTION__, escaped_srch_str);
234
0c6b26d3
GB
235 uri = g_strdup_printf(vik_goto_tool_get_url_format(self), escaped_srch_str);
236
e09b94fe 237 tmpname = a_download_uri_to_tmp_file ( uri, vik_goto_tool_get_download_options(self) );
0c6b26d3 238
79f34e27
RN
239 if ( !tmpname ) {
240 // Some kind of download error, so no tmp file
241 ret = 1;
242 goto done_no_file;
243 }
244
0c6b26d3
GB
245 g_debug("%s: %s", __FILE__, tmpname);
246 if (!vik_goto_tool_parse_file_for_latlon(self, tmpname, &ll)) {
247 ret = -1;
248 goto done;
249 }
250 vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll );
251
252done:
80586339 253 (void)util_remove(tmpname);
79f34e27 254done_no_file:
0c6b26d3 255 g_free(tmpname);
79f34e27
RN
256 g_free(escaped_srch_str);
257 g_free(uri);
0c6b26d3 258 return ret;
6571a7de 259}