]> git.street.me.uk Git - andy/viking.git/blobdiff - src/osm-traces.c
Fix waypoints may not be drawn when created by paste of a text location.
[andy/viking.git] / src / osm-traces.c
index 99ecd7ce693034890110866bb2a2d5a1b2c27f91..33df22c731f9a6c06e61a999099552c0b2ddc469 100644 (file)
@@ -52,12 +52,12 @@ static gint last_active = -1;
 /**
  * Login to use for OSM uploading.
  */
 /**
  * Login to use for OSM uploading.
  */
-static gchar *user = NULL;
+static gchar *osm_user = NULL;
 
 /**
  * Password to use for OSM uploading.
  */
 
 /**
  * Password to use for OSM uploading.
  */
-static gchar *password = NULL;
+static gchar *osm_password = NULL;
 
 /**
  * Mutex to protect auth. token
 
 /**
  * Mutex to protect auth. token
@@ -94,8 +94,8 @@ typedef struct _OsmTracesInfo {
 } OsmTracesInfo;
 
 static VikLayerParam prefs[] = {
 } OsmTracesInfo;
 
 static VikLayerParam prefs[] = {
-  { VIK_LAYER_NUM_TYPES, VIKING_OSM_TRACES_PARAMS_NAMESPACE "username", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("OSM username:"), VIK_LAYER_WIDGET_ENTRY, NULL, NULL, NULL },
-  { VIK_LAYER_NUM_TYPES, VIKING_OSM_TRACES_PARAMS_NAMESPACE "password", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("OSM password:"), VIK_LAYER_WIDGET_PASSWORD, NULL, NULL, NULL },
+  { VIK_LAYER_NUM_TYPES, VIKING_OSM_TRACES_PARAMS_NAMESPACE "username", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("OSM username:"), VIK_LAYER_WIDGET_ENTRY, NULL, NULL, NULL, NULL, NULL, NULL },
+  { VIK_LAYER_NUM_TYPES, VIKING_OSM_TRACES_PARAMS_NAMESPACE "password", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("OSM password:"), VIK_LAYER_WIDGET_PASSWORD, NULL, NULL, NULL, NULL, NULL, NULL },
 };
 
 /**
 };
 
 /**
@@ -125,7 +125,7 @@ static const gchar *get_default_user()
   return default_user;
 }
 
   return default_user;
 }
 
-void osm_set_login(const gchar *user_, const gchar *password_)
+void osm_set_login(const gchar *user, const gchar *password)
 {
   /* Allocate mutex */
   if (login_mutex == NULL)
 {
   /* Allocate mutex */
   if (login_mutex == NULL)
@@ -133,10 +133,10 @@ void osm_set_login(const gchar *user_, const gchar *password_)
     login_mutex = g_mutex_new();
   }
   g_mutex_lock(login_mutex);
     login_mutex = g_mutex_new();
   }
   g_mutex_lock(login_mutex);
-  g_free(user); user = NULL;
-  g_free(password); password = NULL;
-  user        = g_strdup(user_);
-  password    = g_strdup(password_);
+  g_free(osm_user); osm_user = NULL;
+  g_free(osm_password); osm_password = NULL;
+  osm_user        = g_strdup(user);
+  osm_password    = g_strdup(password);
   g_mutex_unlock(login_mutex);
 }
 
   g_mutex_unlock(login_mutex);
 }
 
@@ -144,7 +144,7 @@ gchar *osm_get_login()
 {
   gchar *user_pass = NULL;
   g_mutex_lock(login_mutex);
 {
   gchar *user_pass = NULL;
   g_mutex_lock(login_mutex);
-  user_pass = g_strdup_printf("%s:%s", user, password);
+  user_pass = g_strdup_printf("%s:%s", osm_user, osm_password);
   g_mutex_unlock(login_mutex);
   return user_pass;
 }
   g_mutex_unlock(login_mutex);
   return user_pass;
 }
@@ -265,25 +265,11 @@ static void osm_traces_upload_thread ( OsmTracesInfo *oti, gpointer threaddata )
   /* Due to OSM limits, we have to enforce ele and time fields
    also don't upload invisible tracks */
   static GpxWritingOptions options = { TRUE, TRUE, FALSE, FALSE };
   /* Due to OSM limits, we have to enforce ele and time fields
    also don't upload invisible tracks */
   static GpxWritingOptions options = { TRUE, TRUE, FALSE, FALSE };
-  FILE *file = NULL;
-  gchar *filename = NULL;
-  int fd;
-  GError *error = NULL;
-  int ret;
-
-  g_assert(oti != NULL);
 
 
-  /* Opening temporary file */
-  fd = g_file_open_tmp("viking_osm_upload_XXXXXX.gpx", &filename, &error);
-  if (fd < 0) {
-    g_error(_("failed to open temporary file: %s"), strerror(errno));
+  if (!oti)
     return;
     return;
-  }
-  g_clear_error(&error);
-  g_debug("%s: temporary file = %s", __FUNCTION__, filename);
 
 
-  /* Creating FILE* */
-  file = fdopen(fd, "w");
+  gchar *filename = NULL;
 
   /* writing gpx file */
   if (oti->trk != NULL)
 
   /* writing gpx file */
   if (oti->trk != NULL)
@@ -293,25 +279,23 @@ static void osm_traces_upload_thread ( OsmTracesInfo *oti, gpointer threaddata )
     {
       VikTrack *trk = vik_track_copy(oti->trk, TRUE);
       vik_track_anonymize_times(trk);
     {
       VikTrack *trk = vik_track_copy(oti->trk, TRUE);
       vik_track_anonymize_times(trk);
-      a_gpx_write_track_file(trk, file, &options);
+      filename = a_gpx_write_track_tmp_file(trk, &options);
       vik_track_free(trk);
     }
     else
       vik_track_free(trk);
     }
     else
-      a_gpx_write_track_file(oti->trk, file, &options);
+      filename = a_gpx_write_track_tmp_file (oti->trk, &options);
   }
   else
   {
     /* Upload the whole VikTrwLayer */
   }
   else
   {
     /* Upload the whole VikTrwLayer */
-    a_gpx_write_file(oti->vtl, file, &options);
+    filename = a_gpx_write_tmp_file (oti->vtl, &options);
   }
   
   }
   
-  /* We can close the file */
-  /* This also close the associated fd */
-  fclose(file);
-  file = NULL;
+  if ( !filename )
+    return;
 
   /* finally, upload it */
 
   /* finally, upload it */
-  gint ans = osm_traces_upload_file(user, password, filename,
+  gint ans = osm_traces_upload_file(osm_user, osm_password, filename,
                    oti->name, oti->description, oti->tags, oti->vistype);
 
   //
                    oti->name, oti->description, oti->tags, oti->vistype);
 
   //
@@ -350,7 +334,7 @@ static void osm_traces_upload_thread ( OsmTracesInfo *oti, gpointer threaddata )
     g_free (msg);
   }
   /* Removing temporary file */
     g_free (msg);
   }
   /* Removing temporary file */
-  ret = g_unlink(filename);
+  int ret = g_unlink(filename);
   if (ret != 0) {
     g_critical(_("failed to unlink temporary file: %s"), strerror(errno));
   }
   if (ret != 0) {
     g_critical(_("failed to unlink temporary file: %s"), strerror(errno));
   }
@@ -368,15 +352,15 @@ void osm_login_widgets (GtkWidget *user_entry, GtkWidget *password_entry)
   const gchar *pref_user = a_preferences_get(VIKING_OSM_TRACES_PARAMS_NAMESPACE "username")->s;
   const gchar *pref_password = a_preferences_get(VIKING_OSM_TRACES_PARAMS_NAMESPACE "password")->s;
 
   const gchar *pref_user = a_preferences_get(VIKING_OSM_TRACES_PARAMS_NAMESPACE "username")->s;
   const gchar *pref_password = a_preferences_get(VIKING_OSM_TRACES_PARAMS_NAMESPACE "password")->s;
 
-  if (user != NULL && user[0] != '\0')
-    gtk_entry_set_text(GTK_ENTRY(user_entry), user);
+  if (osm_user != NULL && osm_user[0] != '\0')
+    gtk_entry_set_text(GTK_ENTRY(user_entry), osm_user);
   else if (pref_user != NULL && pref_user[0] != '\0')
     gtk_entry_set_text(GTK_ENTRY(user_entry), pref_user);
   else if (default_user != NULL)
     gtk_entry_set_text(GTK_ENTRY(user_entry), default_user);
 
   else if (pref_user != NULL && pref_user[0] != '\0')
     gtk_entry_set_text(GTK_ENTRY(user_entry), pref_user);
   else if (default_user != NULL)
     gtk_entry_set_text(GTK_ENTRY(user_entry), default_user);
 
-  if (password != NULL && password[0] != '\0')
-    gtk_entry_set_text(GTK_ENTRY(password_entry), password);
+  if (osm_password != NULL && osm_password[0] != '\0')
+    gtk_entry_set_text(GTK_ENTRY(password_entry), osm_password);
   else if (pref_password != NULL)
     gtk_entry_set_text(GTK_ENTRY(password_entry), pref_password);
   /* This is a password -> invisible */
   else if (pref_password != NULL)
     gtk_entry_set_text(GTK_ENTRY(password_entry), pref_password);
   /* This is a password -> invisible */
@@ -444,6 +428,15 @@ void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl, VikTrack *trk )
 
   description_label = gtk_label_new(_("Description:"));
   description_entry = gtk_entry_new();
 
   description_label = gtk_label_new(_("Description:"));
   description_entry = gtk_entry_new();
+  const gchar *description = NULL;
+  if (trk != NULL)
+    description = trk->description;
+  else {
+    VikTRWMetadata *md = vik_trw_layer_get_metadata (vtl);
+    description = md ? md->description : NULL;
+  }
+  if (description)
+    gtk_entry_set_text(GTK_ENTRY(description_entry), description);
   gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), description_label, FALSE, FALSE, 0);
   gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), description_entry, FALSE, FALSE, 0);
   gtk_widget_set_tooltip_text(GTK_WIDGET(description_entry),
   gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), description_label, FALSE, FALSE, 0);
   gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), description_entry, FALSE, FALSE, 0);
   gtk_widget_set_tooltip_text(GTK_WIDGET(description_entry),
@@ -461,6 +454,9 @@ void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl, VikTrack *trk )
 
   tags_label = gtk_label_new(_("Tags:"));
   tags_entry = gtk_entry_new();
 
   tags_label = gtk_label_new(_("Tags:"));
   tags_entry = gtk_entry_new();
+  VikTRWMetadata *md = vik_trw_layer_get_metadata (vtl);
+  if (md->keywords)
+    gtk_entry_set_text(GTK_ENTRY(tags_entry), md->keywords);
   gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), tags_label, FALSE, FALSE, 0);
   gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), tags_entry, FALSE, FALSE, 0);
   gtk_widget_set_tooltip_text(GTK_WIDGET(tags_entry),
   gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), tags_label, FALSE, FALSE, 0);
   gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), tags_entry, FALSE, FALSE, 0);
   gtk_widget_set_tooltip_text(GTK_WIDGET(tags_entry),