]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2007, Evan Battaglia <gtoevan@gmx.net> | |
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 | #ifdef HAVE_CONFIG_H | |
22 | #include "config.h" | |
23 | #endif | |
24 | #ifdef VIK_CONFIG_GEOCACHES | |
25 | #include <string.h> | |
26 | ||
27 | #include <glib/gi18n.h> | |
28 | ||
29 | #include "viking.h" | |
30 | #include "babel.h" | |
31 | #include "gpx.h" | |
32 | #include "acquire.h" | |
33 | #include "preferences.h" | |
34 | ||
35 | // Could have an array of programs instead... | |
36 | #define GC_PROGRAM1 "geo-nearest" | |
37 | #define GC_PROGRAM2 "geo-html2gpx" | |
38 | ||
39 | /* params will be geocaching.username, geocaching.password */ | |
40 | /* we have to make sure these don't collide. */ | |
41 | #define VIKING_GC_PARAMS_GROUP_KEY "geocaching" | |
42 | #define VIKING_GC_PARAMS_NAMESPACE "geocaching." | |
43 | ||
44 | ||
45 | typedef struct { | |
46 | GtkWidget *num_spin; | |
47 | GtkWidget *center_entry; // TODO make separate widgets for lat/lon | |
48 | GtkWidget *miles_radius_spin; | |
49 | ||
50 | GdkGC *circle_gc; | |
51 | VikViewport *vvp; | |
52 | gboolean circle_onscreen; | |
53 | gint circle_x, circle_y, circle_width; | |
54 | } datasource_gc_widgets_t; | |
55 | ||
56 | ||
57 | static gpointer datasource_gc_init ( acq_vik_t *avt ); | |
58 | static void datasource_gc_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data ); | |
59 | static void datasource_gc_get_cmd_string ( datasource_gc_widgets_t *widgets, gchar **cmd, gchar **input_file_type, gpointer not_used ); | |
60 | static void datasource_gc_cleanup ( datasource_gc_widgets_t *widgets ); | |
61 | static gchar *datasource_gc_check_existence (); | |
62 | ||
63 | #define METERSPERMILE 1609.344 | |
64 | ||
65 | VikDataSourceInterface vik_datasource_gc_interface = { | |
66 | N_("Download Geocaches"), | |
67 | N_("Geocaching.com Caches"), | |
68 | VIK_DATASOURCE_AUTO_LAYER_MANAGEMENT, | |
69 | VIK_DATASOURCE_INPUTTYPE_NONE, | |
70 | TRUE, // Yes automatically update the display - otherwise we won't see the geocache waypoints! | |
71 | TRUE, | |
72 | TRUE, | |
73 | (VikDataSourceInitFunc) datasource_gc_init, | |
74 | (VikDataSourceCheckExistenceFunc) datasource_gc_check_existence, | |
75 | (VikDataSourceAddSetupWidgetsFunc) datasource_gc_add_setup_widgets, | |
76 | (VikDataSourceGetCmdStringFunc) datasource_gc_get_cmd_string, | |
77 | (VikDataSourceProcessFunc) a_babel_convert_from_shellcommand, | |
78 | (VikDataSourceProgressFunc) NULL, | |
79 | (VikDataSourceAddProgressWidgetsFunc) NULL, | |
80 | (VikDataSourceCleanupFunc) datasource_gc_cleanup, | |
81 | (VikDataSourceOffFunc) NULL, | |
82 | }; | |
83 | ||
84 | static VikLayerParam prefs[] = { | |
85 | { VIK_LAYER_NUM_TYPES, VIKING_GC_PARAMS_NAMESPACE "username", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("geocaching.com username:"), VIK_LAYER_WIDGET_ENTRY, NULL, NULL, NULL, NULL, NULL }, | |
86 | { VIK_LAYER_NUM_TYPES, VIKING_GC_PARAMS_NAMESPACE "password", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("geocaching.com password:"), VIK_LAYER_WIDGET_ENTRY, NULL, NULL, NULL, NULL, NULL }, | |
87 | }; | |
88 | ||
89 | void a_datasource_gc_init() | |
90 | { | |
91 | a_preferences_register_group ( VIKING_GC_PARAMS_GROUP_KEY, "Geocaching" ); | |
92 | ||
93 | VikLayerParamData tmp; | |
94 | tmp.s = "username"; | |
95 | a_preferences_register(prefs, tmp, VIKING_GC_PARAMS_GROUP_KEY); | |
96 | tmp.s = "password"; | |
97 | a_preferences_register(prefs+1, tmp, VIKING_GC_PARAMS_GROUP_KEY); | |
98 | } | |
99 | ||
100 | ||
101 | static gpointer datasource_gc_init ( acq_vik_t *avt ) | |
102 | { | |
103 | datasource_gc_widgets_t *widgets = g_malloc(sizeof(*widgets)); | |
104 | return widgets; | |
105 | } | |
106 | ||
107 | static gchar *datasource_gc_check_existence () | |
108 | { | |
109 | gboolean OK1 = FALSE; | |
110 | gboolean OK2 = FALSE; | |
111 | ||
112 | gchar *location1 = g_find_program_in_path(GC_PROGRAM1); | |
113 | if ( location1 ) { | |
114 | g_free(location1); | |
115 | OK1 = TRUE; | |
116 | } | |
117 | ||
118 | gchar *location2 = g_find_program_in_path(GC_PROGRAM2); | |
119 | if ( location2 ) { | |
120 | g_free(location2); | |
121 | OK2 = TRUE; | |
122 | } | |
123 | ||
124 | if ( OK1 && OK2 ) | |
125 | return NULL; | |
126 | ||
127 | return g_strdup_printf(_("Can't find %s or %s in path! Check that you have installed it correctly."), GC_PROGRAM1, GC_PROGRAM2); | |
128 | } | |
129 | ||
130 | static void datasource_gc_draw_circle ( datasource_gc_widgets_t *widgets ) | |
131 | { | |
132 | gdouble lat, lon; | |
133 | if ( widgets->circle_onscreen ) { | |
134 | vik_viewport_draw_arc ( widgets->vvp, widgets->circle_gc, FALSE, | |
135 | widgets->circle_x - widgets->circle_width/2, | |
136 | widgets->circle_y - widgets->circle_width/2, | |
137 | widgets->circle_width, widgets->circle_width, 0, 360*64 ); | |
138 | } | |
139 | /* calculate widgets circle_x and circle_y */ | |
140 | /* split up lat,lon into lat and lon */ | |
141 | if ( 2 == sscanf ( gtk_entry_get_text ( GTK_ENTRY(widgets->center_entry) ), "%lf,%lf", &lat, &lon ) ) { | |
142 | struct LatLon ll; | |
143 | VikCoord c; | |
144 | gint x, y; | |
145 | ||
146 | ll.lat = lat; ll.lon = lon; | |
147 | vik_coord_load_from_latlon ( &c, vik_viewport_get_coord_mode ( widgets->vvp ), &ll ); | |
148 | vik_viewport_coord_to_screen ( widgets->vvp, &c, &x, &y ); | |
149 | /* TODO: real calculation */ | |
150 | if ( x > -1000 && y > -1000 && x < (vik_viewport_get_width(widgets->vvp) + 1000) && | |
151 | y < (vik_viewport_get_width(widgets->vvp) + 1000) ) { | |
152 | VikCoord c1, c2; | |
153 | gdouble pixels_per_meter; | |
154 | ||
155 | widgets->circle_x = x; | |
156 | widgets->circle_y = y; | |
157 | ||
158 | /* determine miles per pixel */ | |
159 | vik_viewport_screen_to_coord ( widgets->vvp, 0, vik_viewport_get_height(widgets->vvp)/2, &c1 ); | |
160 | vik_viewport_screen_to_coord ( widgets->vvp, vik_viewport_get_width(widgets->vvp), vik_viewport_get_height(widgets->vvp)/2, &c2 ); | |
161 | pixels_per_meter = ((gdouble)vik_viewport_get_width(widgets->vvp)) / vik_coord_diff(&c1, &c2); | |
162 | ||
163 | /* this is approximate */ | |
164 | widgets->circle_width = gtk_spin_button_get_value_as_float ( GTK_SPIN_BUTTON(widgets->miles_radius_spin) ) | |
165 | * METERSPERMILE * pixels_per_meter * 2; | |
166 | ||
167 | vik_viewport_draw_arc ( widgets->vvp, widgets->circle_gc, FALSE, | |
168 | widgets->circle_x - widgets->circle_width/2, | |
169 | widgets->circle_y - widgets->circle_width/2, | |
170 | widgets->circle_width, widgets->circle_width, 0, 360*64 ); | |
171 | ||
172 | widgets->circle_onscreen = TRUE; | |
173 | } else | |
174 | widgets->circle_onscreen = FALSE; | |
175 | } | |
176 | ||
177 | /* see if onscreen */ | |
178 | /* okay */ | |
179 | vik_viewport_sync ( widgets->vvp ); | |
180 | } | |
181 | ||
182 | static void datasource_gc_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data ) | |
183 | { | |
184 | datasource_gc_widgets_t *widgets = (datasource_gc_widgets_t *)user_data; | |
185 | GtkWidget *num_label, *center_label, *miles_radius_label; | |
186 | struct LatLon ll; | |
187 | gchar *s_ll; | |
188 | ||
189 | num_label = gtk_label_new (_("Number geocaches:")); | |
190 | widgets->num_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new( 20, 1, 1000, 10, 20, 0 )), 10, 0 ); | |
191 | center_label = gtk_label_new (_("Centered around:")); | |
192 | widgets->center_entry = gtk_entry_new(); | |
193 | miles_radius_label = gtk_label_new ("Miles Radius:"); | |
194 | widgets->miles_radius_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new( 5, 1, 1000, 1, 20, 0 )), 25, 1 ); | |
195 | ||
196 | vik_coord_to_latlon ( vik_viewport_get_center(vvp), &ll ); | |
197 | s_ll = g_strdup_printf("%f,%f", ll.lat, ll.lon ); | |
198 | gtk_entry_set_text ( GTK_ENTRY(widgets->center_entry), s_ll ); | |
199 | g_free ( s_ll ); | |
200 | ||
201 | ||
202 | widgets->vvp = vvp; | |
203 | widgets->circle_gc = vik_viewport_new_gc ( vvp, "#000000", 3 ); | |
204 | gdk_gc_set_function ( widgets->circle_gc, GDK_INVERT ); | |
205 | widgets->circle_onscreen = TRUE; | |
206 | datasource_gc_draw_circle ( widgets ); | |
207 | ||
208 | g_signal_connect_swapped ( G_OBJECT(widgets->center_entry), "changed", G_CALLBACK(datasource_gc_draw_circle), widgets ); | |
209 | g_signal_connect_swapped ( G_OBJECT(widgets->miles_radius_spin), "value-changed", G_CALLBACK(datasource_gc_draw_circle), widgets ); | |
210 | ||
211 | /* Packing all these widgets */ | |
212 | GtkBox *box = GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))); | |
213 | gtk_box_pack_start ( box, num_label, FALSE, FALSE, 5 ); | |
214 | gtk_box_pack_start ( box, widgets->num_spin, FALSE, FALSE, 5 ); | |
215 | gtk_box_pack_start ( box, center_label, FALSE, FALSE, 5 ); | |
216 | gtk_box_pack_start ( box, widgets->center_entry, FALSE, FALSE, 5 ); | |
217 | gtk_box_pack_start ( box, miles_radius_label, FALSE, FALSE, 5 ); | |
218 | gtk_box_pack_start ( box, widgets->miles_radius_spin, FALSE, FALSE, 5 ); | |
219 | gtk_widget_show_all(dialog); | |
220 | } | |
221 | ||
222 | static void datasource_gc_get_cmd_string ( datasource_gc_widgets_t *widgets, gchar **cmd, gchar **input_file_type, gpointer not_used ) | |
223 | { | |
224 | //gchar *safe_string = g_shell_quote ( gtk_entry_get_text ( GTK_ENTRY(widgets->center_entry) ) ); | |
225 | gchar *safe_user = g_shell_quote ( a_preferences_get ( VIKING_GC_PARAMS_NAMESPACE "username")->s ); | |
226 | gchar *safe_pass = g_shell_quote ( a_preferences_get ( VIKING_GC_PARAMS_NAMESPACE "password")->s ); | |
227 | gchar *slat, *slon; | |
228 | gdouble lat, lon; | |
229 | if ( 2 != sscanf ( gtk_entry_get_text ( GTK_ENTRY(widgets->center_entry) ), "%lf,%lf", &lat, &lon ) ) { | |
230 | g_warning (_("Broken input - using some defaults")); | |
231 | lat = a_vik_get_default_lat(); | |
232 | lon = a_vik_get_default_long(); | |
233 | } | |
234 | // Convert double as string in C locale | |
235 | slat = a_coords_dtostr ( lat ); | |
236 | slon = a_coords_dtostr ( lon ); | |
237 | ||
238 | // Unix specific shell commands | |
239 | // 1. Remove geocache webpages (maybe be from different location) | |
240 | // 2, Gets upto n geocaches as webpages for the specified user in radius r Miles | |
241 | // 3. Converts webpages into a single waypoint file, ignoring zero location waypoints '-z' | |
242 | // Probably as they are premium member only geocaches and user is only a basic member | |
243 | // Final output is piped into GPSbabel - hence removal of *html is done at beginning of the command sequence | |
244 | *cmd = g_strdup_printf( "rm -f ~/.geo/caches/*html ; %s -P -n%d -r%.1fM -u %s -p %s %s %s ; %s -z ~/.geo/caches/*html ", | |
245 | GC_PROGRAM1, | |
246 | gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(widgets->num_spin) ), | |
247 | gtk_spin_button_get_value_as_float ( GTK_SPIN_BUTTON(widgets->miles_radius_spin) ), | |
248 | safe_user, | |
249 | safe_pass, | |
250 | slat, slon, | |
251 | GC_PROGRAM2 ); | |
252 | *input_file_type = NULL; | |
253 | //g_free ( safe_string ); | |
254 | g_free ( safe_user ); | |
255 | g_free ( safe_pass ); | |
256 | g_free ( slat ); | |
257 | g_free ( slon ); | |
258 | } | |
259 | ||
260 | static void datasource_gc_cleanup ( datasource_gc_widgets_t *widgets ) | |
261 | { | |
262 | if ( widgets->circle_onscreen ) { | |
263 | vik_viewport_draw_arc ( widgets->vvp, widgets->circle_gc, FALSE, | |
264 | widgets->circle_x - widgets->circle_width/2, | |
265 | widgets->circle_y - widgets->circle_width/2, | |
266 | widgets->circle_width, widgets->circle_width, 0, 360*64 ); | |
267 | vik_viewport_sync( widgets->vvp ); | |
268 | } | |
269 | g_free ( widgets ); | |
270 | } | |
271 | #endif /* VIK_CONFIG_GEOCACHES */ |