]> git.street.me.uk Git - andy/viking.git/blame - src/vikgototool.c
Fix CPU usage going to 100% when statusbar items update is called.
[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
GB
27#include "util.h"
28#include "curl_download.h"
6571a7de
GB
29
30#include <string.h>
31
0c6b26d3 32#include <glib.h>
6571a7de 33#include <glib/gi18n.h>
0c6b26d3 34#include <glib/gstdio.h>
6571a7de 35
34e71b99
GB
36static void goto_tool_class_init ( VikGotoToolClass *klass );
37static void goto_tool_init ( VikGotoTool *vlp );
6571a7de
GB
38
39static GObjectClass *parent_class;
40
34e71b99
GB
41static void goto_tool_finalize ( GObject *gob );
42static gchar *goto_tool_get_label ( VikGotoTool *vw );
b529dc9c 43static DownloadMapOptions *goto_tool_get_download_options ( VikGotoTool *self );
6571a7de 44
34e71b99 45typedef struct _VikGotoToolPrivate VikGotoToolPrivate;
6571a7de 46
34e71b99 47struct _VikGotoToolPrivate
6571a7de
GB
48{
49 gint id;
50 gchar *label;
51};
52
34e71b99
GB
53#define GOTO_TOOL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
54 VIK_GOTO_TOOL_TYPE, \
55 VikGotoToolPrivate))
6571a7de 56
34e71b99 57GType vik_goto_tool_get_type()
6571a7de
GB
58{
59 static GType w_type = 0;
60
61 if (!w_type)
62 {
63 static const GTypeInfo w_info =
64 {
34e71b99 65 sizeof (VikGotoToolClass),
6571a7de
GB
66 NULL, /* base_init */
67 NULL, /* base_finalize */
34e71b99 68 (GClassInitFunc) goto_tool_class_init,
6571a7de
GB
69 NULL, /* class_finalize */
70 NULL, /* class_data */
34e71b99 71 sizeof (VikGotoTool),
6571a7de 72 0,
34e71b99 73 (GInstanceInitFunc) goto_tool_init,
6571a7de 74 };
34e71b99 75 w_type = g_type_register_static ( G_TYPE_OBJECT, "VikGotoTool", &w_info, G_TYPE_FLAG_ABSTRACT );
6571a7de
GB
76 }
77
78 return w_type;
79}
80
81enum
82{
83 PROP_0,
84
85 PROP_ID,
86 PROP_LABEL,
87};
88
89static void
34e71b99 90goto_tool_set_property (GObject *object,
6571a7de
GB
91 guint property_id,
92 const GValue *value,
93 GParamSpec *pspec)
94{
34e71b99
GB
95 VikGotoTool *self = VIK_GOTO_TOOL (object);
96 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
6571a7de
GB
97
98 switch (property_id)
99 {
100 case PROP_ID:
101 priv->id = g_value_get_uint (value);
34e71b99 102 g_debug ("VikGotoTool.id: %d", priv->id);
6571a7de
GB
103 break;
104
105 case PROP_LABEL:
106 g_free (priv->label);
107 priv->label = g_value_dup_string (value);
34e71b99 108 g_debug ("VikGotoTool.label: %s", priv->label);
6571a7de
GB
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
118static void
34e71b99 119goto_tool_get_property (GObject *object,
6571a7de
GB
120 guint property_id,
121 GValue *value,
122 GParamSpec *pspec)
123{
34e71b99
GB
124 VikGotoTool *self = VIK_GOTO_TOOL (object);
125 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
6571a7de
GB
126
127 switch (property_id)
128 {
129 case PROP_ID:
130 g_value_set_uint (value, priv->id);
131 break;
132
133 case PROP_LABEL:
134 g_value_set_string (value, priv->label);
135 break;
136
137 default:
138 /* We don't have any other property... */
139 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
140 break;
141 }
142}
143
34e71b99 144static void goto_tool_class_init ( VikGotoToolClass *klass )
6571a7de
GB
145{
146 GObjectClass *gobject_class;
147 GParamSpec *pspec;
148
149 gobject_class = G_OBJECT_CLASS (klass);
34e71b99
GB
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;
6571a7de
GB
153
154 pspec = g_param_spec_string ("label",
155 "Label",
156 "Set the label",
157 "<no-set>" /* default value */,
158 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
159 g_object_class_install_property (gobject_class,
160 PROP_LABEL,
161 pspec);
162
163 pspec = g_param_spec_uint ("id",
164 "Id of the tool",
165 "Set the 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,
171 PROP_ID,
172 pspec);
173
34e71b99 174 klass->get_label = goto_tool_get_label;
0c6b26d3 175 klass->get_download_options = goto_tool_get_download_options;
6571a7de
GB
176
177 parent_class = g_type_class_peek_parent (klass);
178
34e71b99 179 g_type_class_add_private (klass, sizeof (VikGotoToolPrivate));
6571a7de
GB
180}
181
34e71b99 182VikGotoTool *vik_goto_tool_new ()
6571a7de 183{
34e71b99 184 return VIK_GOTO_TOOL ( g_object_new ( VIK_GOTO_TOOL_TYPE, NULL ) );
6571a7de
GB
185}
186
34e71b99 187static void goto_tool_init ( VikGotoTool *self )
6571a7de 188{
34e71b99 189 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
6571a7de
GB
190 priv->label = NULL;
191}
192
34e71b99 193static void goto_tool_finalize ( GObject *gob )
6571a7de 194{
34e71b99 195 VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE ( gob );
6571a7de
GB
196 g_free ( priv->label ); priv->label = NULL;
197 G_OBJECT_CLASS(parent_class)->finalize(gob);
198}
199
34e71b99 200static gchar *goto_tool_get_label ( VikGotoTool *self )
6571a7de 201{
34e71b99
GB
202 VikGotoToolPrivate *priv = NULL;
203 priv = GOTO_TOOL_GET_PRIVATE (self);
6571a7de
GB
204 return g_strdup ( priv->label );
205}
206
b529dc9c 207static DownloadMapOptions *goto_tool_get_download_options ( VikGotoTool *self )
6571a7de 208{
0c6b26d3
GB
209 // Default: return NULL
210 return NULL;
211}
212
213gchar *vik_goto_tool_get_label ( VikGotoTool *self )
214{
215 return VIK_GOTO_TOOL_GET_CLASS( self )->get_label( self );
216}
217
218gchar *vik_goto_tool_get_url_format ( VikGotoTool *self )
219{
220 return VIK_GOTO_TOOL_GET_CLASS( self )->get_url_format( self );
221}
222
b529dc9c 223DownloadMapOptions *vik_goto_tool_get_download_options ( VikGotoTool *self )
0c6b26d3
GB
224{
225 return VIK_GOTO_TOOL_GET_CLASS( self )->get_download_options( self );
226}
227
228gboolean vik_goto_tool_parse_file_for_latlon (VikGotoTool *self, gchar *filename, struct LatLon *ll)
229{
230 return VIK_GOTO_TOOL_GET_CLASS( self )->parse_file_for_latlon( self, filename, ll );
6571a7de
GB
231}
232
34e71b99 233int vik_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord )
6571a7de 234{
0c6b26d3
GB
235 FILE *tmp_file;
236 int tmp_fd;
237 gchar *tmpname;
238 gchar *uri;
239 gchar *escaped_srch_str;
240 int ret = 0; /* OK */
241 struct LatLon ll;
242
243 g_debug("%s: raw goto: %s", __FUNCTION__, srch_str);
244
245 escaped_srch_str = uri_escape(srch_str);
246
247 g_debug("%s: escaped goto: %s", __FUNCTION__, escaped_srch_str);
248
249 if ((tmp_fd = g_file_open_tmp ("vikgoto.XXXXXX", &tmpname, NULL)) == -1) {
250 g_critical(_("couldn't open temp file"));
251 return -1;
252 }
253
254 tmp_file = fdopen(tmp_fd, "r+");
255 uri = g_strdup_printf(vik_goto_tool_get_url_format(self), escaped_srch_str);
256
257 /* TODO: curl may not be available */
825413ba 258 if (curl_download_uri(uri, tmp_file, vik_goto_tool_get_download_options(self), 0, NULL)) { /* error */
0c6b26d3
GB
259 fclose(tmp_file);
260 tmp_file = NULL;
261 ret = -1;
262 goto done;
263 }
264
265 fclose(tmp_file);
266 tmp_file = NULL;
267 g_debug("%s: %s", __FILE__, tmpname);
268 if (!vik_goto_tool_parse_file_for_latlon(self, tmpname, &ll)) {
269 ret = -1;
270 goto done;
271 }
272 vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll );
273
274done:
275 g_free(escaped_srch_str);
276 g_free(uri);
277 g_remove(tmpname);
278 g_free(tmpname);
279 return ret;
6571a7de 280}