]> git.street.me.uk Git - andy/viking.git/blame - src/datasource_gc.c
Fix: generate doc/reference/Makefile when needed
[andy/viking.git] / src / datasource_gc.c
CommitLineData
3333c069
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
a482007a 4 * Copyright (C) 2003-2007, Evan Battaglia <gtoevan@gmx.net>
3333c069
EB
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 */
8aad4ca3 21#ifdef HAVE_CONFIG_H
08a1fa12 22#include "config.h"
8aad4ca3 23#endif
1ef9e637 24#ifdef VIK_CONFIG_GEOCACHES
3333c069
EB
25#include <string.h>
26
4c77d5e0 27#include <glib/gi18n.h>
28c82d8b 28
3333c069
EB
29#include "viking.h"
30#include "babel.h"
31#include "gpx.h"
32#include "acquire.h"
a5c8699d
EB
33#include "preferences.h"
34
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."
39
3333c069
EB
40
41typedef struct {
42 GtkWidget *num_spin;
43 GtkWidget *center_entry;
28c82d8b
EB
44 GtkWidget *miles_radius_spin;
45
46 GdkGC *circle_gc;
47 VikViewport *vvp;
48 gboolean circle_onscreen;
49 gint circle_x, circle_y, circle_width;
3333c069
EB
50} datasource_gc_widgets_t;
51
52
65f0ccab
AF
53static gpointer datasource_gc_init ( );
54static void datasource_gc_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data );
28c82d8b
EB
55static void datasource_gc_get_cmd_string ( datasource_gc_widgets_t *widgets, gchar **cmd, gchar **input_file_type );
56static void datasource_gc_cleanup ( datasource_gc_widgets_t *widgets );
92255687 57static gchar *datasource_gc_check_existence ();
3333c069 58
28c82d8b
EB
59#define METERSPERMILE 1609.344
60
3333c069 61VikDataSourceInterface vik_datasource_gc_interface = {
4c77d5e0
GB
62 N_("Download Geocaches"),
63 N_("Geocaching.com Caches"),
3333c069
EB
64 VIK_DATASOURCE_SHELL_CMD,
65 VIK_DATASOURCE_ADDTOLAYER,
28c82d8b 66 VIK_DATASOURCE_INPUTTYPE_NONE,
d2525524 67 FALSE,
28c82d8b 68 TRUE,
65f0ccab 69 (VikDataSourceInitFunc) datasource_gc_init,
92255687 70 (VikDataSourceCheckExistenceFunc) datasource_gc_check_existence,
65f0ccab 71 (VikDataSourceAddSetupWidgetsFunc) datasource_gc_add_setup_widgets,
3333c069 72 (VikDataSourceGetCmdStringFunc) datasource_gc_get_cmd_string,
3333c069
EB
73 (VikDataSourceProgressFunc) NULL,
74 (VikDataSourceAddProgressWidgetsFunc) NULL,
2b756ea0
RN
75 (VikDataSourceCleanupFunc) datasource_gc_cleanup,
76 (VikDataSourceOffFunc) NULL,
3333c069
EB
77};
78
a5c8699d
EB
79static VikLayerParam prefs[] = {
80 { VIKING_GC_PARAMS_NAMESPACE "username", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("geocaching.com username:"), VIK_LAYER_WIDGET_ENTRY },
81 { VIKING_GC_PARAMS_NAMESPACE "password", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("geocaching.com password:"), VIK_LAYER_WIDGET_ENTRY },
82};
83
84void a_datasource_gc_init()
85{
86 a_preferences_register_group ( VIKING_GC_PARAMS_GROUP_KEY, "Geocaching" );
87
88 VikLayerParamData tmp;
89 tmp.s = "username";
90 a_preferences_register(prefs, tmp, VIKING_GC_PARAMS_GROUP_KEY);
91 tmp.s = "password";
92 a_preferences_register(prefs+1, tmp, VIKING_GC_PARAMS_GROUP_KEY);
93}
94
95
65f0ccab
AF
96static gpointer datasource_gc_init ( )
97{
98 datasource_gc_widgets_t *widgets = g_malloc(sizeof(*widgets));
99 return widgets;
100}
101
92255687
EB
102static gchar *datasource_gc_check_existence ()
103{
104 gchar *gcget_location = g_find_program_in_path("gcget");
105 if ( gcget_location ) {
106 g_free(gcget_location);
107 return NULL;
108 }
4c77d5e0 109 return g_strdup(_("Can't find gcget in path! Check that you have installed gcget correctly."));
92255687 110}
3333c069 111
28c82d8b
EB
112static void datasource_gc_draw_circle ( datasource_gc_widgets_t *widgets )
113{
114 gdouble lat, lon;
115 if ( widgets->circle_onscreen ) {
116 vik_viewport_draw_arc ( widgets->vvp, widgets->circle_gc, FALSE,
117 widgets->circle_x - widgets->circle_width/2,
118 widgets->circle_y - widgets->circle_width/2,
119 widgets->circle_width, widgets->circle_width, 0, 360*64 );
120 }
121 /* calculate widgets circle_x and circle_y */
122 /* split up lat,lon into lat and lon */
123 if ( 2 == sscanf ( gtk_entry_get_text ( GTK_ENTRY(widgets->center_entry) ), "%lf,%lf", &lat, &lon ) ) {
124 struct LatLon ll;
125 VikCoord c;
126 gint x, y;
127
128 ll.lat = lat; ll.lon = lon;
129 vik_coord_load_from_latlon ( &c, vik_viewport_get_coord_mode ( widgets->vvp ), &ll );
130 vik_viewport_coord_to_screen ( widgets->vvp, &c, &x, &y );
131 /* TODO: real calculation */
132 if ( x > -1000 && y > -1000 && x < (vik_viewport_get_width(widgets->vvp) + 1000) &&
133 y < (vik_viewport_get_width(widgets->vvp) + 1000) ) {
134 VikCoord c1, c2;
135 gdouble pixels_per_meter;
136
137 widgets->circle_x = x;
138 widgets->circle_y = y;
139
140 /* determine miles per pixel */
141 vik_viewport_screen_to_coord ( widgets->vvp, 0, vik_viewport_get_height(widgets->vvp)/2, &c1 );
142 vik_viewport_screen_to_coord ( widgets->vvp, vik_viewport_get_width(widgets->vvp), vik_viewport_get_height(widgets->vvp)/2, &c2 );
143 pixels_per_meter = ((gdouble)vik_viewport_get_width(widgets->vvp)) / vik_coord_diff(&c1, &c2);
144
145 /* this is approximate */
146 widgets->circle_width = gtk_spin_button_get_value_as_float ( GTK_SPIN_BUTTON(widgets->miles_radius_spin) )
147 * METERSPERMILE * pixels_per_meter * 2;
148
149 vik_viewport_draw_arc ( widgets->vvp, widgets->circle_gc, FALSE,
150 widgets->circle_x - widgets->circle_width/2,
151 widgets->circle_y - widgets->circle_width/2,
152 widgets->circle_width, widgets->circle_width, 0, 360*64 );
153
154 widgets->circle_onscreen = TRUE;
155 } else
156 widgets->circle_onscreen = FALSE;
157 }
158
159 /* see if onscreen */
160 /* okay */
161 vik_viewport_sync ( widgets->vvp );
162}
163
65f0ccab 164static void datasource_gc_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data )
3333c069 165{
65f0ccab 166 datasource_gc_widgets_t *widgets = (datasource_gc_widgets_t *)user_data;
28c82d8b 167 GtkWidget *num_label, *center_label, *miles_radius_label;
3333c069
EB
168 struct LatLon ll;
169 gchar *s_ll;
170
4c77d5e0 171 num_label = gtk_label_new (_("Number geocaches:"));
ac33062d 172 widgets->num_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new( 100, 1, 1000, 10, 20, 0 )), 25, 0 );
4c77d5e0 173 center_label = gtk_label_new (_("Centered around:"));
3333c069 174 widgets->center_entry = gtk_entry_new();
28c82d8b 175 miles_radius_label = gtk_label_new ("Miles Radius:");
ac33062d 176 widgets->miles_radius_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new( 100, 1, 1000, 5, 20, 0 )), 25, 2 );
3333c069
EB
177
178 vik_coord_to_latlon ( vik_viewport_get_center(vvp), &ll );
179 s_ll = g_strdup_printf("%f,%f", ll.lat, ll.lon );
180 gtk_entry_set_text ( GTK_ENTRY(widgets->center_entry), s_ll );
181 g_free ( s_ll );
182
28c82d8b
EB
183
184 widgets->vvp = vvp;
185 widgets->circle_gc = vik_viewport_new_gc ( vvp, "#000000", 3 );
186 gdk_gc_set_function ( widgets->circle_gc, GDK_INVERT );
187 widgets->circle_onscreen = FALSE;
188 datasource_gc_draw_circle ( widgets );
189
190 g_signal_connect_swapped ( G_OBJECT(widgets->center_entry), "changed", G_CALLBACK(datasource_gc_draw_circle), widgets );
191 g_signal_connect_swapped ( G_OBJECT(widgets->miles_radius_spin), "value-changed", G_CALLBACK(datasource_gc_draw_circle), widgets );
192
3333c069
EB
193 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), num_label, FALSE, FALSE, 5 );
194 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), widgets->num_spin, FALSE, FALSE, 5 );
195 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), center_label, FALSE, FALSE, 5 );
196 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), widgets->center_entry, FALSE, FALSE, 5 );
28c82d8b
EB
197 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), miles_radius_label, FALSE, FALSE, 5 );
198 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), widgets->miles_radius_spin, FALSE, FALSE, 5 );
3333c069 199 gtk_widget_show_all(dialog);
3333c069
EB
200}
201
28c82d8b 202static void datasource_gc_get_cmd_string ( datasource_gc_widgets_t *widgets, gchar **cmd, gchar **input_file_type )
3333c069 203{
92255687 204 gchar *safe_string = g_shell_quote ( gtk_entry_get_text ( GTK_ENTRY(widgets->center_entry) ) );
a5c8699d
EB
205 gchar *safe_user = g_shell_quote ( a_preferences_get ( VIKING_GC_PARAMS_NAMESPACE "username")->s );
206 gchar *safe_pass = g_shell_quote ( a_preferences_get ( VIKING_GC_PARAMS_NAMESPACE "password")->s );
391505be 207 *cmd = g_strdup_printf( "gcget -u %s -p %s %s %d %.2lf", safe_user, safe_pass, safe_string,
28c82d8b
EB
208 gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(widgets->num_spin) ),
209 gtk_spin_button_get_value_as_float ( GTK_SPIN_BUTTON(widgets->miles_radius_spin) ) );
210 *input_file_type = NULL;
92255687 211 g_free ( safe_string );
a5c8699d
EB
212 g_free ( safe_user );
213 g_free ( safe_pass );
3333c069
EB
214}
215
28c82d8b 216static void datasource_gc_cleanup ( datasource_gc_widgets_t *widgets )
3333c069 217{
28c82d8b
EB
218 if ( widgets->circle_onscreen ) {
219 vik_viewport_draw_arc ( widgets->vvp, widgets->circle_gc, FALSE,
220 widgets->circle_x - widgets->circle_width/2,
221 widgets->circle_y - widgets->circle_width/2,
222 widgets->circle_width, widgets->circle_width, 0, 360*64 );
223 vik_viewport_sync( widgets->vvp );
224 }
225 g_free ( widgets );
3333c069 226}
1ef9e637 227#endif /* VIK_CONFIG_GEOCACHES */