]> git.street.me.uk Git - andy/viking.git/blame - src/datasource_osm_my_traces.c
Improve ordering of date/time output on time graphs.
[andy/viking.git] / src / datasource_osm_my_traces.c
CommitLineData
3cc57413
RN
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2/*
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 *
5 * Copyright (C) 2012, 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
3cc57413
RN
25
26#include <glib/gprintf.h>
27#include <glib/gi18n.h>
28#include <glib/gstdio.h>
29
30#include <expat.h>
31
32#include "viking.h"
33#include "gpx.h"
34#include "acquire.h"
35#include "osm-traces.h"
3cc57413 36#include "datasource_gps.h"
6ef14d6b 37#include "bbox.h"
3cc57413
RN
38
39/**
40 * See http://wiki.openstreetmap.org/wiki/API_v0.6#GPS_Traces
41 */
42#define DS_OSM_TRACES_GPX_URL_FMT "api.openstreetmap.org/api/0.6/gpx/%d/data"
43#define DS_OSM_TRACES_GPX_FILES "api.openstreetmap.org/api/0.6/user/gpx_files"
44
45typedef struct {
46 GtkWidget *user_entry;
47 GtkWidget *password_entry;
48 // NB actual user and password values are stored in oms-traces.c
6ef14d6b 49 VikViewport *vvp;
3cc57413
RN
50} datasource_osm_my_traces_t;
51
307abf54 52static gpointer datasource_osm_my_traces_init ( acq_vik_t *avt );
3cc57413
RN
53static void datasource_osm_my_traces_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data );
54static void datasource_osm_my_traces_get_cmd_string ( gpointer user_data, gchar **args, gchar **extra, DownloadMapOptions *options );
55static gboolean datasource_osm_my_traces_process ( VikTrwLayer *vtl, const gchar *cmd, const gchar *extra, BabelStatusFunc status_cb, acq_dialog_widgets_t *adw, DownloadMapOptions *options_unused );
56static void datasource_osm_my_traces_cleanup ( gpointer data );
57
58VikDataSourceInterface vik_datasource_osm_my_traces_interface = {
59 N_("OSM My Traces"),
60 N_("OSM My Traces"),
61 VIK_DATASOURCE_MANUAL_LAYER_MANAGEMENT, // we'll do this ourselves
62 VIK_DATASOURCE_INPUTTYPE_NONE,
63 TRUE,
64 TRUE,
65 FALSE, // Don't use thread method
66 (VikDataSourceInitFunc) datasource_osm_my_traces_init,
67 (VikDataSourceCheckExistenceFunc) NULL,
68 (VikDataSourceAddSetupWidgetsFunc)datasource_osm_my_traces_add_setup_widgets,
69 (VikDataSourceGetCmdStringFunc) datasource_osm_my_traces_get_cmd_string,
70 (VikDataSourceProcessFunc) datasource_osm_my_traces_process,
71 (VikDataSourceProgressFunc) NULL,
72 (VikDataSourceAddProgressWidgetsFunc) NULL,
73 (VikDataSourceCleanupFunc) datasource_osm_my_traces_cleanup,
74 (VikDataSourceOffFunc) NULL,
75
76 NULL,
77 0,
78 NULL,
79 NULL,
80 0
81};
82
307abf54 83static gpointer datasource_osm_my_traces_init ( acq_vik_t *avt )
3cc57413
RN
84{
85 datasource_osm_my_traces_t *data = g_malloc(sizeof(*data));
86 // Reuse GPS functions
87 // Haven't been able to get the thread method to work reliably (or get progress feedback)
88 // So thread version is disabled ATM
89 /*
90 if ( vik_datasource_osm_my_traces_interface.is_thread ) {
91 vik_datasource_osm_my_traces_interface.progress_func = datasource_gps_progress;
92 vik_datasource_osm_my_traces_interface.add_progress_widgets_func = datasource_gps_add_progress_widgets;
93 }
94 */
95 return data;
96}
97
98static void datasource_osm_my_traces_add_setup_widgets ( GtkWidget *dialog, VikViewport *vvp, gpointer user_data )
99{
100 datasource_osm_my_traces_t *data = (datasource_osm_my_traces_t *)user_data;
101
102 GtkWidget *user_label;
103 GtkWidget *password_label;
104 user_label = gtk_label_new(_("Username:"));
105 data->user_entry = gtk_entry_new();
106
9b082b39
RN
107 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), user_label, FALSE, FALSE, 0 );
108 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), data->user_entry, FALSE, FALSE, 0 );
3cc57413
RN
109 gtk_widget_set_tooltip_markup ( GTK_WIDGET(data->user_entry), _("The email or username used to login to OSM") );
110
111 password_label = gtk_label_new ( _("Password:") );
112 data->password_entry = gtk_entry_new ();
113
3cc57413
RN
114 gtk_widget_set_tooltip_markup ( GTK_WIDGET(data->password_entry), _("The password used to login to OSM") );
115
116 osm_login_widgets (data->user_entry, data->password_entry);
8c336533
GB
117
118 /* Packing all widgets */
119 GtkBox *box = GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog)));
120 gtk_box_pack_start ( box, password_label, FALSE, FALSE, 0 );
121 gtk_box_pack_start ( box, data->password_entry, FALSE, FALSE, 0 );
3cc57413 122 gtk_widget_show_all ( dialog );
6ef14d6b
RN
123
124 /* Keep reference to viewport */
125 data->vvp = vvp;
3cc57413
RN
126}
127
128static void datasource_osm_my_traces_get_cmd_string ( gpointer user_data, gchar **args, gchar **extra, DownloadMapOptions *options )
129{
130 datasource_osm_my_traces_t *data = (datasource_osm_my_traces_t*) user_data;
131
132 /* overwrite authentication info */
133 osm_set_login ( gtk_entry_get_text ( GTK_ENTRY(data->user_entry) ),
134 gtk_entry_get_text ( GTK_ENTRY(data->password_entry) ) );
135
136 // If going to use the values passed back into the process function parameters then these need to be set.
137 // But ATM we aren't
138 *args = NULL;
139 *extra = NULL;
140 options = NULL;
141}
142
143typedef enum {
144 tt_unknown = 0,
145 tt_osm,
146 tt_gpx_file,
147 tt_gpx_file_desc,
148 tt_gpx_file_tag,
149} xtag_type;
150
151typedef struct {
152 xtag_type tag_type; /* enum from above for this tag */
153 const char *tag_name; /* xpath-ish tag name */
154} xtag_mapping;
155
156typedef struct {
157 guint id;
158 gchar *name;
159 gchar *vis;
160 gchar *desc;
161 struct LatLon ll;
6ef14d6b
RN
162 gboolean in_current_view; // Is the track LatLon start within the current viewport
163 // This is useful in deciding whether to download a track or not
3cc57413
RN
164 // ATM Only used for display - may want to convert to a time_t for other usage
165 gchar *timestamp;
166 // user made up tags - not being used yet - would be nice to sort/select on these but display will get complicated
167 // GList *tag_list;
168} gpx_meta_data_t;
169
170static gpx_meta_data_t *new_gpx_meta_data_t()
171{
172 gpx_meta_data_t *ret;
173
174 ret = (gpx_meta_data_t *)g_malloc(sizeof(gpx_meta_data_t));
175 ret->id = 0;
176 ret->name = NULL;
177 ret->vis = NULL;
178 ret->desc = NULL;
179 ret->ll.lat = 0.0;
180 ret->ll.lon = 0.0;
6ef14d6b 181 ret->in_current_view = FALSE;
3cc57413
RN
182 ret->timestamp = NULL;
183
184 return ret;
185}
186
187static void free_gpx_meta_data ( gpx_meta_data_t *data, gpointer userdata )
188{
189 g_free(data->name);
190 g_free(data->vis);
191 g_free(data->desc);
192 g_free(data->timestamp);
193}
194
195static void free_gpx_meta_data_list (GList *list)
196{
197 g_list_foreach (list, (GFunc)free_gpx_meta_data, NULL);
198 g_list_free (list);
199}
200
201static gpx_meta_data_t *copy_gpx_meta_data_t (gpx_meta_data_t *src)
202{
203 gpx_meta_data_t *dest = new_gpx_meta_data_t();
204
205 dest->id = src->id;
206 dest->name = g_strdup(src->name);
207 dest->vis = g_strdup(src->vis);
208 dest->desc = g_strdup(src->desc);
209 dest->ll.lat = src->ll.lat;
210 dest->ll.lon = src->ll.lon;
6ef14d6b 211 dest->in_current_view = src->in_current_view;
3cc57413
RN
212 dest->timestamp = g_strdup(src->timestamp);
213
214 return dest;
215}
216
217typedef struct {
218 //GString *xpath;
219 GString *c_cdata;
220 xtag_type current_tag;
221 gpx_meta_data_t *current_gpx_meta_data;
222 GList *list_of_gpx_meta_data;
223} xml_data;
224
225// Same as the gpx.c function
226static const char *get_attr ( const char **attr, const char *key )
227{
228 while ( *attr ) {
229 if ( strcmp(*attr,key) == 0 )
230 return *(attr + 1);
231 attr += 2;
232 }
233 return NULL;
234}
235
236// ATM don't care about actual path as tags are all unique
237static xtag_mapping xtag_path_map[] = {
238 { tt_osm, "osm" },
239 { tt_gpx_file, "gpx_file" },
240 { tt_gpx_file_desc, "description" },
241 { tt_gpx_file_tag, "tag" },
242};
243
244static xtag_type get_tag ( const char *t )
245{
246 xtag_mapping *tm;
247 for (tm = xtag_path_map; tm->tag_type != 0; tm++)
248 if (0 == strcmp(tm->tag_name, t))
249 return tm->tag_type;
250 return tt_unknown;
251}
252
253static void gpx_meta_data_start ( xml_data *xd, const char *el, const char **attr )
254{
255 const gchar *tmp;
256 gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
257 buf[0] = '\0';
258
259 // Don't need to build a path - we can use the tag directly
260 //g_string_append_c ( xd->xpath, '/' );
261 //g_string_append ( xd->xpath, el );
262 //xd->current_tag = get_tag ( xd->xpath->str );
263 xd->current_tag = get_tag ( el );
264 switch ( xd->current_tag ) {
265 case tt_gpx_file:
266 if ( xd->current_gpx_meta_data )
267 free_gpx_meta_data ( xd->current_gpx_meta_data, NULL );
268 xd->current_gpx_meta_data = new_gpx_meta_data_t();
269
270 if ( ( tmp = get_attr ( attr, "id" ) ) )
271 xd->current_gpx_meta_data->id = atoi ( tmp );
272
273 if ( ( tmp = get_attr ( attr, "name" ) ) )
274 xd->current_gpx_meta_data->name = g_strdup ( tmp );
275
276 if ( ( tmp = get_attr ( attr, "lat" ) ) ) {
ad040930 277 g_strlcpy ( buf, tmp, sizeof (buf) );
3cc57413
RN
278 xd->current_gpx_meta_data->ll.lat = g_ascii_strtod ( buf, NULL );
279 }
280
281 if ( ( tmp = get_attr ( attr, "lon" ) ) ) {
ad040930 282 g_strlcpy ( buf, tmp, sizeof (buf) );
3cc57413
RN
283 xd->current_gpx_meta_data->ll.lon = g_ascii_strtod ( buf, NULL );
284 }
285
286 if ( ( tmp = get_attr ( attr, "visibility" ) ) )
287 xd->current_gpx_meta_data->vis = g_strdup ( tmp );
288
289 if ( ( tmp = get_attr ( attr, "timestamp" ) ) )
290 xd->current_gpx_meta_data->timestamp = g_strdup ( tmp );
291
292 g_string_erase ( xd->c_cdata, 0, -1 ); // clear the cdata buffer
293 break;
294 case tt_gpx_file_desc:
295 case tt_gpx_file_tag:
296 g_string_erase ( xd->c_cdata, 0, -1 ); // clear the cdata buffer
297 break;
298 default:
299 g_string_erase ( xd->c_cdata, 0, -1 ); // clear the cdata buffer
300 break;
301 }
302}
303
304static void gpx_meta_data_end ( xml_data *xd, const char *el )
305{
306 //g_string_truncate ( xd->xpath, xd->xpath->len - strlen(el) - 1 );
307 //switch ( xd->current_tag ) {
308 switch ( get_tag ( el ) ) {
309 case tt_gpx_file: {
310 // End of the individual file metadata, thus save what we have read in to the list
311 // Copy it so we can reference it
312 gpx_meta_data_t *current = copy_gpx_meta_data_t ( xd->current_gpx_meta_data );
313 // Stick in the list
314 xd->list_of_gpx_meta_data = g_list_prepend(xd->list_of_gpx_meta_data, current);
315 g_string_erase ( xd->c_cdata, 0, -1 );
316 break;
317 }
318 case tt_gpx_file_desc:
319 // Store the description:
320 if ( xd->current_gpx_meta_data ) {
321 // NB Limit description size as it's displayed on a single line
322 // Hopefully this will prevent the dialog getting too wide...
323 xd->current_gpx_meta_data->desc = g_strndup ( xd->c_cdata->str, 63 );
324 }
325 g_string_erase ( xd->c_cdata, 0, -1 );
326 break;
327 case tt_gpx_file_tag:
328 // One day do something with this...
329 g_string_erase ( xd->c_cdata, 0, -1 );
330 break;
331 default:
332 break;
333 }
334}
335
336static void gpx_meta_data_cdata ( xml_data *xd, const XML_Char *s, int len )
337{
338 switch ( xd->current_tag ) {
339 case tt_gpx_file_desc:
340 case tt_gpx_file_tag:
341 g_string_append_len ( xd->c_cdata, s, len );
342 break;
343 default: break; // ignore cdata from other things
344 }
345}
346
347static gboolean read_gpx_files_metadata_xml ( gchar *tmpname, xml_data *xd )
348{
349 FILE *ff = g_fopen (tmpname, "r");
350 if ( !ff )
351 return FALSE;
352
353 XML_Parser parser = XML_ParserCreate(NULL);
354 enum XML_Status status = XML_STATUS_ERROR;
355
356 XML_SetElementHandler(parser, (XML_StartElementHandler) gpx_meta_data_start, (XML_EndElementHandler) gpx_meta_data_end);
357 XML_SetUserData(parser, xd);
358 XML_SetCharacterDataHandler(parser, (XML_CharacterDataHandler) gpx_meta_data_cdata);
359
360 gchar buf[4096];
361
362 int done=0, len;
363 while (!done) {
364 len = fread(buf, 1, sizeof(buf)-7, ff);
365 done = feof(ff) || !len;
366 status = XML_Parse(parser, buf, len, done);
367 }
368
369 XML_ParserFree (parser);
370
371 fclose ( ff );
372
373 return status != XML_STATUS_ERROR;
374}
375
376static GList *select_from_list (GtkWindow *parent, GList *list, const gchar *title, const gchar *msg )
377{
378 GtkTreeIter iter;
379 GtkCellRenderer *renderer;
380 GtkWidget *view;
381 gchar *latlon_string;
382 int column_runner;
383
384 GtkWidget *dialog = gtk_dialog_new_with_buttons (title,
385 parent,
386 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
387 GTK_STOCK_CANCEL,
388 GTK_RESPONSE_REJECT,
389 GTK_STOCK_OK,
390 GTK_RESPONSE_ACCEPT,
391 NULL);
392 /* When something is selected then OK */
393 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
394 GtkWidget *response_w = NULL;
395#if GTK_CHECK_VERSION (2, 20, 0)
396 /* Default to not apply - as initially nothing is selected! */
397 response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_REJECT );
398#endif
399 GtkWidget *label = gtk_label_new ( msg );
6ef14d6b 400 GtkTreeStore *store = gtk_tree_store_new ( 6, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN );
3cc57413
RN
401 GList *list_runner = list;
402 while (list_runner) {
403 gpx_meta_data_t *gpx_meta_data = (gpx_meta_data_t *)list_runner->data;
404 // To keep display compact three digits of precision for lat/lon should be plenty
405 latlon_string = g_strdup_printf("(%.3f,%.3f)", gpx_meta_data->ll.lat, gpx_meta_data->ll.lon);
406 gtk_tree_store_append(store, &iter, NULL);
407 gtk_tree_store_set ( store, &iter,
408 0, gpx_meta_data->name,
409 1, gpx_meta_data->desc,
410 2, gpx_meta_data->timestamp,
411 3, latlon_string,
412 4, gpx_meta_data->vis,
6ef14d6b 413 5, gpx_meta_data->in_current_view,
3cc57413
RN
414 -1 );
415 list_runner = g_list_next ( list_runner );
416 g_free ( latlon_string );
417 }
418
419 view = gtk_tree_view_new();
420 renderer = gtk_cell_renderer_text_new();
421 column_runner = 0;
422 GtkTreeViewColumn *column;
423
424 column = gtk_tree_view_column_new_with_attributes ( _("Name"), renderer, "text", column_runner, NULL);
425 gtk_tree_view_column_set_sort_column_id (column, column_runner);
426 gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);
427
428 column_runner++;
429 column = gtk_tree_view_column_new_with_attributes ( _("Description"), renderer, "text", column_runner, NULL);
430 gtk_tree_view_column_set_sort_column_id (column, column_runner);
431 gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);
432
433 column_runner++;
434 column = gtk_tree_view_column_new_with_attributes ( _("Time"), renderer, "text", column_runner, NULL);
435 gtk_tree_view_column_set_sort_column_id (column, column_runner);
436 gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);
437
438 column_runner++;
439 column = gtk_tree_view_column_new_with_attributes ( _("Lat/Lon"), renderer, "text", column_runner, NULL);
440 gtk_tree_view_column_set_sort_column_id (column, column_runner);
441 gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);
442
443 column_runner++;
444 column = gtk_tree_view_column_new_with_attributes ( _("Privacy"), renderer, "text", column_runner, NULL); // AKA Visibility
445 gtk_tree_view_column_set_sort_column_id (column, column_runner);
446 gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);
447
6ef14d6b
RN
448 GtkCellRenderer *renderer_toggle = gtk_cell_renderer_toggle_new ();
449 g_object_set (G_OBJECT (renderer_toggle), "activatable", FALSE, NULL); // No user action - value is just for display
450 column_runner++;
451 column = gtk_tree_view_column_new_with_attributes ( _("Within Current View"), renderer_toggle, "active", column_runner, NULL);
452 gtk_tree_view_column_set_sort_column_id (column, column_runner);
453 gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);
454
3cc57413
RN
455 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
456 gtk_tree_selection_set_mode( gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), GTK_SELECTION_MULTIPLE );
457 g_object_unref(store);
458
459 GtkWidget *scrolledwindow = gtk_scrolled_window_new ( NULL, NULL );
460 gtk_scrolled_window_set_policy ( GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC );
461 gtk_container_add ( GTK_CONTAINER(scrolledwindow), view );
462
9b082b39
RN
463 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), label, FALSE, FALSE, 0);
464 gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), scrolledwindow, TRUE, TRUE, 0);
3cc57413
RN
465
466 // Ensure a reasonable number of items are shown, but let the width be automatically sized
467 gtk_widget_set_size_request ( dialog, -1, 400) ;
468 gtk_widget_show_all ( dialog );
469
470 if ( response_w )
471 gtk_widget_grab_focus ( response_w );
472
473 while ( gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT ) {
474
475 // Possibily not the fastest method but we don't have thousands of entries to process...
476 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
477 GList *selected = NULL;
478
479 // because we don't store the full data in the gtk model, we have to scan & look it up
480 if ( gtk_tree_model_get_iter_first( GTK_TREE_MODEL(store), &iter) ) {
481 do {
482 if ( gtk_tree_selection_iter_is_selected ( selection, &iter ) ) {
483 // For every selected item,
484 // compare the name from the displayed view to every gpx entry to find the gpx this selection represents
485 gchar* name;
486 gtk_tree_model_get (GTK_TREE_MODEL(store), &iter, 0, &name, -1 );
487 // I believe the name of these items to be always unique
488 list_runner = list;
489 while (list_runner) {
490 if ( !strcmp ( ((gpx_meta_data_t*)list_runner->data)->name, name ) ) {
491 gpx_meta_data_t *copied = copy_gpx_meta_data_t (list_runner->data);
492 selected = g_list_prepend (selected, copied);
493 break;
494 }
495 list_runner = g_list_next ( list_runner );
496 }
644eea0e 497 g_free ( name );
3cc57413
RN
498 }
499 }
500 while ( gtk_tree_model_iter_next ( GTK_TREE_MODEL(store), &iter ) );
501 }
502
503 if ( selected ) {
504 gtk_widget_destroy ( dialog );
505 return selected;
506 }
507 a_dialog_error_msg(parent, _("Nothing was selected"));
508 }
509 gtk_widget_destroy ( dialog );
510 return NULL;
511}
512
513static void none_found ( GtkWindow *gw )
514{
515 GtkWidget *dialog = NULL;
516
517 dialog = gtk_dialog_new_with_buttons ( "", gw, 0, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL );
518 gtk_window_set_title(GTK_WINDOW(dialog), _("GPS Traces"));
519
520 GtkWidget *search_label = gtk_label_new(_("None found!"));
9b082b39 521 gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), search_label, FALSE, FALSE, 5 );
3cc57413
RN
522 gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
523 gtk_widget_show_all(dialog);
524
525 gtk_dialog_run ( GTK_DIALOG(dialog) );
526 gtk_widget_destroy(dialog);
527}
528
6ef14d6b
RN
529/**
530 * For each track - mark whether the start is in within the viewport
531 */
532static void set_in_current_view_property ( VikTrwLayer *vtl, datasource_osm_my_traces_t *data, GList *gl )
533{
534 gdouble min_lat, max_lat, min_lon, max_lon;
535 /* get Viewport bounding box */
536 vik_viewport_get_min_max_lat_lon ( data->vvp, &min_lat, &max_lat, &min_lon, &max_lon );
537
538 LatLonBBox bbox;
539 bbox.north = max_lat;
540 bbox.east = max_lon;
541 bbox.south = min_lat;
542 bbox.west = min_lon;
543
544 GList *iterator = gl;
545 while ( iterator ) {
546 gpx_meta_data_t* gmd = (gpx_meta_data_t*)iterator->data;
547 // Convert point position into a 'fake' bounding box
548 // TODO - probably should have function to see if point is within bounding box
549 // rather than constructing this fake bounding box for the test
550 LatLonBBox gmd_bbox;
551 gmd_bbox.north = gmd->ll.lat;
552 gmd_bbox.east = gmd->ll.lon;
553 gmd_bbox.south = gmd->ll.lat;
554 gmd_bbox.west = gmd->ll.lon;
555
556 if ( BBOX_INTERSECT ( bbox, gmd_bbox ) )
557 gmd->in_current_view = TRUE;
558
559 iterator = g_list_next ( iterator );
560 }
561}
562
3cc57413
RN
563static gboolean datasource_osm_my_traces_process ( VikTrwLayer *vtl, const gchar *cmd, const gchar *extra, BabelStatusFunc status_cb, acq_dialog_widgets_t *adw, DownloadMapOptions *options_unused )
564{
565 //datasource_osm_my_traces_t *data = (datasource_osm_my_traces_t *)adw->user_data;
566
567 gboolean result;
568
569 gchar *user_pass = osm_get_login();
570
a3697549
RN
571 // Support .zip + bzip2 files directly
572 DownloadMapOptions options = { FALSE, FALSE, NULL, 2, NULL, user_pass, a_try_decompress_file }; // Allow a couple of redirects
3cc57413
RN
573
574 xml_data *xd = g_malloc ( sizeof (xml_data) );
575 //xd->xpath = g_string_new ( "" );
576 xd->c_cdata = g_string_new ( "" );
577 xd->current_tag = tt_unknown;
578 xd->current_gpx_meta_data = new_gpx_meta_data_t();
579 xd->list_of_gpx_meta_data = NULL;
580
581 gchar *tmpname = a_download_uri_to_tmp_file ( DS_OSM_TRACES_GPX_FILES, &options );
582 result = read_gpx_files_metadata_xml ( tmpname, xd );
583 // Test already downloaded metadata file: eg:
6ef14d6b 584 //result = read_gpx_files_metadata_xml ( "/tmp/viking-download.GI47PW", xd );
3cc57413
RN
585
586 if ( tmpname ) {
587 g_remove ( tmpname );
588 g_free ( tmpname );
589 }
590
591 if ( ! result )
592 return FALSE;
593
594 if ( g_list_length ( xd->list_of_gpx_meta_data ) == 0 ) {
595 if (!vik_datasource_osm_my_traces_interface.is_thread)
596 none_found ( GTK_WINDOW(adw->vw) );
597 return FALSE;
598 }
599
600 xd->list_of_gpx_meta_data = g_list_reverse ( xd->list_of_gpx_meta_data );
601
6ef14d6b
RN
602 set_in_current_view_property ( vtl, adw->user_data, xd->list_of_gpx_meta_data );
603
3cc57413
RN
604 if (vik_datasource_osm_my_traces_interface.is_thread) gdk_threads_enter();
605 GList *selected = select_from_list ( GTK_WINDOW(adw->vw), xd->list_of_gpx_meta_data, "Select GPS Traces", "Select the GPS traces you want to add." );
606 if (vik_datasource_osm_my_traces_interface.is_thread) gdk_threads_leave();
607
8aa87b82
RN
608 // If non thread - show program is 'doing something...'
609 if ( !vik_datasource_osm_my_traces_interface.is_thread )
610 vik_window_set_busy_cursor ( adw->vw );
611
3cc57413
RN
612 // If passed in on an existing layer - we will create everything into that.
613 // thus with many differing gpx's - this will combine all waypoints into this single layer!
614 // Hence the preference is to create multiple layers
615 // and so this creation of the layers must be managed here
616
617 gboolean create_new_layer = ( !vtl );
618
619 // Only update the screen on the last layer acquired
620 VikTrwLayer *vtl_last = vtl;
621 gboolean got_something = FALSE;
622
623 GList *selected_iterator = selected;
624 while ( selected_iterator ) {
625
626 VikTrwLayer *vtlX = vtl;
627
628 if ( create_new_layer ) {
629 // Have data but no layer - so create one
0ab35525 630 vtlX = VIK_TRW_LAYER ( vik_layer_create ( VIK_LAYER_TRW, adw->vvp, FALSE ) );
3cc57413
RN
631 if ( ((gpx_meta_data_t*)selected_iterator->data)->name )
632 vik_layer_rename ( VIK_LAYER ( vtlX ), ((gpx_meta_data_t*)selected_iterator->data)->name );
633 else
634 vik_layer_rename ( VIK_LAYER ( vtlX ), _("My OSM Traces") );
3cc57413
RN
635 }
636
637 result = FALSE;
638 gint gpx_id = ((gpx_meta_data_t*)selected_iterator->data)->id;
639 if ( gpx_id ) {
640 gchar *url = g_strdup_printf ( DS_OSM_TRACES_GPX_URL_FMT, gpx_id );
641
642 result = a_babel_convert_from_url ( vtlX, url, "gpx", status_cb, adw, &options );
643 // TODO investigate using a progress bar:
644 // http://developer.gnome.org/gtk/2.24/GtkProgressBar.html
645
646 got_something = got_something || result;
d17be106
RN
647 if ( !result ) {
648 // Report errors to the status bar
649 gchar* msg = g_strdup_printf ( _("Unable to get trace: %s"), url );
650 vik_window_statusbar_update ( adw->vw, msg, VIK_STATUSBAR_INFO );
651 g_free (msg);
652 }
644eea0e 653 g_free ( url );
3cc57413
RN
654 }
655
656 if ( result ) {
6167f24f
RN
657 // Can use the layer
658 vik_aggregate_layer_add_layer ( vik_layers_panel_get_top_layer (adw->vlp), VIK_LAYER(vtlX), TRUE );
3cc57413 659 // Move to area of the track
3cc57413 660 vik_layer_post_read ( VIK_LAYER(vtlX), vik_window_viewport(adw->vw), TRUE );
6167f24f 661 vik_trw_layer_auto_set_view ( vtlX, vik_window_viewport(adw->vw) );
3cc57413
RN
662 vtl_last = vtlX;
663 }
664 else if ( create_new_layer ) {
665 // Layer not needed as no data has been acquired
666 g_object_unref ( vtlX );
667 }
668
669 selected_iterator = g_list_next ( selected_iterator );
670 }
671
672 // Free memory
673 if ( xd->current_gpx_meta_data )
674 free_gpx_meta_data ( xd->current_gpx_meta_data, NULL );
675 g_free ( xd->current_gpx_meta_data );
676 free_gpx_meta_data_list ( xd->list_of_gpx_meta_data );
677 free_gpx_meta_data_list ( selected );
678 g_free ( xd );
679 g_free ( user_pass );
680
681 // Would prefer to keep the update in acquire.c,
682 // however since we may create the layer - need to do the update here
683 if ( got_something )
684 vik_layer_emit_update ( VIK_LAYER(vtl_last) );
685
686 // ATM The user is only informed if all getting *all* of the traces failed
687 if ( selected )
688 result = got_something;
689 else
690 // Process was cancelled but need to return that it proceeded as expected
691 result = TRUE;
692
8aa87b82
RN
693 if ( !vik_datasource_osm_my_traces_interface.is_thread )
694 vik_window_clear_busy_cursor ( adw->vw );
695
3cc57413
RN
696 return result;
697}
698
699static void datasource_osm_my_traces_cleanup ( gpointer data )
700{
701 g_free ( data );
702}