2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 * Copyright (C) 2013-2015, Rob Norris <rw_norris@hotmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <glib/gprintf.h>
29 #include <glib/gi18n.h>
36 /************************ FILTER LIST *******************/
37 // extern VikDataSourceInterface vik_datasource_gps_interface;
39 /*** Input is TRWLayer ***/
40 extern VikDataSourceInterface vik_datasource_bfilter_simplify_interface;
41 extern VikDataSourceInterface vik_datasource_bfilter_compress_interface;
42 extern VikDataSourceInterface vik_datasource_bfilter_dup_interface;
44 /*** Input is a track and a TRWLayer ***/
45 extern VikDataSourceInterface vik_datasource_bfilter_polygon_interface;
46 extern VikDataSourceInterface vik_datasource_bfilter_exclude_polygon_interface;
48 /*** Input is a track ***/
50 const VikDataSourceInterface *filters[] = {
51 &vik_datasource_bfilter_simplify_interface,
52 &vik_datasource_bfilter_compress_interface,
53 &vik_datasource_bfilter_dup_interface,
54 &vik_datasource_bfilter_polygon_interface,
55 &vik_datasource_bfilter_exclude_polygon_interface,
58 const guint N_FILTERS = sizeof(filters) / sizeof(filters[0]);
60 VikTrack *filter_track = NULL;
62 /********************************************************/
64 /* passed along to worker thread */
66 acq_dialog_widgets_t *w;
68 gboolean creating_new_layer;
74 /*********************************************************
75 * Definitions and routines for acquiring data from Data Sources in general
76 *********************************************************/
78 static void progress_func ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w )
80 if ( w->source_interface->is_thread ) {
83 if ( w->source_interface->cleanup_func )
84 w->source_interface->cleanup_func ( w->user_data );
86 g_thread_exit ( NULL );
91 if ( w->source_interface->progress_func )
92 w->source_interface->progress_func ( c, data, w );
96 * Some common things to do on completion of a datasource process
98 * . Update dialog info
99 * . Update main dsisplay
101 static void on_complete_process (w_and_interface_t *wi)
103 if (wi->w->running) {
104 gtk_label_set_text ( GTK_LABEL(wi->w->status), _("Done.") );
105 if ( wi->creating_new_layer ) {
106 /* Only create the layer if it actually contains anything useful */
107 // TODO: create function for this operation to hide detail:
108 if ( ! vik_trw_layer_is_empty ( wi->vtl ) ) {
109 vik_layer_post_read ( VIK_LAYER(wi->vtl), wi->w->vvp, TRUE );
110 vik_aggregate_layer_add_layer ( vik_layers_panel_get_top_layer(wi->w->vlp), VIK_LAYER(wi->vtl), TRUE );
113 gtk_label_set_text ( GTK_LABEL(wi->w->status), _("No data.") );
115 if ( wi->w->source_interface->keep_dialog_open ) {
116 gtk_dialog_set_response_sensitive ( GTK_DIALOG(wi->w->dialog), GTK_RESPONSE_ACCEPT, TRUE );
117 gtk_dialog_set_response_sensitive ( GTK_DIALOG(wi->w->dialog), GTK_RESPONSE_REJECT, FALSE );
119 gtk_dialog_response ( GTK_DIALOG(wi->w->dialog), GTK_RESPONSE_ACCEPT );
121 // Main display update
123 vik_layer_post_read ( VIK_LAYER(wi->vtl), wi->w->vvp, TRUE );
124 // View this data if desired - must be done after post read (so that the bounds are known)
125 if ( wi->w->source_interface->autoview ) {
126 vik_trw_layer_auto_set_view ( wi->vtl, vik_layers_panel_get_viewport(wi->w->vlp) );
128 vik_layers_panel_emit_update ( wi->w->vlp );
132 if ( wi->creating_new_layer )
133 g_object_unref(wi->vtl);
137 static void free_process_options ( ProcessOptions *po )
140 g_free ( po->babelargs );
141 g_free ( po->filename );
142 g_free ( po->input_file_type );
143 g_free ( po->babel_filters );
145 g_free ( po->shell_command );
150 /* this routine is the worker thread. there is only one simultaneous download allowed */
151 static void get_from_anything ( w_and_interface_t *wi )
153 gboolean result = TRUE;
155 VikDataSourceInterface *source_interface = wi->w->source_interface;
157 if ( source_interface->process_func ) {
158 result = source_interface->process_func ( wi->vtl, wi->po, (BabelStatusFunc)progress_func, wi->w, wi->options );
160 free_process_options ( wi->po );
161 g_free ( wi->options );
163 if (wi->w->running && !result) {
165 gtk_label_set_text ( GTK_LABEL(wi->w->status), _("Error: acquisition failed.") );
166 if ( wi->creating_new_layer )
167 g_object_unref ( G_OBJECT ( wi->vtl ) );
172 on_complete_process ( wi );
176 if ( source_interface->cleanup_func )
177 source_interface->cleanup_func ( wi->w->user_data );
179 if ( wi->w->running ) {
180 wi->w->running = FALSE;
188 g_thread_exit ( NULL );
191 /* depending on type of filter, often only vtl or track will be given.
192 * the other can be NULL.
194 static void acquire ( VikWindow *vw,
197 vik_datasource_mode_t mode,
198 VikDataSourceInterface *source_interface,
202 VikDataSourceCleanupFunc cleanup_function )
204 /* for manual dialogs */
205 GtkWidget *dialog = NULL;
207 gchar *args_off = NULL;
208 gchar *fd_off = NULL;
209 acq_dialog_widgets_t *w;
211 gpointer options = NULL;
217 avt.userdata = userdata;
220 gpointer pass_along_data;
221 VikLayerParamData *paramdatas = NULL;
223 w_and_interface_t *wi;
225 /*** INIT AND CHECK EXISTENCE ***/
226 if ( source_interface->init_func )
227 user_data = source_interface->init_func(&avt);
230 pass_along_data = user_data;
232 if ( source_interface->check_existence_func ) {
233 gchar *error_str = source_interface->check_existence_func();
235 a_dialog_error_msg ( GTK_WINDOW(vw), error_str );
236 g_free ( error_str );
241 /* BUILD UI & GET OPTIONS IF NECESSARY. */
243 /* POSSIBILITY 0: NO OPTIONS. DO NOTHING HERE. */
244 /* POSSIBILITY 1: ADD_SETUP_WIDGETS_FUNC */
245 if ( source_interface->add_setup_widgets_func ) {
246 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
248 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
249 GtkWidget *response_w = NULL;
250 #if GTK_CHECK_VERSION (2, 20, 0)
251 response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
254 source_interface->add_setup_widgets_func(dialog, vvp, user_data);
255 gtk_window_set_title ( GTK_WINDOW(dialog), _(source_interface->window_title) );
258 gtk_widget_grab_focus ( response_w );
260 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
261 source_interface->cleanup_func(user_data);
262 gtk_widget_destroy(dialog);
266 /* POSSIBILITY 2: UI BUILDER */
267 else if ( source_interface->params ) {
268 paramdatas = a_uibuilder_run_dialog ( source_interface->window_title, GTK_WINDOW(vw),
269 source_interface->params, source_interface->params_count,
270 source_interface->params_groups, source_interface->params_groups_count,
271 source_interface->params_defaults );
273 pass_along_data = paramdatas;
275 return; /* TODO: do we have to free anything here? */
278 /* CREATE INPUT DATA & GET OPTIONS */
279 ProcessOptions *po = g_malloc0 ( sizeof(ProcessOptions) );
281 if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRWLAYER ) {
282 gchar *name_src = a_gpx_write_tmp_file ( vtl, NULL );
284 source_interface->get_process_options_func ( pass_along_data, po, NULL, name_src, NULL );
286 util_add_to_deletion_list ( name_src );
289 } else if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRWLAYER_TRACK ) {
290 gchar *name_src = a_gpx_write_tmp_file ( vtl, NULL );
291 gchar *name_src_track = a_gpx_write_track_tmp_file ( track, NULL );
293 source_interface->get_process_options_func ( pass_along_data, po, NULL, name_src, name_src_track );
295 util_add_to_deletion_list ( name_src );
296 util_add_to_deletion_list ( name_src_track );
299 g_free ( name_src_track );
300 } else if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRACK ) {
301 gchar *name_src_track = a_gpx_write_track_tmp_file ( track, NULL );
303 source_interface->get_process_options_func ( pass_along_data, po, NULL, NULL, name_src_track );
305 g_free ( name_src_track );
306 } else if ( source_interface->get_process_options_func )
307 source_interface->get_process_options_func ( pass_along_data, po, &options, NULL, NULL );
309 /* Get data for Off command */
310 if ( source_interface->off_func ) {
311 source_interface->off_func ( pass_along_data, &args_off, &fd_off );
314 /* cleanup for option dialogs */
315 if ( source_interface->add_setup_widgets_func ) {
316 gtk_widget_destroy(dialog);
318 } else if ( source_interface->params ) {
319 a_uibuilder_free_paramdatas ( paramdatas, source_interface->params, source_interface->params_count );
322 w = g_malloc(sizeof(*w));
323 wi = g_malloc(sizeof(*wi));
325 wi->w->source_interface = source_interface;
327 wi->options = options;
329 wi->creating_new_layer = (!vtl); // Default if Auto Layer Management is passed in
331 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
332 gtk_dialog_set_response_sensitive ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT, FALSE );
333 gtk_window_set_title ( GTK_WINDOW(dialog), _(source_interface->window_title) );
337 status = gtk_label_new (_("Working..."));
338 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), status, FALSE, FALSE, 5 );
339 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
340 // May not want to see the dialog at all
341 if ( source_interface->is_thread || source_interface->keep_dialog_open )
342 gtk_widget_show_all(dialog);
348 if ( source_interface->add_progress_widgets_func ) {
349 source_interface->add_progress_widgets_func ( dialog, user_data );
351 w->user_data = user_data;
353 if ( mode == VIK_DATASOURCE_ADDTOLAYER ) {
354 VikLayer *current_selected = vik_layers_panel_get_selected ( w->vlp );
355 if ( IS_VIK_TRW_LAYER(current_selected) ) {
356 wi->vtl = VIK_TRW_LAYER(current_selected);
357 wi->creating_new_layer = FALSE;
360 else if ( mode == VIK_DATASOURCE_CREATENEWLAYER ) {
361 wi->creating_new_layer = TRUE;
363 else if ( mode == VIK_DATASOURCE_MANUAL_LAYER_MANAGEMENT ) {
364 // Don't create in acquire - as datasource will perform the necessary actions
365 wi->creating_new_layer = FALSE;
366 VikLayer *current_selected = vik_layers_panel_get_selected ( w->vlp );
367 if ( IS_VIK_TRW_LAYER(current_selected) )
368 wi->vtl = VIK_TRW_LAYER(current_selected);
370 if ( wi->creating_new_layer ) {
371 wi->vtl = VIK_TRW_LAYER ( vik_layer_create ( VIK_LAYER_TRW, w->vvp, FALSE ) );
372 vik_layer_rename ( VIK_LAYER ( wi->vtl ), _(source_interface->layer_title) );
375 if ( source_interface->is_thread ) {
376 if ( po->babelargs || po->url || po->shell_command ) {
377 #if GLIB_CHECK_VERSION (2, 32, 0)
378 g_thread_try_new ( "get_from_anything", (GThreadFunc)get_from_anything, wi, NULL );
380 g_thread_create ( (GThreadFunc)get_from_anything, wi, FALSE, NULL );
382 gtk_dialog_run ( GTK_DIALOG(dialog) );
384 // Cancel and mark for thread to finish
386 // NB Thread will free memory
390 ProcessOptions off_po = { args_off, fd_off, NULL, NULL, NULL };
391 a_babel_convert_from (NULL, &off_po, NULL, NULL, NULL);
397 // Thread finished by normal completion - free memory
403 // This shouldn't happen...
404 gtk_label_set_text ( GTK_LABEL(w->status), _("Unable to create command\nAcquire method failed.") );
405 gtk_dialog_run (GTK_DIALOG (dialog));
409 // bypass thread method malarkly - you'll just have to wait...
410 if ( source_interface->process_func ) {
411 gboolean result = source_interface->process_func ( wi->vtl, po, (BabelStatusFunc) progress_func, w, options );
413 a_dialog_msg ( GTK_WINDOW(vw), GTK_MESSAGE_ERROR, _("Error: acquisition failed."), NULL );
415 free_process_options ( po );
418 on_complete_process ( wi );
419 // Actually show it if necessary
420 if ( wi->w->source_interface->keep_dialog_open )
421 gtk_dialog_run ( GTK_DIALOG(dialog) );
427 gtk_widget_destroy ( dialog );
429 if ( cleanup_function )
430 cleanup_function ( source_interface );
435 * @vw: The #VikWindow to work with
436 * @vlp: The #VikLayersPanel in which a #VikTrwLayer layer may be created/appended
437 * @vvp: The #VikViewport defining the current view
438 * @mode: How layers should be managed
439 * @source_interface: The #VikDataSourceInterface determining how and what actions to take
440 * @userdata: External data to be passed into the #VikDataSourceInterface
441 * @cleanup_function: The function to dispose the #VikDataSourceInterface if necessary
443 * Process the given VikDataSourceInterface for sources with no input data.
445 void a_acquire ( VikWindow *vw,
448 vik_datasource_mode_t mode,
449 VikDataSourceInterface *source_interface,
451 VikDataSourceCleanupFunc cleanup_function )
453 acquire ( vw, vlp, vvp, mode, source_interface, NULL, NULL, userdata, cleanup_function );
456 static void acquire_trwlayer_callback ( GObject *menuitem, gpointer *pass_along )
458 VikDataSourceInterface *iface = g_object_get_data ( menuitem, "vik_acq_iface" );
459 VikWindow *vw = pass_along[0];
460 VikLayersPanel *vlp = pass_along[1];
461 VikViewport *vvp = pass_along[2];
462 VikTrwLayer *vtl = pass_along[3];
463 VikTrack *tr = pass_along[4];
465 acquire ( vw, vlp, vvp, iface->mode, iface, vtl, tr, NULL, NULL );
468 static GtkWidget *acquire_build_menu ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp,
469 VikTrwLayer *vtl, VikTrack *track, /* both passed to acquire, although for many filters only one ness */
470 const gchar *menu_title, vik_datasource_inputtype_t inputtype )
472 static gpointer pass_along[5];
473 GtkWidget *menu_item=NULL, *menu=NULL;
474 GtkWidget *item=NULL;
481 pass_along[4] = track;
483 for ( i = 0; i < N_FILTERS; i++ ) {
484 if ( filters[i]->inputtype == inputtype ) {
485 if ( ! menu_item ) { /* do this just once, but return NULL if no filters */
486 menu = gtk_menu_new();
487 menu_item = gtk_menu_item_new_with_mnemonic ( menu_title );
488 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu );
491 item = gtk_menu_item_new_with_label ( filters[i]->window_title );
492 g_object_set_data ( G_OBJECT(item), "vik_acq_iface", (gpointer) filters[i] );
493 g_signal_connect ( G_OBJECT(item), "activate", G_CALLBACK(acquire_trwlayer_callback), pass_along );
494 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
495 gtk_widget_show ( item );
503 * a_acquire_trwlayer_menu:
505 * Create a sub menu intended for rightclicking on a TRWLayer's menu called "Filter".
507 * Returns: %NULL if no filters.
509 GtkWidget *a_acquire_trwlayer_menu (VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikTrwLayer *vtl)
511 return acquire_build_menu ( vw, vlp, vvp, vtl, NULL, _("_Filter"), VIK_DATASOURCE_INPUTTYPE_TRWLAYER );
515 * a_acquire_trwlayer_track_menu:
517 * Create a sub menu intended for rightclicking on a TRWLayer's menu called "Filter with Track "TRACKNAME"...".
519 * Returns: %NULL if no filters or no filter track has been set.
521 GtkWidget *a_acquire_trwlayer_track_menu (VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikTrwLayer *vtl)
523 if ( filter_track == NULL )
526 gchar *menu_title = g_strdup_printf ( _("Filter with %s"), filter_track->name );
527 GtkWidget *rv = acquire_build_menu ( vw, vlp, vvp, vtl, filter_track,
528 menu_title, VIK_DATASOURCE_INPUTTYPE_TRWLAYER_TRACK );
529 g_free ( menu_title );
535 * a_acquire_track_menu:
537 * Create a sub menu intended for rightclicking on a track's menu called "Filter".
539 * Returns: %NULL if no applicable filters
541 GtkWidget *a_acquire_track_menu (VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikTrack *tr)
543 return acquire_build_menu ( vw, vlp, vvp, NULL, tr, _("Filter"), VIK_DATASOURCE_INPUTTYPE_TRACK );
547 * a_acquire_set_filter_track:
549 * Sets application-wide track to use with filter. references the track.
551 void a_acquire_set_filter_track ( VikTrack *tr )
554 vik_track_free ( filter_track );
557 vik_track_ref ( tr );