Diff of /trunk/src/gconf.c

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

revision 14 by harbaum, Sat Jun 27 19:18:40 2009 UTC revision 198 by harbaum, Thu Nov 19 12:38:03 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 77  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      { "geotoad/username", STORE_STRING, OFFSET(gt.username) },
96      { "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    
103    { NULL, -1, -1 }    { NULL, -1, -1 }
104  };  };
105    
# Line 126  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 156  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 176  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 184  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 195  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      appdata->gt.distance = 1.0;  // in km/mil
237    
238      /* ------------- get proxy settings -------------------- */
239      if(gconf_client_get_bool(appdata->gconf_client,
240                               PROXY_KEY "use_http_proxy", NULL)) {
241        proxy_t *proxy = appdata->proxy = g_new0(proxy_t, 1);
242    
243        /* get basic settings */
244        proxy->host = gconf_client_get_string(appdata->gconf_client,
245                                              PROXY_KEY "host", NULL);
246        proxy->port = gconf_client_get_int(appdata->gconf_client,
247                                           PROXY_KEY "port", NULL);
248        proxy->ignore_hosts =
249          gconf_client_get_string(appdata->gconf_client,
250                                  PROXY_KEY "ignore_hosts", NULL);
251    
252        /* check for authentication */
253        proxy->use_authentication =
254          gconf_client_get_bool(appdata->gconf_client,
255                                PROXY_KEY "use_authentication", NULL);
256    
257        if(proxy->use_authentication) {
258          proxy->authentication_user =
259            gconf_client_get_string(appdata->gconf_client,
260                                    PROXY_KEY "authentication_user", NULL);
261          proxy->authentication_password =
262            gconf_client_get_string(appdata->gconf_client,
263                                    PROXY_KEY "authentication_password", NULL);
264        }
265      }
266    
267    int i, entries = gconf_client_get_int(appdata->gconf_client,    int i, entries = gconf_client_get_int(appdata->gconf_client,
268                                   GCONF_KEY_CNT, NULL);                                   GCONF_KEY_CNT, NULL);
269    
# Line 205  void gconf_load_state(appdata_t *appdata Line 274  void gconf_load_state(appdata_t *appdata
274      char str[128];      char str[128];
275      snprintf(str, sizeof(str), GCONF_KEY_GPX, i);      snprintf(str, sizeof(str), GCONF_KEY_GPX, i);
276      char *fname = gconf_client_get_string(appdata->gconf_client, str, NULL);      char *fname = gconf_client_get_string(appdata->gconf_client, str, NULL);
   
277      if(fname) {      if(fname) {
278        /* check if there's a valid name stored for this file. */        /* check if there's a valid name stored for this file. */
279        /* if yes it's a "closed" file */        /* if yes it's a "closed" file */
# Line 221  void gconf_load_state(appdata_t *appdata Line 289  void gconf_load_state(appdata_t *appdata
289          else          else
290            *gpx = gpx_parse(dialog, fname);            *gpx = gpx_parse(dialog, fname);
291    
292          free(fname);          if(!*gpx) {
293              /* restoring the gpx file failed, mark it as unusable, but save */
294              /* its presence for later use */
295    
296              /* create "closed" entry to remember this file for next */
297              /* load attempt */
298              *gpx = g_new0(gpx_t, 1);
299              (*gpx)->filename = fname;
300              char *p = fname;
301              if(strrchr(fname, '/'))
302                p = strrchr(fname, '/')+1;
303    
304              (*gpx)->name = g_strdup_printf(_("Failed to load: %s"), p);
305              (*gpx)->closed = TRUE;
306            } else
307              free(fname);
308        }        }
     }  
   
     /* use next gpx entry of this was loaded successfully */  
     if(*gpx)  
309        gpx = &((*gpx)->next);        gpx = &((*gpx)->next);
310        }
311    }    }
312    
313    gpx_busy_dialog_destroy(dialog);    gpx_busy_dialog_destroy(dialog);
# Line 255  void gconf_load_state(appdata_t *appdata Line 335  void gconf_load_state(appdata_t *appdata
335    /* restore everything listed in the store table */    /* restore everything listed in the store table */
336    store_t *st = store;    store_t *st = store;
337    while(st->key) {    while(st->key) {
     char key[256];  
338      void **ptr = ((void*)appdata) + st->offset;      void **ptr = ((void*)appdata) + st->offset;
339      snprintf(key, sizeof(key), GCONF_PATH "%s", st->key);      char *key = g_strdup_printf(GCONF_PATH "%s", st->key);
340    
341      switch(st->type) {      /* check if key is present */
342      case STORE_STRING: {      GConfValue *value = gconf_client_get(appdata->gconf_client, key, NULL);
343        char **str = (char**)ptr;      if(value) {
344        *str = gconf_client_get_string(appdata->gconf_client, key, NULL);        gconf_value_free(value);
345    
346          switch(st->type) {
347          case STORE_STRING: {
348            char **str = (char**)ptr;
349            *str = gconf_client_get_string(appdata->gconf_client, key, NULL);
350        } break;        } break;
351    
352      case STORE_BOOL:        case STORE_BOOL:
353        *((int*)ptr) = gconf_client_get_bool(appdata->gconf_client, key, NULL);          *((int*)ptr) = gconf_client_get_bool(appdata->gconf_client, key, NULL);
354        break;          break;
355    
356      case STORE_INT:        case STORE_INT:
357        *((int*)ptr) = gconf_client_get_int(appdata->gconf_client, key, NULL);          *((int*)ptr) = gconf_client_get_int(appdata->gconf_client, key, NULL);
358        break;          break;
359    
360      case STORE_FLOAT:        case STORE_FLOAT:
361        *((float*)ptr) = gconf_client_get_float(appdata->gconf_client, key, NULL);          *((float*)ptr) = gconf_client_get_float(appdata->gconf_client, key, NULL);
362        break;          break;
363    
364      default:        default:
365        printf("Unsupported type %d\n", st->type);          printf("Unsupported type %d\n", st->type);
366        break;          break;
367          }
368      }      }
369        g_free(key);
370      st++;      st++;
371    }    }
372    
373    /* ----- set all kinds of defaults ------- */    /* ----- set all kinds of defaults ------- */
374    
 #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  
   
375    if(!appdata->compass_damping) appdata->compass_damping = 1;    if(!appdata->compass_damping) appdata->compass_damping = 1;
376    
377    if(!appdata->mmpoi_radius)    if(!appdata->mmpoi_radius)
# Line 313  void gconf_load_state(appdata_t *appdata Line 381  void gconf_load_state(appdata_t *appdata
381      appdata->search = SEARCH_NAME | SEARCH_ID;      appdata->search = SEARCH_NAME | SEARCH_ID;
382    
383    if(!appdata->image_path) {    if(!appdata->image_path) {
384    #ifdef USE_MAEMO
385        /* update cachelist by default */
386        appdata->cachelist_update = TRUE;
387    #endif
388    
389      /* use gps by default */      /* use gps by default */
390      appdata->use_gps = TRUE;      appdata->use_gps = TRUE;
# Line 331  void gconf_load_state(appdata_t *appdata Line 403  void gconf_load_state(appdata_t *appdata
403  #endif  #endif
404      appdata->image_path = strdup(DEFAULT_IMAGE_PATH);      appdata->image_path = strdup(DEFAULT_IMAGE_PATH);
405    
406        /* check if this path is actually accessible */
407        /* and change it to the current users home if not */
408        /* (this should only happen on scratchbox) */
409        if(!g_file_test(appdata->image_path, G_FILE_TEST_IS_DIR)) {
410          if(g_mkdir_with_parents(appdata->image_path, 0700) != 0) {
411            char *p = getenv("HOME");
412            if(!p) p = "/tmp/";
413    
414            appdata->image_path =
415              g_strdup_printf("%s%s%s", p,
416                              (p[strlen(p)-1]!='/')?"/":"",
417                              DEFAULT_IMAGE_PATH_HOME);
418            printf("using alt path %s\n", appdata->image_path);
419          }
420        }
421    
422    } else {    } else {
423      /* some versions old versions messed up the path */      /* some versions old versions messed up the path */
424      if(appdata->image_path[strlen(appdata->image_path)-1] != '/') {      if(appdata->image_path[strlen(appdata->image_path)-1] != '/') {
# Line 395  void gconf_load_state(appdata_t *appdata Line 483  void gconf_load_state(appdata_t *appdata
483    /* if there are no entries in the main list, try to add the */    /* if there are no entries in the main list, try to add the */
484    /* "welcome" one */    /* "welcome" one */
485    if(!appdata->gpx) {    if(!appdata->gpx) {
486      char *name = g_strdup("/usr/share/gpxview/welcome.gpx");      char *name = g_strdup(ICONPATH "welcome.gpx");
487      dialog = gpx_busy_dialog_new(GTK_WIDGET(appdata->window));      dialog = gpx_busy_dialog_new(GTK_WIDGET(appdata->window));
488      printf("No GPX file loaded, trying to load demo\n");      printf("No GPX file loaded, trying to load demo\n");
489      appdata->gpx = gpx_parse(dialog, name);      appdata->gpx = gpx_parse(dialog, name);

Legend:
Removed from v.14  
changed lines
  Added in v.198