+ 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 );
+ }
+ }
+
+ 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 );
+
+ 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 );
+ }
+
+ 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 );
+
+ // 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
+ 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;
+ 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 (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;
+ for (GList *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 );