]> git.street.me.uk Git - andy/viking.git/blob - src/acquire.c
Document previous translation updates
[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  *
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.
10  *
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.
15  *
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
19  *
20  */
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <glib/gprintf.h>
28 #include <glib/gi18n.h>
29
30 #include "viking.h"
31 #include "babel.h"
32 #include "gpx.h"
33 #include "acquire.h"
34
35 /************************ FILTER LIST *******************/
36 // extern VikDataSourceInterface vik_datasource_gps_interface;
37 // extern VikDataSourceInterface vik_datasource_google_interface;
38
39 /*** Input is TRWLayer ***/
40 extern VikDataSourceInterface vik_datasource_bfilter_simplify_interface;
41 extern VikDataSourceInterface vik_datasource_bfilter_dup_interface;
42
43 /*** Input is a track and a TRWLayer ***/
44 extern VikDataSourceInterface vik_datasource_bfilter_polygon_interface;
45 extern VikDataSourceInterface vik_datasource_bfilter_exclude_polygon_interface;
46
47 /*** Input is a track ***/
48
49 const VikDataSourceInterface *filters[] = {
50   &vik_datasource_bfilter_simplify_interface,
51   &vik_datasource_bfilter_dup_interface,
52   &vik_datasource_bfilter_polygon_interface,
53   &vik_datasource_bfilter_exclude_polygon_interface,
54 };
55
56 const guint N_FILTERS = sizeof(filters) / sizeof(filters[0]);
57
58 VikTrack *filter_track = NULL;
59 gchar *filter_track_name = NULL;
60
61 /********************************************************/
62
63 /* passed along to worker thread */
64 typedef struct {
65   acq_dialog_widgets_t *w;
66   gchar *cmd;
67   gchar *extra;
68 } w_and_interface_t;
69
70
71 /*********************************************************
72  * Definitions and routines for acquiring data from Data Sources in general
73  *********************************************************/
74
75 static void progress_func ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w )
76 {
77   gdk_threads_enter ();
78   if (!w->ok) {
79     if ( w->source_interface->cleanup_func )
80       w->source_interface->cleanup_func( w->user_data );
81     g_free ( w );
82     gdk_threads_leave();
83     g_thread_exit ( NULL );
84   }
85   gdk_threads_leave ();
86
87   if ( w->source_interface->progress_func )
88     w->source_interface->progress_func ( (gpointer) c, data, w );
89 }
90
91
92 /* this routine is the worker thread.  there is only one simultaneous download allowed */
93 static void get_from_anything ( w_and_interface_t *wi )
94 {
95   gchar *cmd = wi->cmd;
96   gchar *extra = wi->extra;
97   gboolean result = TRUE;
98   VikTrwLayer *vtl;
99
100   gboolean creating_new_layer = TRUE;
101
102   acq_dialog_widgets_t *w = wi->w;
103   VikDataSourceInterface *source_interface = wi->w->source_interface;
104   g_free ( wi );
105   wi = NULL;
106
107   gdk_threads_enter();
108   if (source_interface->mode == VIK_DATASOURCE_ADDTOLAYER) {
109     VikLayer *current_selected = vik_layers_panel_get_selected ( w->vlp );
110     if ( IS_VIK_TRW_LAYER(current_selected) ) {
111       vtl = VIK_TRW_LAYER(current_selected);
112       creating_new_layer = FALSE;
113     }
114   }
115   if ( creating_new_layer ) {
116     vtl = VIK_TRW_LAYER ( vik_layer_create ( VIK_LAYER_TRW, w->vvp, NULL, FALSE ) );
117     vik_layer_rename ( VIK_LAYER ( vtl ), _(source_interface->layer_title) );
118     gtk_label_set_text ( GTK_LABEL(w->status), _("Working...") );
119   }
120   gdk_threads_leave();
121
122   switch ( source_interface->type ) {
123   case VIK_DATASOURCE_GPSBABEL_DIRECT:
124     result = a_babel_convert_from (vtl, cmd, (BabelStatusFunc) progress_func, extra, w);
125     break;
126   case VIK_DATASOURCE_URL:
127     result = a_babel_convert_from_url (vtl, cmd, extra, (BabelStatusFunc) progress_func, w);
128     break;
129   case VIK_DATASOURCE_SHELL_CMD:
130     result = a_babel_convert_from_shellcommand ( vtl, cmd, extra, (BabelStatusFunc) progress_func, w);
131     break;
132   default:
133     g_critical("Houston, we've had a problem.");
134   }
135
136   g_free ( cmd );
137   g_free ( extra );
138
139   if (!result) {
140     gdk_threads_enter();
141     gtk_label_set_text ( GTK_LABEL(w->status), _("Error: acquisition failed.") );
142     if ( creating_new_layer )
143       g_object_unref ( G_OBJECT ( vtl ) );
144     gdk_threads_leave();
145   } 
146   else {
147     gdk_threads_enter();
148     if (w->ok) {
149       gtk_label_set_text ( GTK_LABEL(w->status), _("Done.") );
150       if ( creating_new_layer ) {
151         /* Only create the layer if it actually contains anything useful */
152         if ( g_hash_table_size (vik_trw_layer_get_tracks(vtl)) ||
153              g_hash_table_size (vik_trw_layer_get_waypoints(vtl)) )
154           vik_aggregate_layer_add_layer( vik_layers_panel_get_top_layer(w->vlp), VIK_LAYER(vtl));
155         else
156           gtk_label_set_text ( GTK_LABEL(w->status), _("No data.") );
157       }
158       if ( source_interface->keep_dialog_open ) {
159         gtk_dialog_set_response_sensitive ( GTK_DIALOG(w->dialog), GTK_RESPONSE_ACCEPT, TRUE );
160         gtk_dialog_set_response_sensitive ( GTK_DIALOG(w->dialog), GTK_RESPONSE_REJECT, FALSE );
161       } else {
162         gtk_dialog_response ( GTK_DIALOG(w->dialog), GTK_RESPONSE_ACCEPT );     
163       }
164     } else {
165       /* canceled */
166       if ( creating_new_layer )
167         g_object_unref(vtl);
168     }
169   }
170   if ( source_interface->cleanup_func )
171     source_interface->cleanup_func ( w->user_data );
172
173   if ( w->ok ) {
174     w->ok = FALSE;
175   } else {
176     g_free ( w );
177   }
178
179   gdk_threads_leave();
180   g_thread_exit ( NULL );
181 }
182
183
184 static gchar *write_tmp_trwlayer ( VikTrwLayer *vtl )
185 {
186   int fd_src;
187   gchar *name_src;
188   FILE *f;
189   g_assert ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0);
190   f = fdopen(fd_src, "w");
191   a_gpx_write_file(vtl, f);
192   fclose(f);
193   f = NULL;
194   return name_src;
195 }
196
197 /* TODO: write with name of old track */
198 static gchar *write_tmp_track ( VikTrack *track )
199 {
200   int fd_src;
201   gchar *name_src;
202   FILE *f;
203   g_assert ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0);
204   f = fdopen(fd_src, "w");
205   a_gpx_write_track_file("track", track, f); /* Thank you Guilhem! Just when I needed this function... -- Evan */
206   fclose(f);
207   f = NULL;
208   return name_src;
209 }
210
211 /* TODO: cleanup, getr rid of redundancy */
212
213 /* depending on type of filter, often only vtl or track will be given.
214  * the other can be NULL.
215  */
216 static void acquire ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikDataSourceInterface *source_interface,
217                       VikTrwLayer *vtl, VikTrack *track )
218 {
219   /* for manual dialogs */
220   GtkWidget *dialog = NULL;
221   GtkWidget *status;
222   gchar *cmd, *extra;
223   gchar *cmd_off = NULL;
224   gchar *extra_off = NULL;
225   acq_dialog_widgets_t *w;
226   gpointer user_data;
227
228   /* for UI builder */
229   gpointer pass_along_data;
230   VikLayerParamData *paramdatas = NULL;
231
232   w_and_interface_t *wi;
233
234   /*** INIT AND CHECK EXISTENCE ***/
235   if ( source_interface->init_func )
236     user_data = source_interface->init_func();
237   else
238     user_data = NULL;
239   pass_along_data = user_data;
240
241   if ( source_interface->check_existence_func ) {
242     gchar *error_str = source_interface->check_existence_func();
243     if ( error_str ) {
244       a_dialog_error_msg ( GTK_WINDOW(vw), error_str );
245       g_free ( error_str );
246       return;
247     }
248   }    
249
250   /* BUILD UI & GET OPTIONS IF NECESSARY. */
251
252   /* POSSIBILITY 0: NO OPTIONS. DO NOTHING HERE. */
253   /* POSSIBILITY 1: ADD_SETUP_WIDGETS_FUNC */
254   if ( source_interface->add_setup_widgets_func ) {
255     dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
256
257     source_interface->add_setup_widgets_func(dialog, vvp, user_data);
258     gtk_window_set_title ( GTK_WINDOW(dialog), _(source_interface->window_title) );
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 COMMAND STRING */
279
280   if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRWLAYER ) {
281     gchar *name_src = write_tmp_trwlayer ( vtl );
282
283     ((VikDataSourceGetCmdStringFuncWithInput) source_interface->get_cmd_string_func)
284         ( pass_along_data, &cmd, &extra, name_src );
285
286     g_free ( name_src );
287     /* TODO: delete the tmp file? or delete it only after we're done with it? */
288   } else if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRWLAYER_TRACK ) {
289     gchar *name_src = write_tmp_trwlayer ( vtl );
290     gchar *name_src_track = write_tmp_track ( track );
291
292     ((VikDataSourceGetCmdStringFuncWithInputInput) source_interface->get_cmd_string_func)
293         ( pass_along_data, &cmd, &extra, name_src, name_src_track );
294
295     g_free ( name_src );
296     g_free ( name_src_track );
297   } else if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRACK ) {
298     gchar *name_src_track = write_tmp_track ( track );
299
300     ((VikDataSourceGetCmdStringFuncWithInput) source_interface->get_cmd_string_func)
301         ( pass_along_data, &cmd, &extra, name_src_track );
302
303     g_free ( name_src_track );
304   } else
305     source_interface->get_cmd_string_func ( pass_along_data, &cmd, &extra );
306
307   /* Get data for Off command */
308   if ( source_interface->off_func ) {
309     source_interface->off_func ( pass_along_data, &cmd_off, &extra_off );
310   }
311
312   /* cleanup for option dialogs */
313   if ( source_interface->add_setup_widgets_func ) {
314     gtk_widget_destroy(dialog);
315     dialog = NULL;
316   } else if ( source_interface->params ) {
317     a_uibuilder_free_paramdatas ( paramdatas, source_interface->params, source_interface->params_count );
318   }
319
320   /*** LET'S DO IT! ***/
321
322   if ( ! cmd )
323     return;
324
325   w = g_malloc(sizeof(*w));
326   wi = g_malloc(sizeof(*wi));
327   wi->w = w;
328   wi->w->source_interface = source_interface;
329   wi->cmd = cmd;
330   wi->extra = extra; /* usually input data type (?) */
331
332   dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
333   gtk_dialog_set_response_sensitive ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT, FALSE );
334   gtk_window_set_title ( GTK_WINDOW(dialog), _(source_interface->window_title) );
335
336
337   w->dialog = dialog;
338   w->ok = TRUE;
339   status = gtk_label_new (_("Status: detecting gpsbabel"));
340   gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), status, FALSE, FALSE, 5 );
341   gtk_widget_show_all(status);
342   w->status = status;
343
344   w->vw = vw;
345   w->vlp = vlp;
346   w->vvp = vvp;
347   if ( source_interface->add_progress_widgets_func ) {
348     source_interface->add_progress_widgets_func ( dialog, user_data );
349   }
350   w->user_data = user_data;
351
352
353   g_thread_create((GThreadFunc)get_from_anything, wi, FALSE, NULL );
354
355   gtk_dialog_run ( GTK_DIALOG(dialog) );
356   if ( w->ok )
357     w->ok = FALSE; /* tell thread to stop. TODO: add mutex */
358   else {
359     if ( cmd_off ) {
360       /* Turn off */
361       a_babel_convert_from (NULL, cmd_off, NULL, extra_off, NULL);
362     }
363     g_free ( w ); /* thread has finished; free w */
364   }
365   gtk_widget_destroy ( dialog );
366 }
367
368 void a_acquire ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikDataSourceInterface *source_interface ) {
369   acquire ( vw, vlp, vvp, source_interface, NULL, NULL );
370 }
371
372 static void acquire_trwlayer_callback ( GObject *menuitem, gpointer *pass_along )
373 {
374   VikDataSourceInterface *iface = g_object_get_data ( menuitem, "vik_acq_iface" );
375   VikWindow *vw =       pass_along[0];
376   VikLayersPanel *vlp = pass_along[1];
377   VikViewport *vvp =    pass_along[2];
378   VikTrwLayer *vtl =    pass_along[3];
379   VikTrack *tr =        pass_along[4];
380
381   acquire ( vw, vlp, vvp, iface, vtl, tr );
382 }
383
384 static GtkWidget *acquire_build_menu ( VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp,
385                                 VikTrwLayer *vtl, VikTrack *track, /* both passed to acquire, although for many filters only one ness */
386                                 const gchar *menu_title, vik_datasource_inputtype_t inputtype )
387 {
388   static gpointer pass_along[5];
389   GtkWidget *menu_item=NULL, *menu=NULL;
390   GtkWidget *item=NULL;
391   int i;
392
393   pass_along[0] = vw;
394   pass_along[1] = vlp;
395   pass_along[2] = vvp;
396   pass_along[3] = vtl;
397   pass_along[4] = track;
398
399   for ( i = 0; i < N_FILTERS; i++ ) {
400     if ( filters[i]->inputtype == inputtype ) {
401       if ( ! menu_item ) { /* do this just once, but return NULL if no filters */
402         menu = gtk_menu_new();
403         menu_item = gtk_menu_item_new_with_mnemonic ( menu_title );
404         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu );
405       }
406
407       item = gtk_menu_item_new_with_label ( filters[i]->window_title );
408       g_object_set_data ( G_OBJECT(item), "vik_acq_iface", (gpointer) filters[i] );
409       g_signal_connect ( G_OBJECT(item), "activate", G_CALLBACK(acquire_trwlayer_callback), pass_along );
410       gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
411       gtk_widget_show ( item );
412     }
413   }
414
415   return menu_item;
416 }
417
418 GtkWidget *a_acquire_trwlayer_menu (VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikTrwLayer *vtl)
419 {
420   return acquire_build_menu ( vw, vlp, vvp, vtl, NULL, "_Filter", VIK_DATASOURCE_INPUTTYPE_TRWLAYER );
421 }
422
423 GtkWidget *a_acquire_trwlayer_track_menu (VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikTrwLayer *vtl)
424 {
425   if ( filter_track == NULL )
426     return NULL;
427   else {
428     gchar *menu_title = g_strdup_printf ( "Filter with %s", filter_track_name );
429     GtkWidget *rv = acquire_build_menu ( vw, vlp, vvp, vtl, filter_track,
430                         menu_title, VIK_DATASOURCE_INPUTTYPE_TRWLAYER_TRACK );
431     g_free ( menu_title );
432     return rv;
433   }
434 }
435
436 GtkWidget *a_acquire_track_menu (VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikTrack *tr)
437 {
438   return acquire_build_menu ( vw, vlp, vvp, NULL, tr, "Filter", VIK_DATASOURCE_INPUTTYPE_TRACK );
439 }
440
441 void a_acquire_set_filter_track ( VikTrack *tr, const gchar *name )
442 {
443   if ( filter_track )
444     vik_track_free ( filter_track );
445   if ( filter_track_name )
446     g_free ( filter_track_name );
447
448   filter_track = tr;
449   vik_track_ref ( tr );
450
451   filter_track_name = g_strdup(name);
452 }