]>
Commit | Line | Data |
---|---|---|
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 | |
6571a7de GB |
36 | static GObjectClass *parent_class; |
37 | ||
34e71b99 GB |
38 | static void goto_tool_finalize ( GObject *gob ); |
39 | static gchar *goto_tool_get_label ( VikGotoTool *vw ); | |
b529dc9c | 40 | static DownloadMapOptions *goto_tool_get_download_options ( VikGotoTool *self ); |
6571a7de | 41 | |
34e71b99 | 42 | typedef struct _VikGotoToolPrivate VikGotoToolPrivate; |
6571a7de | 43 | |
34e71b99 | 44 | struct _VikGotoToolPrivate |
6571a7de GB |
45 | { |
46 | gint id; | |
47 | gchar *label; | |
48 | }; | |
49 | ||
34e71b99 GB |
50 | #define GOTO_TOOL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \ |
51 | VIK_GOTO_TOOL_TYPE, \ | |
52 | VikGotoToolPrivate)) | |
6571a7de | 53 | |
07e08106 | 54 | G_DEFINE_ABSTRACT_TYPE (VikGotoTool, vik_goto_tool, G_TYPE_OBJECT) |
6571a7de GB |
55 | |
56 | enum | |
57 | { | |
58 | PROP_0, | |
59 | ||
60 | PROP_ID, | |
61 | PROP_LABEL, | |
62 | }; | |
63 | ||
64 | static void | |
34e71b99 | 65 | goto_tool_set_property (GObject *object, |
6571a7de GB |
66 | guint property_id, |
67 | const GValue *value, | |
68 | GParamSpec *pspec) | |
69 | { | |
34e71b99 GB |
70 | VikGotoTool *self = VIK_GOTO_TOOL (object); |
71 | VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self); | |
6571a7de GB |
72 | |
73 | switch (property_id) | |
74 | { | |
75 | case PROP_ID: | |
76 | priv->id = g_value_get_uint (value); | |
34e71b99 | 77 | g_debug ("VikGotoTool.id: %d", priv->id); |
6571a7de GB |
78 | break; |
79 | ||
80 | case PROP_LABEL: | |
81 | g_free (priv->label); | |
82 | priv->label = g_value_dup_string (value); | |
34e71b99 | 83 | g_debug ("VikGotoTool.label: %s", priv->label); |
6571a7de GB |
84 | break; |
85 | ||
86 | default: | |
87 | /* We don't have any other property... */ | |
88 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
89 | break; | |
90 | } | |
91 | } | |
92 | ||
93 | static void | |
34e71b99 | 94 | goto_tool_get_property (GObject *object, |
6571a7de GB |
95 | guint property_id, |
96 | GValue *value, | |
97 | GParamSpec *pspec) | |
98 | { | |
34e71b99 GB |
99 | VikGotoTool *self = VIK_GOTO_TOOL (object); |
100 | VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self); | |
6571a7de GB |
101 | |
102 | switch (property_id) | |
103 | { | |
104 | case PROP_ID: | |
105 | g_value_set_uint (value, priv->id); | |
106 | break; | |
107 | ||
108 | case PROP_LABEL: | |
109 | g_value_set_string (value, priv->label); | |
110 | break; | |
111 | ||
112 | default: | |
113 | /* We don't have any other property... */ | |
114 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
115 | break; | |
116 | } | |
117 | } | |
118 | ||
07e08106 | 119 | static void vik_goto_tool_class_init ( VikGotoToolClass *klass ) |
6571a7de GB |
120 | { |
121 | GObjectClass *gobject_class; | |
122 | GParamSpec *pspec; | |
123 | ||
124 | gobject_class = G_OBJECT_CLASS (klass); | |
34e71b99 GB |
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; | |
6571a7de GB |
128 | |
129 | pspec = g_param_spec_string ("label", | |
130 | "Label", | |
131 | "Set the label", | |
132 | "<no-set>" /* default value */, | |
133 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
134 | g_object_class_install_property (gobject_class, | |
135 | PROP_LABEL, | |
136 | pspec); | |
137 | ||
138 | pspec = g_param_spec_uint ("id", | |
139 | "Id of the tool", | |
140 | "Set the 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, | |
146 | PROP_ID, | |
147 | pspec); | |
148 | ||
34e71b99 | 149 | klass->get_label = goto_tool_get_label; |
0c6b26d3 | 150 | klass->get_download_options = goto_tool_get_download_options; |
6571a7de GB |
151 | |
152 | parent_class = g_type_class_peek_parent (klass); | |
153 | ||
34e71b99 | 154 | g_type_class_add_private (klass, sizeof (VikGotoToolPrivate)); |
6571a7de GB |
155 | } |
156 | ||
34e71b99 | 157 | VikGotoTool *vik_goto_tool_new () |
6571a7de | 158 | { |
34e71b99 | 159 | return VIK_GOTO_TOOL ( g_object_new ( VIK_GOTO_TOOL_TYPE, NULL ) ); |
6571a7de GB |
160 | } |
161 | ||
07e08106 | 162 | static void vik_goto_tool_init ( VikGotoTool *self ) |
6571a7de | 163 | { |
34e71b99 | 164 | VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self); |
6571a7de GB |
165 | priv->label = NULL; |
166 | } | |
167 | ||
34e71b99 | 168 | static void goto_tool_finalize ( GObject *gob ) |
6571a7de | 169 | { |
34e71b99 | 170 | VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE ( gob ); |
6571a7de GB |
171 | g_free ( priv->label ); priv->label = NULL; |
172 | G_OBJECT_CLASS(parent_class)->finalize(gob); | |
173 | } | |
174 | ||
34e71b99 | 175 | static gchar *goto_tool_get_label ( VikGotoTool *self ) |
6571a7de | 176 | { |
34e71b99 GB |
177 | VikGotoToolPrivate *priv = NULL; |
178 | priv = GOTO_TOOL_GET_PRIVATE (self); | |
6571a7de GB |
179 | return g_strdup ( priv->label ); |
180 | } | |
181 | ||
b529dc9c | 182 | static DownloadMapOptions *goto_tool_get_download_options ( VikGotoTool *self ) |
6571a7de | 183 | { |
0c6b26d3 GB |
184 | // Default: return NULL |
185 | return NULL; | |
186 | } | |
187 | ||
188 | gchar *vik_goto_tool_get_label ( VikGotoTool *self ) | |
189 | { | |
190 | return VIK_GOTO_TOOL_GET_CLASS( self )->get_label( self ); | |
191 | } | |
192 | ||
193 | gchar *vik_goto_tool_get_url_format ( VikGotoTool *self ) | |
194 | { | |
195 | return VIK_GOTO_TOOL_GET_CLASS( self )->get_url_format( self ); | |
196 | } | |
197 | ||
b529dc9c | 198 | DownloadMapOptions *vik_goto_tool_get_download_options ( VikGotoTool *self ) |
0c6b26d3 GB |
199 | { |
200 | return VIK_GOTO_TOOL_GET_CLASS( self )->get_download_options( self ); | |
201 | } | |
202 | ||
203 | gboolean vik_goto_tool_parse_file_for_latlon (VikGotoTool *self, gchar *filename, struct LatLon *ll) | |
204 | { | |
205 | return VIK_GOTO_TOOL_GET_CLASS( self )->parse_file_for_latlon( self, filename, ll ); | |
6571a7de GB |
206 | } |
207 | ||
34e71b99 | 208 | int vik_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord ) |
6571a7de | 209 | { |
0c6b26d3 GB |
210 | FILE *tmp_file; |
211 | int tmp_fd; | |
212 | gchar *tmpname; | |
213 | gchar *uri; | |
214 | gchar *escaped_srch_str; | |
215 | int ret = 0; /* OK */ | |
216 | struct LatLon ll; | |
217 | ||
218 | g_debug("%s: raw goto: %s", __FUNCTION__, srch_str); | |
219 | ||
220 | escaped_srch_str = uri_escape(srch_str); | |
221 | ||
222 | g_debug("%s: escaped goto: %s", __FUNCTION__, escaped_srch_str); | |
223 | ||
224 | if ((tmp_fd = g_file_open_tmp ("vikgoto.XXXXXX", &tmpname, NULL)) == -1) { | |
225 | g_critical(_("couldn't open temp file")); | |
226 | return -1; | |
227 | } | |
228 | ||
229 | tmp_file = fdopen(tmp_fd, "r+"); | |
230 | uri = g_strdup_printf(vik_goto_tool_get_url_format(self), escaped_srch_str); | |
231 | ||
232 | /* TODO: curl may not be available */ | |
825413ba | 233 | if (curl_download_uri(uri, tmp_file, vik_goto_tool_get_download_options(self), 0, NULL)) { /* error */ |
0c6b26d3 GB |
234 | fclose(tmp_file); |
235 | tmp_file = NULL; | |
236 | ret = -1; | |
237 | goto done; | |
238 | } | |
239 | ||
240 | fclose(tmp_file); | |
241 | tmp_file = NULL; | |
242 | g_debug("%s: %s", __FILE__, tmpname); | |
243 | if (!vik_goto_tool_parse_file_for_latlon(self, tmpname, &ll)) { | |
244 | ret = -1; | |
245 | goto done; | |
246 | } | |
247 | vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll ); | |
248 | ||
249 | done: | |
250 | g_free(escaped_srch_str); | |
251 | g_free(uri); | |
252 | g_remove(tmpname); | |
253 | g_free(tmpname); | |
254 | return ret; | |
6571a7de | 255 | } |