Fix loading a world file
[viking.git] / src / datasource_gc.c
blob21d653bfda04c8c9cd1a1d3001ec1a53bb63e229
1 /*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #ifdef VIK_CONFIG_GEOCACHES
25 #include <string.h>
27 #include <glib/gi18n.h>
29 #include "viking.h"
30 #include "babel.h"
31 #include "gpx.h"
32 #include "acquire.h"
33 #include "preferences.h"
35 /* params will be geocaching.username, geocaching.password */
36 /* we have to make sure these don't collide. */
37 #define VIKING_GC_PARAMS_GROUP_KEY "geocaching"
38 #define VIKING_GC_PARAMS_NAMESPACE "geocaching."
41 typedef struct {
42 GtkWidget *num_spin;
43 GtkWidget *center_entry;
44 GtkWidget *miles_radius_spin;
46 GdkGC *circle_gc;
47 VikViewport *vvp;
48 gboolean circle_onscreen;
49 gint circle_x, circle_y, circle_width;
50 } datasource_gc_widgets_t;
53 static gpointer datasource_gc_init ( );
54 static void datasource_gc_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data );
55 static void datasource_gc_get_cmd_string ( datasource_gc_widgets_t *widgets, gchar **cmd, gchar **input_file_type );
56 static void datasource_gc_cleanup ( datasource_gc_widgets_t *widgets );
57 static gchar *datasource_gc_check_existence ();
59 #define METERSPERMILE 1609.344
61 VikDataSourceInterface vik_datasource_gc_interface = {
62 N_("Download Geocaches"),
63 N_("Geocaching.com Caches"),
64 VIK_DATASOURCE_SHELL_CMD,
65 VIK_DATASOURCE_ADDTOLAYER,
66 VIK_DATASOURCE_INPUTTYPE_NONE,
67 TRUE,
68 (VikDataSourceInitFunc) datasource_gc_init,
69 (VikDataSourceCheckExistenceFunc) datasource_gc_check_existence,
70 (VikDataSourceAddSetupWidgetsFunc) datasource_gc_add_setup_widgets,
71 (VikDataSourceGetCmdStringFunc) datasource_gc_get_cmd_string,
72 (VikDataSourceProgressFunc) NULL,
73 (VikDataSourceAddProgressWidgetsFunc) NULL,
74 (VikDataSourceCleanupFunc) datasource_gc_cleanup
77 static VikLayerParam prefs[] = {
78 { VIKING_GC_PARAMS_NAMESPACE "username", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("geocaching.com username:"), VIK_LAYER_WIDGET_ENTRY },
79 { VIKING_GC_PARAMS_NAMESPACE "password", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("geocaching.com password:"), VIK_LAYER_WIDGET_ENTRY },
82 void a_datasource_gc_init()
84 a_preferences_register_group ( VIKING_GC_PARAMS_GROUP_KEY, "Geocaching" );
86 VikLayerParamData tmp;
87 tmp.s = "username";
88 a_preferences_register(prefs, tmp, VIKING_GC_PARAMS_GROUP_KEY);
89 tmp.s = "password";
90 a_preferences_register(prefs+1, tmp, VIKING_GC_PARAMS_GROUP_KEY);
94 static gpointer datasource_gc_init ( )
96 datasource_gc_widgets_t *widgets = g_malloc(sizeof(*widgets));
97 return widgets;
100 static gchar *datasource_gc_check_existence ()
102 gchar *gcget_location = g_find_program_in_path("gcget");
103 if ( gcget_location ) {
104 g_free(gcget_location);
105 return NULL;
107 return g_strdup(_("Can't find gcget in path! Check that you have installed gcget correctly."));
110 static void datasource_gc_draw_circle ( datasource_gc_widgets_t *widgets )
112 gdouble lat, lon;
113 if ( widgets->circle_onscreen ) {
114 vik_viewport_draw_arc ( widgets->vvp, widgets->circle_gc, FALSE,
115 widgets->circle_x - widgets->circle_width/2,
116 widgets->circle_y - widgets->circle_width/2,
117 widgets->circle_width, widgets->circle_width, 0, 360*64 );
119 /* calculate widgets circle_x and circle_y */
120 /* split up lat,lon into lat and lon */
121 if ( 2 == sscanf ( gtk_entry_get_text ( GTK_ENTRY(widgets->center_entry) ), "%lf,%lf", &lat, &lon ) ) {
122 struct LatLon ll;
123 VikCoord c;
124 gint x, y;
126 ll.lat = lat; ll.lon = lon;
127 vik_coord_load_from_latlon ( &c, vik_viewport_get_coord_mode ( widgets->vvp ), &ll );
128 vik_viewport_coord_to_screen ( widgets->vvp, &c, &x, &y );
129 /* TODO: real calculation */
130 if ( x > -1000 && y > -1000 && x < (vik_viewport_get_width(widgets->vvp) + 1000) &&
131 y < (vik_viewport_get_width(widgets->vvp) + 1000) ) {
132 VikCoord c1, c2;
133 gdouble pixels_per_meter;
135 widgets->circle_x = x;
136 widgets->circle_y = y;
138 /* determine miles per pixel */
139 vik_viewport_screen_to_coord ( widgets->vvp, 0, vik_viewport_get_height(widgets->vvp)/2, &c1 );
140 vik_viewport_screen_to_coord ( widgets->vvp, vik_viewport_get_width(widgets->vvp), vik_viewport_get_height(widgets->vvp)/2, &c2 );
141 pixels_per_meter = ((gdouble)vik_viewport_get_width(widgets->vvp)) / vik_coord_diff(&c1, &c2);
143 /* this is approximate */
144 widgets->circle_width = gtk_spin_button_get_value_as_float ( GTK_SPIN_BUTTON(widgets->miles_radius_spin) )
145 * METERSPERMILE * pixels_per_meter * 2;
147 vik_viewport_draw_arc ( widgets->vvp, widgets->circle_gc, FALSE,
148 widgets->circle_x - widgets->circle_width/2,
149 widgets->circle_y - widgets->circle_width/2,
150 widgets->circle_width, widgets->circle_width, 0, 360*64 );
152 widgets->circle_onscreen = TRUE;
153 } else
154 widgets->circle_onscreen = FALSE;
157 /* see if onscreen */
158 /* okay */
159 vik_viewport_sync ( widgets->vvp );
162 static void datasource_gc_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data )
164 datasource_gc_widgets_t *widgets = (datasource_gc_widgets_t *)user_data;
165 GtkWidget *num_label, *center_label, *miles_radius_label;
166 struct LatLon ll;
167 gchar *s_ll;
169 num_label = gtk_label_new (_("Number geocaches:"));
170 widgets->num_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new( 100, 1, 1000, 10, 20, 50 )), 25, 0 );
171 center_label = gtk_label_new (_("Centered around:"));
172 widgets->center_entry = gtk_entry_new();
173 miles_radius_label = gtk_label_new ("Miles Radius:");
174 widgets->miles_radius_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new( 100, 1, 1000, 5, 20, 50 )), 25, 2 );
176 vik_coord_to_latlon ( vik_viewport_get_center(vvp), &ll );
177 s_ll = g_strdup_printf("%f,%f", ll.lat, ll.lon );
178 gtk_entry_set_text ( GTK_ENTRY(widgets->center_entry), s_ll );
179 g_free ( s_ll );
182 widgets->vvp = vvp;
183 widgets->circle_gc = vik_viewport_new_gc ( vvp, "#000000", 3 );
184 gdk_gc_set_function ( widgets->circle_gc, GDK_INVERT );
185 widgets->circle_onscreen = FALSE;
186 datasource_gc_draw_circle ( widgets );
188 g_signal_connect_swapped ( G_OBJECT(widgets->center_entry), "changed", G_CALLBACK(datasource_gc_draw_circle), widgets );
189 g_signal_connect_swapped ( G_OBJECT(widgets->miles_radius_spin), "value-changed", G_CALLBACK(datasource_gc_draw_circle), widgets );
191 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), num_label, FALSE, FALSE, 5 );
192 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), widgets->num_spin, FALSE, FALSE, 5 );
193 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), center_label, FALSE, FALSE, 5 );
194 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), widgets->center_entry, FALSE, FALSE, 5 );
195 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), miles_radius_label, FALSE, FALSE, 5 );
196 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), widgets->miles_radius_spin, FALSE, FALSE, 5 );
197 gtk_widget_show_all(dialog);
200 static void datasource_gc_get_cmd_string ( datasource_gc_widgets_t *widgets, gchar **cmd, gchar **input_file_type )
202 gchar *safe_string = g_shell_quote ( gtk_entry_get_text ( GTK_ENTRY(widgets->center_entry) ) );
203 gchar *safe_user = g_shell_quote ( a_preferences_get ( VIKING_GC_PARAMS_NAMESPACE "username")->s );
204 gchar *safe_pass = g_shell_quote ( a_preferences_get ( VIKING_GC_PARAMS_NAMESPACE "password")->s );
205 *cmd = g_strdup_printf( "gcget -u %s -p -- %s %s %d %.2lf", safe_user, safe_pass, safe_string,
206 gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(widgets->num_spin) ),
207 gtk_spin_button_get_value_as_float ( GTK_SPIN_BUTTON(widgets->miles_radius_spin) ) );
208 *input_file_type = NULL;
209 g_free ( safe_string );
210 g_free ( safe_user );
211 g_free ( safe_pass );
214 static void datasource_gc_cleanup ( datasource_gc_widgets_t *widgets )
216 if ( widgets->circle_onscreen ) {
217 vik_viewport_draw_arc ( widgets->vvp, widgets->circle_gc, FALSE,
218 widgets->circle_x - widgets->circle_width/2,
219 widgets->circle_y - widgets->circle_width/2,
220 widgets->circle_width, widgets->circle_width, 0, 360*64 );
221 vik_viewport_sync( widgets->vvp );
223 g_free ( widgets );
225 #endif /* VIK_CONFIG_GEOCACHES */