Diff of /trunk/src/gconf.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1 by harbaum, Sat Jun 20 11:08:47 2009 UTC revision 196 by harbaum, Thu Nov 19 07:39:00 2009 UTC
# Line 20  Line 20 
20  #include <stddef.h>  #include <stddef.h>
21  #include <stdlib.h>  #include <stdlib.h>
22  #include <ctype.h>  #include <ctype.h>
23    #include <math.h>     // for isnan
24  #include "gpxview.h"  #include "gpxview.h"
25    
26  #define GCONF_PATH         "/apps/gpxview/"  #define GCONF_PATH         "/apps/gpxview/"
# Line 70  static store_t store[] = { Line 71  static store_t store[] = {
71    { "manual_goto_lon",  STORE_FLOAT,  OFFSET(manual_goto.lon) },    { "manual_goto_lon",  STORE_FLOAT,  OFFSET(manual_goto.lon) },
72    { "gps_lat",          STORE_FLOAT,  OFFSET(gps.lat) },    { "gps_lat",          STORE_FLOAT,  OFFSET(gps.lat) },
73    { "gps_lon",          STORE_FLOAT,  OFFSET(gps.lon) },    { "gps_lon",          STORE_FLOAT,  OFFSET(gps.lon) },
   { "load_images",      STORE_BOOL,   OFFSET(load_images) },  
74    { "search_in",        STORE_INT,    OFFSET(search) },    { "search_in",        STORE_INT,    OFFSET(search) },
75    { "search_days",      STORE_INT,    OFFSET(search_days) },    { "search_days",      STORE_INT,    OFFSET(search_days) },
76    { "search_str",       STORE_STRING, OFFSET(search_str) },    { "search_str",       STORE_STRING, OFFSET(search_str) },
# Line 78  static store_t store[] = { Line 78  static store_t store[] = {
78    { "cachelist_items",  STORE_INT,    OFFSET(cachelist_items) },    { "cachelist_items",  STORE_INT,    OFFSET(cachelist_items) },
79    { "compass_damping",  STORE_INT,    OFFSET(compass_damping) },    { "compass_damping",  STORE_INT,    OFFSET(compass_damping) },
80    { "cachelist_hide_found", STORE_BOOL, OFFSET(cachelist_hide_found) },    { "cachelist_hide_found", STORE_BOOL, OFFSET(cachelist_hide_found) },
81      { "cachelist_update", STORE_BOOL,   OFFSET(cachelist_update) },
82      { "disable_gcvote",   STORE_BOOL,   OFFSET(disable_gcvote) },
83  #ifdef USE_MAEMO  #ifdef USE_MAEMO
84    { "mmpoi_dontlaunch", STORE_BOOL,   OFFSET(mmpoi_dontlaunch) },    { "mmpoi_dontlaunch", STORE_BOOL,   OFFSET(mmpoi_dontlaunch) },
85    { "cachelist_dss",    STORE_BOOL,   OFFSET(cachelist_disable_screensaver) },    { "cachelist_dss",    STORE_BOOL,   OFFSET(cachelist_disable_screensaver) },
86    { "goto_dss",         STORE_BOOL,   OFFSET(goto_disable_screensaver) },    { "goto_dss",         STORE_BOOL,   OFFSET(goto_disable_screensaver) },
87    { "cachelist_update", STORE_BOOL,   OFFSET(cachelist_update) },  #endif
88    #ifdef ENABLE_OSM_GPS_MAP
89      { "map_lat",          STORE_FLOAT,  OFFSET(map.pos.lat) },
90      { "map_lon",          STORE_FLOAT,  OFFSET(map.pos.lon) },
91      { "map_zoom",         STORE_INT,    OFFSET(map.zoom) },
92      { "map_source",       STORE_INT,    OFFSET(map.source) },
93  #endif  #endif
94    
95      { "gt_username",      STORE_STRING, OFFSET(gt.username) },
96      { "gt_password",      STORE_STRING, OFFSET(gt.password) },
97      { "gt_filename",      STORE_STRING, OFFSET(gt.filename) },
98      { "gt_distance",      STORE_FLOAT,  OFFSET(gt.distance) },
99      { "gt_lat",           STORE_FLOAT,  OFFSET(gt.lat) },
100      { "gt_lon",           STORE_FLOAT,  OFFSET(gt.lon) },
101      { "gt_flags",         STORE_INT,    OFFSET(gt.flags) },
102    
103    { NULL, -1, -1 }    { NULL, -1, -1 }
104  };  };
105    
# Line 127  char *gconf_restore_closed_name(appdata_ Line 142  char *gconf_restore_closed_name(appdata_
142  void gconf_save_state(appdata_t *appdata) {  void gconf_save_state(appdata_t *appdata) {
143    int entries = 0;    int entries = 0;
144    
145      /* free proxy settings */
146      if(appdata->proxy) {
147        proxy_t *proxy = appdata->proxy;
148    
149        if(proxy->authentication_password) g_free(proxy->authentication_password);
150        if(proxy->authentication_user)     g_free(proxy->authentication_user);
151        if(proxy->host)                    g_free(proxy->host);
152        if(proxy->ignore_hosts)            g_free(proxy->ignore_hosts);
153    
154        g_free(proxy);
155        appdata->proxy = NULL;
156      }
157    
158    gpx_t *gpx = appdata->gpx;    gpx_t *gpx = appdata->gpx;
159    while(gpx) {    while(gpx) {
160      char str[128];      char str[128];
# Line 157  void gconf_save_state(appdata_t *appdata Line 185  void gconf_save_state(appdata_t *appdata
185    /* store everything listed in the store table */    /* store everything listed in the store table */
186    store_t *st = store;    store_t *st = store;
187    while(st->key) {    while(st->key) {
     char key[256];  
188      void **ptr = ((void*)appdata) + st->offset;      void **ptr = ((void*)appdata) + st->offset;
189      snprintf(key, sizeof(key), GCONF_PATH "%s", st->key);      char *key = g_strdup_printf(GCONF_PATH "%s", st->key);
190    
191      switch(st->type) {      switch(st->type) {
192      case STORE_STRING:      case STORE_STRING:
193        if((char*)(*ptr)) {        if((char*)(*ptr)) {
194          gconf_client_set_string(appdata->gconf_client, key, (char*)(*ptr), NULL);          gconf_client_set_string(appdata->gconf_client, key, (char*)(*ptr), NULL);
195        }        }
196          g_free((char*)(*ptr));
197          *ptr = NULL;
198        break;        break;
199    
200      case STORE_BOOL:      case STORE_BOOL:
# Line 177  void gconf_save_state(appdata_t *appdata Line 206  void gconf_save_state(appdata_t *appdata
206        break;        break;
207    
208      case STORE_FLOAT:      case STORE_FLOAT:
209        gconf_client_set_float(appdata->gconf_client, key, *((float*)ptr), NULL);        if(!isnan(*((float*)ptr)))
210            gconf_client_set_float(appdata->gconf_client, key, *((float*)ptr), NULL);
211        break;        break;
212    
213      default:      default:
# Line 185  void gconf_save_state(appdata_t *appdata Line 215  void gconf_save_state(appdata_t *appdata
215        break;        break;
216      }      }
217    
218        g_free(key);
219      st++;      st++;
220    }    }
221  }  }
# Line 196  void gconf_load_state(appdata_t *appdata Line 227  void gconf_load_state(appdata_t *appdata
227    
228    gpx_dialog_t *dialog = NULL;    gpx_dialog_t *dialog = NULL;
229    
230      /* default positions are invalid */
231      appdata->home.lat = appdata->home.lon = NAN;
232      appdata->manual_goto.lat = appdata->manual_goto.lon = NAN;
233      appdata->gps.lat = appdata->gps.lon = NAN;
234    
235      appdata->gt.lat = appdata->gt.lon = NAN;
236    
237      /* ------------- get proxy settings -------------------- */
238      if(gconf_client_get_bool(appdata->gconf_client,
239                               PROXY_KEY "use_http_proxy", NULL)) {
240        proxy_t *proxy = appdata->proxy = g_new0(proxy_t, 1);
241    
242        /* get basic settings */
243        proxy->host = gconf_client_get_string(appdata->gconf_client,
244                                              PROXY_KEY "host", NULL);
245        proxy->port = gconf_client_get_int(appdata->gconf_client,
246                                           PROXY_KEY "port", NULL);
247        proxy->ignore_hosts =
248          gconf_client_get_string(appdata->gconf_client,
249                                  PROXY_KEY "ignore_hosts", NULL);
250    
251        /* check for authentication */
252        proxy->use_authentication =
253          gconf_client_get_bool(appdata->gconf_client,
254                                PROXY_KEY "use_authentication", NULL);
255    
256        if(proxy->use_authentication) {
257          proxy->authentication_user =
258            gconf_client_get_string(appdata->gconf_client,
259                                    PROXY_KEY "authentication_user", NULL);
260          proxy->authentication_password =
261            gconf_client_get_string(appdata->gconf_client,
262                                    PROXY_KEY "authentication_password", NULL);
263        }
264      }
265    
266    int i, entries = gconf_client_get_int(appdata->gconf_client,    int i, entries = gconf_client_get_int(appdata->gconf_client,
267                                   GCONF_KEY_CNT, NULL);                                   GCONF_KEY_CNT, NULL);
268    
# Line 206  void gconf_load_state(appdata_t *appdata Line 273  void gconf_load_state(appdata_t *appdata
273      char str[128];      char str[128];
274      snprintf(str, sizeof(str), GCONF_KEY_GPX, i);      snprintf(str, sizeof(str), GCONF_KEY_GPX, i);
275      char *fname = gconf_client_get_string(appdata->gconf_client, str, NULL);      char *fname = gconf_client_get_string(appdata->gconf_client, str, NULL);
   
276      if(fname) {      if(fname) {
277        /* check if there's a valid name stored for this file. */        /* check if there's a valid name stored for this file. */
278        /* if yes it's a "closed" file */        /* if yes it's a "closed" file */
# Line 222  void gconf_load_state(appdata_t *appdata Line 288  void gconf_load_state(appdata_t *appdata
288          else          else
289            *gpx = gpx_parse(dialog, fname);            *gpx = gpx_parse(dialog, fname);
290    
291          free(fname);          if(!*gpx) {
292              /* restoring the gpx file failed, mark it as unusable, but save */
293              /* its presence for later use */
294    
295              /* create "closed" entry to remember this file for next */
296              /* load attempt */
297              *gpx = g_new0(gpx_t, 1);
298              (*gpx)->filename = fname;
299              char *p = fname;
300              if(strrchr(fname, '/'))
301                p = strrchr(fname, '/')+1;
302    
303              (*gpx)->name = g_strdup_printf(_("Failed to load: %s"), p);
304              (*gpx)->closed = TRUE;
305            } else
306              free(fname);
307        }        }
     }  
   
     /* use next gpx entry of this was loaded successfully */  
     if(*gpx)  
308        gpx = &((*gpx)->next);        gpx = &((*gpx)->next);
309        }
310    }    }
311    
312    gpx_busy_dialog_destroy(dialog);    gpx_busy_dialog_destroy(dialog);
# Line 256  void gconf_load_state(appdata_t *appdata Line 334  void gconf_load_state(appdata_t *appdata
334    /* restore everything listed in the store table */    /* restore everything listed in the store table */
335    store_t *st = store;    store_t *st = store;
336    while(st->key) {    while(st->key) {
     char key[256];  
337      void **ptr = ((void*)appdata) + st->offset;      void **ptr = ((void*)appdata) + st->offset;
338      snprintf(key, sizeof(key), GCONF_PATH "%s", st->key);      char *key = g_strdup_printf(GCONF_PATH "%s", st->key);
339    
340      switch(st->type) {      /* check if key is present */
341      case STORE_STRING: {      GConfValue *value = gconf_client_get(appdata->gconf_client, key, NULL);
342        char **str = (char**)ptr;      if(value) {
343        *str = gconf_client_get_string(appdata->gconf_client, key, NULL);        gconf_value_free(value);
344    
345          switch(st->type) {
346          case STORE_STRING: {
347            char **str = (char**)ptr;
348            *str = gconf_client_get_string(appdata->gconf_client, key, NULL);
349        } break;        } break;
350    
351      case STORE_BOOL:        case STORE_BOOL:
352        *((int*)ptr) = gconf_client_get_bool(appdata->gconf_client, key, NULL);          *((int*)ptr) = gconf_client_get_bool(appdata->gconf_client, key, NULL);
353        break;          break;
354    
355      case STORE_INT:        case STORE_INT:
356        *((int*)ptr) = gconf_client_get_int(appdata->gconf_client, key, NULL);          *((int*)ptr) = gconf_client_get_int(appdata->gconf_client, key, NULL);
357        break;          break;
358    
359      case STORE_FLOAT:        case STORE_FLOAT:
360        *((float*)ptr) = gconf_client_get_float(appdata->gconf_client, key, NULL);          *((float*)ptr) = gconf_client_get_float(appdata->gconf_client, key, NULL);
361        break;          break;
362    
363      default:        default:
364        printf("Unsupported type %d\n", st->type);          printf("Unsupported type %d\n", st->type);
365        break;          break;
366          }
367      }      }
368        g_free(key);
369      st++;      st++;
370    }    }
371    
372    /* ----- set all kinds of defaults ------- */    /* ----- set all kinds of defaults ------- */
373    
 #if 0  
   if(!appdata->home.lon || !appdata->home.lat) {  
     appdata->home.lat = DEFAULT_LAT;  
     appdata->home.lon = DEFAULT_LON;  
   }  
   
   if(!appdata->manual_goto.lon || !appdata->manual_goto.lat) {  
     appdata->manual_goto.lat = DEFAULT_LAT;  
     appdata->manual_goto.lon = DEFAULT_LON;  
   }  
   
   if(!appdata->gps.lon || !appdata->gps.lat) {  
     appdata->gps.lat = DEFAULT_LAT;  
     appdata->gps.lon = DEFAULT_LON;  
   }  
 #endif  
   
374    if(!appdata->compass_damping) appdata->compass_damping = 1;    if(!appdata->compass_damping) appdata->compass_damping = 1;
375    
376    if(!appdata->mmpoi_radius)    if(!appdata->mmpoi_radius)
# Line 314  void gconf_load_state(appdata_t *appdata Line 380  void gconf_load_state(appdata_t *appdata
380      appdata->search = SEARCH_NAME | SEARCH_ID;      appdata->search = SEARCH_NAME | SEARCH_ID;
381    
382    if(!appdata->image_path) {    if(!appdata->image_path) {
383      /* if we get here, there's no config at all yet. So this is a */  #ifdef USE_MAEMO
384      /* good place to set all kinds of useful defaults */      /* update cachelist by default */
385      appdata->load_images = TRUE;      appdata->cachelist_update = TRUE;
386    #endif
387    
388        /* use gps by default */
389        appdata->use_gps = TRUE;
390    
391  #ifndef USE_MAEMO  #ifndef USE_MAEMO
392      char *p = getenv("HOME");      char *p = getenv("HOME");
# Line 333  void gconf_load_state(appdata_t *appdata Line 402  void gconf_load_state(appdata_t *appdata
402  #endif  #endif
403      appdata->image_path = strdup(DEFAULT_IMAGE_PATH);      appdata->image_path = strdup(DEFAULT_IMAGE_PATH);
404    
405        /* check if this path is actually accessible */
406        /* and change it to the current users home if not */
407        /* (this should only happen on scratchbox) */
408        if(!g_file_test(appdata->image_path, G_FILE_TEST_IS_DIR)) {
409          if(g_mkdir_with_parents(appdata->image_path, 0700) != 0) {
410            char *p = getenv("HOME");
411            if(!p) p = "/tmp/";
412    
413            appdata->image_path =
414              g_strdup_printf("%s%s%s", p,
415                              (p[strlen(p)-1]!='/')?"/":"",
416                              DEFAULT_IMAGE_PATH_HOME);
417            printf("using alt path %s\n", appdata->image_path);
418          }
419        }
420    
421    } else {    } else {
422      /* some versions old versions messed up the path */      /* some versions old versions messed up the path */
423      if(appdata->image_path[strlen(appdata->image_path)-1] != '/') {      if(appdata->image_path[strlen(appdata->image_path)-1] != '/') {
# Line 394  void gconf_load_state(appdata_t *appdata Line 479  void gconf_load_state(appdata_t *appdata
479    if(!appdata->cachelist_items)    if(!appdata->cachelist_items)
480      appdata->cachelist_items = CACHELIST_ITEM_DEFAULT;      appdata->cachelist_items = CACHELIST_ITEM_DEFAULT;
481    
482      /* if there are no entries in the main list, try to add the */
483      /* "welcome" one */
484      if(!appdata->gpx) {
485        char *name = g_strdup(ICONPATH "welcome.gpx");
486        dialog = gpx_busy_dialog_new(GTK_WIDGET(appdata->window));
487        printf("No GPX file loaded, trying to load demo\n");
488        appdata->gpx = gpx_parse(dialog, name);
489        gpx_busy_dialog_destroy(dialog);
490        g_free(name);
491      }
492  }  }
493    

Legend:
Removed from v.1  
changed lines
  Added in v.196