+
+ gchar *tmp;
+ const gchar *name = vik_layer_get_name(VIK_LAYER(vtl));
+ if ( name ) {
+ tmp = entitize ( name );
+ fprintf ( f, " <name>%s</name>\n", tmp );
+ g_free ( tmp );
+ }
+
+ VikTRWMetadata *md = vik_trw_layer_get_metadata (vtl);
+ if ( md ) {
+ if ( md->author ) {
+ tmp = entitize ( md->author );
+ fprintf ( f, " <author>%s</author>\n", tmp );
+ g_free ( tmp );
+ }
+ if ( md->description ) {
+ tmp = entitize ( md->description );
+ fprintf ( f, " <desc>%s</desc>\n", tmp );
+ g_free ( tmp );
+ }
+ if ( md->timestamp ) {
+ tmp = entitize ( md->timestamp );
+ fprintf ( f, " <time>%s</time>\n", tmp );
+ g_free ( tmp );
+ }
+ if ( md->keywords ) {
+ tmp = entitize ( md->keywords );
+ fprintf ( f, " <keywords>%s</keywords>\n", tmp );
+ g_free ( tmp );
+ }
+ }
+
+ // 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 );
+
+ GList *iter;
+ for (iter = g_list_first (gl); iter != NULL; iter = g_list_next (iter)) {
+ gpx_write_waypoint ( (VikWaypoint*)iter->data, &context );
+ }
+
+ g_list_free ( 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 );
+ }
+ 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 );
+
+ // 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 );
+
+ // g_list_concat doesn't copy memory properly
+ // so process each list separately
+
+ GpxWritingContext context_tmp = context;
+ 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
+ for (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;
+ for (iter = g_list_first (glrte); iter != NULL; iter = g_list_next (iter)) {
+ gpx_write_track ( (VikTrack*)iter->data, &context_tmp );
+ }
+
+ g_list_free ( gl );
+ g_list_free ( glrte );
+