]> git.street.me.uk Git - andy/viking.git/blobdiff - src/gpx.c
[QA] Fix clang warning: format string is not a string literal
[andy/viking.git] / src / gpx.c
index f42b8cc2a1f5ea607a6e7a8d3858d6af6c1019a0..cc1a932ddade2d1def42f12c3a94af67a3f5cead 100644 (file)
--- a/src/gpx.c
+++ b/src/gpx.c
@@ -5,7 +5,7 @@
  * Copyright (C) 2007, Quy Tonthat <qtonthat@gmail.com>
  * Copyright (C) 2008, Hein Ragas <viking@ragas.nl>
  * Copyright (C) 2009, Tal B <tal.bav@gmail.com>
  * Copyright (C) 2007, Quy Tonthat <qtonthat@gmail.com>
  * Copyright (C) 2008, Hein Ragas <viking@ragas.nl>
  * Copyright (C) 2009, Tal B <tal.bav@gmail.com>
- * Copyright (c) 2012, Rob Norris <rw_norris@hotmail.com>
+ * Copyright (c) 2012-2014, Rob Norris <rw_norris@hotmail.com>
  *
  * Some of the code adapted from GPSBabel 1.2.7
  * http://gpsbabel.sf.net/
  *
  * Some of the code adapted from GPSBabel 1.2.7
  * http://gpsbabel.sf.net/
@@ -39,6 +39,8 @@
 #include <string.h>
 #endif
 #include <glib.h>
 #include <string.h>
 #endif
 #include <glib.h>
+#include <glib/gstdio.h>
+#include <glib/gi18n.h>
 #ifdef HAVE_MATH_H
 #include <math.h>
 #endif
 #ifdef HAVE_MATH_H
 #include <math.h>
 #endif
@@ -61,6 +63,7 @@ typedef enum {
         tt_wpt_ele,
         tt_wpt_sym,
         tt_wpt_time,
         tt_wpt_ele,
         tt_wpt_sym,
         tt_wpt_time,
+        tt_wpt_url,
         tt_wpt_link,            /* New in GPX 1.1 */
 
         tt_trk,
         tt_wpt_link,            /* New in GPX 1.1 */
 
         tt_trk,
@@ -135,6 +138,7 @@ tag_mapping tag_path_map[] = {
         { tt_wpt_desc, "/gpx/wpt/desc" },
         { tt_wpt_sym, "/gpx/wpt/sym" },
         { tt_wpt_sym, "/loc/waypoint/type" },
         { tt_wpt_desc, "/gpx/wpt/desc" },
         { tt_wpt_sym, "/gpx/wpt/sym" },
         { tt_wpt_sym, "/loc/waypoint/type" },
+        { tt_wpt_url, "/gpx/wpt/url" },
         { tt_wpt_link, "/gpx/wpt/link" },                    /* GPX 1.1 */
 
         { tt_trk, "/gpx/trk" },
         { tt_wpt_link, "/gpx/wpt/link" },                    /* GPX 1.1 */
 
         { tt_trk, "/gpx/trk" },
@@ -286,6 +290,7 @@ static void gpx_start(VikTrwLayer *vtl, const char *el, const char **attr)
      case tt_wpt_name:
      case tt_wpt_ele:
      case tt_wpt_time:
      case tt_wpt_name:
      case tt_wpt_ele:
      case tt_wpt_time:
+     case tt_wpt_url:
      case tt_wpt_link:
      case tt_trk_cmt:
      case tt_trk_desc:
      case tt_wpt_link:
      case tt_trk_cmt:
      case tt_trk_desc:
@@ -421,6 +426,11 @@ static void gpx_end(VikTrwLayer *vtl, const char *el)
        g_string_erase ( c_cdata, 0, -1 );
        break;
 
        g_string_erase ( c_cdata, 0, -1 );
        break;
 
+     case tt_wpt_url:
+       vik_waypoint_set_url ( c_wp, c_cdata->str );
+       g_string_erase ( c_cdata, 0, -1 );
+       break;
+
      case tt_wpt_link:
        vik_waypoint_set_image ( c_wp, c_cdata->str );
        g_string_erase ( c_cdata, 0, -1 );
      case tt_wpt_link:
        vik_waypoint_set_image ( c_wp, c_cdata->str );
        g_string_erase ( c_cdata, 0, -1 );
@@ -524,6 +534,7 @@ static void gpx_cdata(void *dta, const XML_Char *s, int len)
     case tt_wpt_cmt:
     case tt_wpt_desc:
     case tt_wpt_sym:
     case tt_wpt_cmt:
     case tt_wpt_desc:
     case tt_wpt_sym:
+    case tt_wpt_url:
     case tt_wpt_link:
     case tt_trk_cmt:
     case tt_trk_desc:
     case tt_wpt_link:
     case tt_trk_cmt:
     case tt_trk_desc:
@@ -809,6 +820,12 @@ static void gpx_write_waypoint ( VikWaypoint *wp, GpxWritingContext *context )
     fprintf ( f, "  <desc>%s</desc>\n", tmp );
     g_free ( tmp );
   }
     fprintf ( f, "  <desc>%s</desc>\n", tmp );
     g_free ( tmp );
   }
+  if ( wp->url )
+  {
+    tmp = entitize(wp->url);
+    fprintf ( f, "  <url>%s</url>\n", tmp );
+    g_free ( tmp );
+  }
   if ( wp->image )
   {
     tmp = entitize(wp->image);
   if ( wp->image )
   {
     tmp = entitize(wp->image);
@@ -1057,57 +1074,61 @@ void a_gpx_write_file ( VikTrwLayer *vtl, FILE *f, GpxWritingOptions *options )
     }
   }
 
     }
   }
 
-  // gather waypoints in a list, then sort
-  // g_hash_table_get_values: glib 2.14+
-  GList *gl = g_hash_table_get_values ( vik_trw_layer_get_waypoints ( vtl ) );
-  gl = g_list_sort ( gl, gpx_waypoint_compare );
+  if ( vik_trw_layer_get_waypoints_visibility(vtl) || (options && options->hidden) ) {
+    // gather waypoints in a list, then sort
+    GList *gl = g_hash_table_get_values ( vik_trw_layer_get_waypoints ( vtl ) );
+    gl = g_list_sort ( gl, gpx_waypoint_compare );
 
 
-  GList *iter;
-  for (iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
-    gpx_write_waypoint ( (VikWaypoint*)iter->data, &context );
+    for (GList *iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
+      gpx_write_waypoint ( (VikWaypoint*)iter->data, &context );
+    }
+    g_list_free ( gl );
   }
 
   }
 
-  g_list_free ( gl );
+  GList *gl = NULL;
+  if ( vik_trw_layer_get_tracks_visibility(vtl) || (options && options->hidden) ) {
+    //gl = g_hash_table_get_values ( vik_trw_layer_get_tracks ( vtl ) );
+    // Forming the list manually seems to produce one that is more likely to be nearer to the creation order
+    gpointer key, value;
+    GHashTableIter ght_iter;
+    g_hash_table_iter_init ( &ght_iter, vik_trw_layer_get_tracks ( vtl ) );
+    while ( g_hash_table_iter_next (&ght_iter, &key, &value) ) {
+      gl = g_list_prepend ( gl ,value );
+    }
+    gl = g_list_reverse ( gl );
 
 
-  //gl = g_hash_table_get_values ( vik_trw_layer_get_tracks ( vtl ) );
-  // Forming the list manually seems to produce one that is more likely to be nearer to the creation order
-  gl = NULL;
-  gpointer key, value;
-  GHashTableIter ght_iter;
-  g_hash_table_iter_init ( &ght_iter, vik_trw_layer_get_tracks ( vtl ) );
-  while ( g_hash_table_iter_next (&ght_iter, &key, &value) ) {
-    gl = g_list_prepend ( gl ,value );
+    // Sort method determined by preference
+    if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_TIME )
+      gl = g_list_sort ( gl, vik_track_compare_timestamp );
+    else if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_ALPHA )
+      gl = g_list_sort ( gl, gpx_track_compare_name );
   }
   }
-  gl = g_list_reverse ( gl );
-
-  // Sort method determined by preference
-  if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_TIME )
-    gl = g_list_sort ( gl, vik_track_compare_timestamp );
-  else if ( a_vik_get_gpx_export_trk_sort() == VIK_GPX_EXPORT_TRK_SORT_ALPHA )
-    gl = g_list_sort ( gl, gpx_track_compare_name );
 
 
+  GList *glrte = NULL;
   // Routes sorted by name
   // Routes sorted by name
-  GList *glrte = g_hash_table_get_values ( vik_trw_layer_get_routes ( vtl ) );
-  glrte = g_list_sort ( glrte, gpx_track_compare_name );
+  if ( vik_trw_layer_get_tracks_visibility(vtl) || (options && options->hidden) ) {
+    glrte = g_hash_table_get_values ( vik_trw_layer_get_routes ( vtl ) );
+    glrte = g_list_sort ( glrte, gpx_track_compare_name );
+  }
 
   // g_list_concat doesn't copy memory properly
   // so process each list separately
 
   GpxWritingContext context_tmp = context;
 
   // g_list_concat doesn't copy memory properly
   // so process each list separately
 
   GpxWritingContext context_tmp = context;
-  GpxWritingOptions opt_tmp = { FALSE, FALSE, FALSE };
+  GpxWritingOptions opt_tmp = { FALSE, FALSE, FALSE, FALSE };
   // Force trackpoints on tracks
   if ( !context.options )
     context_tmp.options = &opt_tmp;
   context_tmp.options->is_route = FALSE;
 
   // Loop around each list and write each one
   // Force trackpoints on tracks
   if ( !context.options )
     context_tmp.options = &opt_tmp;
   context_tmp.options->is_route = FALSE;
 
   // Loop around each list and write each one
-  for (iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
+  for (GList *iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
     gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
   }
 
   // Routes (to get routepoints)
   context_tmp.options->is_route = TRUE;
     gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
   }
 
   // Routes (to get routepoints)
   context_tmp.options->is_route = TRUE;
-  for (iter = g_list_first (glrte); iter != NULL; iter = g_list_next (iter)) {
+  for (GList *iter = g_list_first (glrte); iter != NULL; iter = g_list_next (iter)) {
     gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
   }
 
     gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
   }
 
@@ -1124,3 +1145,59 @@ void a_gpx_write_track_file ( VikTrack *trk, FILE *f, GpxWritingOptions *options
   gpx_write_track ( trk, &context );
   gpx_write_footer ( f );
 }
   gpx_write_track ( trk, &context );
   gpx_write_footer ( f );
 }
+
+/**
+ * Common write of a temporary GPX file
+ */
+static gchar* write_tmp_file ( VikTrwLayer *vtl, VikTrack *trk, GpxWritingOptions *options )
+{
+       gchar *tmp_filename = NULL;
+       GError *error = NULL;
+       // Opening temporary file
+       int fd = g_file_open_tmp("viking_XXXXXX.gpx", &tmp_filename, &error);
+       if (fd < 0) {
+               g_warning ( _("failed to open temporary file: %s"), error->message );
+               g_clear_error ( &error );
+               return NULL;
+       }
+       g_debug ("%s: temporary file = %s", __FUNCTION__, tmp_filename);
+
+       FILE *ff = fdopen (fd, "w");
+
+       if ( trk )
+               a_gpx_write_track_file ( trk, ff, options );
+       else
+               a_gpx_write_file ( vtl, ff, options );
+
+       fclose (ff);
+
+       return tmp_filename;
+}
+
+/*
+ * a_gpx_write_tmp_file:
+ * @vtl:     The #VikTrwLayer to write
+ * @options: Possible ways of writing the file data (can be NULL)
+ *
+ * Returns: The name of newly created temporary GPX file
+ *          This file should be removed once used and the string freed.
+ *          If NULL then the process failed.
+ */
+gchar* a_gpx_write_tmp_file ( VikTrwLayer *vtl, GpxWritingOptions *options )
+{
+       return write_tmp_file ( vtl, NULL, options );
+}
+
+/*
+ * a_gpx_write_track_tmp_file:
+ * @trk:     The #VikTrack to write
+ * @options: Possible ways of writing the file data (can be NULL)
+ *
+ * Returns: The name of newly created temporary GPX file
+ *          This file should be removed once used and the string freed.
+ *          If NULL then the process failed.
+ */
+gchar* a_gpx_write_track_tmp_file ( VikTrack *trk, GpxWritingOptions *options )
+{
+       return write_tmp_file ( NULL, trk, options );
+}