]> git.street.me.uk Git - andy/viking.git/blobdiff - src/osm-traces.c
Remove dependencies to gob2
[andy/viking.git] / src / osm-traces.c
index 2a68004fdf2db2be8dc4601059c50e162950f997..cd410f396fff986ae0801efb13e64a1ed0669c38 100644 (file)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 
 #include <stdio.h>
 #include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
 #include <errno.h>
 
 #include <curl/curl.h>
 
 #include <glib.h>
 #include <glib/gstdio.h>
+#include <glib/gi18n.h>
 
 #include "viking.h"
 #include "viktrwlayer.h"
 #include "osm-traces.h"
 #include "gpx.h"
 #include "background.h"
+#include "preferences.h"
+
+/* params will be osm_traces.username, osm_traces.password */
+/* we have to make sure these don't collide. */
+#define VIKING_OSM_TRACES_PARAMS_GROUP_KEY "osm_traces"
+#define VIKING_OSM_TRACES_PARAMS_NAMESPACE "osm_traces."
 
 /**
  * Login to use for OSM uploading.
@@ -63,8 +70,14 @@ typedef struct _OsmTracesInfo {
   gchar *tags;
   gboolean public;
   VikTrwLayer *vtl;
+  gchar *track_name;
 } OsmTracesInfo;
 
+static VikLayerParam prefs[] = {
+  { VIKING_OSM_TRACES_PARAMS_NAMESPACE "username", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("OSM username:"), VIK_LAYER_WIDGET_ENTRY },
+  { VIKING_OSM_TRACES_PARAMS_NAMESPACE "password", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("OSM password:"), VIK_LAYER_WIDGET_PASSWORD },
+};
+
 /**
  * Free an OsmTracesInfo struct.
  */
@@ -75,6 +88,7 @@ static void oti_free(OsmTracesInfo *oti)
     g_free(oti->name); oti->name = NULL;
     g_free(oti->description); oti->description = NULL;
     g_free(oti->tags); oti->tags = NULL;
+    g_free(oti->track_name); oti->track_name = NULL;
     
     g_object_unref(oti->vtl); oti->vtl = NULL;
   }
@@ -82,6 +96,16 @@ static void oti_free(OsmTracesInfo *oti)
   g_free(oti);
 }
 
+static const gchar *get_default_user()
+{
+  const gchar *default_user = NULL;
+
+  /* Retrieve "standard" EMAIL varenv */
+  default_user = g_getenv("EMAIL");
+
+  return default_user;
+}
+
 static void set_login(const gchar *user_, const gchar *password_)
 {
   /* Allocate mutex */
@@ -106,6 +130,19 @@ static gchar *get_login()
   return user_pass;
 }
 
+/* initialisation */
+void osm_traces_init () {
+  /* Preferences */
+  a_preferences_register_group ( VIKING_OSM_TRACES_PARAMS_GROUP_KEY, "OpenStreetMap traces" );
+
+  VikLayerParamData tmp;
+  tmp.s = "";
+  a_preferences_register(prefs, tmp, VIKING_OSM_TRACES_PARAMS_GROUP_KEY);
+  tmp.s = "";
+  a_preferences_register(prefs+1, tmp, VIKING_OSM_TRACES_PARAMS_GROUP_KEY);
+
+}
+
 /*
  * Upload a file
  */
@@ -125,7 +162,7 @@ void osm_traces_upload_file(const char *user,
   struct curl_httppost *last=NULL;
   gchar *public_string;
 
-  char *base_url = "http://www.openstreetmap.org/api/0.4/gpx/create";
+  char *base_url = "http://www.openstreetmap.org/api/0.5/gpx/create";
 
   gchar *user_pass = get_login();
 
@@ -142,7 +179,6 @@ void osm_traces_upload_file(const char *user,
   curl_formadd(&post, &last,
                CURLFORM_COPYNAME, "tags",
                CURLFORM_COPYCONTENTS, tags, CURLFORM_END);
-  /* Public field fixed to "1" actually */
   if (public)
     public_string = "1";
   else
@@ -166,6 +202,8 @@ void osm_traces_upload_file(const char *user,
   curl_easy_setopt(curl, CURLOPT_USERPWD, user_pass);
   curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
   curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &curl_error_buffer);
+  if (vik_verbose)
+    curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
 
   /* Execute request */
   res = curl_easy_perform(curl);
@@ -177,14 +215,14 @@ void osm_traces_upload_file(const char *user,
     {
       g_debug("received valid curl response: %ld", code);
       if (code != 200)
-        g_warning("failed to upload data: HTTP response is %ld", code);
+        g_warning(_("failed to upload data: HTTP response is %ld"), code);
     }
     else
-      g_error("curl_easy_getinfo failed: %d", res);
+      g_error(_("curl_easy_getinfo failed: %d"), res);
   }
   else
     {
-      g_warning("curl request failed: %s", curl_error_buffer);
+      g_warning(_("curl request failed: %s"), curl_error_buffer);
     }
 
   /* Memory */
@@ -199,6 +237,8 @@ void osm_traces_upload_file(const char *user,
  */
 static void osm_traces_upload_thread ( OsmTracesInfo *oti, gpointer threaddata )
 {
+  /* Due to OSM limits, we have to enforce ele and time fields */
+  static GpxWritingOptions options = { TRUE, TRUE };
   FILE *file = NULL;
   gchar *filename = NULL;
   int fd;
@@ -210,7 +250,7 @@ static void osm_traces_upload_thread ( OsmTracesInfo *oti, gpointer threaddata )
   /* 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));
+    g_error(_("failed to open temporary file: %s"), strerror(errno));
     return;
   }
   g_clear_error(&error);
@@ -220,11 +260,22 @@ static void osm_traces_upload_thread ( OsmTracesInfo *oti, gpointer threaddata )
   file = fdopen(fd, "w");
 
   /* writing gpx file */
-  a_gpx_write_file(oti->vtl, file);
+  if (oti->track_name != NULL)
+  {
+    /* Upload only the selected track */
+    VikTrack *track = vik_trw_layer_get_track(oti->vtl, oti->track_name);
+    a_gpx_write_track_file_options(&options, oti->track_name, track, file);
+  }
+  else
+  {
+    /* Upload the whole VikTrwLayer */
+    a_gpx_write_file_options(&options, oti->vtl, file);
+  }
   
   /* We can close the file */
   /* This also close the associated fd */
   fclose(file);
+  file = NULL;
 
   /* finally, upload it */
   osm_traces_upload_file(user, password, filename,
@@ -233,16 +284,19 @@ static void osm_traces_upload_thread ( OsmTracesInfo *oti, gpointer threaddata )
   /* Removing temporary file */
   ret = g_unlink(filename);
   if (ret != 0) {
-    g_error("failed to unlink temporary file: %s", strerror(errno));
+    g_error(_("failed to unlink temporary file: %s"), strerror(errno));
   }
 }
 
 /**
  * Uploading a VikTrwLayer
+ *
+ * @param vtl VikTrwLayer
+ * @param track_name if not null, the name of the track to upload
  */
-static void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl )
+static void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl, const gchar *track_name )
 {
-  GtkWidget *dia = gtk_dialog_new_with_buttons ("OSM upload",
+  GtkWidget *dia = gtk_dialog_new_with_buttons (_("OSM upload"),
                                                  VIK_GTK_WINDOW_FROM_LAYER(vtl),
                                                  GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                                  GTK_STOCK_CANCEL,
@@ -251,6 +305,9 @@ static void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl )
                                                  GTK_RESPONSE_ACCEPT,
                                                  NULL);
 
+  const gchar *default_user = get_default_user();
+  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 *name = NULL;
   GtkWidget *user_label, *user_entry;
   GtkWidget *password_label, *password_entry;
@@ -262,22 +319,28 @@ static void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl )
 
   dialog_tips = gtk_tooltips_new();
 
-  user_label = gtk_label_new("Email:");
+  user_label = gtk_label_new(_("Email:"));
   user_entry = gtk_entry_new();
-  if (user != NULL)
+  if (user != NULL && user[0] != '\0')
     gtk_entry_set_text(GTK_ENTRY(user_entry), 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);
   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), user_label, FALSE, FALSE, 0);
   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), user_entry, FALSE, FALSE, 0);
   gtk_widget_show_all ( user_label );
   gtk_widget_show_all ( user_entry );
   gtk_tooltips_set_tip (dialog_tips, user_entry,
-                        "The email used as login",
-                        "Enter the email you use to login into www.openstreetmap.org.");
+                        _("The email used as login"),
+                        _("Enter the email you use to login into www.openstreetmap.org."));
 
-  password_label = gtk_label_new("Password:");
+  password_label = gtk_label_new(_("Password:"));
   password_entry = gtk_entry_new();
-  if (password != NULL)
+  if (password != NULL && password[0] != '\0')
     gtk_entry_set_text(GTK_ENTRY(password_entry), password);
+  else if (pref_password != NULL)
+    gtk_entry_set_text(GTK_ENTRY(password_entry), pref_password);
   /* This is a password -> invisible */
   gtk_entry_set_visibility(GTK_ENTRY(password_entry), FALSE);
   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), password_label, FALSE, FALSE, 0);
@@ -285,49 +348,52 @@ static void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl )
   gtk_widget_show_all ( password_label );
   gtk_widget_show_all ( password_entry );
   gtk_tooltips_set_tip (dialog_tips, password_entry,
-                        "The password used to login",
-                        "Enter the password you use to login into www.openstreetmap.org.");
+                        _("The password used to login"),
+                        _("Enter the password you use to login into www.openstreetmap.org."));
 
-  name_label = gtk_label_new("File's name:");
+  name_label = gtk_label_new(_("File's name:"));
   name_entry = gtk_entry_new();
-  name = vik_layer_get_name(VIK_LAYER(vtl));
+  if (track_name != NULL)
+    name = track_name;
+  else
+    name = vik_layer_get_name(VIK_LAYER(vtl));
   gtk_entry_set_text(GTK_ENTRY(name_entry), name);
   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), name_label, FALSE, FALSE, 0);
   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), name_entry, FALSE, FALSE, 0);
   gtk_widget_show_all ( name_label );
   gtk_widget_show_all ( name_entry );
   gtk_tooltips_set_tip (dialog_tips, name_entry,
-                        "The name of the file on OSM",
-                        "This is the name of the file created on the server. "
-                       "This is not the name of the local file.");
+                        _("The name of the file on OSM"),
+                        _("This is the name of the file created on the server. "
+                       "This is not the name of the local file."));
 
-  description_label = gtk_label_new("Description:");
+  description_label = gtk_label_new(_("Description:"));
   description_entry = gtk_entry_new();
   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), description_label, FALSE, FALSE, 0);
   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), description_entry, FALSE, FALSE, 0);
   gtk_widget_show_all ( description_label );
   gtk_widget_show_all ( description_entry );
   gtk_tooltips_set_tip (dialog_tips, description_entry,
-                        "The description of the trace",
+                        _("The description of the trace"),
                         "");
 
-  tags_label = gtk_label_new("Tags:");
+  tags_label = gtk_label_new(_("Tags:"));
   tags_entry = gtk_entry_new();
   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), tags_label, FALSE, FALSE, 0);
   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), tags_entry, FALSE, FALSE, 0);
   gtk_widget_show_all ( tags_label );
   gtk_widget_show_all ( tags_entry );
   gtk_tooltips_set_tip (dialog_tips, tags_entry,
-                        "The tags associated to the trace",
+                        _("The tags associated to the trace"),
                         "");
 
-  public = gtk_check_button_new_with_label("Public");
+  public = gtk_check_button_new_with_label(_("Public"));
   /* Set public by default */
   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(public), TRUE);
   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), public, FALSE, FALSE, 0);
   gtk_widget_show_all ( public );
   gtk_tooltips_set_tip (dialog_tips, public,
-                        "Indicates if the trace is public or not",
+                        _("Indicates if the trace is public or not"),
                         "");
 
   if ( gtk_dialog_run ( GTK_DIALOG(dia) ) == GTK_RESPONSE_ACCEPT )
@@ -346,8 +412,9 @@ static void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl )
     info->tags        = g_strdup(gtk_entry_get_text(GTK_ENTRY(tags_entry)));
     info->public      = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(public));
     info->vtl         = VIK_TRW_LAYER(g_object_ref(vtl));
+    info->track_name  = (track_name == NULL) ? NULL : g_strdup(track_name);
 
-    title = g_strdup_printf("Uploading %s to OSM", info->name);
+    title = g_strdup_printf(_("Uploading %s to OSM"), info->name);
 
     /* launch the thread */
     a_background_thread(VIK_GTK_WINDOW_FROM_LAYER(vtl),          /* parent window */
@@ -363,9 +430,17 @@ static void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl )
 }
 
 /**
- * Function called by the button
+ * Function called by the entry menu of a TrwLayer
  */
 void osm_traces_upload_cb ( gpointer layer_and_vlp[2], guint file_type )
 {
-  osm_traces_upload_viktrwlayer(VIK_TRW_LAYER(layer_and_vlp[0]));
+  osm_traces_upload_viktrwlayer(VIK_TRW_LAYER(layer_and_vlp[0]), NULL);
+}
+
+/**
+ * Function called by the entry menu of a single track
+ */
+void osm_traces_upload_track_cb ( gpointer pass_along[6] )
+{
+  osm_traces_upload_viktrwlayer(VIK_TRW_LAYER(pass_along[0]), pass_along[3]);
 }