]>
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 | 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 |
35 | static GObjectClass *parent_class; |
36 | ||
34e71b99 GB |
37 | static void goto_tool_finalize ( GObject *gob ); |
38 | static gchar *goto_tool_get_label ( VikGotoTool *vw ); | |
b529dc9c | 39 | static DownloadMapOptions *goto_tool_get_download_options ( VikGotoTool *self ); |
6571a7de | 40 | |
34e71b99 | 41 | typedef struct _VikGotoToolPrivate VikGotoToolPrivate; |
6571a7de | 42 | |
34e71b99 | 43 | struct _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 | 53 | G_DEFINE_ABSTRACT_TYPE (VikGotoTool, vik_goto_tool, G_TYPE_OBJECT) |
6571a7de GB |
54 | |
55 | enum | |
56 | { | |
57 | PROP_0, | |
58 | ||
59 | PROP_ID, | |
60 | PROP_LABEL, | |
61 | }; | |
62 | ||
63 | static void | |
34e71b99 | 64 | goto_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 | ||
92 | static void | |
34e71b99 | 93 | goto_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 | 118 | static 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 | 156 | VikGotoTool *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 | 161 | static 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 | 167 | static 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 | 174 | static 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 | ||
b529dc9c | 181 | static DownloadMapOptions *goto_tool_get_download_options ( VikGotoTool *self ) |
6571a7de | 182 | { |
0c6b26d3 GB |
183 | // Default: return NULL |
184 | return NULL; | |
185 | } | |
186 | ||
187 | gchar *vik_goto_tool_get_label ( VikGotoTool *self ) | |
188 | { | |
189 | return VIK_GOTO_TOOL_GET_CLASS( self )->get_label( self ); | |
190 | } | |
191 | ||
192 | gchar *vik_goto_tool_get_url_format ( VikGotoTool *self ) | |
193 | { | |
194 | return VIK_GOTO_TOOL_GET_CLASS( self )->get_url_format( self ); | |
195 | } | |
196 | ||
b529dc9c | 197 | DownloadMapOptions *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 | ||
202 | gboolean 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 | ||
34e71b99 | 207 | int vik_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord ) |
6571a7de | 208 | { |
0c6b26d3 GB |
209 | gchar *tmpname; |
210 | gchar *uri; | |
211 | gchar *escaped_srch_str; | |
212 | int ret = 0; /* OK */ | |
213 | struct LatLon ll; | |
214 | ||
215 | g_debug("%s: raw goto: %s", __FUNCTION__, srch_str); | |
216 | ||
217 | escaped_srch_str = uri_escape(srch_str); | |
218 | ||
219 | g_debug("%s: escaped goto: %s", __FUNCTION__, escaped_srch_str); | |
220 | ||
0c6b26d3 GB |
221 | uri = g_strdup_printf(vik_goto_tool_get_url_format(self), escaped_srch_str); |
222 | ||
e09b94fe | 223 | tmpname = a_download_uri_to_tmp_file ( uri, vik_goto_tool_get_download_options(self) ); |
0c6b26d3 | 224 | |
0c6b26d3 GB |
225 | g_debug("%s: %s", __FILE__, tmpname); |
226 | if (!vik_goto_tool_parse_file_for_latlon(self, tmpname, &ll)) { | |
227 | ret = -1; | |
228 | goto done; | |
229 | } | |
230 | vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll ); | |
231 | ||
232 | done: | |
233 | g_free(escaped_srch_str); | |
234 | g_free(uri); | |
81bf47ee | 235 | util_remove(tmpname); |
0c6b26d3 GB |
236 | g_free(tmpname); |
237 | return ret; | |
6571a7de | 238 | } |