]> git.street.me.uk Git - andy/viking.git/blame - src/osm-traces.c
Fix error identified by cppcheck 1.52.
[andy/viking.git] / src / osm-traces.c
CommitLineData
3e7553ae
GB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
4c77d5e0
GB
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
3e7553ae
GB
24
25#include <stdio.h>
26#include <string.h>
3e7553ae 27#include <errno.h>
36420b5f 28#include <time.h>
3e7553ae
GB
29
30#include <curl/curl.h>
3e7553ae
GB
31#include <curl/easy.h>
32
33#include <glib.h>
34#include <glib/gstdio.h>
4c77d5e0 35#include <glib/gi18n.h>
3e7553ae
GB
36
37#include "viking.h"
38#include "viktrwlayer.h"
39#include "osm-traces.h"
40#include "gpx.h"
41#include "background.h"
843b99df
GB
42#include "preferences.h"
43
9de6196a 44/* params will be osm_traces.username, osm_traces.password */
843b99df
GB
45/* we have to make sure these don't collide. */
46#define VIKING_OSM_TRACES_PARAMS_GROUP_KEY "osm_traces"
47#define VIKING_OSM_TRACES_PARAMS_NAMESPACE "osm_traces."
3e7553ae
GB
48
49/**
50 * Login to use for OSM uploading.
51 */
52static gchar *user = NULL;
53
54/**
55 * Password to use for OSM uploading.
56 */
57static gchar *password = NULL;
58
59/**
60 * Mutex to protect auth. token
61 */
62static GMutex *login_mutex = NULL;
63
8b589250
GS
64/**
65 * Different type of trace visibility.
66 */
67typedef struct _OsmTraceVis_t {
68 const gchar *combostr;
69 const gchar *apistr;
70} OsmTraceVis_t;
71
72static const OsmTraceVis_t OsmTraceVis[] = {
502d25de
GB
73 { N_("Identifiable (public w/ timestamps)"), "identifiable" },
74 { N_("Trackable (private w/ timestamps)"), "trackable" },
75 { N_("Public"), "public" },
76 { N_("Private"), "private" },
8b589250
GS
77 { NULL, NULL },
78};
79
3e7553ae
GB
80/**
81 * Struct hosting needed info.
82 */
83typedef struct _OsmTracesInfo {
84 gchar *name;
85 gchar *description;
86 gchar *tags;
8b589250 87 const OsmTraceVis_t *vistype;
3e7553ae 88 VikTrwLayer *vtl;
5092de80 89 gchar *track_name;
3e7553ae
GB
90} OsmTracesInfo;
91
843b99df
GB
92static VikLayerParam prefs[] = {
93 { VIKING_OSM_TRACES_PARAMS_NAMESPACE "username", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("OSM username:"), VIK_LAYER_WIDGET_ENTRY },
dd8bde3a 94 { VIKING_OSM_TRACES_PARAMS_NAMESPACE "password", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("OSM password:"), VIK_LAYER_WIDGET_PASSWORD },
843b99df
GB
95};
96
3e7553ae
GB
97/**
98 * Free an OsmTracesInfo struct.
99 */
100static void oti_free(OsmTracesInfo *oti)
101{
102 if (oti) {
103 /* Fields have been g_strdup'ed */
104 g_free(oti->name); oti->name = NULL;
105 g_free(oti->description); oti->description = NULL;
106 g_free(oti->tags); oti->tags = NULL;
5092de80 107 g_free(oti->track_name); oti->track_name = NULL;
3e7553ae
GB
108
109 g_object_unref(oti->vtl); oti->vtl = NULL;
110 }
111 /* Main struct has been g_malloc'ed */
112 g_free(oti);
113}
114
624318ca 115static const gchar *get_default_user()
51c15f41
GB
116{
117 const gchar *default_user = NULL;
118
119 /* Retrieve "standard" EMAIL varenv */
120 default_user = g_getenv("EMAIL");
121
122 return default_user;
123}
124
3e7553ae
GB
125static void set_login(const gchar *user_, const gchar *password_)
126{
127 /* Allocate mutex */
128 if (login_mutex == NULL)
129 {
130 login_mutex = g_mutex_new();
131 }
132 g_mutex_lock(login_mutex);
133 g_free(user); user = NULL;
134 g_free(password); password = NULL;
135 user = g_strdup(user_);
136 password = g_strdup(password_);
137 g_mutex_unlock(login_mutex);
138}
139
140static gchar *get_login()
141{
142 gchar *user_pass = NULL;
143 g_mutex_lock(login_mutex);
144 user_pass = g_strdup_printf("%s:%s", user, password);
145 g_mutex_unlock(login_mutex);
146 return user_pass;
147}
148
843b99df
GB
149/* initialisation */
150void osm_traces_init () {
151 /* Preferences */
d11de0ca 152 a_preferences_register_group ( VIKING_OSM_TRACES_PARAMS_GROUP_KEY, _("OpenStreetMap Traces") );
843b99df
GB
153
154 VikLayerParamData tmp;
155 tmp.s = "";
156 a_preferences_register(prefs, tmp, VIKING_OSM_TRACES_PARAMS_GROUP_KEY);
157 tmp.s = "";
158 a_preferences_register(prefs+1, tmp, VIKING_OSM_TRACES_PARAMS_GROUP_KEY);
159
160}
161
3e7553ae
GB
162/*
163 * Upload a file
36420b5f
RN
164 * returns a basic status:
165 * < 0 : curl error
166 * == 0 : OK
167 * > 0 : HTTP error
168 */
169static gint osm_traces_upload_file(const char *user,
170 const char *password,
171 const char *file,
172 const char *filename,
173 const char *description,
174 const char *tags,
175 const OsmTraceVis_t *vistype)
3e7553ae
GB
176{
177 CURL *curl;
178 CURLcode res;
179 char curl_error_buffer[CURL_ERROR_SIZE];
180 struct curl_slist *headers = NULL;
181 struct curl_httppost *post=NULL;
182 struct curl_httppost *last=NULL;
3e7553ae 183
ffb5a285 184 char *base_url = "http://www.openstreetmap.org/api/0.6/gpx/create";
3e7553ae
GB
185
186 gchar *user_pass = get_login();
187
36420b5f
RN
188 gint result = 0; // Default to it worked!
189
3e7553ae
GB
190 g_debug("%s: %s %s %s %s %s %s", __FUNCTION__,
191 user, password, file, filename, description, tags);
192
193 /* Init CURL */
194 curl = curl_easy_init();
195
196 /* Filling the form */
197 curl_formadd(&post, &last,
198 CURLFORM_COPYNAME, "description",
199 CURLFORM_COPYCONTENTS, description, CURLFORM_END);
200 curl_formadd(&post, &last,
201 CURLFORM_COPYNAME, "tags",
202 CURLFORM_COPYCONTENTS, tags, CURLFORM_END);
3e7553ae 203 curl_formadd(&post, &last,
8b589250
GS
204 CURLFORM_COPYNAME, "visibility",
205 CURLFORM_COPYCONTENTS, vistype->apistr, CURLFORM_END);
3e7553ae
GB
206 curl_formadd(&post, &last,
207 CURLFORM_COPYNAME, "file",
208 CURLFORM_FILE, file,
209 CURLFORM_FILENAME, filename,
210 CURLFORM_CONTENTTYPE, "text/xml", CURLFORM_END);
211
212 /* Prepare request */
213 /* As explained in http://wiki.openstreetmap.org/index.php/User:LA2 */
214 /* Expect: header seems to produce incompatibilites between curl and httpd */
215 headers = curl_slist_append(headers, "Expect: ");
216 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
217 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
218 curl_easy_setopt(curl, CURLOPT_URL, base_url);
219 curl_easy_setopt(curl, CURLOPT_USERPWD, user_pass);
220 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
5a7d2873 221 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_error_buffer);
2936913d
GB
222 if (vik_verbose)
223 curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
3e7553ae
GB
224
225 /* Execute request */
226 res = curl_easy_perform(curl);
227 if (res == CURLE_OK)
228 {
229 long code;
230 res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
231 if (res == CURLE_OK)
232 {
233 g_debug("received valid curl response: %ld", code);
36420b5f 234 if (code != 200) {
4c77d5e0 235 g_warning(_("failed to upload data: HTTP response is %ld"), code);
36420b5f
RN
236 result = code;
237 }
3e7553ae 238 }
36420b5f 239 else {
4c77d5e0 240 g_error(_("curl_easy_getinfo failed: %d"), res);
36420b5f 241 result = -1;
3e7553ae 242 }
36420b5f
RN
243 }
244 else {
245 g_warning(_("curl request failed: %s"), curl_error_buffer);
246 result = -2;
247 }
3e7553ae
GB
248
249 /* Memory */
250 g_free(user_pass); user_pass = NULL;
251
252 curl_formfree(post);
36420b5f
RN
253 curl_easy_cleanup(curl);
254 return result;
3e7553ae
GB
255}
256
257/**
258 * uploading function executed by the background" thread
259 */
260static void osm_traces_upload_thread ( OsmTracesInfo *oti, gpointer threaddata )
261{
0b72c435
OK
262 /* Due to OSM limits, we have to enforce ele and time fields */
263 static GpxWritingOptions options = { TRUE, TRUE };
3e7553ae
GB
264 FILE *file = NULL;
265 gchar *filename = NULL;
266 int fd;
267 GError *error = NULL;
268 int ret;
269
270 g_assert(oti != NULL);
271
272 /* Opening temporary file */
273 fd = g_file_open_tmp("viking_osm_upload_XXXXXX.gpx", &filename, &error);
274 if (fd < 0) {
4c77d5e0 275 g_error(_("failed to open temporary file: %s"), strerror(errno));
3e7553ae
GB
276 return;
277 }
278 g_clear_error(&error);
279 g_debug("%s: temporary file = %s", __FUNCTION__, filename);
280
281 /* Creating FILE* */
282 file = fdopen(fd, "w");
283
284 /* writing gpx file */
5092de80
GB
285 if (oti->track_name != NULL)
286 {
287 /* Upload only the selected track */
288 VikTrack *track = vik_trw_layer_get_track(oti->vtl, oti->track_name);
0b72c435 289 a_gpx_write_track_file_options(&options, oti->track_name, track, file);
5092de80
GB
290 }
291 else
0b72c435 292 {
5092de80 293 /* Upload the whole VikTrwLayer */
0b72c435
OK
294 a_gpx_write_file_options(&options, oti->vtl, file);
295 }
3e7553ae
GB
296
297 /* We can close the file */
298 /* This also close the associated fd */
299 fclose(file);
8c060406 300 file = NULL;
3e7553ae
GB
301
302 /* finally, upload it */
36420b5f
RN
303 gint ans = osm_traces_upload_file(user, password, filename,
304 oti->name, oti->description, oti->tags, oti->vistype);
305
306 //
307 // Show result in statusbar or failure in dialog for user feedback
308 //
309
310 // Get current time to put into message to show when result was generated
311 // since need to show difference between operations (when displayed on statusbar)
312 // NB If on dialog then don't need time.
313 time_t timenow;
314 struct tm* timeinfo;
315 time ( &timenow );
316 timeinfo = localtime ( &timenow );
317 gchar timestr[80];
318 // Compact time only - as days/date isn't very useful here
319 strftime ( timestr, sizeof(timestr), "%X)", timeinfo );
320
321 //
322 // Test to see if window it was invoked on is still valid
323 // Not sure if this test really works! (i.e. if the window was closed in the mean time)
324 //
325 if ( IS_VIK_WINDOW ((VikWindow *)VIK_GTK_WINDOW_FROM_LAYER(oti->vtl)) ) {
326 gchar* msg;
327 if ( ans == 0 ) {
328 // Success
329 msg = g_strdup_printf ( "%s (@%s)", _("Uploaded to OSM"), timestr );
330 vik_statusbar_set_message ( vik_window_get_statusbar ( (VikWindow *)VIK_GTK_WINDOW_FROM_LAYER(oti->vtl) ), VIK_STATUSBAR_INFO, msg );
331 }
332 // Use UPPER CASE for bad news :(
333 else if ( ans < 0 ) {
334 msg = g_strdup_printf ( "%s (@%s)", _("FAILED TO UPLOAD DATA TO OSM - CURL PROBLEM"), timestr );
335 vik_statusbar_set_message ( vik_window_get_statusbar ( (VikWindow *)VIK_GTK_WINDOW_FROM_LAYER(oti->vtl) ), VIK_STATUSBAR_INFO, msg );
336 //a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_LAYER(oti->vtl), msg );
337 }
338 else {
339 msg = g_strdup_printf ( "%s : %s %d (@%s)", _("FAILED TO UPLOAD DATA TO OSM"), _("HTTP response code"), ans, timestr );
340 vik_statusbar_set_message ( vik_window_get_statusbar ( (VikWindow *)VIK_GTK_WINDOW_FROM_LAYER(oti->vtl) ), VIK_STATUSBAR_INFO, msg );
341 VikTrwLayer *vtl = oti->vtl;
342 // Crashes here - multi-thread issue...
343 //a_dialog_error_msg ( VIK_GTK_WINDOW_FROM_LAYER(vtl), msg );
344 }
345 g_free (msg);
346 }
3e7553ae
GB
347 /* Removing temporary file */
348 ret = g_unlink(filename);
349 if (ret != 0) {
4c77d5e0 350 g_error(_("failed to unlink temporary file: %s"), strerror(errno));
3e7553ae
GB
351 }
352}
353
354/**
355 * Uploading a VikTrwLayer
5092de80
GB
356 *
357 * @param vtl VikTrwLayer
358 * @param track_name if not null, the name of the track to upload
3e7553ae 359 */
5092de80 360static void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl, const gchar *track_name )
3e7553ae 361{
4c77d5e0 362 GtkWidget *dia = gtk_dialog_new_with_buttons (_("OSM upload"),
3e7553ae
GB
363 VIK_GTK_WINDOW_FROM_LAYER(vtl),
364 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
365 GTK_STOCK_CANCEL,
366 GTK_RESPONSE_REJECT,
367 GTK_STOCK_OK,
368 GTK_RESPONSE_ACCEPT,
369 NULL);
370
843b99df
GB
371 const gchar *default_user = get_default_user();
372 const gchar *pref_user = a_preferences_get(VIKING_OSM_TRACES_PARAMS_NAMESPACE "username")->s;
373 const gchar *pref_password = a_preferences_get(VIKING_OSM_TRACES_PARAMS_NAMESPACE "password")->s;
3e7553ae
GB
374 const gchar *name = NULL;
375 GtkWidget *user_label, *user_entry;
376 GtkWidget *password_label, *password_entry;
377 GtkWidget *name_label, *name_entry;
378 GtkWidget *description_label, *description_entry;
379 GtkWidget *tags_label, *tags_entry;
8b589250
GS
380 GtkComboBox *visibility;
381 const OsmTraceVis_t *vis_t;
3e7553ae 382
4c77d5e0 383 user_label = gtk_label_new(_("Email:"));
3e7553ae 384 user_entry = gtk_entry_new();
843b99df 385 if (user != NULL && user[0] != '\0')
3e7553ae 386 gtk_entry_set_text(GTK_ENTRY(user_entry), user);
843b99df
GB
387 else if (pref_user != NULL && pref_user[0] != '\0')
388 gtk_entry_set_text(GTK_ENTRY(user_entry), pref_user);
389 else if (default_user != NULL)
390 gtk_entry_set_text(GTK_ENTRY(user_entry), default_user);
3e7553ae
GB
391 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), user_label, FALSE, FALSE, 0);
392 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), user_entry, FALSE, FALSE, 0);
064a4ce9
SB
393 gtk_widget_set_tooltip_markup(GTK_WIDGET(user_entry),
394 _("The email used as login\n"
395 "<small>Enter the email you use to login into www.openstreetmap.org.</small>"));
3e7553ae 396
4c77d5e0 397 password_label = gtk_label_new(_("Password:"));
3e7553ae 398 password_entry = gtk_entry_new();
843b99df 399 if (password != NULL && password[0] != '\0')
3e7553ae 400 gtk_entry_set_text(GTK_ENTRY(password_entry), password);
843b99df
GB
401 else if (pref_password != NULL)
402 gtk_entry_set_text(GTK_ENTRY(password_entry), pref_password);
3e7553ae
GB
403 /* This is a password -> invisible */
404 gtk_entry_set_visibility(GTK_ENTRY(password_entry), FALSE);
405 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), password_label, FALSE, FALSE, 0);
406 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), password_entry, FALSE, FALSE, 0);
064a4ce9
SB
407 gtk_widget_set_tooltip_markup(GTK_WIDGET(password_entry),
408 _("The password used to login\n"
409 "<small>Enter the password you use to login into www.openstreetmap.org.</small>"));
3e7553ae 410
4c77d5e0 411 name_label = gtk_label_new(_("File's name:"));
3e7553ae 412 name_entry = gtk_entry_new();
5092de80
GB
413 if (track_name != NULL)
414 name = track_name;
415 else
416 name = vik_layer_get_name(VIK_LAYER(vtl));
3e7553ae
GB
417 gtk_entry_set_text(GTK_ENTRY(name_entry), name);
418 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), name_label, FALSE, FALSE, 0);
419 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), name_entry, FALSE, FALSE, 0);
064a4ce9
SB
420 gtk_widget_set_tooltip_markup(GTK_WIDGET(name_entry),
421 _("The name of the file on OSM\n"
422 "<small>This is the name of the file created on the server."
423 "This is not the name of the local file.</small>"));
3e7553ae 424
4c77d5e0 425 description_label = gtk_label_new(_("Description:"));
3e7553ae
GB
426 description_entry = gtk_entry_new();
427 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), description_label, FALSE, FALSE, 0);
428 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), description_entry, FALSE, FALSE, 0);
064a4ce9
SB
429 gtk_widget_set_tooltip_text(GTK_WIDGET(description_entry),
430 _("The description of the trace"));
3e7553ae 431
4c77d5e0 432 tags_label = gtk_label_new(_("Tags:"));
3e7553ae
GB
433 tags_entry = gtk_entry_new();
434 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), tags_label, FALSE, FALSE, 0);
435 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), tags_entry, FALSE, FALSE, 0);
064a4ce9
SB
436 gtk_widget_set_tooltip_text(GTK_WIDGET(tags_entry),
437 _("The tags associated to the trace"));
3e7553ae 438
8b589250
GS
439 visibility = GTK_COMBO_BOX(gtk_combo_box_new_text ());
440 for (vis_t = OsmTraceVis; vis_t->combostr != NULL; vis_t++)
441 gtk_combo_box_append_text(visibility, vis_t->combostr);
442 /* Set identifiable by default */
443 gtk_combo_box_set_active(visibility, 0);
444 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), GTK_WIDGET(visibility), FALSE, FALSE, 0);
445
76e2179a
RN
446 /* User should think about it first... */
447 gtk_dialog_set_default_response ( GTK_DIALOG(dia), GTK_RESPONSE_REJECT );
448
8b589250
GS
449 gtk_widget_show_all ( dia );
450 gtk_widget_grab_focus ( description_entry );
3e7553ae
GB
451
452 if ( gtk_dialog_run ( GTK_DIALOG(dia) ) == GTK_RESPONSE_ACCEPT )
453 {
454 gchar *title = NULL;
455
456 /* overwrite authentication info */
457 set_login(gtk_entry_get_text(GTK_ENTRY(user_entry)),
458 gtk_entry_get_text(GTK_ENTRY(password_entry)));
459
460 /* Storing data for the future thread */
461 OsmTracesInfo *info = g_malloc(sizeof(OsmTracesInfo));
462 info->name = g_strdup(gtk_entry_get_text(GTK_ENTRY(name_entry)));
463 info->description = g_strdup(gtk_entry_get_text(GTK_ENTRY(description_entry)));
464 /* TODO Normalize tags: they will be used as URL part */
465 info->tags = g_strdup(gtk_entry_get_text(GTK_ENTRY(tags_entry)));
8b589250 466 info->vistype = &OsmTraceVis[gtk_combo_box_get_active(visibility)];
3e7553ae 467 info->vtl = VIK_TRW_LAYER(g_object_ref(vtl));
5092de80 468 info->track_name = (track_name == NULL) ? NULL : g_strdup(track_name);
3e7553ae 469
4c77d5e0 470 title = g_strdup_printf(_("Uploading %s to OSM"), info->name);
3e7553ae
GB
471
472 /* launch the thread */
473 a_background_thread(VIK_GTK_WINDOW_FROM_LAYER(vtl), /* parent window */
474 title, /* description string */
475 (vik_thr_func) osm_traces_upload_thread, /* function to call within thread */
476 info, /* pass along data */
477 (vik_thr_free_func) oti_free, /* function to free pass along data */
478 (vik_thr_free_func) NULL,
479 1 );
480 g_free ( title ); title = NULL;
481 }
482 gtk_widget_destroy ( dia );
483}
484
485/**
5092de80 486 * Function called by the entry menu of a TrwLayer
3e7553ae
GB
487 */
488void osm_traces_upload_cb ( gpointer layer_and_vlp[2], guint file_type )
489{
5092de80
GB
490 osm_traces_upload_viktrwlayer(VIK_TRW_LAYER(layer_and_vlp[0]), NULL);
491}
492
493/**
494 * Function called by the entry menu of a single track
495 */
496void osm_traces_upload_track_cb ( gpointer pass_along[6] )
497{
498 osm_traces_upload_viktrwlayer(VIK_TRW_LAYER(pass_along[0]), pass_along[3]);
3e7553ae 499}