Contents of /trunk/src/gconf.c

Parent Directory Parent Directory | Revision Log Revision Log


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