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