Contents of /trunk/src/gconf.c

Parent Directory Parent Directory | Revision Log Revision Log


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