Contents of /trunk/src/gconf.c

Parent Directory Parent Directory | Revision Log Revision Log


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