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
22 #include <glib/gprintf.h>
29 /* passed along to worker thread */
31 acq_dialog_widgets_t *w;
36 extern VikDataSourceInterface vik_datasource_gps_interface;
37 extern VikDataSourceInterface vik_datasource_google_interface;
39 /*********************************************************
40 * Definitions and routines for acquiring data from Data Sources in general
41 *********************************************************/
43 static void progress_func ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w )
48 if ( w->interface->cleanup_func )
49 w->interface->cleanup_func( w->specific_data );
51 g_thread_exit ( NULL );
55 if ( w->interface->progress_func )
56 w->interface->progress_func ( (gpointer) c, data, w );
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 )
63 gchar *extra = wi->extra;
67 gboolean creating_new_layer = TRUE;
69 acq_dialog_widgets_t *w = wi->w;
70 VikDataSourceInterface *interface = wi->w->interface;
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;
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..." );
88 if ( interface->type == VIK_DATASOURCE_GPSBABEL_DIRECT )
89 result = a_babel_convert_from (vtl, cmd, (BabelStatusFunc) progress_func, extra, w);
91 result = a_babel_convert_from_shellcommand ( vtl, cmd, extra, (BabelStatusFunc) progress_func, w);
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 ) );
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 );
113 if ( creating_new_layer )
117 if ( interface->cleanup_func )
118 interface->cleanup_func ( w->specific_data );
127 g_thread_exit ( NULL );
131 void a_acquire ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikDataSourceInterface *interface )
133 GtkWidget *dialog = NULL;
136 acq_dialog_widgets_t *w;
138 w_and_interface_t *wi;
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);
149 interface->get_cmd_string_func ( first_dialog_data, &cmd, &extra );
150 interface->first_cleanup_func(first_dialog_data);
151 gtk_widget_destroy(dialog);
154 interface->get_cmd_string_func ( NULL, &cmd, &extra );
159 w = g_malloc(sizeof(*w));
160 wi = g_malloc(sizeof(*wi));
162 wi->w->interface = interface;
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 );
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);
181 if ( interface->add_progress_widgets_func )
182 w->specific_data = interface->add_progress_widgets_func ( dialog );
184 w->specific_data = NULL;
186 g_thread_create((GThreadFunc)get_from_anything, wi, FALSE, NULL );
188 gtk_dialog_run ( GTK_DIALOG(dialog) );
190 w->ok = FALSE; /* tell thread to stop. TODO: add mutex */
192 g_free ( w ); /* thread has finished; free w */
194 gtk_widget_destroy ( dialog );