Parent Directory | Revision Log
Display geomath result on map
1 | /* |
2 | * Copyright (C) 2008 Till Harbaum <till@harbaum.org>. |
3 | * |
4 | * This file is part of GPXView. |
5 | * |
6 | * GPXView 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 3 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * GPXView 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 GPXView. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | #include <stddef.h> |
21 | #include <stdlib.h> |
22 | #include <ctype.h> |
23 | #include <math.h> // for isnan |
24 | #include "gpxview.h" |
25 | |
26 | #define GCONF_PATH "/apps/gpxview/" |
27 | #define GCONF_KEY_GPX GCONF_PATH "gpx%d" |
28 | #define GCONF_KEY_CNT GCONF_PATH "entries" |
29 | |
30 | #define GCONF_KEY_LOC_NAME GCONF_PATH "location%d/name" |
31 | #define GCONF_KEY_LOC_LAT GCONF_PATH "location%d/latitude" |
32 | #define GCONF_KEY_LOC_LON GCONF_PATH "location%d/longitude" |
33 | #define GCONF_KEY_LOC_CNT GCONF_PATH "location_entries" |
34 | |
35 | #define GCONF_KEY_CLOSED GCONF_PATH "closed/%s" |
36 | |
37 | #include <string.h> |
38 | |
39 | enum { |
40 | STORE_STRING, STORE_FLOAT, STORE_INT, STORE_BOOL, |
41 | }; |
42 | |
43 | typedef struct { |
44 | char *key; |
45 | int type; |
46 | int offset; |
47 | } store_t; |
48 | |
49 | #define OFFSET(a) offsetof(appdata_t, a) |
50 | |
51 | static store_t store[] = { |
52 | { "image_path", STORE_STRING, OFFSET(image_path) }, |
53 | { "path", STORE_STRING, OFFSET(path) }, |
54 | { "geotext/text", STORE_STRING, OFFSET(geotext_text) }, |
55 | { "geotext/shift", STORE_INT, OFFSET(geotext_shift) }, |
56 | { "mmpoi_path", STORE_STRING, OFFSET(mmpoi_path) }, |
57 | { "garmin_path", STORE_STRING, OFFSET(garmin_path) }, |
58 | { "fnotes_path", STORE_STRING, OFFSET(fieldnotes_path) }, |
59 | { "garmin_ign_found", STORE_BOOL, OFFSET(garmin_ign_found) }, |
60 | { "active_location", STORE_INT, OFFSET(active_location) }, |
61 | { "mmpoi_use_radius", STORE_BOOL, OFFSET(mmpoi_use_radius) }, |
62 | { "mmpoi_radius", STORE_FLOAT, OFFSET(mmpoi_radius) }, |
63 | { "mmpoi_ign_found", STORE_BOOL, OFFSET(mmpoi_dont_export_found) }, |
64 | { "mmpoi_ign_disabl", STORE_BOOL, OFFSET(mmpoi_dont_export_disabled) }, |
65 | { "use_gps", STORE_BOOL, OFFSET(use_gps) }, |
66 | { "imperial", STORE_BOOL, OFFSET(imperial) }, |
67 | { "compass_locked", STORE_BOOL, OFFSET(compass_locked) }, |
68 | { "latitude", STORE_FLOAT, OFFSET(home.lat) }, |
69 | { "longitude", STORE_FLOAT, OFFSET(home.lon) }, |
70 | { "gps_lat", STORE_FLOAT, OFFSET(gps.lat) }, |
71 | { "gps_lon", STORE_FLOAT, OFFSET(gps.lon) }, |
72 | { "search_in", STORE_INT, OFFSET(search) }, |
73 | { "search_days", STORE_INT, OFFSET(search_days) }, |
74 | { "search_str", STORE_STRING, OFFSET(search_str) }, |
75 | { "gpxlist_items", STORE_INT, OFFSET(gpxlist_items) }, |
76 | { "cachelist_items", STORE_INT, OFFSET(cachelist_items) }, |
77 | { "compass_damping", STORE_INT, OFFSET(compass_damping) }, |
78 | { "cachelist_hide_found", STORE_BOOL, OFFSET(cachelist_hide_found) }, |
79 | { "cachelist_update", STORE_BOOL, OFFSET(cachelist_update) }, |
80 | { "disable_gcvote", STORE_BOOL, OFFSET(disable_gcvote) }, |
81 | { "username", STORE_STRING, OFFSET(username) }, |
82 | #ifdef USE_MAEMO |
83 | { "mmpoi_dontlaunch", STORE_BOOL, OFFSET(mmpoi_dontlaunch) }, |
84 | { "cachelist_dss", STORE_BOOL, OFFSET(cachelist_disable_screensaver) }, |
85 | { "goto_dss", STORE_BOOL, OFFSET(goto_disable_screensaver) }, |
86 | #endif |
87 | #ifdef ENABLE_OSM_GPS_MAP |
88 | { "map_lat", STORE_FLOAT, OFFSET(map.pos.lat) }, |
89 | { "map_lon", STORE_FLOAT, OFFSET(map.pos.lon) }, |
90 | { "map_zoom", STORE_INT, OFFSET(map.zoom) }, |
91 | { "map_source", STORE_INT, OFFSET(map.source) }, |
92 | #endif |
93 | |
94 | { "geotoad/password", STORE_STRING, OFFSET(gt.password) }, |
95 | { "geotoad/filename", STORE_STRING, OFFSET(gt.filename) }, |
96 | { "geotoad/distance", STORE_FLOAT, OFFSET(gt.distance) }, |
97 | { "geotoad/lat", STORE_FLOAT, OFFSET(gt.lat) }, |
98 | { "geotoad/lon", STORE_FLOAT, OFFSET(gt.lon) }, |
99 | { "geotoad/flags", STORE_INT, OFFSET(gt.flags) }, |
100 | |
101 | { NULL, -1, -1 } |
102 | }; |
103 | |
104 | static char *get_basename(char *name) { |
105 | char *p = strrchr(name, '/'); |
106 | if(!p) p = name; |
107 | else p = p+1; |
108 | |
109 | g_assert(*p); |
110 | |
111 | /* escape all non alnum characters */ |
112 | p = g_strdup(p); |
113 | int i; |
114 | for(i=0;i<strlen(p);i++) |
115 | if(!isalnum(p[i])) |
116 | p[i] = '_'; |
117 | |
118 | return p; |
119 | } |
120 | |
121 | void gconf_remove_closed_name(appdata_t *appdata, char *filename) { |
122 | if(!filename || !strlen(filename)) return; |
123 | |
124 | char *key = g_strdup_printf(GCONF_KEY_CLOSED, get_basename(filename)); |
125 | gconf_client_unset(appdata->gconf_client, key, NULL); |
126 | g_free(key); |
127 | } |
128 | |
129 | void gconf_save_closed_name(appdata_t *appdata, char *filename, char *name) { |
130 | if(!filename || !strlen(filename)) return; |
131 | |
132 | char *key = g_strdup_printf(GCONF_KEY_CLOSED, get_basename(filename)); |
133 | gconf_client_set_string(appdata->gconf_client, key, name, NULL); |
134 | g_free(key); |
135 | } |
136 | |
137 | char *gconf_restore_closed_name(appdata_t *appdata, char *filename) { |
138 | if(!filename || !strlen(filename)) return NULL; |
139 | |
140 | char *key = g_strdup_printf(GCONF_KEY_CLOSED, get_basename(filename)); |
141 | char *ret = gconf_client_get_string(appdata->gconf_client, key, NULL); |
142 | g_free(key); |
143 | return ret; |
144 | } |
145 | |
146 | void gconf_save_state(appdata_t *appdata) { |
147 | int entries = 0; |
148 | |
149 | /* free proxy settings */ |
150 | if(appdata->proxy) { |
151 | proxy_t *proxy = appdata->proxy; |
152 | |
153 | if(proxy->authentication_password) g_free(proxy->authentication_password); |
154 | if(proxy->authentication_user) g_free(proxy->authentication_user); |
155 | if(proxy->host) g_free(proxy->host); |
156 | if(proxy->ignore_hosts) g_free(proxy->ignore_hosts); |
157 | |
158 | g_free(proxy); |
159 | appdata->proxy = NULL; |
160 | } |
161 | |
162 | gpx_t *gpx = appdata->gpx; |
163 | while(gpx) { |
164 | char str[128]; |
165 | snprintf(str, sizeof(str), GCONF_KEY_GPX, entries++); |
166 | gconf_client_set_string(appdata->gconf_client, str, gpx->filename, NULL); |
167 | gpx = gpx->next; |
168 | } |
169 | |
170 | gconf_client_set_int(appdata->gconf_client, GCONF_KEY_CNT, entries, NULL); |
171 | |
172 | /* -------------- save locations (excl. home location) --------------- */ |
173 | entries = 0; |
174 | location_t *loc = appdata->location; |
175 | while(loc) { |
176 | char str[128]; |
177 | snprintf(str, sizeof(str), GCONF_KEY_LOC_NAME, entries); |
178 | gconf_client_set_string(appdata->gconf_client, str, loc->name, NULL); |
179 | snprintf(str, sizeof(str), GCONF_KEY_LOC_LAT, entries); |
180 | gconf_client_set_float(appdata->gconf_client, str, loc->pos.lat, NULL); |
181 | snprintf(str, sizeof(str), GCONF_KEY_LOC_LON, entries); |
182 | gconf_client_set_float(appdata->gconf_client, str, loc->pos.lon, NULL); |
183 | entries++; |
184 | loc = loc->next; |
185 | } |
186 | |
187 | gconf_client_set_int(appdata->gconf_client, GCONF_KEY_LOC_CNT, entries, NULL); |
188 | |
189 | /* store everything listed in the store table */ |
190 | store_t *st = store; |
191 | while(st->key) { |
192 | void **ptr = ((void*)appdata) + st->offset; |
193 | char *key = g_strdup_printf(GCONF_PATH "%s", st->key); |
194 | |
195 | switch(st->type) { |
196 | case STORE_STRING: |
197 | if((char*)(*ptr)) { |
198 | gconf_client_set_string(appdata->gconf_client, key, (char*)(*ptr), NULL); |
199 | } |
200 | g_free((char*)(*ptr)); |
201 | *ptr = NULL; |
202 | break; |
203 | |
204 | case STORE_BOOL: |
205 | gconf_client_set_bool(appdata->gconf_client, key, *((int*)ptr), NULL); |
206 | break; |
207 | |
208 | case STORE_INT: |
209 | gconf_client_set_int(appdata->gconf_client, key, *((int*)ptr), NULL); |
210 | break; |
211 | |
212 | case STORE_FLOAT: |
213 | if(!isnan(*((float*)ptr))) |
214 | gconf_client_set_float(appdata->gconf_client, key, *((float*)ptr), NULL); |
215 | break; |
216 | |
217 | default: |
218 | printf("Unsupported type %d\n", st->type); |
219 | break; |
220 | } |
221 | |
222 | g_free(key); |
223 | st++; |
224 | } |
225 | } |
226 | |
227 | void gconf_load_state(appdata_t *appdata) { |
228 | gpx_t **gpx = &appdata->gpx; |
229 | |
230 | while(*gpx) gpx = &((*gpx)->next); |
231 | |
232 | gpx_dialog_t *dialog = NULL; |
233 | |
234 | /* default positions are invalid */ |
235 | appdata->home.lat = appdata->home.lon = NAN; |
236 | appdata->gps.lat = appdata->gps.lon = NAN; |
237 | appdata->geomath.lat = appdata->geomath.lon = NAN; |
238 | |
239 | appdata->gt.lat = appdata->gt.lon = NAN; |
240 | appdata->gt.distance = 1.0; // in km/mil |
241 | |
242 | /* ------------- get proxy settings -------------------- */ |
243 | if(gconf_client_get_bool(appdata->gconf_client, |
244 | PROXY_KEY "use_http_proxy", NULL)) { |
245 | proxy_t *proxy = appdata->proxy = g_new0(proxy_t, 1); |
246 | |
247 | /* get basic settings */ |
248 | proxy->host = gconf_client_get_string(appdata->gconf_client, |
249 | PROXY_KEY "host", NULL); |
250 | proxy->port = gconf_client_get_int(appdata->gconf_client, |
251 | PROXY_KEY "port", NULL); |
252 | proxy->ignore_hosts = |
253 | gconf_client_get_string(appdata->gconf_client, |
254 | PROXY_KEY "ignore_hosts", NULL); |
255 | |
256 | /* check for authentication */ |
257 | proxy->use_authentication = |
258 | gconf_client_get_bool(appdata->gconf_client, |
259 | PROXY_KEY "use_authentication", NULL); |
260 | |
261 | if(proxy->use_authentication) { |
262 | proxy->authentication_user = |
263 | gconf_client_get_string(appdata->gconf_client, |
264 | PROXY_KEY "authentication_user", NULL); |
265 | proxy->authentication_password = |
266 | gconf_client_get_string(appdata->gconf_client, |
267 | PROXY_KEY "authentication_password", NULL); |
268 | } |
269 | } |
270 | |
271 | /* restore everything listed in the store table */ |
272 | store_t *st = store; |
273 | while(st->key) { |
274 | void **ptr = ((void*)appdata) + st->offset; |
275 | char *key = g_strdup_printf(GCONF_PATH "%s", st->key); |
276 | |
277 | /* check if key is present */ |
278 | GConfValue *value = gconf_client_get(appdata->gconf_client, key, NULL); |
279 | if(value) { |
280 | gconf_value_free(value); |
281 | |
282 | switch(st->type) { |
283 | case STORE_STRING: { |
284 | char **str = (char**)ptr; |
285 | *str = gconf_client_get_string(appdata->gconf_client, key, NULL); |
286 | } break; |
287 | |
288 | case STORE_BOOL: |
289 | *((int*)ptr) = gconf_client_get_bool(appdata->gconf_client, key, NULL); |
290 | break; |
291 | |
292 | case STORE_INT: |
293 | *((int*)ptr) = gconf_client_get_int(appdata->gconf_client, key, NULL); |
294 | break; |
295 | |
296 | case STORE_FLOAT: |
297 | *((float*)ptr) = gconf_client_get_float(appdata->gconf_client, key, NULL); |
298 | break; |
299 | |
300 | default: |
301 | printf("Unsupported type %d\n", st->type); |
302 | break; |
303 | } |
304 | } |
305 | g_free(key); |
306 | st++; |
307 | } |
308 | |
309 | int i, entries = gconf_client_get_int(appdata->gconf_client, |
310 | GCONF_KEY_CNT, NULL); |
311 | |
312 | if(entries) |
313 | dialog = gpx_busy_dialog_new(GTK_WIDGET(appdata->window)); |
314 | |
315 | for(i=0;i<entries;i++) { |
316 | char str[128]; |
317 | snprintf(str, sizeof(str), GCONF_KEY_GPX, i); |
318 | char *fname = gconf_client_get_string(appdata->gconf_client, str, NULL); |
319 | if(fname) { |
320 | /* check if there's a valid name stored for this file. */ |
321 | /* if yes it's a "closed" file */ |
322 | char *name = gconf_restore_closed_name(appdata, fname); |
323 | if(name) { |
324 | *gpx = g_new0(gpx_t, 1); |
325 | (*gpx)->filename = fname; |
326 | (*gpx)->name = g_strdup(name); |
327 | (*gpx)->closed = TRUE; |
328 | } else { |
329 | if(g_file_test(fname, G_FILE_TEST_IS_DIR)) |
330 | *gpx = gpx_parse_dir(dialog, fname, appdata->username); |
331 | else |
332 | *gpx = gpx_parse(dialog, fname, appdata->username); |
333 | |
334 | if(!*gpx) { |
335 | /* restoring the gpx file failed, mark it as unusable, but save */ |
336 | /* its presence for later use */ |
337 | |
338 | /* create "closed" entry to remember this file for next */ |
339 | /* load attempt */ |
340 | *gpx = g_new0(gpx_t, 1); |
341 | (*gpx)->filename = fname; |
342 | char *p = fname; |
343 | if(strrchr(fname, '/')) |
344 | p = strrchr(fname, '/')+1; |
345 | |
346 | (*gpx)->name = g_strdup_printf(_("Failed to load: %s"), p); |
347 | (*gpx)->closed = TRUE; |
348 | } else |
349 | free(fname); |
350 | } |
351 | gpx = &((*gpx)->next); |
352 | } |
353 | } |
354 | |
355 | gpx_busy_dialog_destroy(dialog); |
356 | |
357 | /* ------------- load locations --------------------- */ |
358 | entries = gconf_client_get_int(appdata->gconf_client, |
359 | GCONF_KEY_LOC_CNT, NULL); |
360 | |
361 | location_t **loc = &appdata->location; |
362 | for(i=0;i<entries;i++) { |
363 | *loc = g_new0(location_t, 1); |
364 | if(*loc) { |
365 | char str[128]; |
366 | snprintf(str, sizeof(str), GCONF_KEY_LOC_NAME, i); |
367 | (*loc)->name = gconf_client_get_string(appdata->gconf_client, str, NULL); |
368 | snprintf(str, sizeof(str), GCONF_KEY_LOC_LAT, i); |
369 | (*loc)->pos.lat = gconf_client_get_float(appdata->gconf_client, str, NULL); |
370 | snprintf(str, sizeof(str), GCONF_KEY_LOC_LON, i); |
371 | (*loc)->pos.lon = gconf_client_get_float(appdata->gconf_client, str, NULL); |
372 | |
373 | loc = &((*loc)->next); |
374 | } |
375 | } |
376 | |
377 | /* ----- set all kinds of defaults ------- */ |
378 | |
379 | if(!appdata->compass_damping) appdata->compass_damping = 1; |
380 | |
381 | if(!appdata->mmpoi_radius) |
382 | appdata->mmpoi_radius = 100.0; // 100 km |
383 | |
384 | if(!appdata->search) |
385 | appdata->search = SEARCH_NAME | SEARCH_ID; |
386 | |
387 | if(!appdata->image_path) { |
388 | #ifdef USE_MAEMO |
389 | /* update cachelist by default */ |
390 | appdata->cachelist_update = TRUE; |
391 | #endif |
392 | |
393 | /* use gps by default */ |
394 | appdata->use_gps = TRUE; |
395 | |
396 | #ifndef USE_MAEMO |
397 | char *p = getenv("HOME"); |
398 | if(p) { |
399 | /* build image path in home directory */ |
400 | appdata->image_path = |
401 | malloc(strlen(p)+strlen(DEFAULT_IMAGE_PATH_HOME)+2); |
402 | strcpy(appdata->image_path, p); |
403 | if(appdata->image_path[strlen(appdata->image_path)-1] != '/') |
404 | strcat(appdata->image_path, "/"); |
405 | strcat(appdata->image_path, DEFAULT_IMAGE_PATH_HOME); |
406 | } else |
407 | #endif |
408 | appdata->image_path = strdup(DEFAULT_IMAGE_PATH); |
409 | |
410 | /* check if this path is actually accessible */ |
411 | /* and change it to the current users home if not */ |
412 | /* (this should only happen on scratchbox) */ |
413 | if(!g_file_test(appdata->image_path, G_FILE_TEST_IS_DIR)) { |
414 | if(g_mkdir_with_parents(appdata->image_path, 0700) != 0) { |
415 | char *p = getenv("HOME"); |
416 | if(!p) p = "/tmp/"; |
417 | |
418 | appdata->image_path = |
419 | g_strdup_printf("%s%s%s", p, |
420 | (p[strlen(p)-1]!='/')?"/":"", |
421 | DEFAULT_IMAGE_PATH_HOME); |
422 | printf("using alt path %s\n", appdata->image_path); |
423 | } |
424 | } |
425 | |
426 | } else { |
427 | /* some versions old versions messed up the path */ |
428 | if(appdata->image_path[strlen(appdata->image_path)-1] != '/') { |
429 | printf("adjusting image path\n"); |
430 | appdata->image_path = realloc(appdata->image_path, |
431 | strlen(appdata->image_path)+2); |
432 | strcat(appdata->image_path, "/"); |
433 | } |
434 | } |
435 | |
436 | if(!appdata->mmpoi_path) { |
437 | char *p = getenv("HOME"); |
438 | if(p) { |
439 | /* build mmpoi path in home directory */ |
440 | appdata->mmpoi_path = |
441 | malloc(strlen(p)+strlen(DEFAULT_MMPOI_PATH)+2); |
442 | strcpy(appdata->mmpoi_path, p); |
443 | if(appdata->mmpoi_path[strlen(appdata->mmpoi_path)-1] != '/') |
444 | strcat(appdata->mmpoi_path, "/"); |
445 | strcat(appdata->mmpoi_path, DEFAULT_MMPOI_PATH); |
446 | } else |
447 | appdata->mmpoi_path = strdup(DEFAULT_MMPOI_PATH); |
448 | } |
449 | |
450 | if(!appdata->fieldnotes_path) { |
451 | char *p = getenv("HOME"); |
452 | if(p) { |
453 | /* build fieldnotes path in home directory */ |
454 | appdata->fieldnotes_path = |
455 | malloc(strlen(p)+strlen(DEFAULT_FIELDNOTES_PATH)+2); |
456 | strcpy(appdata->fieldnotes_path, p); |
457 | if(appdata->fieldnotes_path[strlen(appdata->fieldnotes_path)-1] != '/') |
458 | strcat(appdata->fieldnotes_path, "/"); |
459 | strcat(appdata->fieldnotes_path, DEFAULT_FIELDNOTES_PATH); |
460 | } else |
461 | appdata->fieldnotes_path = strdup(DEFAULT_FIELDNOTES_PATH); |
462 | } |
463 | |
464 | if(!appdata->garmin_path) { |
465 | char *p = getenv("HOME"); |
466 | if(p) { |
467 | /* build image path in home directory */ |
468 | appdata->garmin_path = |
469 | malloc(strlen(p)+strlen(DEFAULT_GARMIN_PATH)+2); |
470 | strcpy(appdata->garmin_path, p); |
471 | if(appdata->garmin_path[strlen(appdata->garmin_path)-1] != '/') |
472 | strcat(appdata->garmin_path, "/"); |
473 | strcat(appdata->garmin_path, DEFAULT_GARMIN_PATH); |
474 | } else |
475 | appdata->garmin_path = strdup(DEFAULT_GARMIN_PATH); |
476 | } |
477 | |
478 | /* make sure image path actually exists */ |
479 | checkdir(appdata->image_path); |
480 | |
481 | if(!appdata->gpxlist_items) |
482 | appdata->gpxlist_items = GPXLIST_ITEM_DEFAULT; |
483 | |
484 | if(!appdata->cachelist_items) |
485 | appdata->cachelist_items = CACHELIST_ITEM_DEFAULT; |
486 | |
487 | /* if there are no entries in the main list, try to add the */ |
488 | /* "welcome" one */ |
489 | if(!appdata->gpx) { |
490 | char *name = g_strdup(ICONPATH "welcome.gpx"); |
491 | dialog = gpx_busy_dialog_new(GTK_WIDGET(appdata->window)); |
492 | printf("No GPX file loaded, trying to load demo\n"); |
493 | appdata->gpx = gpx_parse(dialog, name, appdata->username); |
494 | gpx_busy_dialog_destroy(dialog); |
495 | g_free(name); |
496 | } |
497 | } |
498 |