Contents of /trunk/src/gconf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 246 - (show annotations)
Tue Jan 19 20:26:37 2010 UTC (14 years, 3 months ago) by harbaum
File MIME type: text/plain
File size: 16332 byte(s)
Initial Espeak support
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 #ifdef ESPEAK
94 { "espeak/enabled", STORE_BOOL, OFFSET(espeak.enabled) },
95 #endif
96
97 { "geotoad/password", STORE_STRING, OFFSET(gt.password) },
98 { "geotoad/filename", STORE_STRING, OFFSET(gt.filename) },
99 { "geotoad/distance", STORE_FLOAT, OFFSET(gt.distance) },
100 { "geotoad/lat", STORE_FLOAT, OFFSET(gt.lat) },
101 { "geotoad/lon", STORE_FLOAT, OFFSET(gt.lon) },
102 { "geotoad/flags", STORE_INT, OFFSET(gt.flags) },
103
104 { NULL, -1, -1 }
105 };
106
107 static char *get_basename(char *name) {
108 char *p = strrchr(name, '/');
109 if(!p) p = name;
110 else p = p+1;
111
112 g_assert(*p);
113
114 /* escape all non alnum characters */
115 p = g_strdup(p);
116 int i;
117 for(i=0;i<strlen(p);i++)
118 if(!isalnum(p[i]))
119 p[i] = '_';
120
121 return p;
122 }
123
124 void gconf_remove_closed_name(appdata_t *appdata, char *filename) {
125 if(!filename || !strlen(filename)) return;
126
127 char *key = g_strdup_printf(GCONF_KEY_CLOSED, get_basename(filename));
128 gconf_client_unset(appdata->gconf_client, key, NULL);
129 g_free(key);
130 }
131
132 void gconf_save_closed_name(appdata_t *appdata, char *filename, char *name) {
133 if(!filename || !strlen(filename)) return;
134
135 char *key = g_strdup_printf(GCONF_KEY_CLOSED, get_basename(filename));
136 gconf_client_set_string(appdata->gconf_client, key, name, NULL);
137 g_free(key);
138 }
139
140 char *gconf_restore_closed_name(appdata_t *appdata, char *filename) {
141 if(!filename || !strlen(filename)) return NULL;
142
143 char *key = g_strdup_printf(GCONF_KEY_CLOSED, get_basename(filename));
144 char *ret = gconf_client_get_string(appdata->gconf_client, key, NULL);
145 g_free(key);
146 return ret;
147 }
148
149 void gconf_save_state(appdata_t *appdata) {
150 int entries = 0;
151
152 printf("saving gconf state\n");
153
154 /* free proxy settings */
155 if(appdata->proxy) {
156 proxy_t *proxy = appdata->proxy;
157
158 if(proxy->authentication_password) g_free(proxy->authentication_password);
159 if(proxy->authentication_user) g_free(proxy->authentication_user);
160 if(proxy->host) g_free(proxy->host);
161 if(proxy->ignore_hosts) g_free(proxy->ignore_hosts);
162
163 g_free(proxy);
164 appdata->proxy = NULL;
165 }
166
167 gpx_t *gpx = appdata->gpx;
168 while(gpx) {
169 char str[128];
170 snprintf(str, sizeof(str), GCONF_KEY_GPX, entries++);
171 gconf_client_set_string(appdata->gconf_client, str, gpx->filename, NULL);
172 gpx = gpx->next;
173 }
174
175 gconf_client_set_int(appdata->gconf_client, GCONF_KEY_CNT, entries, NULL);
176
177 /* -------------- save locations (excl. home location) --------------- */
178 entries = 0;
179 location_t *loc = appdata->location;
180 while(loc) {
181 char str[128];
182 snprintf(str, sizeof(str), GCONF_KEY_LOC_NAME, entries);
183 gconf_client_set_string(appdata->gconf_client, str, loc->name, NULL);
184 snprintf(str, sizeof(str), GCONF_KEY_LOC_LAT, entries);
185 gconf_client_set_float(appdata->gconf_client, str, loc->pos.lat, NULL);
186 snprintf(str, sizeof(str), GCONF_KEY_LOC_LON, entries);
187 gconf_client_set_float(appdata->gconf_client, str, loc->pos.lon, NULL);
188 entries++;
189 loc = loc->next;
190 }
191
192 gconf_client_set_int(appdata->gconf_client, GCONF_KEY_LOC_CNT, entries, NULL);
193
194 /* store everything listed in the store table */
195 store_t *st = store;
196 while(st->key) {
197 void **ptr = ((void*)appdata) + st->offset;
198 char *key = g_strdup_printf(GCONF_PATH "%s", st->key);
199
200 switch(st->type) {
201 case STORE_STRING:
202 if((char*)(*ptr)) {
203 gconf_client_set_string(appdata->gconf_client, key, (char*)(*ptr), NULL);
204 }
205 g_free((char*)(*ptr));
206 *ptr = NULL;
207 break;
208
209 case STORE_BOOL:
210 gconf_client_set_bool(appdata->gconf_client, key, *((int*)ptr), NULL);
211 break;
212
213 case STORE_INT:
214 gconf_client_set_int(appdata->gconf_client, key, *((int*)ptr), NULL);
215 break;
216
217 case STORE_FLOAT:
218 if(!isnan(*((float*)ptr)))
219 gconf_client_set_float(appdata->gconf_client, key, *((float*)ptr), NULL);
220 break;
221
222 default:
223 printf("Unsupported type %d\n", st->type);
224 break;
225 }
226
227 g_free(key);
228 st++;
229 }
230 }
231
232 void gconf_load_state(appdata_t *appdata) {
233 gpx_t **gpx = &appdata->gpx;
234
235 while(*gpx) gpx = &((*gpx)->next);
236
237 gpx_dialog_t *dialog = NULL;
238
239 /* default positions are invalid */
240 appdata->home.lat = appdata->home.lon = NAN;
241 appdata->gps.lat = appdata->gps.lon = NAN;
242 appdata->geomath.lat = appdata->geomath.lon = NAN;
243
244 appdata->gt.lat = appdata->gt.lon = NAN;
245 appdata->gt.distance = 1.0; // in km/mil
246
247 /* ------------- get proxy settings -------------------- */
248 if(gconf_client_get_bool(appdata->gconf_client,
249 PROXY_KEY "use_http_proxy", NULL)) {
250 proxy_t *proxy = appdata->proxy = g_new0(proxy_t, 1);
251
252 /* get basic settings */
253 proxy->host = gconf_client_get_string(appdata->gconf_client,
254 PROXY_KEY "host", NULL);
255 proxy->port = gconf_client_get_int(appdata->gconf_client,
256 PROXY_KEY "port", NULL);
257 proxy->ignore_hosts =
258 gconf_client_get_string(appdata->gconf_client,
259 PROXY_KEY "ignore_hosts", NULL);
260
261 /* check for authentication */
262 proxy->use_authentication =
263 gconf_client_get_bool(appdata->gconf_client,
264 PROXY_KEY "use_authentication", NULL);
265
266 if(proxy->use_authentication) {
267 proxy->authentication_user =
268 gconf_client_get_string(appdata->gconf_client,
269 PROXY_KEY "authentication_user", NULL);
270 proxy->authentication_password =
271 gconf_client_get_string(appdata->gconf_client,
272 PROXY_KEY "authentication_password", NULL);
273 }
274 }
275
276 /* restore everything listed in the store table */
277 store_t *st = store;
278 while(st->key) {
279 void **ptr = ((void*)appdata) + st->offset;
280 char *key = g_strdup_printf(GCONF_PATH "%s", st->key);
281
282 /* check if key is present */
283 GConfValue *value = gconf_client_get(appdata->gconf_client, key, NULL);
284 if(value) {
285 gconf_value_free(value);
286
287 switch(st->type) {
288 case STORE_STRING: {
289 char **str = (char**)ptr;
290 *str = gconf_client_get_string(appdata->gconf_client, key, NULL);
291 } break;
292
293 case STORE_BOOL:
294 *((int*)ptr) = gconf_client_get_bool(appdata->gconf_client, key, NULL);
295 break;
296
297 case STORE_INT:
298 *((int*)ptr) = gconf_client_get_int(appdata->gconf_client, key, NULL);
299 break;
300
301 case STORE_FLOAT:
302 *((float*)ptr) = gconf_client_get_float(appdata->gconf_client, key, NULL);
303 break;
304
305 default:
306 printf("Unsupported type %d\n", st->type);
307 break;
308 }
309 }
310 g_free(key);
311 st++;
312 }
313
314 int i, entries = gconf_client_get_int(appdata->gconf_client,
315 GCONF_KEY_CNT, NULL);
316
317 if(entries)
318 dialog = gpx_busy_dialog_new(GTK_WIDGET(appdata->window));
319
320 for(i=0;i<entries;i++) {
321 char str[128];
322 snprintf(str, sizeof(str), GCONF_KEY_GPX, i);
323 char *fname = gconf_client_get_string(appdata->gconf_client, str, NULL);
324 if(fname) {
325 /* check if there's a valid name stored for this file. */
326 /* if yes it's a "closed" file */
327 char *name = gconf_restore_closed_name(appdata, fname);
328 if(name) {
329 *gpx = g_new0(gpx_t, 1);
330 (*gpx)->filename = fname;
331 (*gpx)->name = g_strdup(name);
332 (*gpx)->closed = TRUE;
333 } else {
334 if(g_file_test(fname, G_FILE_TEST_IS_DIR))
335 *gpx = gpx_parse_dir(dialog, fname, appdata->username);
336 else
337 *gpx = gpx_parse(dialog, fname, appdata->username);
338
339 if(!*gpx) {
340 /* restoring the gpx file failed, mark it as unusable, but save */
341 /* its presence for later use */
342
343 /* create "closed" entry to remember this file for next */
344 /* load attempt */
345 *gpx = g_new0(gpx_t, 1);
346 (*gpx)->filename = fname;
347 char *p = fname;
348 if(strrchr(fname, '/'))
349 p = strrchr(fname, '/')+1;
350
351 (*gpx)->name = g_strdup_printf(_("Failed to load: %s"), p);
352 (*gpx)->closed = TRUE;
353 } else
354 free(fname);
355 }
356 gpx = &((*gpx)->next);
357 }
358 }
359
360 gpx_busy_dialog_destroy(dialog);
361
362 /* ------------- load locations --------------------- */
363 entries = gconf_client_get_int(appdata->gconf_client,
364 GCONF_KEY_LOC_CNT, NULL);
365
366 location_t **loc = &appdata->location;
367 for(i=0;i<entries;i++) {
368 *loc = g_new0(location_t, 1);
369 if(*loc) {
370 char str[128];
371 snprintf(str, sizeof(str), GCONF_KEY_LOC_NAME, i);
372 (*loc)->name = gconf_client_get_string(appdata->gconf_client, str, NULL);
373 snprintf(str, sizeof(str), GCONF_KEY_LOC_LAT, i);
374 (*loc)->pos.lat = gconf_client_get_float(appdata->gconf_client, str, NULL);
375 snprintf(str, sizeof(str), GCONF_KEY_LOC_LON, i);
376 (*loc)->pos.lon = gconf_client_get_float(appdata->gconf_client, str, NULL);
377
378 loc = &((*loc)->next);
379 }
380 }
381
382 /* ----- set all kinds of defaults ------- */
383
384 if(!appdata->compass_damping) appdata->compass_damping = 1;
385
386 if(!appdata->mmpoi_radius)
387 appdata->mmpoi_radius = 100.0; // 100 km
388
389 if(!appdata->search)
390 appdata->search = SEARCH_NAME | SEARCH_ID;
391
392 if(!appdata->image_path) {
393 #ifdef USE_MAEMO
394 /* update cachelist by default */
395 appdata->cachelist_update = TRUE;
396 #endif
397
398 /* use gps by default */
399 appdata->use_gps = TRUE;
400
401 #ifndef USE_MAEMO
402 char *p = getenv("HOME");
403 if(p) {
404 /* build image path in home directory */
405 appdata->image_path =
406 malloc(strlen(p)+strlen(DEFAULT_IMAGE_PATH_HOME)+2);
407 strcpy(appdata->image_path, p);
408 if(appdata->image_path[strlen(appdata->image_path)-1] != '/')
409 strcat(appdata->image_path, "/");
410 strcat(appdata->image_path, DEFAULT_IMAGE_PATH_HOME);
411 } else
412 #endif
413 appdata->image_path = strdup(DEFAULT_IMAGE_PATH);
414
415 /* check if this path is actually accessible */
416 /* and change it to the current users home if not */
417 /* (this should only happen on scratchbox) */
418 if(!g_file_test(appdata->image_path, G_FILE_TEST_IS_DIR)) {
419 if(g_mkdir_with_parents(appdata->image_path, 0700) != 0) {
420 char *p = getenv("HOME");
421 if(!p) p = "/tmp/";
422
423 appdata->image_path =
424 g_strdup_printf("%s%s%s", p,
425 (p[strlen(p)-1]!='/')?"/":"",
426 DEFAULT_IMAGE_PATH_HOME);
427 printf("using alt path %s\n", appdata->image_path);
428 }
429 }
430
431 } else {
432 /* some versions old versions messed up the path */
433 if(appdata->image_path[strlen(appdata->image_path)-1] != '/') {
434 printf("adjusting image path\n");
435 appdata->image_path = realloc(appdata->image_path,
436 strlen(appdata->image_path)+2);
437 strcat(appdata->image_path, "/");
438 }
439 }
440
441 if(!appdata->mmpoi_path) {
442 char *p = getenv("HOME");
443 if(p) {
444 /* build mmpoi path in home directory */
445 appdata->mmpoi_path =
446 malloc(strlen(p)+strlen(DEFAULT_MMPOI_PATH)+2);
447 strcpy(appdata->mmpoi_path, p);
448 if(appdata->mmpoi_path[strlen(appdata->mmpoi_path)-1] != '/')
449 strcat(appdata->mmpoi_path, "/");
450 strcat(appdata->mmpoi_path, DEFAULT_MMPOI_PATH);
451 } else
452 appdata->mmpoi_path = strdup(DEFAULT_MMPOI_PATH);
453 }
454
455 if(!appdata->fieldnotes_path) {
456 char *p = getenv("HOME");
457 if(p) {
458 /* build fieldnotes path in home directory */
459 appdata->fieldnotes_path =
460 malloc(strlen(p)+strlen(DEFAULT_FIELDNOTES_PATH)+2);
461 strcpy(appdata->fieldnotes_path, p);
462 if(appdata->fieldnotes_path[strlen(appdata->fieldnotes_path)-1] != '/')
463 strcat(appdata->fieldnotes_path, "/");
464 strcat(appdata->fieldnotes_path, DEFAULT_FIELDNOTES_PATH);
465 } else
466 appdata->fieldnotes_path = strdup(DEFAULT_FIELDNOTES_PATH);
467 }
468
469 if(!appdata->garmin_path) {
470 char *p = getenv("HOME");
471 if(p) {
472 /* build image path in home directory */
473 appdata->garmin_path =
474 malloc(strlen(p)+strlen(DEFAULT_GARMIN_PATH)+2);
475 strcpy(appdata->garmin_path, p);
476 if(appdata->garmin_path[strlen(appdata->garmin_path)-1] != '/')
477 strcat(appdata->garmin_path, "/");
478 strcat(appdata->garmin_path, DEFAULT_GARMIN_PATH);
479 } else
480 appdata->garmin_path = strdup(DEFAULT_GARMIN_PATH);
481 }
482
483 /* make sure image path actually exists */
484 checkdir(appdata->image_path);
485
486 if(!appdata->gpxlist_items)
487 appdata->gpxlist_items = GPXLIST_ITEM_DEFAULT;
488
489 if(!appdata->cachelist_items)
490 appdata->cachelist_items = CACHELIST_ITEM_DEFAULT;
491
492 /* if there are no entries in the main list, try to add the */
493 /* "welcome" one */
494 if(!appdata->gpx) {
495 char *name = g_strdup(ICONPATH "welcome.gpx");
496 dialog = gpx_busy_dialog_new(GTK_WIDGET(appdata->window));
497 printf("No GPX file loaded, trying to load demo\n");
498 appdata->gpx = gpx_parse(dialog, name, appdata->username);
499 gpx_busy_dialog_destroy(dialog);
500 g_free(name);
501 }
502 }
503