#include <stdio.h>
#include <string.h>
#include <errno.h>
+#include <time.h>
#include <curl/curl.h>
-#include <curl/types.h>
#include <curl/easy.h>
#include <glib.h>
gchar *tags;
const OsmTraceVis_t *vistype;
VikTrwLayer *vtl;
- gchar *track_name;
+ VikTrack *trk;
} OsmTracesInfo;
static VikLayerParam prefs[] = {
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;
}
/* initialisation */
void osm_traces_init () {
/* Preferences */
- a_preferences_register_group ( VIKING_OSM_TRACES_PARAMS_GROUP_KEY, "OpenStreetMap traces" );
+ a_preferences_register_group ( VIKING_OSM_TRACES_PARAMS_GROUP_KEY, _("OpenStreetMap Traces") );
VikLayerParamData tmp;
tmp.s = "";
/*
* Upload a file
- */
-void osm_traces_upload_file(const char *user,
- const char *password,
- const char *file,
- const char *filename,
- const char *description,
- const char *tags,
- const OsmTraceVis_t *vistype)
+ * returns a basic status:
+ * < 0 : curl error
+ * == 0 : OK
+ * > 0 : HTTP error
+ */
+static gint osm_traces_upload_file(const char *user,
+ const char *password,
+ const char *file,
+ const char *filename,
+ const char *description,
+ const char *tags,
+ const OsmTraceVis_t *vistype)
{
CURL *curl;
CURLcode res;
gchar *user_pass = get_login();
+ gint result = 0; // Default to it worked!
+
g_debug("%s: %s %s %s %s %s %s", __FUNCTION__,
user, password, file, filename, description, tags);
if (res == CURLE_OK)
{
g_debug("received valid curl response: %ld", code);
- if (code != 200)
+ if (code != 200) {
g_warning(_("failed to upload data: HTTP response is %ld"), code);
+ result = code;
+ }
}
- else
- g_error(_("curl_easy_getinfo failed: %d"), res);
- }
- else
- {
- g_warning(_("curl request failed: %s"), curl_error_buffer);
+ else {
+ g_critical(_("curl_easy_getinfo failed: %d"), res);
+ result = -1;
}
+ }
+ else {
+ g_warning(_("curl request failed: %s"), curl_error_buffer);
+ result = -2;
+ }
/* Memory */
g_free(user_pass); user_pass = NULL;
curl_formfree(post);
- curl_easy_cleanup(curl);
+ curl_easy_cleanup(curl);
+ return result;
}
/**
file = fdopen(fd, "w");
/* writing gpx file */
- if (oti->track_name != NULL)
+ if (oti->trk != 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);
+ a_gpx_write_track_file_options(&options, oti->trk, file);
}
else
{
file = NULL;
/* finally, upload it */
- osm_traces_upload_file(user, password, filename,
- oti->name, oti->description, oti->tags, oti->vistype);
-
+ gint ans = osm_traces_upload_file(user, password, filename,
+ oti->name, oti->description, oti->tags, oti->vistype);
+
+ //
+ // Show result in statusbar or failure in dialog for user feedback
+ //
+
+ // Get current time to put into message to show when result was generated
+ // since need to show difference between operations (when displayed on statusbar)
+ // NB If on dialog then don't need time.
+ time_t timenow;
+ struct tm* timeinfo;
+ time ( &timenow );
+ timeinfo = localtime ( &timenow );
+ gchar timestr[80];
+ // Compact time only - as days/date isn't very useful here
+ strftime ( timestr, sizeof(timestr), "%X)", timeinfo );
+
+ //
+ // Test to see if window it was invoked on is still valid
+ // Not sure if this test really works! (i.e. if the window was closed in the mean time)
+ //
+ if ( IS_VIK_WINDOW ((VikWindow *)VIK_GTK_WINDOW_FROM_LAYER(oti->vtl)) ) {
+ gchar* msg;
+ if ( ans == 0 ) {
+ // Success
+ msg = g_strdup_printf ( "%s (@%s)", _("Uploaded to OSM"), timestr );
+ }
+ // Use UPPER CASE for bad news :(
+ else if ( ans < 0 ) {
+ msg = g_strdup_printf ( "%s (@%s)", _("FAILED TO UPLOAD DATA TO OSM - CURL PROBLEM"), timestr );
+ }
+ else {
+ msg = g_strdup_printf ( "%s : %s %d (@%s)", _("FAILED TO UPLOAD DATA TO OSM"), _("HTTP response code"), ans, timestr );
+ }
+ // From the background so should use the signalling method to update display:
+ // TODO: This only works with statically assigned strings ATM
+ vik_window_signal_statusbar_update ( (VikWindow*)VIK_GTK_WINDOW_FROM_LAYER(oti->vtl), msg, VIK_STATUSBAR_INFO );
+ // Thus can't free the memory yet...
+ // Luckily OSM traces isn't heavily used so this is not a significant memory leak
+ //g_free (msg);
+ // But this is better than potentially crashing from multi thread GUI updates
+ }
/* Removing temporary file */
ret = g_unlink(filename);
if (ret != 0) {
- g_error(_("failed to unlink temporary file: %s"), strerror(errno));
+ g_critical(_("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
+ * @param trk if not null, the track to upload
*/
-static void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl, const gchar *track_name )
+static void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl, VikTrack *trk )
{
GtkWidget *dia = gtk_dialog_new_with_buttons (_("OSM upload"),
VIK_GTK_WINDOW_FROM_LAYER(vtl),
name_label = gtk_label_new(_("File's name:"));
name_entry = gtk_entry_new();
- if (track_name != NULL)
- name = track_name;
+ if (trk != NULL)
+ name = trk->name;
else
name = vik_layer_get_name(VIK_LAYER(vtl));
gtk_entry_set_text(GTK_ENTRY(name_entry), name);
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 );
info->tags = g_strdup(gtk_entry_get_text(GTK_ENTRY(tags_entry)));
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);
+ info->trk = trk;
title = g_strdup_printf(_("Uploading %s to OSM"), info->name);
/**
* Function called by the entry menu of a single track
*/
-void osm_traces_upload_track_cb ( gpointer pass_along[6] )
+// TODO: Fix this dodgy usage of magic 8 ball array sized numbering
+// At least have some common definition somewhere...
+void osm_traces_upload_track_cb ( gpointer pass_along[8] )
{
- osm_traces_upload_viktrwlayer(VIK_TRW_LAYER(pass_along[0]), pass_along[3]);
+ if ( pass_along[7] ) {
+ VikTrack *trk = VIK_TRACK(pass_along[7]);
+ osm_traces_upload_viktrwlayer(VIK_TRW_LAYER(pass_along[0]), trk);
+ }
}