]> git.street.me.uk Git - andy/viking.git/blob - src/datasource_gc.c
Mark many strings translatable
[andy/viking.git] / src / datasource_gc.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, 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 #include "config.h"
22 #ifdef VIK_CONFIG_GEOCACHES
23 #include <string.h>
24
25 #include <glib/gi18n.h>
26
27 #include "viking.h"
28 #include "babel.h"
29 #include "gpx.h"
30 #include "acquire.h"
31
32 typedef struct {
33   GtkWidget *num_spin;
34   GtkWidget *center_entry;
35   GtkWidget *miles_radius_spin;
36
37   GdkGC *circle_gc;
38   VikViewport *vvp;
39   gboolean circle_onscreen;
40   gint circle_x, circle_y, circle_width;
41 } datasource_gc_widgets_t;
42
43
44 static gpointer datasource_gc_init ( );
45 static void datasource_gc_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data );
46 static void datasource_gc_get_cmd_string ( datasource_gc_widgets_t *widgets, gchar **cmd, gchar **input_file_type );    
47 static void datasource_gc_cleanup ( datasource_gc_widgets_t *widgets );
48 static gchar *datasource_gc_check_existence ();
49
50 #define METERSPERMILE 1609.344
51
52 VikDataSourceInterface vik_datasource_gc_interface = {
53   N_("Download Geocaches"),
54   N_("Geocaching.com Caches"),
55   VIK_DATASOURCE_SHELL_CMD,
56   VIK_DATASOURCE_ADDTOLAYER,
57   VIK_DATASOURCE_INPUTTYPE_NONE,
58   TRUE,
59   (VikDataSourceInitFunc)               datasource_gc_init,
60   (VikDataSourceCheckExistenceFunc)     datasource_gc_check_existence,
61   (VikDataSourceAddSetupWidgetsFunc)    datasource_gc_add_setup_widgets,
62   (VikDataSourceGetCmdStringFunc)       datasource_gc_get_cmd_string,
63   (VikDataSourceProgressFunc)           NULL,
64   (VikDataSourceAddProgressWidgetsFunc) NULL,
65   (VikDataSourceCleanupFunc)            datasource_gc_cleanup
66 };
67
68 static gpointer datasource_gc_init ( )
69 {
70   datasource_gc_widgets_t *widgets = g_malloc(sizeof(*widgets));
71   return widgets;
72 }
73
74 static gchar *datasource_gc_check_existence ()
75 {
76   gchar *gcget_location = g_find_program_in_path("gcget");
77   if ( gcget_location ) {
78     g_free(gcget_location);
79     return NULL;
80   }
81   return g_strdup(_("Can't find gcget in path! Check that you have installed gcget correctly."));
82 }
83
84 static void datasource_gc_draw_circle ( datasource_gc_widgets_t *widgets )
85 {
86   gdouble lat, lon;
87   if ( widgets->circle_onscreen ) {
88     vik_viewport_draw_arc ( widgets->vvp, widgets->circle_gc, FALSE,
89                 widgets->circle_x - widgets->circle_width/2,
90                 widgets->circle_y - widgets->circle_width/2,
91                 widgets->circle_width, widgets->circle_width, 0, 360*64 );
92   }
93   /* calculate widgets circle_x and circle_y */
94   /* split up lat,lon into lat and lon */
95   if ( 2 == sscanf ( gtk_entry_get_text ( GTK_ENTRY(widgets->center_entry) ), "%lf,%lf", &lat, &lon ) ) {
96     struct LatLon ll;
97     VikCoord c;
98     gint x, y;
99
100     ll.lat = lat; ll.lon = lon;
101     vik_coord_load_from_latlon ( &c, vik_viewport_get_coord_mode ( widgets->vvp ), &ll );
102     vik_viewport_coord_to_screen ( widgets->vvp, &c, &x, &y );
103     /* TODO: real calculation */
104     if ( x > -1000 && y > -1000 && x < (vik_viewport_get_width(widgets->vvp) + 1000) &&
105         y < (vik_viewport_get_width(widgets->vvp) + 1000) ) {
106       VikCoord c1, c2;
107       gdouble pixels_per_meter;
108
109       widgets->circle_x = x;
110       widgets->circle_y = y;
111
112       /* determine miles per pixel */
113       vik_viewport_screen_to_coord ( widgets->vvp, 0, vik_viewport_get_height(widgets->vvp)/2, &c1 );
114       vik_viewport_screen_to_coord ( widgets->vvp, vik_viewport_get_width(widgets->vvp), vik_viewport_get_height(widgets->vvp)/2, &c2 );
115       pixels_per_meter = ((gdouble)vik_viewport_get_width(widgets->vvp)) / vik_coord_diff(&c1, &c2);
116
117       /* this is approximate */
118       widgets->circle_width = gtk_spin_button_get_value_as_float ( GTK_SPIN_BUTTON(widgets->miles_radius_spin) )
119                 * METERSPERMILE * pixels_per_meter * 2;
120
121       vik_viewport_draw_arc ( widgets->vvp, widgets->circle_gc, FALSE,
122                 widgets->circle_x - widgets->circle_width/2,
123                 widgets->circle_y - widgets->circle_width/2,
124                 widgets->circle_width, widgets->circle_width, 0, 360*64 );
125
126       widgets->circle_onscreen = TRUE;
127     } else
128       widgets->circle_onscreen = FALSE;
129   }
130
131   /* see if onscreen */
132   /* okay */
133   vik_viewport_sync ( widgets->vvp );
134 }
135
136 static void datasource_gc_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data )
137 {
138   datasource_gc_widgets_t *widgets = (datasource_gc_widgets_t *)user_data;
139   GtkWidget *num_label, *center_label, *miles_radius_label;
140   struct LatLon ll;
141   gchar *s_ll;
142
143   num_label = gtk_label_new (_("Number geocaches:"));
144   widgets->num_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new( 100, 1, 1000, 10, 20, 50 )), 25, 0 );
145   center_label = gtk_label_new (_("Centered around:"));
146   widgets->center_entry = gtk_entry_new();
147   miles_radius_label = gtk_label_new ("Miles Radius:");
148   widgets->miles_radius_spin = gtk_spin_button_new ( GTK_ADJUSTMENT(gtk_adjustment_new( 100, 1, 1000, 5, 20, 50 )), 25, 2 );
149
150   vik_coord_to_latlon ( vik_viewport_get_center(vvp), &ll );
151   s_ll = g_strdup_printf("%f,%f", ll.lat, ll.lon );
152   gtk_entry_set_text ( GTK_ENTRY(widgets->center_entry), s_ll );
153   g_free ( s_ll );
154
155
156   widgets->vvp = vvp;
157   widgets->circle_gc = vik_viewport_new_gc ( vvp, "#000000", 3 );
158   gdk_gc_set_function ( widgets->circle_gc, GDK_INVERT );
159   widgets->circle_onscreen = FALSE;
160   datasource_gc_draw_circle ( widgets );
161
162   g_signal_connect_swapped ( G_OBJECT(widgets->center_entry), "changed", G_CALLBACK(datasource_gc_draw_circle), widgets );
163   g_signal_connect_swapped ( G_OBJECT(widgets->miles_radius_spin), "value-changed", G_CALLBACK(datasource_gc_draw_circle), widgets );
164
165   gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), num_label, FALSE, FALSE, 5 );
166   gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), widgets->num_spin, FALSE, FALSE, 5 );
167   gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), center_label, FALSE, FALSE, 5 );
168   gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), widgets->center_entry, FALSE, FALSE, 5 );
169   gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), miles_radius_label, FALSE, FALSE, 5 );
170   gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), widgets->miles_radius_spin, FALSE, FALSE, 5 );
171   gtk_widget_show_all(dialog);
172 }
173
174 static void datasource_gc_get_cmd_string ( datasource_gc_widgets_t *widgets, gchar **cmd, gchar **input_file_type )
175 {
176   gchar *safe_string = g_shell_quote ( gtk_entry_get_text ( GTK_ENTRY(widgets->center_entry) ) );
177   *cmd = g_strdup_printf( "gcget %s %d %.2lf", safe_string, 
178         gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(widgets->num_spin) ),
179         gtk_spin_button_get_value_as_float ( GTK_SPIN_BUTTON(widgets->miles_radius_spin) ) );
180   *input_file_type = NULL;
181   g_free ( safe_string );
182 }
183
184 static void datasource_gc_cleanup ( datasource_gc_widgets_t *widgets )
185 {
186   if ( widgets->circle_onscreen ) {
187     vik_viewport_draw_arc ( widgets->vvp, widgets->circle_gc, FALSE,
188                 widgets->circle_x - widgets->circle_width/2,
189                 widgets->circle_y - widgets->circle_width/2,
190                 widgets->circle_width, widgets->circle_width, 0, 360*64 );
191     vik_viewport_sync( widgets->vvp );
192   }
193   g_free ( widgets );
194 }
195 #endif /* VIK_CONFIG_GEOCACHES */