]>
Commit | Line | Data |
---|---|---|
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 | 48 | |
49cf4658 RN |
49 | #define VIK_SETTINGS_OSM_TRACE_VIS "osm_trace_visibility" |
50 | static gint last_active = -1; | |
51 | ||
3e7553ae GB |
52 | /** |
53 | * Login to use for OSM uploading. | |
54 | */ | |
c60f82df | 55 | static gchar *osm_user = NULL; |
3e7553ae GB |
56 | |
57 | /** | |
58 | * Password to use for OSM uploading. | |
59 | */ | |
c60f82df | 60 | static gchar *osm_password = NULL; |
3e7553ae GB |
61 | |
62 | /** | |
63 | * Mutex to protect auth. token | |
64 | */ | |
65 | static GMutex *login_mutex = NULL; | |
66 | ||
8b589250 GS |
67 | /** |
68 | * Different type of trace visibility. | |
69 | */ | |
70 | typedef struct _OsmTraceVis_t { | |
71 | const gchar *combostr; | |
72 | const gchar *apistr; | |
73 | } OsmTraceVis_t; | |
74 | ||
75 | static const OsmTraceVis_t OsmTraceVis[] = { | |
502d25de GB |
76 | { N_("Identifiable (public w/ timestamps)"), "identifiable" }, |
77 | { N_("Trackable (private w/ timestamps)"), "trackable" }, | |
78 | { N_("Public"), "public" }, | |
79 | { N_("Private"), "private" }, | |
8b589250 GS |
80 | { NULL, NULL }, |
81 | }; | |
82 | ||
3e7553ae GB |
83 | /** |
84 | * Struct hosting needed info. | |
85 | */ | |
86 | typedef struct _OsmTracesInfo { | |
87 | gchar *name; | |
88 | gchar *description; | |
89 | gchar *tags; | |
9fcdb288 | 90 | gboolean anonymize_times; // ATM only available on a single track. |
8b589250 | 91 | const OsmTraceVis_t *vistype; |
3e7553ae | 92 | VikTrwLayer *vtl; |
ce4bd1cf | 93 | VikTrack *trk; |
3e7553ae GB |
94 | } OsmTracesInfo; |
95 | ||
843b99df | 96 | static VikLayerParam prefs[] = { |
63959706 RN |
97 | { VIK_LAYER_NUM_TYPES, VIKING_OSM_TRACES_PARAMS_NAMESPACE "username", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("OSM username:"), VIK_LAYER_WIDGET_ENTRY, NULL, NULL, NULL, NULL, NULL, NULL }, |
98 | { VIK_LAYER_NUM_TYPES, VIKING_OSM_TRACES_PARAMS_NAMESPACE "password", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("OSM password:"), VIK_LAYER_WIDGET_PASSWORD, NULL, NULL, NULL, NULL, NULL, NULL }, | |
843b99df GB |
99 | }; |
100 | ||
3e7553ae GB |
101 | /** |
102 | * Free an OsmTracesInfo struct. | |
103 | */ | |
104 | static void oti_free(OsmTracesInfo *oti) | |
105 | { | |
106 | if (oti) { | |
107 | /* Fields have been g_strdup'ed */ | |
108 | g_free(oti->name); oti->name = NULL; | |
109 | g_free(oti->description); oti->description = NULL; | |
110 | g_free(oti->tags); oti->tags = NULL; | |
111 | ||
112 | g_object_unref(oti->vtl); oti->vtl = NULL; | |
113 | } | |
114 | /* Main struct has been g_malloc'ed */ | |
115 | g_free(oti); | |
116 | } | |
117 | ||
624318ca | 118 | static const gchar *get_default_user() |
51c15f41 GB |
119 | { |
120 | const gchar *default_user = NULL; | |
121 | ||
122 | /* Retrieve "standard" EMAIL varenv */ | |
123 | default_user = g_getenv("EMAIL"); | |
124 | ||
125 | return default_user; | |
126 | } | |
127 | ||
c60f82df | 128 | void osm_set_login(const gchar *user, const gchar *password) |
3e7553ae | 129 | { |
3e7553ae | 130 | g_mutex_lock(login_mutex); |
c60f82df RN |
131 | g_free(osm_user); osm_user = NULL; |
132 | g_free(osm_password); osm_password = NULL; | |
133 | osm_user = g_strdup(user); | |
134 | osm_password = g_strdup(password); | |
3e7553ae GB |
135 | g_mutex_unlock(login_mutex); |
136 | } | |
137 | ||
3cc57413 | 138 | gchar *osm_get_login() |
3e7553ae GB |
139 | { |
140 | gchar *user_pass = NULL; | |
141 | g_mutex_lock(login_mutex); | |
c60f82df | 142 | user_pass = g_strdup_printf("%s:%s", osm_user, osm_password); |
3e7553ae GB |
143 | g_mutex_unlock(login_mutex); |
144 | return user_pass; | |
145 | } | |
146 | ||
843b99df GB |
147 | /* initialisation */ |
148 | void osm_traces_init () { | |
149 | /* Preferences */ | |
d11de0ca | 150 | a_preferences_register_group ( VIKING_OSM_TRACES_PARAMS_GROUP_KEY, _("OpenStreetMap Traces") ); |
843b99df GB |
151 | |
152 | VikLayerParamData tmp; | |
153 | tmp.s = ""; | |
154 | a_preferences_register(prefs, tmp, VIKING_OSM_TRACES_PARAMS_GROUP_KEY); | |
155 | tmp.s = ""; | |
156 | a_preferences_register(prefs+1, tmp, VIKING_OSM_TRACES_PARAMS_GROUP_KEY); | |
157 | ||
e3da2277 RN |
158 | login_mutex = vik_mutex_new(); |
159 | } | |
160 | ||
161 | void osm_traces_uninit() | |
162 | { | |
163 | vik_mutex_free(login_mutex); | |
843b99df GB |
164 | } |
165 | ||
3e7553ae GB |
166 | /* |
167 | * Upload a file | |
36420b5f RN |
168 | * returns a basic status: |
169 | * < 0 : curl error | |
170 | * == 0 : OK | |
171 | * > 0 : HTTP error | |
172 | */ | |
173 | static gint osm_traces_upload_file(const char *user, | |
174 | const char *password, | |
175 | const char *file, | |
176 | const char *filename, | |
177 | const char *description, | |
178 | const char *tags, | |
179 | const OsmTraceVis_t *vistype) | |
3e7553ae GB |
180 | { |
181 | CURL *curl; | |
182 | CURLcode res; | |
183 | char curl_error_buffer[CURL_ERROR_SIZE]; | |
184 | struct curl_slist *headers = NULL; | |
185 | struct curl_httppost *post=NULL; | |
186 | struct curl_httppost *last=NULL; | |
3e7553ae | 187 | |
ffb5a285 | 188 | char *base_url = "http://www.openstreetmap.org/api/0.6/gpx/create"; |
3e7553ae | 189 | |
3cc57413 | 190 | gchar *user_pass = osm_get_login(); |
3e7553ae | 191 | |
36420b5f RN |
192 | gint result = 0; // Default to it worked! |
193 | ||
3e7553ae GB |
194 | g_debug("%s: %s %s %s %s %s %s", __FUNCTION__, |
195 | user, password, file, filename, description, tags); | |
196 | ||
197 | /* Init CURL */ | |
198 | curl = curl_easy_init(); | |
199 | ||
200 | /* Filling the form */ | |
201 | curl_formadd(&post, &last, | |
202 | CURLFORM_COPYNAME, "description", | |
203 | CURLFORM_COPYCONTENTS, description, CURLFORM_END); | |
204 | curl_formadd(&post, &last, | |
205 | CURLFORM_COPYNAME, "tags", | |
206 | CURLFORM_COPYCONTENTS, tags, CURLFORM_END); | |
3e7553ae | 207 | curl_formadd(&post, &last, |
8b589250 GS |
208 | CURLFORM_COPYNAME, "visibility", |
209 | CURLFORM_COPYCONTENTS, vistype->apistr, CURLFORM_END); | |
3e7553ae GB |
210 | curl_formadd(&post, &last, |
211 | CURLFORM_COPYNAME, "file", | |
212 | CURLFORM_FILE, file, | |
213 | CURLFORM_FILENAME, filename, | |
214 | CURLFORM_CONTENTTYPE, "text/xml", CURLFORM_END); | |
215 | ||
216 | /* Prepare request */ | |
217 | /* As explained in http://wiki.openstreetmap.org/index.php/User:LA2 */ | |
218 | /* Expect: header seems to produce incompatibilites between curl and httpd */ | |
219 | headers = curl_slist_append(headers, "Expect: "); | |
220 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); | |
221 | curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); | |
222 | curl_easy_setopt(curl, CURLOPT_URL, base_url); | |
223 | curl_easy_setopt(curl, CURLOPT_USERPWD, user_pass); | |
224 | curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); | |
5a7d2873 | 225 | curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_error_buffer); |
2936913d GB |
226 | if (vik_verbose) |
227 | curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 ); | |
3e7553ae GB |
228 | |
229 | /* Execute request */ | |
230 | res = curl_easy_perform(curl); | |
231 | if (res == CURLE_OK) | |
232 | { | |
233 | long code; | |
234 | res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); | |
235 | if (res == CURLE_OK) | |
236 | { | |
237 | g_debug("received valid curl response: %ld", code); | |
36420b5f | 238 | if (code != 200) { |
4c77d5e0 | 239 | g_warning(_("failed to upload data: HTTP response is %ld"), code); |
36420b5f RN |
240 | result = code; |
241 | } | |
3e7553ae | 242 | } |
36420b5f | 243 | else { |
24ff9c7b | 244 | g_critical(_("curl_easy_getinfo failed: %d"), res); |
36420b5f | 245 | result = -1; |
3e7553ae | 246 | } |
36420b5f RN |
247 | } |
248 | else { | |
249 | g_warning(_("curl request failed: %s"), curl_error_buffer); | |
250 | result = -2; | |
251 | } | |
3e7553ae GB |
252 | |
253 | /* Memory */ | |
254 | g_free(user_pass); user_pass = NULL; | |
255 | ||
256 | curl_formfree(post); | |
36420b5f RN |
257 | curl_easy_cleanup(curl); |
258 | return result; | |
3e7553ae GB |
259 | } |
260 | ||
261 | /** | |
262 | * uploading function executed by the background" thread | |
263 | */ | |
264 | static void osm_traces_upload_thread ( OsmTracesInfo *oti, gpointer threaddata ) | |
265 | { | |
208d2084 RN |
266 | /* Due to OSM limits, we have to enforce ele and time fields |
267 | also don't upload invisible tracks */ | |
0d2b891f | 268 | static GpxWritingOptions options = { TRUE, TRUE, FALSE, FALSE }; |
3e7553ae | 269 | |
12ed2b58 | 270 | if (!oti) |
3e7553ae | 271 | return; |
3e7553ae | 272 | |
12ed2b58 | 273 | gchar *filename = NULL; |
3e7553ae GB |
274 | |
275 | /* writing gpx file */ | |
ce4bd1cf | 276 | if (oti->trk != NULL) |
5092de80 GB |
277 | { |
278 | /* Upload only the selected track */ | |
9fcdb288 RN |
279 | if ( oti->anonymize_times ) |
280 | { | |
281 | VikTrack *trk = vik_track_copy(oti->trk, TRUE); | |
282 | vik_track_anonymize_times(trk); | |
12ed2b58 | 283 | filename = a_gpx_write_track_tmp_file(trk, &options); |
9fcdb288 RN |
284 | vik_track_free(trk); |
285 | } | |
286 | else | |
12ed2b58 | 287 | filename = a_gpx_write_track_tmp_file (oti->trk, &options); |
5092de80 GB |
288 | } |
289 | else | |
0b72c435 | 290 | { |
5092de80 | 291 | /* Upload the whole VikTrwLayer */ |
12ed2b58 | 292 | filename = a_gpx_write_tmp_file (oti->vtl, &options); |
0b72c435 | 293 | } |
3e7553ae | 294 | |
12ed2b58 RN |
295 | if ( !filename ) |
296 | return; | |
3e7553ae GB |
297 | |
298 | /* finally, upload it */ | |
c60f82df | 299 | gint ans = osm_traces_upload_file(osm_user, osm_password, filename, |
36420b5f RN |
300 | oti->name, oti->description, oti->tags, oti->vistype); |
301 | ||
302 | // | |
303 | // Show result in statusbar or failure in dialog for user feedback | |
304 | // | |
305 | ||
306 | // Get current time to put into message to show when result was generated | |
307 | // since need to show difference between operations (when displayed on statusbar) | |
308 | // NB If on dialog then don't need time. | |
309 | time_t timenow; | |
310 | struct tm* timeinfo; | |
311 | time ( &timenow ); | |
312 | timeinfo = localtime ( &timenow ); | |
313 | gchar timestr[80]; | |
314 | // Compact time only - as days/date isn't very useful here | |
315 | strftime ( timestr, sizeof(timestr), "%X)", timeinfo ); | |
316 | ||
317 | // | |
318 | // Test to see if window it was invoked on is still valid | |
319 | // Not sure if this test really works! (i.e. if the window was closed in the mean time) | |
320 | // | |
321 | if ( IS_VIK_WINDOW ((VikWindow *)VIK_GTK_WINDOW_FROM_LAYER(oti->vtl)) ) { | |
322 | gchar* msg; | |
323 | if ( ans == 0 ) { | |
324 | // Success | |
325 | msg = g_strdup_printf ( "%s (@%s)", _("Uploaded to OSM"), timestr ); | |
36420b5f RN |
326 | } |
327 | // Use UPPER CASE for bad news :( | |
328 | else if ( ans < 0 ) { | |
329 | msg = g_strdup_printf ( "%s (@%s)", _("FAILED TO UPLOAD DATA TO OSM - CURL PROBLEM"), timestr ); | |
36420b5f RN |
330 | } |
331 | else { | |
332 | msg = g_strdup_printf ( "%s : %s %d (@%s)", _("FAILED TO UPLOAD DATA TO OSM"), _("HTTP response code"), ans, timestr ); | |
36420b5f | 333 | } |
39ae014f RN |
334 | vik_window_statusbar_update ( (VikWindow*)VIK_GTK_WINDOW_FROM_LAYER(oti->vtl), msg, VIK_STATUSBAR_INFO ); |
335 | g_free (msg); | |
36420b5f | 336 | } |
3e7553ae | 337 | /* Removing temporary file */ |
12ed2b58 | 338 | int ret = g_unlink(filename); |
3e7553ae | 339 | if (ret != 0) { |
24ff9c7b | 340 | g_critical(_("failed to unlink temporary file: %s"), strerror(errno)); |
3e7553ae GB |
341 | } |
342 | } | |
343 | ||
3cc57413 RN |
344 | /** |
345 | * | |
346 | */ | |
347 | void osm_login_widgets (GtkWidget *user_entry, GtkWidget *password_entry) | |
348 | { | |
349 | if (!user_entry || !password_entry) | |
350 | return; | |
351 | ||
352 | const gchar *default_user = get_default_user(); | |
353 | const gchar *pref_user = a_preferences_get(VIKING_OSM_TRACES_PARAMS_NAMESPACE "username")->s; | |
354 | const gchar *pref_password = a_preferences_get(VIKING_OSM_TRACES_PARAMS_NAMESPACE "password")->s; | |
355 | ||
c60f82df RN |
356 | if (osm_user != NULL && osm_user[0] != '\0') |
357 | gtk_entry_set_text(GTK_ENTRY(user_entry), osm_user); | |
3cc57413 RN |
358 | else if (pref_user != NULL && pref_user[0] != '\0') |
359 | gtk_entry_set_text(GTK_ENTRY(user_entry), pref_user); | |
360 | else if (default_user != NULL) | |
361 | gtk_entry_set_text(GTK_ENTRY(user_entry), default_user); | |
362 | ||
c60f82df RN |
363 | if (osm_password != NULL && osm_password[0] != '\0') |
364 | gtk_entry_set_text(GTK_ENTRY(password_entry), osm_password); | |
3cc57413 RN |
365 | else if (pref_password != NULL) |
366 | gtk_entry_set_text(GTK_ENTRY(password_entry), pref_password); | |
367 | /* This is a password -> invisible */ | |
368 | gtk_entry_set_visibility(GTK_ENTRY(password_entry), FALSE); | |
369 | } | |
370 | ||
3e7553ae GB |
371 | /** |
372 | * Uploading a VikTrwLayer | |
5092de80 GB |
373 | * |
374 | * @param vtl VikTrwLayer | |
ce4bd1cf | 375 | * @param trk if not null, the track to upload |
3e7553ae | 376 | */ |
3185b5d3 | 377 | void osm_traces_upload_viktrwlayer ( VikTrwLayer *vtl, VikTrack *trk ) |
3e7553ae | 378 | { |
4c77d5e0 | 379 | GtkWidget *dia = gtk_dialog_new_with_buttons (_("OSM upload"), |
3e7553ae GB |
380 | VIK_GTK_WINDOW_FROM_LAYER(vtl), |
381 | GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, | |
382 | GTK_STOCK_CANCEL, | |
383 | GTK_RESPONSE_REJECT, | |
384 | GTK_STOCK_OK, | |
385 | GTK_RESPONSE_ACCEPT, | |
386 | NULL); | |
387 | ||
388 | const gchar *name = NULL; | |
389 | GtkWidget *user_label, *user_entry; | |
390 | GtkWidget *password_label, *password_entry; | |
391 | GtkWidget *name_label, *name_entry; | |
392 | GtkWidget *description_label, *description_entry; | |
393 | GtkWidget *tags_label, *tags_entry; | |
1bc1c05b | 394 | GtkWidget *visibility; |
9fcdb288 | 395 | GtkWidget *anonymize_checkbutton = NULL; |
8b589250 | 396 | const OsmTraceVis_t *vis_t; |
3e7553ae | 397 | |
4c77d5e0 | 398 | user_label = gtk_label_new(_("Email:")); |
3e7553ae | 399 | user_entry = gtk_entry_new(); |
9b082b39 RN |
400 | gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), user_label, FALSE, FALSE, 0); |
401 | gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), user_entry, FALSE, FALSE, 0); | |
064a4ce9 SB |
402 | gtk_widget_set_tooltip_markup(GTK_WIDGET(user_entry), |
403 | _("The email used as login\n" | |
404 | "<small>Enter the email you use to login into www.openstreetmap.org.</small>")); | |
3e7553ae | 405 | |
4c77d5e0 | 406 | password_label = gtk_label_new(_("Password:")); |
3e7553ae | 407 | password_entry = gtk_entry_new(); |
9b082b39 RN |
408 | gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), password_label, FALSE, FALSE, 0); |
409 | gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), password_entry, FALSE, FALSE, 0); | |
064a4ce9 SB |
410 | gtk_widget_set_tooltip_markup(GTK_WIDGET(password_entry), |
411 | _("The password used to login\n" | |
412 | "<small>Enter the password you use to login into www.openstreetmap.org.</small>")); | |
3e7553ae | 413 | |
3cc57413 RN |
414 | osm_login_widgets ( user_entry, password_entry ); |
415 | ||
4c77d5e0 | 416 | name_label = gtk_label_new(_("File's name:")); |
3e7553ae | 417 | name_entry = gtk_entry_new(); |
ce4bd1cf RN |
418 | if (trk != NULL) |
419 | name = trk->name; | |
5092de80 GB |
420 | else |
421 | name = vik_layer_get_name(VIK_LAYER(vtl)); | |
3e7553ae | 422 | gtk_entry_set_text(GTK_ENTRY(name_entry), name); |
9b082b39 RN |
423 | gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), name_label, FALSE, FALSE, 0); |
424 | gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), name_entry, FALSE, FALSE, 0); | |
064a4ce9 SB |
425 | gtk_widget_set_tooltip_markup(GTK_WIDGET(name_entry), |
426 | _("The name of the file on OSM\n" | |
427 | "<small>This is the name of the file created on the server." | |
428 | "This is not the name of the local file.</small>")); | |
3e7553ae | 429 | |
4c77d5e0 | 430 | description_label = gtk_label_new(_("Description:")); |
3e7553ae | 431 | description_entry = gtk_entry_new(); |
ee7125f8 RN |
432 | const gchar *description = NULL; |
433 | if (trk != NULL) | |
434 | description = trk->description; | |
435 | else { | |
436 | VikTRWMetadata *md = vik_trw_layer_get_metadata (vtl); | |
437 | description = md ? md->description : NULL; | |
438 | } | |
439 | if (description) | |
440 | gtk_entry_set_text(GTK_ENTRY(description_entry), description); | |
9b082b39 RN |
441 | gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), description_label, FALSE, FALSE, 0); |
442 | gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), description_entry, FALSE, FALSE, 0); | |
064a4ce9 SB |
443 | gtk_widget_set_tooltip_text(GTK_WIDGET(description_entry), |
444 | _("The description of the trace")); | |
3e7553ae | 445 | |
9fcdb288 RN |
446 | if (trk != NULL) { |
447 | GtkWidget *label = gtk_label_new(_("Anonymize Times:")); | |
448 | anonymize_checkbutton = gtk_check_button_new (); | |
449 | gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), label, FALSE, FALSE, 0); | |
450 | gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), anonymize_checkbutton, FALSE, FALSE, 0); | |
451 | gtk_widget_set_tooltip_text(GTK_WIDGET(anonymize_checkbutton), | |
452 | _("Anonymize times of the trace.\n" | |
453 | "<small>You may choose to make the trace identifiable, yet mask the actual real time values</small>")); | |
454 | } | |
455 | ||
4c77d5e0 | 456 | tags_label = gtk_label_new(_("Tags:")); |
3e7553ae | 457 | tags_entry = gtk_entry_new(); |
ee7125f8 RN |
458 | VikTRWMetadata *md = vik_trw_layer_get_metadata (vtl); |
459 | if (md->keywords) | |
460 | gtk_entry_set_text(GTK_ENTRY(tags_entry), md->keywords); | |
9b082b39 RN |
461 | gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), tags_label, FALSE, FALSE, 0); |
462 | gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), tags_entry, FALSE, FALSE, 0); | |
064a4ce9 SB |
463 | gtk_widget_set_tooltip_text(GTK_WIDGET(tags_entry), |
464 | _("The tags associated to the trace")); | |
3e7553ae | 465 | |
1bc1c05b | 466 | visibility = vik_combo_box_text_new(); |
8b589250 | 467 | for (vis_t = OsmTraceVis; vis_t->combostr != NULL; vis_t++) |
1bc1c05b RN |
468 | vik_combo_box_text_append (visibility, vis_t->combostr); |
469 | ||
49cf4658 RN |
470 | // Set identifiable by default or use the settings for the value |
471 | if ( last_active < 0 ) { | |
472 | gint find_entry = -1; | |
473 | gint wanted_entry = -1; | |
474 | gchar *vis = NULL; | |
475 | if ( a_settings_get_string ( VIK_SETTINGS_OSM_TRACE_VIS, &vis ) ) { | |
476 | // Use setting | |
477 | if ( vis ) { | |
478 | for (vis_t = OsmTraceVis; vis_t->apistr != NULL; vis_t++) { | |
479 | find_entry++; | |
480 | if (!strcmp(vis, vis_t->apistr)) { | |
481 | wanted_entry = find_entry; | |
482 | } | |
483 | } | |
484 | } | |
485 | // If not found set it to the first entry, otherwise use the entry | |
486 | last_active = ( wanted_entry < 0 ) ? 0 : wanted_entry; | |
487 | } | |
488 | else | |
489 | last_active = 0; | |
490 | } | |
491 | gtk_combo_box_set_active(GTK_COMBO_BOX(visibility), last_active); | |
9b082b39 | 492 | gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dia))), GTK_WIDGET(visibility), FALSE, FALSE, 0); |
8b589250 | 493 | |
76e2179a RN |
494 | /* User should think about it first... */ |
495 | gtk_dialog_set_default_response ( GTK_DIALOG(dia), GTK_RESPONSE_REJECT ); | |
496 | ||
8b589250 GS |
497 | gtk_widget_show_all ( dia ); |
498 | gtk_widget_grab_focus ( description_entry ); | |
3e7553ae GB |
499 | |
500 | if ( gtk_dialog_run ( GTK_DIALOG(dia) ) == GTK_RESPONSE_ACCEPT ) | |
501 | { | |
502 | gchar *title = NULL; | |
503 | ||
504 | /* overwrite authentication info */ | |
3cc57413 RN |
505 | osm_set_login(gtk_entry_get_text(GTK_ENTRY(user_entry)), |
506 | gtk_entry_get_text(GTK_ENTRY(password_entry))); | |
3e7553ae GB |
507 | |
508 | /* Storing data for the future thread */ | |
509 | OsmTracesInfo *info = g_malloc(sizeof(OsmTracesInfo)); | |
510 | info->name = g_strdup(gtk_entry_get_text(GTK_ENTRY(name_entry))); | |
511 | info->description = g_strdup(gtk_entry_get_text(GTK_ENTRY(description_entry))); | |
512 | /* TODO Normalize tags: they will be used as URL part */ | |
513 | info->tags = g_strdup(gtk_entry_get_text(GTK_ENTRY(tags_entry))); | |
1bc1c05b | 514 | info->vistype = &OsmTraceVis[gtk_combo_box_get_active(GTK_COMBO_BOX(visibility))]; |
3e7553ae | 515 | info->vtl = VIK_TRW_LAYER(g_object_ref(vtl)); |
ce4bd1cf | 516 | info->trk = trk; |
9fcdb288 RN |
517 | if (trk != NULL && anonymize_checkbutton != NULL ) |
518 | info->anonymize_times = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(anonymize_checkbutton)); | |
519 | else | |
520 | info->anonymize_times = FALSE; | |
3e7553ae | 521 | |
49cf4658 RN |
522 | // Save visibility value for default reuse |
523 | last_active = gtk_combo_box_get_active(GTK_COMBO_BOX(visibility)); | |
524 | a_settings_set_string ( VIK_SETTINGS_OSM_TRACE_VIS, OsmTraceVis[last_active].apistr ); | |
525 | ||
4c77d5e0 | 526 | title = g_strdup_printf(_("Uploading %s to OSM"), info->name); |
3e7553ae | 527 | |
c75da936 RN |
528 | // launch the thread |
529 | a_background_thread( BACKGROUND_POOL_REMOTE, | |
530 | VIK_GTK_WINDOW_FROM_LAYER(vtl), /* parent window */ | |
531 | title, /* description string */ | |
532 | (vik_thr_func) osm_traces_upload_thread, /* function to call within thread */ | |
533 | info, /* pass along data */ | |
534 | (vik_thr_free_func) oti_free, /* function to free pass along data */ | |
535 | (vik_thr_free_func) NULL, | |
536 | 1 ); | |
3e7553ae GB |
537 | g_free ( title ); title = NULL; |
538 | } | |
539 | gtk_widget_destroy ( dia ); | |
540 | } |