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