]> git.street.me.uk Git - andy/viking.git/blob - src/acquire.c
[QA] Coverity: Unchecked return value from library
[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  * Copyright (C) 2013-2015, Rob Norris <rw_norris@hotmail.com>
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <glib/gprintf.h>
29 #include <glib/gi18n.h>
30
31 #include "viking.h"
32 #include "babel.h"
33 #include "gpx.h"
34 #include "acquire.h"
35
36 /************************ FILTER LIST *******************/
37 // extern VikDataSourceInterface vik_datasource_gps_interface;
38
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
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;
47
48 /*** Input is a track ***/
49
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,
56 };
57
58 const guint N_FILTERS = sizeof(filters) / sizeof(filters[0]);
59
60 VikTrack *filter_track = NULL;
61
62 /********************************************************/
63
64 /* passed along to worker thread */
65 typedef struct {
66   acq_dialog_widgets_t *w;
67   ProcessOptions *po;
68   gboolean creating_new_layer;
69   VikTrwLayer *vtl;
70   gpointer options;
71 } w_and_interface_t;
72
73
74 /*********************************************************
75  * Definitions and routines for acquiring data from Data Sources in general
76  *********************************************************/
77
78 static void progress_func ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w )
79 {
80   if ( w->source_interface->is_thread ) {
81     gdk_threads_enter ();
82     if ( !w->running ) {
83       if ( w->source_interface->cleanup_func )
84         w->source_interface->cleanup_func ( w->user_data );
85       gdk_threads_leave ();
86       g_thread_exit ( NULL );
87     }
88     gdk_threads_leave ();
89   }
90
91   if ( w->source_interface->progress_func )
92     w->source_interface->progress_func ( c, data, w );
93 }
94
95 /**
96  * Some common things to do on completion of a datasource process
97  *  . Update layer
98  *  . Update dialog info
99  *  . Update main dsisplay
100  */
101 static void on_complete_process (w_and_interface_t *wi)
102 {
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 );
111       }
112       else
113         gtk_label_set_text ( GTK_LABEL(wi->w->status), _("No data.") );
114     }
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 );
118     } else {
119       gtk_dialog_response ( GTK_DIALOG(wi->w->dialog), GTK_RESPONSE_ACCEPT );
120     }
121     // Main display update
122     if ( wi->vtl ) {
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) );
127       }
128       vik_layers_panel_emit_update ( wi->w->vlp );
129     }
130   } else {
131     /* cancelled */
132     if ( wi->creating_new_layer )
133       g_object_unref(wi->vtl);
134   }
135 }
136
137 static void free_process_options ( ProcessOptions *po )
138 {
139   if ( po ) {
140     g_free ( po->babelargs );
141     g_free ( po->filename );
142     g_free ( po->input_file_type );
143     g_free ( po->babel_filters );
144     g_free ( po->url );
145     g_free ( po->shell_command );
146     g_free ( po );
147   }
148 }
149
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 )
152 {
153   gboolean result = TRUE;
154
155   VikDataSourceInterface *source_interface = wi->w->source_interface;
156
157   if ( source_interface->process_func ) {
158     result = source_interface->process_func ( wi->vtl, wi->po, (BabelStatusFunc)progress_func, wi->w, wi->options );
159   }
160   free_process_options ( wi->po );
161   g_free ( wi->options );
162
163   if (wi->w->running && !result) {
164     gdk_threads_enter();
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 ) );
168     gdk_threads_leave();
169   } 
170   else {
171     gdk_threads_enter();
172     on_complete_process ( wi );
173     gdk_threads_leave();
174   }
175
176   if ( source_interface->cleanup_func )
177     source_interface->cleanup_func ( wi->w->user_data );
178
179   if ( wi->w->running ) {
180     wi->w->running = FALSE;
181   }
182   else {
183     g_free ( wi->w );
184     g_free ( wi );
185     wi = NULL;
186   }
187
188   g_thread_exit ( NULL );
189 }
190
191 /* depending on type of filter, often only vtl or track will be given.
192  * the other can be NULL.
193  */
194 static void acquire ( VikWindow *vw,
195                       VikLayersPanel *vlp,
196                       VikViewport *vvp,
197                       vik_datasource_mode_t mode,
198                       VikDataSourceInterface *source_interface,
199                       VikTrwLayer *vtl,
200                       VikTrack *track,
201                       gpointer userdata,
202                       VikDataSourceCleanupFunc cleanup_function )
203 {
204   /* for manual dialogs */
205   GtkWidget *dialog = NULL;
206   GtkWidget *status;
207   gchar *args_off = NULL;
208   gchar *fd_off = NULL;
209   acq_dialog_widgets_t *w;
210   gpointer user_data;
211   gpointer options = NULL;
212
213   acq_vik_t avt;
214   avt.vlp = vlp;
215   avt.vvp = vvp;
216   avt.vw = vw;
217   avt.userdata = userdata;
218
219   /* for UI builder */
220   gpointer pass_along_data;
221   VikLayerParamData *paramdatas = NULL;
222
223   w_and_interface_t *wi;
224
225   /*** INIT AND CHECK EXISTENCE ***/
226   if ( source_interface->init_func )
227     user_data = source_interface->init_func(&avt);
228   else
229     user_data = NULL;
230   pass_along_data = user_data;
231
232   if ( source_interface->check_existence_func ) {
233     gchar *error_str = source_interface->check_existence_func();
234     if ( error_str ) {
235       a_dialog_error_msg ( GTK_WINDOW(vw), error_str );
236       g_free ( error_str );
237       return;
238     }
239   }    
240
241   /* BUILD UI & GET OPTIONS IF NECESSARY. */
242
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 );
247
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 );
252 #endif
253
254     source_interface->add_setup_widgets_func(dialog, vvp, user_data);
255     gtk_window_set_title ( GTK_WINDOW(dialog), _(source_interface->window_title) );
256
257     if ( response_w )
258       gtk_widget_grab_focus ( response_w );
259
260     if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
261       source_interface->cleanup_func(user_data);
262       gtk_widget_destroy(dialog);
263       return;
264     }
265   }
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 );
272     if ( paramdatas )
273       pass_along_data = paramdatas;
274     else
275       return; /* TODO: do we have to free anything here? */
276   }
277
278   /* CREATE INPUT DATA & GET OPTIONS */
279   ProcessOptions *po = g_malloc0 ( sizeof(ProcessOptions) );
280
281   if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRWLAYER ) {
282     gchar *name_src = a_gpx_write_tmp_file ( vtl, NULL );
283
284     source_interface->get_process_options_func ( pass_along_data, po, NULL, name_src, NULL );
285
286     util_add_to_deletion_list ( name_src );
287
288     g_free ( 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 );
292
293     source_interface->get_process_options_func ( pass_along_data, po, NULL, name_src, name_src_track );
294
295     util_add_to_deletion_list ( name_src );
296     util_add_to_deletion_list ( name_src_track );
297
298     g_free ( name_src );
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 );
302
303     source_interface->get_process_options_func ( pass_along_data, po, NULL, NULL, name_src_track );
304
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 );
308
309   /* Get data for Off command */
310   if ( source_interface->off_func ) {
311     source_interface->off_func ( pass_along_data, &args_off, &fd_off );
312   }
313
314   /* cleanup for option dialogs */
315   if ( source_interface->add_setup_widgets_func ) {
316     gtk_widget_destroy(dialog);
317     dialog = NULL;
318   } else if ( source_interface->params ) {
319     a_uibuilder_free_paramdatas ( paramdatas, source_interface->params, source_interface->params_count );
320   }
321
322   w = g_malloc(sizeof(*w));
323   wi = g_malloc(sizeof(*wi));
324   wi->w = w;
325   wi->w->source_interface = source_interface;
326   wi->po = po;
327   wi->options = options;
328   wi->vtl = vtl;
329   wi->creating_new_layer = (!vtl); // Default if Auto Layer Management is passed in
330
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) );
334
335   w->dialog = dialog;
336   w->running = TRUE;
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);
343   w->status = status;
344
345   w->vw = vw;
346   w->vlp = vlp;
347   w->vvp = vvp;
348   if ( source_interface->add_progress_widgets_func ) {
349     source_interface->add_progress_widgets_func ( dialog, user_data );
350   }
351   w->user_data = user_data;
352
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;
358     }
359   }
360   else if ( mode == VIK_DATASOURCE_CREATENEWLAYER ) {
361     wi->creating_new_layer = TRUE;
362   }
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);
369   }
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) );
373   }
374
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 );
379 #else
380       g_thread_create ( (GThreadFunc)get_from_anything, wi, FALSE, NULL );
381 #endif
382       gtk_dialog_run ( GTK_DIALOG(dialog) );
383       if (w->running) {
384         // Cancel and mark for thread to finish
385         w->running = FALSE;
386         // NB Thread will free memory
387       } else {
388         if ( args_off ) {
389           /* Turn off */
390           ProcessOptions off_po = { args_off, fd_off, NULL, NULL, NULL };
391           a_babel_convert_from (NULL, &off_po, NULL, NULL, NULL);
392           g_free ( args_off );
393         }
394         if ( fd_off )
395           g_free ( fd_off );
396
397         // Thread finished by normal completion - free memory
398         g_free ( w );
399         g_free ( wi );
400       }
401     }
402     else {
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));
406     }
407   }
408   else {
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 );
412       if ( !result )
413         a_dialog_msg ( GTK_WINDOW(vw), GTK_MESSAGE_ERROR, _("Error: acquisition failed."), NULL );
414     }
415     free_process_options ( po );
416     g_free ( options );
417
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) );
422
423     g_free ( w );
424     g_free ( wi );
425   }
426
427   gtk_widget_destroy ( dialog );
428
429   if ( cleanup_function )
430     cleanup_function ( source_interface );
431 }
432
433 /**
434  * a_acquire:
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
442  *
443  * Process the given VikDataSourceInterface for sources with no input data.
444  */
445 void a_acquire ( VikWindow *vw,
446                  VikLayersPanel *vlp,
447                  VikViewport *vvp,
448                  vik_datasource_mode_t mode,
449                  VikDataSourceInterface *source_interface,
450                  gpointer userdata,
451                  VikDataSourceCleanupFunc cleanup_function )
452 {
453   acquire ( vw, vlp, vvp, mode, source_interface, NULL, NULL, userdata, cleanup_function );
454 }
455
456 static void acquire_trwlayer_callback ( GObject *menuitem, gpointer *pass_along )
457 {
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];
464
465   acquire ( vw, vlp, vvp, iface->mode, iface, vtl, tr, NULL, NULL );
466 }
467
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 )
471 {
472   static gpointer pass_along[5];
473   GtkWidget *menu_item=NULL, *menu=NULL;
474   GtkWidget *item=NULL;
475   int i;
476
477   pass_along[0] = vw;
478   pass_along[1] = vlp;
479   pass_along[2] = vvp;
480   pass_along[3] = vtl;
481   pass_along[4] = track;
482
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 );
489       }
490
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 );
496     }
497   }
498
499   return menu_item;
500 }
501
502 /**
503  * a_acquire_trwlayer_menu:
504  *
505  * Create a sub menu intended for rightclicking on a TRWLayer's menu called "Filter".
506  * 
507  * Returns: %NULL if no filters.
508  */
509 GtkWidget *a_acquire_trwlayer_menu (VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikTrwLayer *vtl)
510 {
511   return acquire_build_menu ( vw, vlp, vvp, vtl, NULL, _("_Filter"), VIK_DATASOURCE_INPUTTYPE_TRWLAYER );
512 }
513
514 /**
515  * a_acquire_trwlayer_track_menu:
516  *
517  * Create a sub menu intended for rightclicking on a TRWLayer's menu called "Filter with Track "TRACKNAME"...".
518  * 
519  * Returns: %NULL if no filters or no filter track has been set.
520  */
521 GtkWidget *a_acquire_trwlayer_track_menu (VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikTrwLayer *vtl)
522 {
523   if ( filter_track == NULL )
524     return NULL;
525   else {
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 );
530     return rv;
531   }
532 }
533
534 /**
535  * a_acquire_track_menu:
536  *
537  * Create a sub menu intended for rightclicking on a track's menu called "Filter".
538  * 
539  * Returns: %NULL if no applicable filters
540  */
541 GtkWidget *a_acquire_track_menu (VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikTrack *tr)
542 {
543   return acquire_build_menu ( vw, vlp, vvp, NULL, tr, _("Filter"), VIK_DATASOURCE_INPUTTYPE_TRACK );
544 }
545
546 /**
547  * a_acquire_set_filter_track:
548  *
549  * Sets application-wide track to use with filter. references the track.
550  */
551 void a_acquire_set_filter_track ( VikTrack *tr )
552 {
553   if ( filter_track )
554     vik_track_free ( filter_track );
555
556   filter_track = tr;
557   vik_track_ref ( tr );
558 }