]> git.street.me.uk Git - andy/viking.git/blame - src/acquire.c
Add OSM GPS Tiles in default data config
[andy/viking.git] / src / acquire.c
CommitLineData
1d1bc3c1
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
17acdaec 5 * Copyright (C) 2013-2015, Rob Norris <rw_norris@hotmail.com>
1d1bc3c1
EB
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 */
4c77d5e0
GB
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25
8c060406 26#include <stdio.h>
1d1bc3c1
EB
27#include <string.h>
28#include <glib/gprintf.h>
4c77d5e0 29#include <glib/gi18n.h>
1d1bc3c1
EB
30
31#include "viking.h"
32#include "babel.h"
33#include "gpx.h"
7b3479e3 34#include "acquire.h"
1d1bc3c1 35
28c82d8b
EB
36/************************ FILTER LIST *******************/
37// extern VikDataSourceInterface vik_datasource_gps_interface;
28c82d8b
EB
38
39/*** Input is TRWLayer ***/
40extern VikDataSourceInterface vik_datasource_bfilter_simplify_interface;
0e94c83d 41extern VikDataSourceInterface vik_datasource_bfilter_compress_interface;
28c82d8b 42extern VikDataSourceInterface vik_datasource_bfilter_dup_interface;
f74395cc 43extern VikDataSourceInterface vik_datasource_bfilter_manual_interface;
28c82d8b
EB
44
45/*** Input is a track and a TRWLayer ***/
46extern VikDataSourceInterface vik_datasource_bfilter_polygon_interface;
47extern VikDataSourceInterface vik_datasource_bfilter_exclude_polygon_interface;
48
49/*** Input is a track ***/
50
51const VikDataSourceInterface *filters[] = {
52 &vik_datasource_bfilter_simplify_interface,
0e94c83d 53 &vik_datasource_bfilter_compress_interface,
28c82d8b 54 &vik_datasource_bfilter_dup_interface,
f74395cc 55 &vik_datasource_bfilter_manual_interface,
28c82d8b
EB
56 &vik_datasource_bfilter_polygon_interface,
57 &vik_datasource_bfilter_exclude_polygon_interface,
58};
59
60const guint N_FILTERS = sizeof(filters) / sizeof(filters[0]);
61
62VikTrack *filter_track = NULL;
28c82d8b
EB
63
64/********************************************************/
65
7b3479e3 66/* passed along to worker thread */
1d1bc3c1 67typedef struct {
7b3479e3 68 acq_dialog_widgets_t *w;
17acdaec 69 ProcessOptions *po;
b2aa700f
RN
70 gboolean creating_new_layer;
71 VikTrwLayer *vtl;
155f558c 72 DownloadFileOptions *options;
7b3479e3 73} w_and_interface_t;
1d1bc3c1 74
1d1bc3c1 75
7b3479e3 76/*********************************************************
cf697cc2 77 * Definitions and routines for acquiring data from Data Sources in general
7b3479e3 78 *********************************************************/
1d1bc3c1 79
cf697cc2
EB
80static void progress_func ( BabelProgressCode c, gpointer data, acq_dialog_widgets_t *w )
81{
b2aa700f
RN
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 );
89 }
90 gdk_threads_leave ();
cf697cc2 91 }
cf697cc2 92
62ddf770 93 if ( w->source_interface->progress_func )
5564dd66 94 w->source_interface->progress_func ( c, data, w );
cf697cc2
EB
95}
96
b2aa700f
RN
97/**
98 * Some common things to do on completion of a datasource process
99 * . Update layer
100 * . Update dialog info
101 * . Update main dsisplay
102 */
103static void on_complete_process (w_and_interface_t *wi)
104{
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:
bec82ff5 110 if ( ! vik_trw_layer_is_empty ( wi->vtl ) ) {
b2aa700f 111 vik_layer_post_read ( VIK_LAYER(wi->vtl), wi->w->vvp, TRUE );
c3a02429 112 vik_aggregate_layer_add_layer ( vik_layers_panel_get_top_layer(wi->w->vlp), VIK_LAYER(wi->vtl), TRUE );
b2aa700f
RN
113 }
114 else
115 gtk_label_set_text ( GTK_LABEL(wi->w->status), _("No data.") );
116 }
b2aa700f
RN
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 );
122 }
123 // Main display update
20981fd6
RN
124 if ( wi->vtl ) {
125 vik_layer_post_read ( VIK_LAYER(wi->vtl), wi->w->vvp, TRUE );
35f0da98
RN
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) );
129 }
b2aa700f 130 vik_layers_panel_emit_update ( wi->w->vlp );
20981fd6 131 }
b2aa700f
RN
132 } else {
133 /* cancelled */
134 if ( wi->creating_new_layer )
135 g_object_unref(wi->vtl);
136 }
137}
28c82d8b 138
17acdaec
RN
139static void free_process_options ( ProcessOptions *po )
140{
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 );
149 }
150}
151
1d1bc3c1 152/* this routine is the worker thread. there is only one simultaneous download allowed */
7b3479e3 153static void get_from_anything ( w_and_interface_t *wi )
1d1bc3c1 154{
0d337f27 155 gboolean result = TRUE;
805d282e 156
62ddf770 157 VikDataSourceInterface *source_interface = wi->w->source_interface;
1d1bc3c1 158
17acdaec
RN
159 if ( source_interface->process_func ) {
160 result = source_interface->process_func ( wi->vtl, wi->po, (BabelStatusFunc)progress_func, wi->w, wi->options );
161 }
162 free_process_options ( wi->po );
ed691ed1 163 g_free ( wi->options );
7b3479e3 164
9a91dd66 165 if (wi->w->running && !result) {
1d1bc3c1 166 gdk_threads_enter();
b2aa700f
RN
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 ) );
1d1bc3c1
EB
170 gdk_threads_leave();
171 }
5f304fd7 172 else {
65f0ccab 173 gdk_threads_enter();
b2aa700f
RN
174 on_complete_process ( wi );
175 gdk_threads_leave();
1d1bc3c1 176 }
b2aa700f 177
62ddf770 178 if ( source_interface->cleanup_func )
b2aa700f 179 source_interface->cleanup_func ( wi->w->user_data );
7b3479e3 180
b2aa700f
RN
181 if ( wi->w->running ) {
182 wi->w->running = FALSE;
7b3479e3 183 }
9a91dd66
RN
184 else {
185 g_free ( wi->w );
186 g_free ( wi );
187 wi = NULL;
188 }
7b3479e3 189
1d1bc3c1
EB
190 g_thread_exit ( NULL );
191}
192
28c82d8b
EB
193/* depending on type of filter, often only vtl or track will be given.
194 * the other can be NULL.
195 */
9cc13848
RN
196static 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 )
1d1bc3c1 205{
28c82d8b 206 /* for manual dialogs */
1d1bc3c1 207 GtkWidget *dialog = NULL;
7b3479e3 208 GtkWidget *status;
17acdaec
RN
209 gchar *args_off = NULL;
210 gchar *fd_off = NULL;
7b3479e3 211 acq_dialog_widgets_t *w;
65f0ccab 212 gpointer user_data;
155f558c 213 DownloadFileOptions *options = g_malloc0 ( sizeof(DownloadFileOptions) );
7b3479e3 214
307abf54
RN
215 acq_vik_t avt;
216 avt.vlp = vlp;
217 avt.vvp = vvp;
218 avt.vw = vw;
2801a19b 219 avt.userdata = userdata;
307abf54 220
28c82d8b
EB
221 /* for UI builder */
222 gpointer pass_along_data;
7ee4103e 223 VikLayerParamData *paramdatas = NULL;
28c82d8b 224
7b3479e3
EB
225 w_and_interface_t *wi;
226
28c82d8b 227 /*** INIT AND CHECK EXISTENCE ***/
62ddf770 228 if ( source_interface->init_func )
307abf54 229 user_data = source_interface->init_func(&avt);
28c82d8b
EB
230 else
231 user_data = NULL;
232 pass_along_data = user_data;
233
62ddf770
MA
234 if ( source_interface->check_existence_func ) {
235 gchar *error_str = source_interface->check_existence_func();
92255687
EB
236 if ( error_str ) {
237 a_dialog_error_msg ( GTK_WINDOW(vw), error_str );
238 g_free ( error_str );
239 return;
240 }
241 }
242
28c82d8b
EB
243 /* BUILD UI & GET OPTIONS IF NECESSARY. */
244
245 /* POSSIBILITY 0: NO OPTIONS. DO NOTHING HERE. */
246 /* POSSIBILITY 1: ADD_SETUP_WIDGETS_FUNC */
62ddf770 247 if ( source_interface->add_setup_widgets_func ) {
f4b1d29b 248 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
92255687 249
5f7a8ef6
RN
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
255
62ddf770
MA
256 source_interface->add_setup_widgets_func(dialog, vvp, user_data);
257 gtk_window_set_title ( GTK_WINDOW(dialog), _(source_interface->window_title) );
a8d46e0b 258
5f7a8ef6
RN
259 if ( response_w )
260 gtk_widget_grab_focus ( response_w );
261
7b3479e3 262 if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
62ddf770 263 source_interface->cleanup_func(user_data);
7b3479e3
EB
264 gtk_widget_destroy(dialog);
265 return;
266 }
28c82d8b
EB
267 }
268 /* POSSIBILITY 2: UI BUILDER */
62ddf770 269 else if ( source_interface->params ) {
13fde155 270 paramdatas = a_uibuilder_run_dialog ( source_interface->window_title, GTK_WINDOW(vw),
62ddf770
MA
271 source_interface->params, source_interface->params_count,
272 source_interface->params_groups, source_interface->params_groups_count,
273 source_interface->params_defaults );
28c82d8b
EB
274 if ( paramdatas )
275 pass_along_data = paramdatas;
276 else
277 return; /* TODO: do we have to free anything here? */
278 }
279
17acdaec
RN
280 /* CREATE INPUT DATA & GET OPTIONS */
281 ProcessOptions *po = g_malloc0 ( sizeof(ProcessOptions) );
28c82d8b 282
62ddf770 283 if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRWLAYER ) {
12ed2b58 284 gchar *name_src = a_gpx_write_tmp_file ( vtl, NULL );
28c82d8b 285
17acdaec 286 source_interface->get_process_options_func ( pass_along_data, po, NULL, name_src, NULL );
28c82d8b 287
44871dd1
RN
288 util_add_to_deletion_list ( name_src );
289
28c82d8b 290 g_free ( name_src );
62ddf770 291 } else if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRWLAYER_TRACK ) {
12ed2b58
RN
292 gchar *name_src = a_gpx_write_tmp_file ( vtl, NULL );
293 gchar *name_src_track = a_gpx_write_track_tmp_file ( track, NULL );
28c82d8b 294
17acdaec 295 source_interface->get_process_options_func ( pass_along_data, po, NULL, name_src, name_src_track );
28c82d8b 296
44871dd1
RN
297 util_add_to_deletion_list ( name_src );
298 util_add_to_deletion_list ( name_src_track );
299
28c82d8b
EB
300 g_free ( name_src );
301 g_free ( name_src_track );
62ddf770 302 } else if ( source_interface->inputtype == VIK_DATASOURCE_INPUTTYPE_TRACK ) {
12ed2b58 303 gchar *name_src_track = a_gpx_write_track_tmp_file ( track, NULL );
28c82d8b 304
17acdaec 305 source_interface->get_process_options_func ( pass_along_data, po, NULL, NULL, name_src_track );
28c82d8b
EB
306
307 g_free ( name_src_track );
17acdaec 308 } else if ( source_interface->get_process_options_func )
155f558c 309 source_interface->get_process_options_func ( pass_along_data, po, options, NULL, NULL );
28c82d8b 310
2b756ea0
RN
311 /* Get data for Off command */
312 if ( source_interface->off_func ) {
17acdaec 313 source_interface->off_func ( pass_along_data, &args_off, &fd_off );
2b756ea0
RN
314 }
315
28c82d8b 316 /* cleanup for option dialogs */
62ddf770 317 if ( source_interface->add_setup_widgets_func ) {
7b3479e3
EB
318 gtk_widget_destroy(dialog);
319 dialog = NULL;
62ddf770
MA
320 } else if ( source_interface->params ) {
321 a_uibuilder_free_paramdatas ( paramdatas, source_interface->params, source_interface->params_count );
28c82d8b
EB
322 }
323
7b3479e3
EB
324 w = g_malloc(sizeof(*w));
325 wi = g_malloc(sizeof(*wi));
326 wi->w = w;
62ddf770 327 wi->w->source_interface = source_interface;
17acdaec 328 wi->po = po;
ed691ed1 329 wi->options = options;
b2aa700f 330 wi->vtl = vtl;
9cc13848 331 wi->creating_new_layer = (!vtl); // Default if Auto Layer Management is passed in
1d1bc3c1 332
f4b1d29b 333 dialog = gtk_dialog_new_with_buttons ( "", GTK_WINDOW(vw), 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL );
1d1bc3c1 334 gtk_dialog_set_response_sensitive ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT, FALSE );
62ddf770 335 gtk_window_set_title ( GTK_WINDOW(dialog), _(source_interface->window_title) );
1d1bc3c1 336
1d1bc3c1 337 w->dialog = dialog;
b2aa700f
RN
338 w->running = TRUE;
339 status = gtk_label_new (_("Working..."));
9b082b39 340 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), status, FALSE, FALSE, 5 );
5f7a8ef6 341 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
b2aa700f
RN
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);
1d1bc3c1 345 w->status = status;
7b3479e3 346
1d1bc3c1
EB
347 w->vw = vw;
348 w->vlp = vlp;
349 w->vvp = vvp;
62ddf770
MA
350 if ( source_interface->add_progress_widgets_func ) {
351 source_interface->add_progress_widgets_func ( dialog, user_data );
65f0ccab
AF
352 }
353 w->user_data = user_data;
7b3479e3 354
9cc13848 355 if ( mode == VIK_DATASOURCE_ADDTOLAYER ) {
b2aa700f
RN
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;
360 }
361 }
9cc13848
RN
362 else if ( mode == VIK_DATASOURCE_CREATENEWLAYER ) {
363 wi->creating_new_layer = TRUE;
364 }
365 else if ( mode == VIK_DATASOURCE_MANUAL_LAYER_MANAGEMENT ) {
3cc57413
RN
366 // Don't create in acquire - as datasource will perform the necessary actions
367 wi->creating_new_layer = FALSE;
6ef14d6b
RN
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);
3cc57413 371 }
b2aa700f 372 if ( wi->creating_new_layer ) {
0ab35525 373 wi->vtl = VIK_TRW_LAYER ( vik_layer_create ( VIK_LAYER_TRW, w->vvp, FALSE ) );
b2aa700f
RN
374 vik_layer_rename ( VIK_LAYER ( wi->vtl ), _(source_interface->layer_title) );
375 }
a8d46e0b 376
b2aa700f 377 if ( source_interface->is_thread ) {
17acdaec 378 if ( po->babelargs || po->url || po->shell_command ) {
b832ea69
RN
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
b2aa700f
RN
384 gtk_dialog_run ( GTK_DIALOG(dialog) );
385 if (w->running) {
9a91dd66
RN
386 // Cancel and mark for thread to finish
387 w->running = FALSE;
388 // NB Thread will free memory
b2aa700f 389 } else {
17acdaec 390 if ( args_off ) {
b2aa700f 391 /* Turn off */
17acdaec
RN
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 );
b2aa700f 395 }
17acdaec
RN
396 if ( fd_off )
397 g_free ( fd_off );
9a91dd66
RN
398
399 // Thread finished by normal completion - free memory
400 g_free ( w );
401 g_free ( wi );
b2aa700f
RN
402 }
403 }
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));
b2aa700f
RN
408 }
409 }
7b3479e3 410 else {
b2aa700f
RN
411 // bypass thread method malarkly - you'll just have to wait...
412 if ( source_interface->process_func ) {
17acdaec 413 gboolean result = source_interface->process_func ( wi->vtl, po, (BabelStatusFunc) progress_func, w, options );
b2aa700f
RN
414 if ( !result )
415 a_dialog_msg ( GTK_WINDOW(vw), GTK_MESSAGE_ERROR, _("Error: acquisition failed."), NULL );
2b756ea0 416 }
17acdaec 417 free_process_options ( po );
ed691ed1 418 g_free ( options );
b2aa700f
RN
419
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) );
9a91dd66
RN
424
425 g_free ( w );
426 g_free ( wi );
7b3479e3 427 }
b2aa700f 428
1d1bc3c1 429 gtk_widget_destroy ( dialog );
2801a19b
RN
430
431 if ( cleanup_function )
432 cleanup_function ( source_interface );
1d1bc3c1 433}
7b3479e3 434
fba47910
GB
435/**
436 * a_acquire:
2801a19b
RN
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
9cc13848 440 * @mode: How layers should be managed
2801a19b
RN
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
fba47910
GB
444 *
445 * Process the given VikDataSourceInterface for sources with no input data.
446 */
9cc13848
RN
447void 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 )
2801a19b 454{
9cc13848 455 acquire ( vw, vlp, vvp, mode, source_interface, NULL, NULL, userdata, cleanup_function );
28c82d8b
EB
456}
457
458static void acquire_trwlayer_callback ( GObject *menuitem, gpointer *pass_along )
459{
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];
466
9cc13848 467 acquire ( vw, vlp, vvp, iface->mode, iface, vtl, tr, NULL, NULL );
28c82d8b
EB
468}
469
470static 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 )
473{
0d337f27 474 static gpointer pass_along[5];
7ee4103e
GB
475 GtkWidget *menu_item=NULL, *menu=NULL;
476 GtkWidget *item=NULL;
28c82d8b
EB
477 int i;
478
28c82d8b
EB
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;
484
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();
c6fb43ba 489 menu_item = gtk_menu_item_new_with_mnemonic ( menu_title );
28c82d8b
EB
490 gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu );
491 }
492
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 );
498 }
499 }
500
501 return menu_item;
502}
503
fba47910
GB
504/**
505 * a_acquire_trwlayer_menu:
506 *
507 * Create a sub menu intended for rightclicking on a TRWLayer's menu called "Filter".
508 *
509 * Returns: %NULL if no filters.
510 */
28c82d8b
EB
511GtkWidget *a_acquire_trwlayer_menu (VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikTrwLayer *vtl)
512{
0d6911da 513 return acquire_build_menu ( vw, vlp, vvp, vtl, NULL, _("_Filter"), VIK_DATASOURCE_INPUTTYPE_TRWLAYER );
28c82d8b
EB
514}
515
fba47910
GB
516/**
517 * a_acquire_trwlayer_track_menu:
518 *
519 * Create a sub menu intended for rightclicking on a TRWLayer's menu called "Filter with Track "TRACKNAME"...".
520 *
521 * Returns: %NULL if no filters or no filter track has been set.
522 */
28c82d8b
EB
523GtkWidget *a_acquire_trwlayer_track_menu (VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikTrwLayer *vtl)
524{
525 if ( filter_track == NULL )
526 return NULL;
527 else {
0d6911da 528 gchar *menu_title = g_strdup_printf ( _("Filter with %s"), filter_track->name );
28c82d8b
EB
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;
533 }
534}
535
fba47910
GB
536/**
537 * a_acquire_track_menu:
538 *
539 * Create a sub menu intended for rightclicking on a track's menu called "Filter".
540 *
541 * Returns: %NULL if no applicable filters
542 */
28c82d8b
EB
543GtkWidget *a_acquire_track_menu (VikWindow *vw, VikLayersPanel *vlp, VikViewport *vvp, VikTrack *tr)
544{
0d6911da 545 return acquire_build_menu ( vw, vlp, vvp, NULL, tr, _("Filter"), VIK_DATASOURCE_INPUTTYPE_TRACK );
28c82d8b
EB
546}
547
fba47910
GB
548/**
549 * a_acquire_set_filter_track:
550 *
551 * Sets application-wide track to use with filter. references the track.
552 */
ce4bd1cf 553void a_acquire_set_filter_track ( VikTrack *tr )
28c82d8b
EB
554{
555 if ( filter_track )
556 vik_track_free ( filter_track );
28c82d8b
EB
557
558 filter_track = tr;
559 vik_track_ref ( tr );
28c82d8b 560}