]> git.street.me.uk Git - andy/viking.git/blob - src/acquire.c
Geocaching.com (via gcget) data source
[andy/viking.git] / src / acquire.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 <string.h>
22 #include <glib/gprintf.h>
23
24 #include "viking.h"
25 #include "babel.h"
26 #include "gpx.h"
27 #include "acquire.h"
28
29 /* passed along to worker thread */
30 typedef struct {
31   acq_dialog_widgets_t *w;
32   gchar *cmd;
33   gchar *extra;
34 } w_and_interface_t;
35
36 extern VikDataSourceInterface vik_datasource_gps_interface;
37 extern VikDataSourceInterface vik_datasource_google_interface;
38
39 /*********************************************************
40  * Definitions and routines for acquiring data from Data Sources in general
41  *********************************************************/
42
43 static void progress_func ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w )
44 {
45   gdk_threads_enter ();
46   if (!w->ok) {
47     g_free ( w );
48     if ( w->interface->cleanup_func )
49       w->interface->cleanup_func( w->specific_data );
50     gdk_threads_leave();
51     g_thread_exit ( NULL );
52   }
53   gdk_threads_leave ();
54
55   if ( w->interface->progress_func )
56     w->interface->progress_func ( (gpointer) c, data, w );
57 }
58
59 /* this routine is the worker thread.  there is only one simultaneous download allowed */
60 static void get_from_anything ( w_and_interface_t *wi )
61 {
62   gchar *cmd = wi->cmd;
63   gchar *extra = wi->extra;
64   gboolean result;
65   VikTrwLayer *vtl;
66
67   gboolean creating_new_layer = TRUE;
68
69   acq_dialog_widgets_t *w = wi->w;
70   VikDataSourceInterface *interface = wi->w->interface;
71   g_free ( wi );
72
73   gdk_threads_enter();
74   if (interface->mode == VIK_DATASOURCE_ADDTOLAYER) {
75     VikLayer *current_selected = vik_layers_panel_get_selected ( w->vlp );
76     if ( IS_VIK_TRW_LAYER(current_selected) ) {
77       vtl = VIK_TRW_LAYER(current_selected);
78       creating_new_layer = FALSE;
79     }
80   }
81   if ( creating_new_layer ) {
82     vtl = VIK_TRW_LAYER ( vik_layer_create ( VIK_LAYER_TRW, w->vvp, NULL, FALSE ) );
83     vik_layer_rename ( VIK_LAYER ( vtl ), interface->layer_title );
84     gtk_label_set_text ( GTK_LABEL(w->status), "Working..." );
85   }
86   gdk_threads_leave();
87
88   if ( interface->type == VIK_DATASOURCE_GPSBABEL_DIRECT )
89     result = a_babel_convert_from (vtl, cmd, (BabelStatusFunc) progress_func, extra, w);
90   else
91     result = a_babel_convert_from_shellcommand ( vtl, cmd, extra, (BabelStatusFunc) progress_func, w);
92
93   g_free ( cmd );
94   g_free ( extra );
95
96   if (!result) {
97     gdk_threads_enter();
98     gtk_label_set_text ( GTK_LABEL(w->status), "Error: couldn't find gpsbabel." );
99     if ( creating_new_layer )
100       g_object_unref ( G_OBJECT ( vtl ) );
101     gdk_threads_leave();
102   } 
103
104   gdk_threads_enter();
105   if (w->ok) {
106     gtk_label_set_text ( GTK_LABEL(w->status), "Done." );
107     if ( creating_new_layer )
108     vik_aggregate_layer_add_layer( vik_layers_panel_get_top_layer(w->vlp), VIK_LAYER(vtl));
109     gtk_dialog_set_response_sensitive ( GTK_DIALOG(w->dialog), GTK_RESPONSE_ACCEPT, TRUE );
110     gtk_dialog_set_response_sensitive ( GTK_DIALOG(w->dialog), GTK_RESPONSE_REJECT, FALSE );
111   } else {
112     /* canceled */
113     if ( creating_new_layer )
114       g_object_unref(vtl);
115   }
116
117   if ( interface->cleanup_func )
118     interface->cleanup_func ( w->specific_data );
119
120   if ( w->ok ) {
121     w->ok = FALSE;
122   } else {
123     g_free ( w );
124   }
125
126   gdk_threads_leave();
127   g_thread_exit ( NULL );
128 }
129
130
131 void a_acquire ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikDataSourceInterface *interface )
132 {
133   GtkWidget *dialog = NULL;
134   GtkWidget *status;
135   gchar *cmd, *extra;
136   acq_dialog_widgets_t *w;
137
138   w_and_interface_t *wi;
139
140   if ( interface->add_widgets_func ) {
141     gpointer first_dialog_data;
142     dialog = gtk_dialog_new_with_buttons ( "", NULL, 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
143     first_dialog_data = interface->add_widgets_func(dialog, vvp);
144     if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
145       interface->first_cleanup_func(first_dialog_data);
146       gtk_widget_destroy(dialog);
147       return;
148     }
149     interface->get_cmd_string_func ( first_dialog_data, &cmd, &extra );
150     interface->first_cleanup_func(first_dialog_data);
151     gtk_widget_destroy(dialog);
152     dialog = NULL;
153   } else
154     interface->get_cmd_string_func ( NULL, &cmd, &extra );
155
156   if ( ! cmd )
157     return;
158
159   w = g_malloc(sizeof(*w));
160   wi = g_malloc(sizeof(*wi));
161   wi->w = w;
162   wi->w->interface = interface;
163   wi->cmd = cmd;
164   wi->extra = extra;
165
166   dialog = gtk_dialog_new_with_buttons ( "", NULL, 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
167   gtk_dialog_set_response_sensitive ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT, FALSE );
168
169
170   w->dialog = dialog;
171   w->ok = TRUE;
172
173   status = gtk_label_new ("Status: detecting gpsbabel");
174   gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), status, FALSE, FALSE, 5 );
175   gtk_widget_show_all(status);
176   w->status = status;
177
178   w->vw = vw;
179   w->vlp = vlp;
180   w->vvp = vvp;
181   if ( interface->add_progress_widgets_func )
182     w->specific_data = interface->add_progress_widgets_func ( dialog );
183   else
184     w->specific_data = NULL;
185
186   g_thread_create((GThreadFunc)get_from_anything, wi, FALSE, NULL );
187
188   gtk_dialog_run ( GTK_DIALOG(dialog) );
189   if ( w->ok )
190     w->ok = FALSE; /* tell thread to stop. TODO: add mutex */
191   else {
192     g_free ( w ); /* thread has finished; free w */
193   }
194   gtk_widget_destroy ( dialog );
195 }
196