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 167 by harbaum, Mon Nov 9 07:50:37 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) },
   { "cachelist_update", STORE_BOOL,   OFFSET(cachelist_update) },  
87  #endif  #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
94    { NULL, -1, -1 }    { NULL, -1, -1 }
95  };  };
96    
# Line 127  char *gconf_restore_closed_name(appdata_ Line 133  char *gconf_restore_closed_name(appdata_
133  void gconf_save_state(appdata_t *appdata) {  void gconf_save_state(appdata_t *appdata) {
134    int entries = 0;    int entries = 0;
135    
136      /* free proxy settings */
137      if(appdata->proxy) {
138        proxy_t *proxy = appdata->proxy;
139    
140        if(proxy->authentication_password) g_free(proxy->authentication_password);
141        if(proxy->authentication_user)     g_free(proxy->authentication_user);
142        if(proxy->host)                    g_free(proxy->host);
143        if(proxy->ignore_hosts)            g_free(proxy->ignore_hosts);
144    
145        g_free(proxy);
146        appdata->proxy = NULL;
147      }
148    
149    gpx_t *gpx = appdata->gpx;    gpx_t *gpx = appdata->gpx;
150    while(gpx) {    while(gpx) {
151      char str[128];      char str[128];
# Line 157  void gconf_save_state(appdata_t *appdata Line 176  void gconf_save_state(appdata_t *appdata
176    /* store everything listed in the store table */    /* store everything listed in the store table */
177    store_t *st = store;    store_t *st = store;
178    while(st->key) {    while(st->key) {
     char key[256];  
179      void **ptr = ((void*)appdata) + st->offset;      void **ptr = ((void*)appdata) + st->offset;
180      snprintf(key, sizeof(key), GCONF_PATH "%s", st->key);      char *key = g_strdup_printf(GCONF_PATH "%s", st->key);
181    
182      switch(st->type) {      switch(st->type) {
183      case STORE_STRING:      case STORE_STRING:
# Line 177  void gconf_save_state(appdata_t *appdata Line 195  void gconf_save_state(appdata_t *appdata
195        break;        break;
196    
197      case STORE_FLOAT:      case STORE_FLOAT:
198        gconf_client_set_float(appdata->gconf_client, key, *((float*)ptr), NULL);        if(!isnan(*((float*)ptr)))
199            gconf_client_set_float(appdata->gconf_client, key, *((float*)ptr), NULL);
200        break;        break;
201    
202      default:      default:
# Line 185  void gconf_save_state(appdata_t *appdata Line 204  void gconf_save_state(appdata_t *appdata
204        break;        break;
205      }      }
206    
207        g_free(key);
208      st++;      st++;
209    }    }
210  }  }
# Line 196  void gconf_load_state(appdata_t *appdata Line 216  void gconf_load_state(appdata_t *appdata
216    
217    gpx_dialog_t *dialog = NULL;    gpx_dialog_t *dialog = NULL;
218    
219      /* default positions are invalid */
220      appdata->home.lat = appdata->home.lon = NAN;
221      appdata->manual_goto.lat = appdata->manual_goto.lon = NAN;
222      appdata->gps.lat = appdata->gps.lon = NAN;
223    
224      /* ------------- get proxy settings -------------------- */
225      if(gconf_client_get_bool(appdata->gconf_client,
226                               PROXY_KEY "use_http_proxy", NULL)) {
227        proxy_t *proxy = appdata->proxy = g_new0(proxy_t, 1);
228    
229        /* get basic settings */
230        proxy->host = gconf_client_get_string(appdata->gconf_client,
231                                              PROXY_KEY "host", NULL);
232        proxy->port = gconf_client_get_int(appdata->gconf_client,
233                                           PROXY_KEY "port", NULL);
234        proxy->ignore_hosts =
235          gconf_client_get_string(appdata->gconf_client,
236                                  PROXY_KEY "ignore_hosts", NULL);
237    
238        /* check for authentication */
239        proxy->use_authentication =
240          gconf_client_get_bool(appdata->gconf_client,
241                                PROXY_KEY "use_authentication", NULL);
242    
243        if(proxy->use_authentication) {
244          proxy->authentication_user =
245            gconf_client_get_string(appdata->gconf_client,
246                                    PROXY_KEY "authentication_user", NULL);
247          proxy->authentication_password =
248            gconf_client_get_string(appdata->gconf_client,
249                                    PROXY_KEY "authentication_password", NULL);
250        }
251      }
252    
253    int i, entries = gconf_client_get_int(appdata->gconf_client,    int i, entries = gconf_client_get_int(appdata->gconf_client,
254                                   GCONF_KEY_CNT, NULL);                                   GCONF_KEY_CNT, NULL);
255    
# Line 206  void gconf_load_state(appdata_t *appdata Line 260  void gconf_load_state(appdata_t *appdata
260      char str[128];      char str[128];
261      snprintf(str, sizeof(str), GCONF_KEY_GPX, i);      snprintf(str, sizeof(str), GCONF_KEY_GPX, i);
262      char *fname = gconf_client_get_string(appdata->gconf_client, str, NULL);      char *fname = gconf_client_get_string(appdata->gconf_client, str, NULL);
   
263      if(fname) {      if(fname) {
264        /* check if there's a valid name stored for this file. */        /* check if there's a valid name stored for this file. */
265        /* if yes it's a "closed" file */        /* if yes it's a "closed" file */
# Line 222  void gconf_load_state(appdata_t *appdata Line 275  void gconf_load_state(appdata_t *appdata
275          else          else
276            *gpx = gpx_parse(dialog, fname);            *gpx = gpx_parse(dialog, fname);
277    
278          free(fname);          if(!*gpx) {
279              /* restoring the gpx file failed, mark it as unusable, but save */
280              /* its presence for later use */
281    
282              /* create "closed" entry to remember this file for next */
283              /* load attempt */
284              *gpx = g_new0(gpx_t, 1);
285              (*gpx)->filename = fname;
286              char *p = fname;
287              if(strrchr(fname, '/'))
288                p = strrchr(fname, '/')+1;
289    
290              (*gpx)->name = g_strdup_printf(_("Failed to load: %s"), p);
291              (*gpx)->closed = TRUE;
292            } else
293              free(fname);
294        }        }
     }  
   
     /* use next gpx entry of this was loaded successfully */  
     if(*gpx)  
295        gpx = &((*gpx)->next);        gpx = &((*gpx)->next);
296        }
297    }    }
298    
299    gpx_busy_dialog_destroy(dialog);    gpx_busy_dialog_destroy(dialog);
# Line 256  void gconf_load_state(appdata_t *appdata Line 321  void gconf_load_state(appdata_t *appdata
321    /* restore everything listed in the store table */    /* restore everything listed in the store table */
322    store_t *st = store;    store_t *st = store;
323    while(st->key) {    while(st->key) {
     char key[256];  
324      void **ptr = ((void*)appdata) + st->offset;      void **ptr = ((void*)appdata) + st->offset;
325      snprintf(key, sizeof(key), GCONF_PATH "%s", st->key);      char *key = g_strdup_printf(GCONF_PATH "%s", st->key);
326    
327      switch(st->type) {      /* check if key is present */
328      case STORE_STRING: {      GConfValue *value = gconf_client_get(appdata->gconf_client, key, NULL);
329        char **str = (char**)ptr;      if(value) {
330        *str = gconf_client_get_string(appdata->gconf_client, key, NULL);        gconf_value_free(value);
331    
332          switch(st->type) {
333          case STORE_STRING: {
334            char **str = (char**)ptr;
335            *str = gconf_client_get_string(appdata->gconf_client, key, NULL);
336        } break;        } break;
337    
338      case STORE_BOOL:        case STORE_BOOL:
339        *((int*)ptr) = gconf_client_get_bool(appdata->gconf_client, key, NULL);          *((int*)ptr) = gconf_client_get_bool(appdata->gconf_client, key, NULL);
340        break;          break;
341    
342      case STORE_INT:        case STORE_INT:
343        *((int*)ptr) = gconf_client_get_int(appdata->gconf_client, key, NULL);          *((int*)ptr) = gconf_client_get_int(appdata->gconf_client, key, NULL);
344        break;          break;
345    
346      case STORE_FLOAT:        case STORE_FLOAT:
347        *((float*)ptr) = gconf_client_get_float(appdata->gconf_client, key, NULL);          *((float*)ptr) = gconf_client_get_float(appdata->gconf_client, key, NULL);
348        break;          break;
349    
350      default:        default:
351        printf("Unsupported type %d\n", st->type);          printf("Unsupported type %d\n", st->type);
352        break;          break;
353          }
354      }      }
355        g_free(key);
356      st++;      st++;
357    }    }
358    
359    /* ----- set all kinds of defaults ------- */    /* ----- set all kinds of defaults ------- */
360    
 #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  
   
361    if(!appdata->compass_damping) appdata->compass_damping = 1;    if(!appdata->compass_damping) appdata->compass_damping = 1;
362    
363    if(!appdata->mmpoi_radius)    if(!appdata->mmpoi_radius)
# Line 314  void gconf_load_state(appdata_t *appdata Line 367  void gconf_load_state(appdata_t *appdata
367      appdata->search = SEARCH_NAME | SEARCH_ID;      appdata->search = SEARCH_NAME | SEARCH_ID;
368    
369    if(!appdata->image_path) {    if(!appdata->image_path) {
370      /* if we get here, there's no config at all yet. So this is a */  #ifdef USE_MAEMO
371      /* good place to set all kinds of useful defaults */      /* update cachelist by default */
372      appdata->load_images = TRUE;      appdata->cachelist_update = TRUE;
373    #endif
374    
375        /* use gps by default */
376        appdata->use_gps = TRUE;
377    
378  #ifndef USE_MAEMO  #ifndef USE_MAEMO
379      char *p = getenv("HOME");      char *p = getenv("HOME");
# Line 333  void gconf_load_state(appdata_t *appdata Line 389  void gconf_load_state(appdata_t *appdata
389  #endif  #endif
390      appdata->image_path = strdup(DEFAULT_IMAGE_PATH);      appdata->image_path = strdup(DEFAULT_IMAGE_PATH);
391    
392        /* check if this path is actually accessible */
393        /* and change it to the current users home if not */
394        /* (this should only happen on scratchbox) */
395        if(!g_file_test(appdata->image_path, G_FILE_TEST_IS_DIR)) {
396          if(g_mkdir_with_parents(appdata->image_path, 0700) != 0) {
397            char *p = getenv("HOME");
398            if(!p) p = "/tmp/";
399    
400            appdata->image_path =
401              g_strdup_printf("%s%s%s", p,
402                              (p[strlen(p)-1]!='/')?"/":"",
403                              DEFAULT_IMAGE_PATH_HOME);
404            printf("using alt path %s\n", appdata->image_path);
405          }
406        }
407    
408    } else {    } else {
409      /* some versions old versions messed up the path */      /* some versions old versions messed up the path */
410      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 466  void gconf_load_state(appdata_t *appdata
466    if(!appdata->cachelist_items)    if(!appdata->cachelist_items)
467      appdata->cachelist_items = CACHELIST_ITEM_DEFAULT;      appdata->cachelist_items = CACHELIST_ITEM_DEFAULT;
468    
469      /* if there are no entries in the main list, try to add the */
470      /* "welcome" one */
471      if(!appdata->gpx) {
472        char *name = g_strdup(ICONPATH "welcome.gpx");
473        dialog = gpx_busy_dialog_new(GTK_WIDGET(appdata->window));
474        printf("No GPX file loaded, trying to load demo\n");
475        appdata->gpx = gpx_parse(dialog, name);
476        gpx_busy_dialog_destroy(dialog);
477        g_free(name);
478      }
479  }  }
480    

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