]> git.street.me.uk Git - andy/viking.git/blobdiff - src/osm-traces.c
Add support for kml files.
[andy/viking.git] / src / osm-traces.c
index 8980dfa982ea5ab0a0c1ef9514ff22d50928e2eb..3357b2f1c6c0c7c4809a2377aec9e854f59746a8 100644 (file)
 #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.
@@ -55,6 +61,22 @@ static gchar *password = NULL;
  */
 static GMutex *login_mutex = NULL;
 
+/**
+ * Different type of trace visibility.
+ */
+typedef struct _OsmTraceVis_t {
+       const gchar *combostr;
+       const gchar *apistr;
+} OsmTraceVis_t;
+
+static const OsmTraceVis_t OsmTraceVis[] = {
+       { N_("Identifiable (public w/ timestamps)"),    "identifiable" },
+       { N_("Trackable (private w/ timestamps)"),      "trackable" },
+       { N_("Public"),                                 "public" },
+       { N_("Private"),                                "private" },
+       { NULL, NULL },
+};
+
 /**
  * Struct hosting needed info.
  */
@@ -62,11 +84,16 @@ typedef struct _OsmTracesInfo {
   gchar *name;
   gchar *description;
   gchar *tags;
-  gboolean public;
+  const OsmTraceVis_t *vistype;
   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.
  */
@@ -119,6 +146,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
  */
@@ -128,7 +168,7 @@ void osm_traces_upload_file(const char *user,
                            const char *filename,
                            const char *description,
                            const char *tags,
-                           gboolean public)
+                           const OsmTraceVis_t *vistype)
 {
   CURL *curl;
   CURLcode res;
@@ -136,9 +176,8 @@ void osm_traces_upload_file(const char *user,
   struct curl_slist *headers = NULL;
   struct curl_httppost *post=NULL;
   struct curl_httppost *last=NULL;
-  gchar *public_string;
 
-  char *base_url = "http://www.openstreetmap.org/api/0.5/gpx/create";
+  char *base_url = "http://www.openstreetmap.org/api/0.6/gpx/create";
 
   gchar *user_pass = get_login();
 
@@ -155,13 +194,9 @@ void osm_traces_upload_file(const char *user,
   curl_formadd(&post, &last,
                CURLFORM_COPYNAME, "tags",
                CURLFORM_COPYCONTENTS, tags, CURLFORM_END);
-  if (public)
-    public_string = "1";
-  else
-    public_string = "0";
   curl_formadd(&post, &last,
-               CURLFORM_COPYNAME, "public",
-               CURLFORM_COPYCONTENTS, public_string, CURLFORM_END);
+               CURLFORM_COPYNAME, "visibility",
+               CURLFORM_COPYCONTENTS, vistype->apistr, CURLFORM_END);
   curl_formadd(&post, &last,
                CURLFORM_COPYNAME, "file",
                CURLFORM_FILE, file,
@@ -177,7 +212,9 @@ void osm_traces_upload_file(const char *user,
   curl_easy_setopt(curl, CURLOPT_URL, base_url);
   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);
+  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);
@@ -253,7 +290,7 @@ static void osm_traces_upload_thread ( OsmTracesInfo *oti, gpointer threaddata )
 
   /* finally, upload it */
   osm_traces_upload_file(user, password, filename,
-                         oti->name, oti->description, oti->tags, oti->public);
+                         oti->name, oti->description, oti->tags, oti->vistype);
 
   /* Removing temporary file */
   ret = g_unlink(filename);
@@ -279,48 +316,45 @@ static void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl, const gchar *track
                                                  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;
   GtkWidget *name_label, *name_entry;
   GtkWidget *description_label, *description_entry;
   GtkWidget *tags_label, *tags_entry;
-  GtkWidget *public;
-  GtkTooltips* dialog_tips;
-
-  dialog_tips = gtk_tooltips_new();
+  GtkComboBox *visibility;
+  const OsmTraceVis_t *vis_t;
 
   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
-  {
-    const gchar *default_user = get_default_user();
-    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);
   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."));
+  gtk_widget_set_tooltip_markup(GTK_WIDGET(user_entry),
+                        _("The email used as login\n"
+                        "<small>Enter the email you use to login into www.openstreetmap.org.</small>"));
 
   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);
   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), password_entry, FALSE, FALSE, 0);
-  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."));
+  gtk_widget_set_tooltip_markup(GTK_WIDGET(password_entry),
+                        _("The password used to login\n"
+                        "<small>Enter the password you use to login into www.openstreetmap.org.</small>"));
 
   name_label = gtk_label_new(_("File's name:"));
   name_entry = gtk_entry_new();
@@ -331,41 +365,37 @@ static void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl, const gchar *track
   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."));
+  gtk_widget_set_tooltip_markup(GTK_WIDGET(name_entry),
+                        _("The name of the file on OSM\n"
+                        "<small>This is the name of the file created on the server."
+                       "This is not the name of the local file.</small>"));
 
   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"),
-                        "");
+  gtk_widget_set_tooltip_text(GTK_WIDGET(description_entry),
+                        _("The description of the trace"));
 
   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"),
-                        "");
-
-  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"),
-                        "");
+  gtk_widget_set_tooltip_text(GTK_WIDGET(tags_entry),
+                        _("The tags associated to the trace"));
+
+  visibility = GTK_COMBO_BOX(gtk_combo_box_new_text ());
+  for (vis_t = OsmTraceVis; vis_t->combostr != NULL; vis_t++)
+       gtk_combo_box_append_text(visibility, vis_t->combostr);
+  /* Set identifiable by default */
+  gtk_combo_box_set_active(visibility, 0);
+  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), GTK_WIDGET(visibility), FALSE, FALSE, 0);
+
+  /* User should think about it first... */
+  gtk_dialog_set_default_response ( GTK_DIALOG(dia), GTK_RESPONSE_REJECT );
+
+  gtk_widget_show_all ( dia );
+  gtk_widget_grab_focus ( description_entry );
 
   if ( gtk_dialog_run ( GTK_DIALOG(dia) ) == GTK_RESPONSE_ACCEPT )
   {
@@ -381,7 +411,7 @@ static void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl, const gchar *track
     info->description = g_strdup(gtk_entry_get_text(GTK_ENTRY(description_entry)));
     /* TODO Normalize tags: they will be used as URL part */
     info->tags        = g_strdup(gtk_entry_get_text(GTK_ENTRY(tags_entry)));
-    info->public      = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(public));
+    info->vistype     = &OsmTraceVis[gtk_combo_box_get_active(visibility)];
     info->vtl         = VIK_TRW_LAYER(g_object_ref(vtl));
     info->track_name  = (track_name == NULL) ? NULL : g_strdup(track_name);