]> git.street.me.uk Git - andy/viking.git/blame - src/osm-traces.c
Fix: Don't abort program when attempting webtool center with different x & y factors.
[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
GB
27#include <errno.h>
28
29#include <curl/curl.h>
3e7553ae
GB
30#include <curl/easy.h>
31
32#include <glib.h>
33#include <glib/gstdio.h>
4c77d5e0 34#include <glib/gi18n.h>
3e7553ae
GB
35
36#include "viking.h"
37#include "viktrwlayer.h"
38#include "osm-traces.h"
39#include "gpx.h"
40#include "background.h"
843b99df
GB
41#include "preferences.h"
42
9de6196a 43/* params will be osm_traces.username, osm_traces.password */
843b99df
GB
44/* we have to make sure these don't collide. */
45#define VIKING_OSM_TRACES_PARAMS_GROUP_KEY "osm_traces"
46#define VIKING_OSM_TRACES_PARAMS_NAMESPACE "osm_traces."
3e7553ae
GB
47
48/**
49 * Login to use for OSM uploading.
50 */
51static gchar *user = NULL;
52
53/**
54 * Password to use for OSM uploading.
55 */
56static gchar *password = NULL;
57
58/**
59 * Mutex to protect auth. token
60 */
61static GMutex *login_mutex = NULL;
62
8b589250
GS
63/**
64 * Different type of trace visibility.
65 */
66typedef struct _OsmTraceVis_t {
67 const gchar *combostr;
68 const gchar *apistr;
69} OsmTraceVis_t;
70
71static const OsmTraceVis_t OsmTraceVis[] = {
502d25de
GB
72 { N_("Identifiable (public w/ timestamps)"), "identifiable" },
73 { N_("Trackable (private w/ timestamps)"), "trackable" },
74 { N_("Public"), "public" },
75 { N_("Private"), "private" },
8b589250
GS
76 { NULL, NULL },
77};
78
3e7553ae
GB
79/**
80 * Struct hosting needed info.
81 */
82typedef struct _OsmTracesInfo {
83 gchar *name;
84 gchar *description;
85 gchar *tags;
8b589250 86 const OsmTraceVis_t *vistype;
3e7553ae 87 VikTrwLayer *vtl;
5092de80 88 gchar *track_name;
3e7553ae
GB
89} OsmTracesInfo;
90
843b99df
GB
91static VikLayerParam prefs[] = {
92 { VIKING_OSM_TRACES_PARAMS_NAMESPACE "username", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("OSM username:"), VIK_LAYER_WIDGET_ENTRY },
dd8bde3a 93 { VIKING_OSM_TRACES_PARAMS_NAMESPACE "password", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("OSM password:"), VIK_LAYER_WIDGET_PASSWORD },
843b99df
GB
94};
95
3e7553ae
GB
96/**
97 * Free an OsmTracesInfo struct.
98 */
99static void oti_free(OsmTracesInfo *oti)
100{
101 if (oti) {
102 /* Fields have been g_strdup'ed */
103 g_free(oti->name); oti->name = NULL;
104 g_free(oti->description); oti->description = NULL;
105 g_free(oti->tags); oti->tags = NULL;
5092de80 106 g_free(oti->track_name); oti->track_name = NULL;
3e7553ae
GB
107
108 g_object_unref(oti->vtl); oti->vtl = NULL;
109 }
110 /* Main struct has been g_malloc'ed */
111 g_free(oti);
112}
113
624318ca 114static const gchar *get_default_user()
51c15f41
GB
115{
116 const gchar *default_user = NULL;
117
118 /* Retrieve "standard" EMAIL varenv */
119 default_user = g_getenv("EMAIL");
120
121 return default_user;
122}
123
3e7553ae
GB
124static void set_login(const gchar *user_, const gchar *password_)
125{
126 /* Allocate mutex */
127 if (login_mutex == NULL)
128 {
129 login_mutex = g_mutex_new();
130 }
131 g_mutex_lock(login_mutex);
132 g_free(user); user = NULL;
133 g_free(password); password = NULL;
134 user = g_strdup(user_);
135 password = g_strdup(password_);
136 g_mutex_unlock(login_mutex);
137}
138
139static gchar *get_login()
140{
141 gchar *user_pass = NULL;
142 g_mutex_lock(login_mutex);
143 user_pass = g_strdup_printf("%s:%s", user, password);
144 g_mutex_unlock(login_mutex);
145 return user_pass;
146}
147
843b99df
GB
148/* initialisation */
149void osm_traces_init () {
150 /* Preferences */
d11de0ca 151 a_preferences_register_group ( VIKING_OSM_TRACES_PARAMS_GROUP_KEY, _("OpenStreetMap Traces") );
843b99df
GB
152
153 VikLayerParamData tmp;
154 tmp.s = "";
155 a_preferences_register(prefs, tmp, VIKING_OSM_TRACES_PARAMS_GROUP_KEY);
156 tmp.s = "";
157 a_preferences_register(prefs+1, tmp, VIKING_OSM_TRACES_PARAMS_GROUP_KEY);
158
159}
160
3e7553ae
GB
161/*
162 * Upload a file
163 */
164void osm_traces_upload_file(const char *user,
165 const char *password,
166 const char *file,
167 const char *filename,
168 const char *description,
169 const char *tags,
8b589250 170 const OsmTraceVis_t *vistype)
3e7553ae
GB
171{
172 CURL *curl;
173 CURLcode res;
174 char curl_error_buffer[CURL_ERROR_SIZE];
175 struct curl_slist *headers = NULL;
176 struct curl_httppost *post=NULL;
177 struct curl_httppost *last=NULL;
3e7553ae 178
ffb5a285 179 char *base_url = "http://www.openstreetmap.org/api/0.6/gpx/create";
3e7553ae
GB
180
181 gchar *user_pass = get_login();
182
183 g_debug("%s: %s %s %s %s %s %s", __FUNCTION__,
184 user, password, file, filename, description, tags);
185
186 /* Init CURL */
187 curl = curl_easy_init();
188
189 /* Filling the form */
190 curl_formadd(&post, &last,
191 CURLFORM_COPYNAME, "description",
192 CURLFORM_COPYCONTENTS, description, CURLFORM_END);
193 curl_formadd(&post, &last,
194 CURLFORM_COPYNAME, "tags",
195 CURLFORM_COPYCONTENTS, tags, CURLFORM_END);
3e7553ae 196 curl_formadd(&post, &last,
8b589250
GS
197 CURLFORM_COPYNAME, "visibility",
198 CURLFORM_COPYCONTENTS, vistype->apistr, CURLFORM_END);
3e7553ae
GB
199 curl_formadd(&post, &last,
200 CURLFORM_COPYNAME, "file",
201 CURLFORM_FILE, file,
202 CURLFORM_FILENAME, filename,
203 CURLFORM_CONTENTTYPE, "text/xml", CURLFORM_END);
204
205 /* Prepare request */
206 /* As explained in http://wiki.openstreetmap.org/index.php/User:LA2 */
207 /* Expect: header seems to produce incompatibilites between curl and httpd */
208 headers = curl_slist_append(headers, "Expect: ");
209 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
210 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
211 curl_easy_setopt(curl, CURLOPT_URL, base_url);
212 curl_easy_setopt(curl, CURLOPT_USERPWD, user_pass);
213 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
5a7d2873 214 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_error_buffer);
2936913d
GB
215 if (vik_verbose)
216 curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
3e7553ae
GB
217
218 /* Execute request */
219 res = curl_easy_perform(curl);
220 if (res == CURLE_OK)
221 {
222 long code;
223 res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
224 if (res == CURLE_OK)
225 {
226 g_debug("received valid curl response: %ld", code);
227 if (code != 200)
4c77d5e0 228 g_warning(_("failed to upload data: HTTP response is %ld"), code);
3e7553ae
GB
229 }
230 else
4c77d5e0 231 g_error(_("curl_easy_getinfo failed: %d"), res);
3e7553ae
GB
232 }
233 else
234 {
4c77d5e0 235 g_warning(_("curl request failed: %s"), curl_error_buffer);
3e7553ae
GB
236 }
237
238 /* Memory */
239 g_free(user_pass); user_pass = NULL;
240
241 curl_formfree(post);
242 curl_easy_cleanup(curl);
243}
244
245/**
246 * uploading function executed by the background" thread
247 */
248static void osm_traces_upload_thread ( OsmTracesInfo *oti, gpointer threaddata )
249{
0b72c435
OK
250 /* Due to OSM limits, we have to enforce ele and time fields */
251 static GpxWritingOptions options = { TRUE, TRUE };
3e7553ae
GB
252 FILE *file = NULL;
253 gchar *filename = NULL;
254 int fd;
255 GError *error = NULL;
256 int ret;
257
258 g_assert(oti != NULL);
259
260 /* Opening temporary file */
261 fd = g_file_open_tmp("viking_osm_upload_XXXXXX.gpx", &filename, &error);
262 if (fd < 0) {
4c77d5e0 263 g_error(_("failed to open temporary file: %s"), strerror(errno));
3e7553ae
GB
264 return;
265 }
266 g_clear_error(&error);
267 g_debug("%s: temporary file = %s", __FUNCTION__, filename);
268
269 /* Creating FILE* */
270 file = fdopen(fd, "w");
271
272 /* writing gpx file */
5092de80
GB
273 if (oti->track_name != NULL)
274 {
275 /* Upload only the selected track */
276 VikTrack *track = vik_trw_layer_get_track(oti->vtl, oti->track_name);
0b72c435 277 a_gpx_write_track_file_options(&options, oti->track_name, track, file);
5092de80
GB
278 }
279 else
0b72c435 280 {
5092de80 281 /* Upload the whole VikTrwLayer */
0b72c435
OK
282 a_gpx_write_file_options(&options, oti->vtl, file);
283 }
3e7553ae
GB
284
285 /* We can close the file */
286 /* This also close the associated fd */
287 fclose(file);
8c060406 288 file = NULL;
3e7553ae
GB
289
290 /* finally, upload it */
291 osm_traces_upload_file(user, password, filename,
8b589250 292 oti->name, oti->description, oti->tags, oti->vistype);
3e7553ae
GB
293
294 /* Removing temporary file */
295 ret = g_unlink(filename);
296 if (ret != 0) {
4c77d5e0 297 g_error(_("failed to unlink temporary file: %s"), strerror(errno));
3e7553ae
GB
298 }
299}
300
301/**
302 * Uploading a VikTrwLayer
5092de80
GB
303 *
304 * @param vtl VikTrwLayer
305 * @param track_name if not null, the name of the track to upload
3e7553ae 306 */
5092de80 307static void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl, const gchar *track_name )
3e7553ae 308{
4c77d5e0 309 GtkWidget *dia = gtk_dialog_new_with_buttons (_("OSM upload"),
3e7553ae
GB
310 VIK_GTK_WINDOW_FROM_LAYER(vtl),
311 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
312 GTK_STOCK_CANCEL,
313 GTK_RESPONSE_REJECT,
314 GTK_STOCK_OK,
315 GTK_RESPONSE_ACCEPT,
316 NULL);
317
843b99df
GB
318 const gchar *default_user = get_default_user();
319 const gchar *pref_user = a_preferences_get(VIKING_OSM_TRACES_PARAMS_NAMESPACE "username")->s;
320 const gchar *pref_password = a_preferences_get(VIKING_OSM_TRACES_PARAMS_NAMESPACE "password")->s;
3e7553ae
GB
321 const gchar *name = NULL;
322 GtkWidget *user_label, *user_entry;
323 GtkWidget *password_label, *password_entry;
324 GtkWidget *name_label, *name_entry;
325 GtkWidget *description_label, *description_entry;
326 GtkWidget *tags_label, *tags_entry;
8b589250
GS
327 GtkComboBox *visibility;
328 const OsmTraceVis_t *vis_t;
3e7553ae 329
4c77d5e0 330 user_label = gtk_label_new(_("Email:"));
3e7553ae 331 user_entry = gtk_entry_new();
843b99df 332 if (user != NULL && user[0] != '\0')
3e7553ae 333 gtk_entry_set_text(GTK_ENTRY(user_entry), user);
843b99df
GB
334 else if (pref_user != NULL && pref_user[0] != '\0')
335 gtk_entry_set_text(GTK_ENTRY(user_entry), pref_user);
336 else if (default_user != NULL)
337 gtk_entry_set_text(GTK_ENTRY(user_entry), default_user);
3e7553ae
GB
338 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), user_label, FALSE, FALSE, 0);
339 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), user_entry, FALSE, FALSE, 0);
064a4ce9
SB
340 gtk_widget_set_tooltip_markup(GTK_WIDGET(user_entry),
341 _("The email used as login\n"
342 "<small>Enter the email you use to login into www.openstreetmap.org.</small>"));
3e7553ae 343
4c77d5e0 344 password_label = gtk_label_new(_("Password:"));
3e7553ae 345 password_entry = gtk_entry_new();
843b99df 346 if (password != NULL && password[0] != '\0')
3e7553ae 347 gtk_entry_set_text(GTK_ENTRY(password_entry), password);
843b99df
GB
348 else if (pref_password != NULL)
349 gtk_entry_set_text(GTK_ENTRY(password_entry), pref_password);
3e7553ae
GB
350 /* This is a password -> invisible */
351 gtk_entry_set_visibility(GTK_ENTRY(password_entry), FALSE);
352 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), password_label, FALSE, FALSE, 0);
353 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), password_entry, FALSE, FALSE, 0);
064a4ce9
SB
354 gtk_widget_set_tooltip_markup(GTK_WIDGET(password_entry),
355 _("The password used to login\n"
356 "<small>Enter the password you use to login into www.openstreetmap.org.</small>"));
3e7553ae 357
4c77d5e0 358 name_label = gtk_label_new(_("File's name:"));
3e7553ae 359 name_entry = gtk_entry_new();
5092de80
GB
360 if (track_name != NULL)
361 name = track_name;
362 else
363 name = vik_layer_get_name(VIK_LAYER(vtl));
3e7553ae
GB
364 gtk_entry_set_text(GTK_ENTRY(name_entry), name);
365 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), name_label, FALSE, FALSE, 0);
366 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), name_entry, FALSE, FALSE, 0);
064a4ce9
SB
367 gtk_widget_set_tooltip_markup(GTK_WIDGET(name_entry),
368 _("The name of the file on OSM\n"
369 "<small>This is the name of the file created on the server."
370 "This is not the name of the local file.</small>"));
3e7553ae 371
4c77d5e0 372 description_label = gtk_label_new(_("Description:"));
3e7553ae
GB
373 description_entry = gtk_entry_new();
374 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), description_label, FALSE, FALSE, 0);
375 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), description_entry, FALSE, FALSE, 0);
064a4ce9
SB
376 gtk_widget_set_tooltip_text(GTK_WIDGET(description_entry),
377 _("The description of the trace"));
3e7553ae 378
4c77d5e0 379 tags_label = gtk_label_new(_("Tags:"));
3e7553ae
GB
380 tags_entry = gtk_entry_new();
381 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), tags_label, FALSE, FALSE, 0);
382 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), tags_entry, FALSE, FALSE, 0);
064a4ce9
SB
383 gtk_widget_set_tooltip_text(GTK_WIDGET(tags_entry),
384 _("The tags associated to the trace"));
3e7553ae 385
8b589250
GS
386 visibility = GTK_COMBO_BOX(gtk_combo_box_new_text ());
387 for (vis_t = OsmTraceVis; vis_t->combostr != NULL; vis_t++)
388 gtk_combo_box_append_text(visibility, vis_t->combostr);
389 /* Set identifiable by default */
390 gtk_combo_box_set_active(visibility, 0);
391 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dia)->vbox), GTK_WIDGET(visibility), FALSE, FALSE, 0);
392
76e2179a
RN
393 /* User should think about it first... */
394 gtk_dialog_set_default_response ( GTK_DIALOG(dia), GTK_RESPONSE_REJECT );
395
8b589250
GS
396 gtk_widget_show_all ( dia );
397 gtk_widget_grab_focus ( description_entry );
3e7553ae
GB
398
399 if ( gtk_dialog_run ( GTK_DIALOG(dia) ) == GTK_RESPONSE_ACCEPT )
400 {
401 gchar *title = NULL;
402
403 /* overwrite authentication info */
404 set_login(gtk_entry_get_text(GTK_ENTRY(user_entry)),
405 gtk_entry_get_text(GTK_ENTRY(password_entry)));
406
407 /* Storing data for the future thread */
408 OsmTracesInfo *info = g_malloc(sizeof(OsmTracesInfo));
409 info->name = g_strdup(gtk_entry_get_text(GTK_ENTRY(name_entry)));
410 info->description = g_strdup(gtk_entry_get_text(GTK_ENTRY(description_entry)));
411 /* TODO Normalize tags: they will be used as URL part */
412 info->tags = g_strdup(gtk_entry_get_text(GTK_ENTRY(tags_entry)));
8b589250 413 info->vistype = &OsmTraceVis[gtk_combo_box_get_active(visibility)];
3e7553ae 414 info->vtl = VIK_TRW_LAYER(g_object_ref(vtl));
5092de80 415 info->track_name = (track_name == NULL) ? NULL : g_strdup(track_name);
3e7553ae 416
4c77d5e0 417 title = g_strdup_printf(_("Uploading %s to OSM"), info->name);
3e7553ae
GB
418
419 /* launch the thread */
420 a_background_thread(VIK_GTK_WINDOW_FROM_LAYER(vtl), /* parent window */
421 title, /* description string */
422 (vik_thr_func) osm_traces_upload_thread, /* function to call within thread */
423 info, /* pass along data */
424 (vik_thr_free_func) oti_free, /* function to free pass along data */
425 (vik_thr_free_func) NULL,
426 1 );
427 g_free ( title ); title = NULL;
428 }
429 gtk_widget_destroy ( dia );
430}
431
432/**
5092de80 433 * Function called by the entry menu of a TrwLayer
3e7553ae
GB
434 */
435void osm_traces_upload_cb ( gpointer layer_and_vlp[2], guint file_type )
436{
5092de80
GB
437 osm_traces_upload_viktrwlayer(VIK_TRW_LAYER(layer_and_vlp[0]), NULL);
438}
439
440/**
441 * Function called by the entry menu of a single track
442 */
443void osm_traces_upload_track_cb ( gpointer pass_along[6] )
444{
445 osm_traces_upload_viktrwlayer(VIK_TRW_LAYER(pass_along[0]), pass_along[3]);
3e7553ae 446}