Parent Directory | Revision Log
Invalid positions marked by NANs
1 | harbaum | 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 | harbaum | 34 | #include <math.h> // for isnan |
24 | harbaum | 1 | #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 | { "manual_goto_lat", STORE_FLOAT, OFFSET(manual_goto.lat) }, | ||
71 | { "manual_goto_lon", STORE_FLOAT, OFFSET(manual_goto.lon) }, | ||
72 | { "gps_lat", STORE_FLOAT, OFFSET(gps.lat) }, | ||
73 | { "gps_lon", STORE_FLOAT, OFFSET(gps.lon) }, | ||
74 | { "search_in", STORE_INT, OFFSET(search) }, | ||
75 | { "search_days", STORE_INT, OFFSET(search_days) }, | ||
76 | { "search_str", STORE_STRING, OFFSET(search_str) }, | ||
77 | { "gpxlist_items", STORE_INT, OFFSET(gpxlist_items) }, | ||
78 | { "cachelist_items", STORE_INT, OFFSET(cachelist_items) }, | ||
79 | { "compass_damping", STORE_INT, OFFSET(compass_damping) }, | ||
80 | { "cachelist_hide_found", STORE_BOOL, OFFSET(cachelist_hide_found) }, | ||
81 | #ifdef USE_MAEMO | ||
82 | { "mmpoi_dontlaunch", STORE_BOOL, OFFSET(mmpoi_dontlaunch) }, | ||
83 | { "cachelist_dss", STORE_BOOL, OFFSET(cachelist_disable_screensaver) }, | ||
84 | { "goto_dss", STORE_BOOL, OFFSET(goto_disable_screensaver) }, | ||
85 | { "cachelist_update", STORE_BOOL, OFFSET(cachelist_update) }, | ||
86 | #endif | ||
87 | |||
88 | { NULL, -1, -1 } | ||
89 | }; | ||
90 | |||
91 | static char *get_basename(char *name) { | ||
92 | char *p = strrchr(name, '/'); | ||
93 | if(!p) p = name; | ||
94 | else p = p+1; | ||
95 | |||
96 | g_assert(*p); | ||
97 | |||
98 | /* escape all non alnum characters */ | ||
99 | p = g_strdup(p); | ||
100 | int i; | ||
101 | for(i=0;i<strlen(p);i++) | ||
102 | if(!isalnum(p[i])) | ||
103 | p[i] = '_'; | ||
104 | |||
105 | return p; | ||
106 | } | ||
107 | |||
108 | void gconf_remove_closed_name(appdata_t *appdata, char *filename) { | ||
109 | char *key = g_strdup_printf(GCONF_KEY_CLOSED, get_basename(filename)); | ||
110 | gconf_client_unset(appdata->gconf_client, key, NULL); | ||
111 | g_free(key); | ||
112 | } | ||
113 | |||
114 | void gconf_save_closed_name(appdata_t *appdata, char *filename, char *name) { | ||
115 | char *key = g_strdup_printf(GCONF_KEY_CLOSED, get_basename(filename)); | ||
116 | gconf_client_set_string(appdata->gconf_client, key, name, NULL); | ||
117 | g_free(key); | ||
118 | } | ||
119 | |||
120 | char *gconf_restore_closed_name(appdata_t *appdata, char *filename) { | ||
121 | char *key = g_strdup_printf(GCONF_KEY_CLOSED, get_basename(filename)); | ||
122 | char *ret = gconf_client_get_string(appdata->gconf_client, key, NULL); | ||
123 | g_free(key); | ||
124 | return ret; | ||
125 | } | ||
126 | |||
127 | void gconf_save_state(appdata_t *appdata) { | ||
128 | int entries = 0; | ||
129 | |||
130 | gpx_t *gpx = appdata->gpx; | ||
131 | while(gpx) { | ||
132 | char str[128]; | ||
133 | snprintf(str, sizeof(str), GCONF_KEY_GPX, entries++); | ||
134 | gconf_client_set_string(appdata->gconf_client, str, gpx->filename, NULL); | ||
135 | gpx = gpx->next; | ||
136 | } | ||
137 | |||
138 | gconf_client_set_int(appdata->gconf_client, GCONF_KEY_CNT, entries, NULL); | ||
139 | |||
140 | /* -------------- save locations (excl. home location) --------------- */ | ||
141 | entries = 0; | ||
142 | location_t *loc = appdata->location; | ||
143 | while(loc) { | ||
144 | char str[128]; | ||
145 | snprintf(str, sizeof(str), GCONF_KEY_LOC_NAME, entries); | ||
146 | gconf_client_set_string(appdata->gconf_client, str, loc->name, NULL); | ||
147 | snprintf(str, sizeof(str), GCONF_KEY_LOC_LAT, entries); | ||
148 | gconf_client_set_float(appdata->gconf_client, str, loc->pos.lat, NULL); | ||
149 | snprintf(str, sizeof(str), GCONF_KEY_LOC_LON, entries); | ||
150 | gconf_client_set_float(appdata->gconf_client, str, loc->pos.lon, NULL); | ||
151 | entries++; | ||
152 | loc = loc->next; | ||
153 | } | ||
154 | |||
155 | gconf_client_set_int(appdata->gconf_client, GCONF_KEY_LOC_CNT, entries, NULL); | ||
156 | |||
157 | /* store everything listed in the store table */ | ||
158 | store_t *st = store; | ||
159 | while(st->key) { | ||
160 | void **ptr = ((void*)appdata) + st->offset; | ||
161 | harbaum | 34 | char *key = g_strdup_printf(GCONF_PATH "%s", st->key); |
162 | harbaum | 1 | |
163 | switch(st->type) { | ||
164 | case STORE_STRING: | ||
165 | if((char*)(*ptr)) { | ||
166 | gconf_client_set_string(appdata->gconf_client, key, (char*)(*ptr), NULL); | ||
167 | } | ||
168 | break; | ||
169 | |||
170 | case STORE_BOOL: | ||
171 | gconf_client_set_bool(appdata->gconf_client, key, *((int*)ptr), NULL); | ||
172 | break; | ||
173 | |||
174 | case STORE_INT: | ||
175 | gconf_client_set_int(appdata->gconf_client, key, *((int*)ptr), NULL); | ||
176 | break; | ||
177 | |||
178 | case STORE_FLOAT: | ||
179 | harbaum | 34 | if(!isnan(*((float*)ptr))) |
180 | gconf_client_set_float(appdata->gconf_client, key, *((float*)ptr), NULL); | ||
181 | harbaum | 1 | break; |
182 | |||
183 | default: | ||
184 | printf("Unsupported type %d\n", st->type); | ||
185 | break; | ||
186 | } | ||
187 | |||
188 | harbaum | 34 | g_free(key); |
189 | harbaum | 1 | st++; |
190 | } | ||
191 | } | ||
192 | |||
193 | void gconf_load_state(appdata_t *appdata) { | ||
194 | gpx_t **gpx = &appdata->gpx; | ||
195 | |||
196 | while(*gpx) gpx = &((*gpx)->next); | ||
197 | |||
198 | gpx_dialog_t *dialog = NULL; | ||
199 | |||
200 | harbaum | 34 | /* default positions are invalid */ |
201 | appdata->home.lat = appdata->home.lon = NAN; | ||
202 | appdata->manual_goto.lat = appdata->manual_goto.lon = NAN; | ||
203 | appdata->gps.lat = appdata->gps.lon = NAN; | ||
204 | |||
205 | harbaum | 1 | int i, entries = gconf_client_get_int(appdata->gconf_client, |
206 | GCONF_KEY_CNT, NULL); | ||
207 | |||
208 | if(entries) | ||
209 | dialog = gpx_busy_dialog_new(GTK_WIDGET(appdata->window)); | ||
210 | |||
211 | for(i=0;i<entries;i++) { | ||
212 | char str[128]; | ||
213 | snprintf(str, sizeof(str), GCONF_KEY_GPX, i); | ||
214 | char *fname = gconf_client_get_string(appdata->gconf_client, str, NULL); | ||
215 | |||
216 | if(fname) { | ||
217 | /* check if there's a valid name stored for this file. */ | ||
218 | /* if yes it's a "closed" file */ | ||
219 | char *name = gconf_restore_closed_name(appdata, fname); | ||
220 | if(name) { | ||
221 | *gpx = g_new0(gpx_t, 1); | ||
222 | (*gpx)->filename = fname; | ||
223 | (*gpx)->name = g_strdup(name); | ||
224 | (*gpx)->closed = TRUE; | ||
225 | } else { | ||
226 | if(g_file_test(fname, G_FILE_TEST_IS_DIR)) | ||
227 | *gpx = gpx_parse_dir(dialog, fname); | ||
228 | else | ||
229 | *gpx = gpx_parse(dialog, fname); | ||
230 | |||
231 | free(fname); | ||
232 | } | ||
233 | } | ||
234 | |||
235 | /* use next gpx entry of this was loaded successfully */ | ||
236 | if(*gpx) | ||
237 | gpx = &((*gpx)->next); | ||
238 | } | ||
239 | |||
240 | gpx_busy_dialog_destroy(dialog); | ||
241 | |||
242 | /* ------------- load locations --------------------- */ | ||
243 | entries = gconf_client_get_int(appdata->gconf_client, | ||
244 | GCONF_KEY_LOC_CNT, NULL); | ||
245 | |||
246 | location_t **loc = &appdata->location; | ||
247 | for(i=0;i<entries;i++) { | ||
248 | *loc = g_new0(location_t, 1); | ||
249 | if(*loc) { | ||
250 | char str[128]; | ||
251 | snprintf(str, sizeof(str), GCONF_KEY_LOC_NAME, i); | ||
252 | (*loc)->name = gconf_client_get_string(appdata->gconf_client, str, NULL); | ||
253 | snprintf(str, sizeof(str), GCONF_KEY_LOC_LAT, i); | ||
254 | (*loc)->pos.lat = gconf_client_get_float(appdata->gconf_client, str, NULL); | ||
255 | snprintf(str, sizeof(str), GCONF_KEY_LOC_LON, i); | ||
256 | (*loc)->pos.lon = gconf_client_get_float(appdata->gconf_client, str, NULL); | ||
257 | |||
258 | loc = &((*loc)->next); | ||
259 | } | ||
260 | } | ||
261 | |||
262 | /* restore everything listed in the store table */ | ||
263 | store_t *st = store; | ||
264 | while(st->key) { | ||
265 | void **ptr = ((void*)appdata) + st->offset; | ||
266 | harbaum | 34 | char *key = g_strdup_printf(GCONF_PATH "%s", st->key); |
267 | harbaum | 1 | |
268 | harbaum | 34 | /* check if key is present */ |
269 | GConfValue *value = gconf_client_get(appdata->gconf_client, key, NULL); | ||
270 | if(value) { | ||
271 | gconf_value_free(value); | ||
272 | |||
273 | switch(st->type) { | ||
274 | case STORE_STRING: { | ||
275 | char **str = (char**)ptr; | ||
276 | *str = gconf_client_get_string(appdata->gconf_client, key, NULL); | ||
277 | harbaum | 1 | } break; |
278 | harbaum | 34 | |
279 | case STORE_BOOL: | ||
280 | *((int*)ptr) = gconf_client_get_bool(appdata->gconf_client, key, NULL); | ||
281 | break; | ||
282 | |||
283 | case STORE_INT: | ||
284 | *((int*)ptr) = gconf_client_get_int(appdata->gconf_client, key, NULL); | ||
285 | break; | ||
286 | |||
287 | case STORE_FLOAT: | ||
288 | *((float*)ptr) = gconf_client_get_float(appdata->gconf_client, key, NULL); | ||
289 | break; | ||
290 | |||
291 | default: | ||
292 | printf("Unsupported type %d\n", st->type); | ||
293 | break; | ||
294 | } | ||
295 | harbaum | 1 | } |
296 | harbaum | 34 | g_free(key); |
297 | harbaum | 1 | st++; |
298 | } | ||
299 | |||
300 | /* ----- set all kinds of defaults ------- */ | ||
301 | |||
302 | if(!appdata->compass_damping) appdata->compass_damping = 1; | ||
303 | |||
304 | if(!appdata->mmpoi_radius) | ||
305 | appdata->mmpoi_radius = 100.0; // 100 km | ||
306 | |||
307 | if(!appdata->search) | ||
308 | appdata->search = SEARCH_NAME | SEARCH_ID; | ||
309 | |||
310 | if(!appdata->image_path) { | ||
311 | |||
312 | harbaum | 14 | /* use gps by default */ |
313 | appdata->use_gps = TRUE; | ||
314 | |||
315 | harbaum | 1 | #ifndef USE_MAEMO |
316 | char *p = getenv("HOME"); | ||
317 | if(p) { | ||
318 | /* build image path in home directory */ | ||
319 | appdata->image_path = | ||
320 | malloc(strlen(p)+strlen(DEFAULT_IMAGE_PATH_HOME)+2); | ||
321 | strcpy(appdata->image_path, p); | ||
322 | if(appdata->image_path[strlen(appdata->image_path)-1] != '/') | ||
323 | strcat(appdata->image_path, "/"); | ||
324 | strcat(appdata->image_path, DEFAULT_IMAGE_PATH_HOME); | ||
325 | } else | ||
326 | #endif | ||
327 | appdata->image_path = strdup(DEFAULT_IMAGE_PATH); | ||
328 | |||
329 | } else { | ||
330 | /* some versions old versions messed up the path */ | ||
331 | if(appdata->image_path[strlen(appdata->image_path)-1] != '/') { | ||
332 | printf("adjusting image path\n"); | ||
333 | appdata->image_path = realloc(appdata->image_path, | ||
334 | strlen(appdata->image_path)+2); | ||
335 | strcat(appdata->image_path, "/"); | ||
336 | } | ||
337 | } | ||
338 | |||
339 | if(!appdata->mmpoi_path) { | ||
340 | char *p = getenv("HOME"); | ||
341 | if(p) { | ||
342 | /* build mmpoi path in home directory */ | ||
343 | appdata->mmpoi_path = | ||
344 | malloc(strlen(p)+strlen(DEFAULT_MMPOI_PATH)+2); | ||
345 | strcpy(appdata->mmpoi_path, p); | ||
346 | if(appdata->mmpoi_path[strlen(appdata->mmpoi_path)-1] != '/') | ||
347 | strcat(appdata->mmpoi_path, "/"); | ||
348 | strcat(appdata->mmpoi_path, DEFAULT_MMPOI_PATH); | ||
349 | } else | ||
350 | appdata->mmpoi_path = strdup(DEFAULT_MMPOI_PATH); | ||
351 | } | ||
352 | |||
353 | if(!appdata->fieldnotes_path) { | ||
354 | char *p = getenv("HOME"); | ||
355 | if(p) { | ||
356 | /* build fieldnotes path in home directory */ | ||
357 | appdata->fieldnotes_path = | ||
358 | malloc(strlen(p)+strlen(DEFAULT_FIELDNOTES_PATH)+2); | ||
359 | strcpy(appdata->fieldnotes_path, p); | ||
360 | if(appdata->fieldnotes_path[strlen(appdata->fieldnotes_path)-1] != '/') | ||
361 | strcat(appdata->fieldnotes_path, "/"); | ||
362 | strcat(appdata->fieldnotes_path, DEFAULT_FIELDNOTES_PATH); | ||
363 | } else | ||
364 | appdata->fieldnotes_path = strdup(DEFAULT_FIELDNOTES_PATH); | ||
365 | } | ||
366 | |||
367 | if(!appdata->garmin_path) { | ||
368 | char *p = getenv("HOME"); | ||
369 | if(p) { | ||
370 | /* build image path in home directory */ | ||
371 | appdata->garmin_path = | ||
372 | malloc(strlen(p)+strlen(DEFAULT_GARMIN_PATH)+2); | ||
373 | strcpy(appdata->garmin_path, p); | ||
374 | if(appdata->garmin_path[strlen(appdata->garmin_path)-1] != '/') | ||
375 | strcat(appdata->garmin_path, "/"); | ||
376 | strcat(appdata->garmin_path, DEFAULT_GARMIN_PATH); | ||
377 | } else | ||
378 | appdata->garmin_path = strdup(DEFAULT_GARMIN_PATH); | ||
379 | } | ||
380 | |||
381 | /* make sure image path actually exists */ | ||
382 | checkdir(appdata->image_path); | ||
383 | |||
384 | if(!appdata->gpxlist_items) | ||
385 | appdata->gpxlist_items = GPXLIST_ITEM_DEFAULT; | ||
386 | |||
387 | if(!appdata->cachelist_items) | ||
388 | appdata->cachelist_items = CACHELIST_ITEM_DEFAULT; | ||
389 | |||
390 | harbaum | 13 | /* if there are no entries in the main list, try to add the */ |
391 | /* "welcome" one */ | ||
392 | if(!appdata->gpx) { | ||
393 | harbaum | 17 | char *name = g_strdup(ICONPATH "welcome.gpx"); |
394 | harbaum | 13 | dialog = gpx_busy_dialog_new(GTK_WIDGET(appdata->window)); |
395 | printf("No GPX file loaded, trying to load demo\n"); | ||
396 | appdata->gpx = gpx_parse(dialog, name); | ||
397 | gpx_busy_dialog_destroy(dialog); | ||
398 | g_free(name); | ||
399 | } | ||
400 | harbaum | 1 | } |
401 |