Add missing trailing dot in sentences
[viking/guyou.git] / src / acquire.c
blob3581689b5c82ab1c42e2cf84aa8546f97cea8699
1 /*
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
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <stdio.h>
27 #include <string.h>
28 #include <glib/gprintf.h>
29 #include <glib/gi18n.h>
31 #include "viking.h"
32 #include "babel.h"
33 #include "gpx.h"
34 #include "acquire.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;
43 extern VikDataSourceInterface vik_datasource_bfilter_manual_interface;
45 /*** Input is a track and a TRWLayer ***/
46 extern VikDataSourceInterface vik_datasource_bfilter_polygon_interface;
47 extern VikDataSourceInterface vik_datasource_bfilter_exclude_polygon_interface;
49 /*** Input is a track ***/
51 const VikDataSourceInterface *filters[] = {
52 &vik_datasource_bfilter_simplify_interface,
53 &vik_datasource_bfilter_compress_interface,
54 &vik_datasource_bfilter_dup_interface,
55 &vik_datasource_bfilter_manual_interface,
56 &vik_datasource_bfilter_polygon_interface,
57 &vik_datasource_bfilter_exclude_polygon_interface,
60 const guint N_FILTERS = sizeof(filters) / sizeof(filters[0]);
62 VikTrack *filter_track = NULL;
64 /********************************************************/
66 /* passed along to worker thread */
67 typedef struct {
68 acq_dialog_widgets_t *w;
69 ProcessOptions *po;
70 gboolean creating_new_layer;
71 VikTrwLayer *vtl;
72 DownloadFileOptions *options;
73 } w_and_interface_t;
76 /*********************************************************
77 * Definitions and routines for acquiring data from Data Sources in general
78 *********************************************************/
80 static void progress_func ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w )
82 if ( w->source_interface->is_thread ) {
83 gdk_threads_enter ();
84 if ( !w->running ) {
85 if ( w->source_interface->cleanup_func )
86 w->source_interface->cleanup_func ( w->user_data );
87 gdk_threads_leave ();
88 g_thread_exit ( NULL );
90 gdk_threads_leave ();
93 if ( w->source_interface->progress_func )
94 w->source_interface->progress_func ( c, data, w );
97 /**
98 * Some common things to do on completion of a datasource process
99 * . Update layer
100 * . Update dialog info
101 * . Update main dsisplay
103 static void on_complete_process (w_and_interface_t *wi)
105 if (wi->w->running) {
106 gtk_label_set_text ( GTK_LABEL(wi->w->status), _("Done.") );
107 if ( wi->creating_new_layer ) {
108 /* Only create the layer if it actually contains anything useful */
109 // TODO: create function for this operation to hide detail:
110 if ( ! vik_trw_layer_is_empty ( wi->vtl ) ) {
111 vik_layer_post_read ( VIK_LAYER(wi->vtl), wi->w->vvp, TRUE );
112 vik_aggregate_layer_add_layer ( vik_layers_panel_get_top_layer(wi->w->vlp), VIK_LAYER(wi->vtl), TRUE );
114 else
115 gtk_label_set_text ( GTK_LABEL(wi->w->status), _("No data.") );
117 if ( wi->w->source_interface->keep_dialog_open ) {
118 gtk_dialog_set_response_sensitive ( GTK_DIALOG(wi->w->dialog), GTK_RESPONSE_ACCEPT, TRUE );
119 gtk_dialog_set_response_sensitive ( GTK_DIALOG(wi->w->dialog), GTK_RESPONSE_REJECT, FALSE );
120 } else {
121 gtk_dialog_response ( GTK_DIALOG(wi->w->dialog), GTK_RESPONSE_ACCEPT );
123 // Main display update
124 if ( wi->vtl ) {
125 vik_layer_post_read ( VIK_LAYER(wi->vtl), wi->w->vvp, TRUE );
126 // View this data if desired - must be done after post read (so that the bounds are known)
127 if ( wi->w->source_interface->autoview ) {
128 vik_trw_layer_auto_set_view ( wi->vtl, vik_layers_panel_get_viewport(wi->w->vlp) );
130 vik_layers_panel_emit_update ( wi->w->vlp );
132 } else {
133 /* cancelled */
134 if ( wi->creating_new_layer )
135 g_object_unref(wi->vtl);
139 static void free_process_options ( ProcessOptions *po )
141 if ( po ) {
142 g_free ( po->babelargs );
143 g_free ( po->filename );
144 g_free ( po->input_file_type );
145 g_free ( po->babel_filters );
146 g_free ( po->url );
147 g_free ( po->shell_command );
148 g_free ( po );
152 /* this routine is the worker thread. there is only one simultaneous download allowed */
153 static void get_from_anything ( w_and_interface_t *wi )
155 gboolean result = TRUE;
157 VikDataSourceInterface *source_interface = wi->w->source_interface;
159 if ( source_interface->process_func ) {
160 result = source_interface->process_func ( wi->vtl, wi->po, (BabelStatusFunc)progress_func, wi->w, wi->options );
162 free_process_options ( wi->po );
163 g_free ( wi->options );
165 if (wi->w->running && !result) {
166 gdk_threads_enter();
167 gtk_label_set_text ( GTK_LABEL(wi->w->status), _("Error: acquisition failed.") );
168 if ( wi->creating_new_layer )
169 g_object_unref ( G_OBJECT ( wi->vtl ) );
170 gdk_threads_leave();
172 else {
173 gdk_threads_enter();
174 on_complete_process ( wi );
175 gdk_threads_leave();
178 if ( source_interface->cleanup_func )
179 source_interface->cleanup_func ( wi->w->user_data );
181 if ( wi->w->running ) {
182 wi->w->running = FALSE;
184 else {
185 g_free ( wi->w );
186 g_free ( wi );
187 wi = NULL;
190 g_thread_exit ( NULL );
193 /* depending on type of filter, often only vtl or track will be given.
194 * the other can be NULL.
196 static void acquire ( VikWindow *vw,
197 VikLayersPanel *vlp,
198 VikViewport *vvp,
199 vik_datasource_mode_t mode,
200 VikDataSourceInterface *source_interface,
201 VikTrwLayer *vtl,
202 VikTrack *track,
203 gpointer userdata,
204 VikDataSourceCleanupFunc cleanup_function )
206 /* for manual dialogs */
207 GtkWidget *dialog = NULL;
208 GtkWidget *status;
209 gchar *args_off = NULL;
210 gchar *fd_off = NULL;
211 acq_dialog_widgets_t *w;
212 gpointer user_data;
213 DownloadFileOptions *options = g_malloc0 ( sizeof(DownloadFileOptions) );
215 acq_vik_t avt;
216 avt.vlp = vlp;
217 avt.vvp = vvp;
218 avt.vw = vw;
219 avt.userdata = userdata;
221 /* for UI builder */
222 gpointer pass_along_data;
223 VikLayerParamData *paramdatas = NULL;
225 w_and_interface_t *wi;
227 /*** INIT AND CHECK EXISTENCE ***/
228 if ( source_interface->init_func )
229 user_data = source_interface->init_func(&avt);
230 else
231 user_data = NULL;
232 pass_along_data = user_data;
234 if ( source_interface->check_existence_func ) {
235 gchar *error_str = source_interface->check_existence_func();
236 if ( error_str ) {
237 a_dialog_error_msg ( GTK_WINDOW(vw), error_str );
238 g_free ( error_str );
239 return;
243 /* BUILD UI & GET OPTIONS IF NECESSARY. */
245 /* POSSIBILITY 0: NO OPTIONS. DO NOTHING HERE. */
246 /* POSSIBILITY 1: ADD_SETUP_WIDGETS_FUNC */
247 if ( source_interface->add_setup_widgets_func ) {
248 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
250 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
251 GtkWidget *response_w = NULL;
252 #if GTK_CHECK_VERSION (2, 20, 0)
253 response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
254 #endif
256 source_interface->add_setup_widgets_func(dialog, vvp, user_data);
257 gtk_window_set_title ( GTK_WINDOW(dialog), _(source_interface->window_title) );
259 if ( response_w )
260 gtk_widget_grab_focus ( response_w );
262 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
263 source_interface->cleanup_func(user_data);
264 gtk_widget_destroy(dialog);
265 return;
268 /* POSSIBILITY 2: UI BUILDER */
269 else if ( source_interface->params ) {
270 paramdatas = a_uibuilder_run_dialog ( source_interface->window_title, GTK_WINDOW(vw),
271 source_interface->params, source_interface->params_count,
272 source_interface->params_groups, source_interface->params_groups_count,
273 source_interface->params_defaults );
274 if ( paramdatas )
275 pass_along_data = paramdatas;
276 else
277 return; /* TODO: do we have to free anything here? */
280 /* CREATE INPUT DATA & GET OPTIONS */
281 ProcessOptions *po = g_malloc0 ( sizeof(ProcessOptions) );
283 if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRWLAYER ) {
284 gchar *name_src = a_gpx_write_tmp_file ( vtl, NULL );
286 source_interface->get_process_options_func ( pass_along_data, po, NULL, name_src, NULL );
288 util_add_to_deletion_list ( name_src );
290 g_free ( name_src );
291 } else if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRWLAYER_TRACK ) {
292 gchar *name_src = a_gpx_write_tmp_file ( vtl, NULL );
293 gchar *name_src_track = a_gpx_write_track_tmp_file ( track, NULL );
295 source_interface->get_process_options_func ( pass_along_data, po, NULL, name_src, name_src_track );
297 util_add_to_deletion_list ( name_src );
298 util_add_to_deletion_list ( name_src_track );
300 g_free ( name_src );
301 g_free ( name_src_track );
302 } else if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRACK ) {
303 gchar *name_src_track = a_gpx_write_track_tmp_file ( track, NULL );
305 source_interface->get_process_options_func ( pass_along_data, po, NULL, NULL, name_src_track );
307 g_free ( name_src_track );
308 } else if ( source_interface->get_process_options_func )
309 source_interface->get_process_options_func ( pass_along_data, po, options, NULL, NULL );
311 /* Get data for Off command */
312 if ( source_interface->off_func ) {
313 source_interface->off_func ( pass_along_data, &args_off, &fd_off );
316 /* cleanup for option dialogs */
317 if ( source_interface->add_setup_widgets_func ) {
318 gtk_widget_destroy(dialog);
319 dialog = NULL;
320 } else if ( source_interface->params ) {
321 a_uibuilder_free_paramdatas ( paramdatas, source_interface->params, source_interface->params_count );
324 w = g_malloc(sizeof(*w));
325 wi = g_malloc(sizeof(*wi));
326 wi->w = w;
327 wi->w->source_interface = source_interface;
328 wi->po = po;
329 wi->options = options;
330 wi->vtl = vtl;
331 wi->creating_new_layer = (!vtl); // Default if Auto Layer Management is passed in
333 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
334 gtk_dialog_set_response_sensitive ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT, FALSE );
335 gtk_window_set_title ( GTK_WINDOW(dialog), _(source_interface->window_title) );
337 w->dialog = dialog;
338 w->running = TRUE;
339 status = gtk_label_new (_("Working..."));
340 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), status, FALSE, FALSE, 5 );
341 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
342 // May not want to see the dialog at all
343 if ( source_interface->is_thread || source_interface->keep_dialog_open )
344 gtk_widget_show_all(dialog);
345 w->status = status;
347 w->vw = vw;
348 w->vlp = vlp;
349 w->vvp = vvp;
350 if ( source_interface->add_progress_widgets_func ) {
351 source_interface->add_progress_widgets_func ( dialog, user_data );
353 w->user_data = user_data;
355 if ( mode == VIK_DATASOURCE_ADDTOLAYER ) {
356 VikLayer *current_selected = vik_layers_panel_get_selected ( w->vlp );
357 if ( IS_VIK_TRW_LAYER(current_selected) ) {
358 wi->vtl = VIK_TRW_LAYER(current_selected);
359 wi->creating_new_layer = FALSE;
362 else if ( mode == VIK_DATASOURCE_CREATENEWLAYER ) {
363 wi->creating_new_layer = TRUE;
365 else if ( mode == VIK_DATASOURCE_MANUAL_LAYER_MANAGEMENT ) {
366 // Don't create in acquire - as datasource will perform the necessary actions
367 wi->creating_new_layer = FALSE;
368 VikLayer *current_selected = vik_layers_panel_get_selected ( w->vlp );
369 if ( IS_VIK_TRW_LAYER(current_selected) )
370 wi->vtl = VIK_TRW_LAYER(current_selected);
372 if ( wi->creating_new_layer ) {
373 wi->vtl = VIK_TRW_LAYER ( vik_layer_create ( VIK_LAYER_TRW, w->vvp, FALSE ) );
374 vik_layer_rename ( VIK_LAYER ( wi->vtl ), _(source_interface->layer_title) );
377 if ( source_interface->is_thread ) {
378 if ( po->babelargs || po->url || po->shell_command ) {
379 #if GLIB_CHECK_VERSION (2, 32, 0)
380 g_thread_try_new ( "get_from_anything", (GThreadFunc)get_from_anything, wi, NULL );
381 #else
382 g_thread_create ( (GThreadFunc)get_from_anything, wi, FALSE, NULL );
383 #endif
384 gtk_dialog_run ( GTK_DIALOG(dialog) );
385 if (w->running) {
386 // Cancel and mark for thread to finish
387 w->running = FALSE;
388 // NB Thread will free memory
389 } else {
390 if ( args_off ) {
391 /* Turn off */
392 ProcessOptions off_po = { args_off, fd_off, NULL, NULL, NULL };
393 a_babel_convert_from (NULL, &off_po, NULL, NULL, NULL);
394 g_free ( args_off );
396 if ( fd_off )
397 g_free ( fd_off );
399 // Thread finished by normal completion - free memory
400 g_free ( w );
401 g_free ( wi );
404 else {
405 // This shouldn't happen...
406 gtk_label_set_text ( GTK_LABEL(w->status), _("Unable to create command\nAcquire method failed.") );
407 gtk_dialog_run (GTK_DIALOG (dialog));
410 else {
411 // bypass thread method malarkly - you'll just have to wait...
412 if ( source_interface->process_func ) {
413 gboolean result = source_interface->process_func ( wi->vtl, po, (BabelStatusFunc) progress_func, w, options );
414 if ( !result )
415 a_dialog_error_msg ( GTK_WINDOW(vw), _("Error: acquisition failed.") );
417 free_process_options ( po );
418 g_free ( options );
420 on_complete_process ( wi );
421 // Actually show it if necessary
422 if ( wi->w->source_interface->keep_dialog_open )
423 gtk_dialog_run ( GTK_DIALOG(dialog) );
425 g_free ( w );
426 g_free ( wi );
429 gtk_widget_destroy ( dialog );
431 if ( cleanup_function )
432 cleanup_function ( source_interface );
436 * a_acquire:
437 * @vw: The #VikWindow to work with
438 * @vlp: The #VikLayersPanel in which a #VikTrwLayer layer may be created/appended
439 * @vvp: The #VikViewport defining the current view
440 * @mode: How layers should be managed
441 * @source_interface: The #VikDataSourceInterface determining how and what actions to take
442 * @userdata: External data to be passed into the #VikDataSourceInterface
443 * @cleanup_function: The function to dispose the #VikDataSourceInterface if necessary
445 * Process the given VikDataSourceInterface for sources with no input data.
447 void a_acquire ( VikWindow *vw,
448 VikLayersPanel *vlp,
449 VikViewport *vvp,
450 vik_datasource_mode_t mode,
451 VikDataSourceInterface *source_interface,
452 gpointer userdata,
453 VikDataSourceCleanupFunc cleanup_function )
455 acquire ( vw, vlp, vvp, mode, source_interface, NULL, NULL, userdata, cleanup_function );
458 static void acquire_trwlayer_callback ( GObject *menuitem, gpointer *pass_along )
460 VikDataSourceInterface *iface = g_object_get_data ( menuitem, "vik_acq_iface" );
461 VikWindow *vw = pass_along[0];
462 VikLayersPanel *vlp = pass_along[1];
463 VikViewport *vvp = pass_along[2];
464 VikTrwLayer *vtl = pass_along[3];
465 VikTrack *tr = pass_along[4];
467 acquire ( vw, vlp, vvp, iface->mode, iface, vtl, tr, NULL, NULL );
470 static GtkWidget *acquire_build_menu ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp,
471 VikTrwLayer *vtl, VikTrack *track, /* both passed to acquire, although for many filters only one ness */
472 const gchar *menu_title, vik_datasource_inputtype_t inputtype )
474 static gpointer pass_along[5];
475 GtkWidget *menu_item=NULL, *menu=NULL;
476 GtkWidget *item=NULL;
477 int i;
479 pass_along[0] = vw;
480 pass_along[1] = vlp;
481 pass_along[2] = vvp;
482 pass_along[3] = vtl;
483 pass_along[4] = track;
485 for ( i = 0; i < N_FILTERS; i++ ) {
486 if ( filters[i]->inputtype == inputtype ) {
487 if ( ! menu_item ) { /* do this just once, but return NULL if no filters */
488 menu = gtk_menu_new();
489 menu_item = gtk_menu_item_new_with_mnemonic ( menu_title );
490 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu );
493 item = gtk_menu_item_new_with_label ( filters[i]->window_title );
494 g_object_set_data ( G_OBJECT(item), "vik_acq_iface", (gpointer) filters[i] );
495 g_signal_connect ( G_OBJECT(item), "activate", G_CALLBACK(acquire_trwlayer_callback), pass_along );
496 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
497 gtk_widget_show ( item );
501 return menu_item;
505 * a_acquire_trwlayer_menu:
507 * Create a sub menu intended for rightclicking on a TRWLayer's menu called "Filter".
509 * Returns: %NULL if no filters.
511 GtkWidget *a_acquire_trwlayer_menu (VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikTrwLayer *vtl)
513 return acquire_build_menu ( vw, vlp, vvp, vtl, NULL, _("_Filter"), VIK_DATASOURCE_INPUTTYPE_TRWLAYER );
517 * a_acquire_trwlayer_track_menu:
519 * Create a sub menu intended for rightclicking on a TRWLayer's menu called "Filter with Track "TRACKNAME"...".
521 * Returns: %NULL if no filters or no filter track has been set.
523 GtkWidget *a_acquire_trwlayer_track_menu (VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikTrwLayer *vtl)
525 if ( filter_track == NULL )
526 return NULL;
527 else {
528 gchar *menu_title = g_strdup_printf ( _("Filter with %s"), filter_track->name );
529 GtkWidget *rv = acquire_build_menu ( vw, vlp, vvp, vtl, filter_track,
530 menu_title, VIK_DATASOURCE_INPUTTYPE_TRWLAYER_TRACK );
531 g_free ( menu_title );
532 return rv;
537 * a_acquire_track_menu:
539 * Create a sub menu intended for rightclicking on a track's menu called "Filter".
541 * Returns: %NULL if no applicable filters
543 GtkWidget *a_acquire_track_menu (VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikTrack *tr)
545 return acquire_build_menu ( vw, vlp, vvp, NULL, tr, _("Filter"), VIK_DATASOURCE_INPUTTYPE_TRACK );
549 * a_acquire_set_filter_track:
551 * Sets application-wide track to use with filter. references the track.
553 void a_acquire_set_filter_track ( VikTrack *tr )
555 if ( filter_track )
556 vik_track_free ( filter_track );
558 filter_track = tr;
559 vik_track_ref ( tr );