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
)
47 if ( w
->interface
->cleanup_func
)
48 w
->interface
->cleanup_func( w
->user_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
->user_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
;
139 w_and_interface_t
*wi
;
141 g_assert(interface
->init_func
);
142 user_data
= interface
->init_func();
144 if ( interface
->check_existence_func
) {
145 gchar
*error_str
= interface
->check_existence_func();
147 a_dialog_error_msg ( GTK_WINDOW(vw
), error_str
);
148 g_free ( error_str
);
153 if ( interface
->add_setup_widgets_func
) {
154 dialog
= gtk_dialog_new_with_buttons ( "", NULL
, 0, GTK_STOCK_OK
, GTK_RESPONSE_ACCEPT
, GTK_STOCK_CANCEL
, GTK_RESPONSE_REJECT
, NULL
);
156 interface
->add_setup_widgets_func(dialog
, vvp
, user_data
);
157 gtk_window_set_title ( GTK_WINDOW(dialog
), interface
->window_title
);
159 if ( gtk_dialog_run ( GTK_DIALOG(dialog
) ) != GTK_RESPONSE_ACCEPT
) {
160 interface
->cleanup_func(user_data
);
161 gtk_widget_destroy(dialog
);
164 interface
->get_cmd_string_func ( user_data
, &cmd
, &extra
);
165 gtk_widget_destroy(dialog
);
168 interface
->get_cmd_string_func ( user_data
, &cmd
, &extra
);
173 w
= g_malloc(sizeof(*w
));
174 wi
= g_malloc(sizeof(*wi
));
176 wi
->w
->interface
= interface
;
180 dialog
= gtk_dialog_new_with_buttons ( "", NULL
, 0, GTK_STOCK_OK
, GTK_RESPONSE_ACCEPT
, GTK_STOCK_CANCEL
, GTK_RESPONSE_REJECT
, NULL
);
181 gtk_dialog_set_response_sensitive ( GTK_DIALOG(dialog
), GTK_RESPONSE_ACCEPT
, FALSE
);
182 gtk_window_set_title ( GTK_WINDOW(dialog
), interface
->window_title
);
187 status
= gtk_label_new ("Status: detecting gpsbabel");
188 gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog
)->vbox
), status
, FALSE
, FALSE
, 5 );
189 gtk_widget_show_all(status
);
195 if ( interface
->add_progress_widgets_func
) {
196 interface
->add_progress_widgets_func ( dialog
, user_data
);
198 w
->user_data
= user_data
;
201 g_thread_create((GThreadFunc
)get_from_anything
, wi
, FALSE
, NULL
);
203 gtk_dialog_run ( GTK_DIALOG(dialog
) );
205 w
->ok
= FALSE
; /* tell thread to stop. TODO: add mutex */
207 g_free ( w
); /* thread has finished; free w */
209 gtk_widget_destroy ( dialog
);