From: Kaj-Michael Lang Date: Mon, 4 Feb 2008 12:26:25 +0000 (+0200) Subject: Use helper to get int values from gconf with default and range check. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b8ca6eba7193220663a1fdc0adf2e80830f9f704;p=mapper Use helper to get int values from gconf with default and range check. Misc other cleanups. --- diff --git a/src/config-gconf.c b/src/config-gconf.c index e90ae2f..4df36f8 100644 --- a/src/config-gconf.c +++ b/src/config-gconf.c @@ -55,46 +55,39 @@ #include "gpx.h" #include "filter.h" +#define PROXY_PORT (8080) + +gint mapper_gconf_get_bound_int(const gchar *path, gint def, gint min, gint max); +gint mapper_gconf_get_int(const gchar *path, gint def); +gfloat mapper_gconf_get_float(const gchar *path, gfloat def); + void config_update_proxy(void) { if (_http_proxy_host) g_free(_http_proxy_host); +_http_proxy_host=NULL; +_http_proxy_port=0; + /* Get proxy data and register for updates. */ if (gconf_client_get_bool(gconf_client, GCONF_KEY_HTTP_PROXY_ON, NULL)) { - _http_proxy_host = gconf_client_get_string(gconf_client, GCONF_KEY_HTTP_PROXY_HOST, NULL); - _http_proxy_port = gconf_client_get_int(gconf_client, GCONF_KEY_HTTP_PROXY_PORT, NULL); -} else { - _http_proxy_host = NULL; - _http_proxy_port = 0; + _http_proxy_host=gconf_client_get_string(gconf_client, GCONF_KEY_HTTP_PROXY_HOST, NULL); + _http_proxy_port=mapper_gconf_get_int(GCONF_KEY_HTTP_PROXY_PORT, PROXY_PORT); } } static void -config_save_track(gchar *config_dir) +track_save(const gchar *config_dir, const gchar *file) { GnomeVFSHandle *handle; -gchar *track_file; -track_file = gnome_vfs_uri_make_full_from_relative(config_dir, CONFIG_FILE_TRACK); -if (GNOME_VFS_OK == gnome_vfs_create(&handle, track_file, GNOME_VFS_OPEN_WRITE, FALSE, 0600)) { +gchar *tfile; +tfile=gnome_vfs_uri_make_full_from_relative(config_dir, file); +if (GNOME_VFS_OK == gnome_vfs_create(&handle, tfile, GNOME_VFS_OPEN_WRITE, FALSE, 0600)) { write_gpx(&_track, handle); gnome_vfs_close(handle); } -g_free(track_file); -} - -static void -config_save_route(gchar *config_dir) -{ -GnomeVFSHandle *handle; -gchar *route_file; -route_file = gnome_vfs_uri_make_full_from_relative(config_dir, CONFIG_FILE_ROUTE); -if (GNOME_VFS_OK == gnome_vfs_create(&handle, route_file, GNOME_VFS_OPEN_WRITE, FALSE, 0600)) { - write_gpx(&_route, handle); - gnome_vfs_close(handle); -} -g_free(route_file); +g_free(tfile); } gboolean @@ -104,6 +97,7 @@ GList *curr = _repo_list; GSList *temp_list = NULL; gint curr_repo_index = 0; +g_assert(gconf_client); /* Save the repositories. */ for (curr = _repo_list; curr != NULL; curr = curr->next) { /* Build from each part of a repo, delimited by newline characters: @@ -137,6 +131,8 @@ return TRUE; static void config_default_repo(void) { +g_assert(gconf_client); + /* We have no repositories - create a default one. */ RepoData *repo = g_new(RepoData, 1); @@ -171,8 +167,10 @@ config_load_repo(void) { GSList *list, *curr; GConfValue *value; +guint curr_repo_index; -guint curr_repo_index = gconf_client_get_int(gconf_client, GCONF_KEY_CURRREPO, NULL); +g_assert(gconf_client); +curr_repo_index = gconf_client_get_int(gconf_client, GCONF_KEY_CURRREPO, NULL); list = gconf_client_get_list(gconf_client, GCONF_KEY_REPOSITORIES, GCONF_VALUE_STRING, NULL); for (curr = list; curr != NULL; curr = curr->next) { @@ -193,8 +191,7 @@ return TRUE; gboolean config_save_home(void) { -if (!gconf_client) - return FALSE; +g_assert(gconf_client); /* Save home lat/lon. */ gconf_client_set_float(gconf_client, GCONF_KEY_HOME_LAT, _home.lat, NULL); @@ -207,6 +204,8 @@ return TRUE; gboolean config_save_filter(GpsTrackFilter *f) { +g_assert(gconf_client); + /* Filtering */ gconf_client_set_bool(gconf_client, GCONF_KEY_GPS_FILTER, f->enabled, NULL); gconf_client_set_float(gconf_client, GCONF_KEY_GPS_FILTER_HDOP, f->hdop, NULL); @@ -230,6 +229,8 @@ gchar buffer[16]; gfloat center_lat, center_lon; gint i; +g_assert(gconf_client); + /* Initialize config_dir. */ config_dir = gnome_vfs_expand_initial_tilde(CONFIG_DIR_NAME); g_mkdir_with_parents(config_dir, 0700); @@ -269,15 +270,10 @@ gconf_client_set_string(gconf_client, GCONF_KEY_UNITS, UNITS_TEXT[_units], NULL) /* Save Custom Key Actions. */ for (i = 0; i < CUSTOM_KEY_ENUM_COUNT; i++) - gconf_client_set_string(gconf_client, - CUSTOM_KEY_GCONF[i], - CUSTOM_ACTION_TEXT[_action[i]], - NULL); + gconf_client_set_string(gconf_client, CUSTOM_KEY_GCONF[i], CUSTOM_ACTION_TEXT[_action[i]], NULL); /* Save Deg Format. */ -gconf_client_set_string(gconf_client, - GCONF_KEY_DEG_FORMAT, - DEG_FORMAT_TEXT[_degformat], NULL); +gconf_client_set_string(gconf_client, GCONF_KEY_DEG_FORMAT, DEG_FORMAT_TEXT[_degformat], NULL); /* Save Speed Limit On flag. */ gconf_client_set_bool(gconf_client, GCONF_KEY_SPEED_LIMIT_ON, _speed_on, NULL); @@ -286,9 +282,7 @@ gconf_client_set_bool(gconf_client, GCONF_KEY_SPEED_LIMIT_ON, _speed_on, NULL); gconf_client_set_int(gconf_client, GCONF_KEY_SPEED_LIMIT, _speed_limit, NULL); /* Save Info Font Size. */ -gconf_client_set_string(gconf_client, - GCONF_KEY_INFO_FONT_SIZE, - INFO_FONT_TEXT[_info_font_size], NULL); +gconf_client_set_string(gconf_client, GCONF_KEY_INFO_FONT_SIZE, INFO_FONT_TEXT[_info_font_size], NULL); /* Save last latitude/longiture. */ gconf_client_set_float(gconf_client, GCONF_KEY_LAT, _gps->data.lat, NULL); @@ -359,13 +353,10 @@ else /* Save Show POI below zoom. */ gconf_client_set_int(gconf_client, GCONF_KEY_POI_ZOOM, _poi_zoom, NULL); -#if 0 -config_save_track(config_dir); -config_save_route(config_dir); -#endif - gconf_client_suggest_sync(gconf_client, NULL); +#if 0 gconf_client_clear_cache(gconf_client); +#endif g_free(config_dir); return TRUE; } @@ -414,6 +405,7 @@ if (!gconf_client) { exit(1); } +/* Preload configuration */ gconf_client_add_dir (gconf_client, GCONF_KEY_PREFIX, GCONF_CLIENT_PRELOAD_ONELEVEL, NULL); CenterMode _center_mode = CENTER_LEAD; @@ -426,53 +418,22 @@ config_dir = gnome_vfs_expand_initial_tilde(CONFIG_DIR_NAME); g_mkdir_with_parents(config_dir, 0700); /* Get Receiver MAC from GConf. Default is scanned via hci_inquiry. */ -_gps->io.address = gconf_client_get_string(gconf_client, GCONF_KEY_GPS_MAC, NULL); -_gps->io.port = gconf_client_get_int(gconf_client, GCONF_KEY_GPS_PORT, NULL); - -value=gconf_client_get(gconf_client, GCONF_KEY_GPS_TYPE, NULL); -if (value) { - GpsIOSourceType type; - - type=gconf_value_get_int(value); - if (type>GPS_IO_TYPE_MIN && typeio.type=type; - else - _gps->io.type=GPS_IO_SIMULATION; - gconf_value_free(value); -} else { - _gps->io.type=GPS_IO_SIMULATION; -} -g_debug("GPS Type: %d", _gps->io.type); +_gps->io.address=gconf_client_get_string(gconf_client, GCONF_KEY_GPS_MAC, NULL); +_gps->io.port=mapper_gconf_get_int(GCONF_KEY_GPS_PORT, 2497); +_gps->io.type=(GpsIOSourceType)mapper_gconf_get_bound_int(GCONF_KEY_GPS_TYPE, GPS_IO_SIMULATION, GPS_IO_TYPE_MIN+1, GPS_IO_TYPE_MAX-1); /* Get Auto-Download. Default is FALSE. */ _auto_download = gconf_client_get_bool(gconf_client, GCONF_KEY_AUTO_DOWNLOAD, NULL); -/* Get Center Ratio - Default is 3. */ -_center_ratio = gconf_client_get_int(gconf_client, GCONF_KEY_CENTER_SENSITIVITY, NULL); -if (!_center_ratio) - _center_ratio = 7; +_center_ratio=mapper_gconf_get_int(GCONF_KEY_CENTER_SENSITIVITY, 7); +_lead_ratio=mapper_gconf_get_int(GCONF_KEY_LEAD_AMOUNT, 5); +_draw_width=mapper_gconf_get_int(GCONF_KEY_DRAW_WIDTH, 4); /* Invalidate destination */ _dest.valid=FALSE; -/* Get Lead Ratio - Default is 5. */ -_lead_ratio = gconf_client_get_int(gconf_client, GCONF_KEY_LEAD_AMOUNT, NULL); -if (!_lead_ratio) - _lead_ratio = 5; - -/* Get Draw Line Width- Default is 4. */ -_draw_width = gconf_client_get_int(gconf_client, GCONF_KEY_DRAW_WIDTH, NULL); -if (!_draw_width) - _draw_width = 4; - /* Get Announce Advance Notice - Default is 30. */ -value = gconf_client_get(gconf_client, GCONF_KEY_ANNOUNCE_NOTICE, NULL); -if (value) { - _announce_notice_ratio = gconf_value_get_int(value); - gconf_value_free(value); -} else { - _announce_notice_ratio = 8; -} +_announce_notice_ratio=mapper_gconf_get_int(GCONF_KEY_ANNOUNCE_NOTICE, 8); /* Get Enable Voice flag. Default is TRUE. */ value = gconf_client_get(gconf_client, GCONF_KEY_ENABLE_VOICE, NULL); @@ -483,25 +444,8 @@ if (value) { _enable_voice = TRUE; } - /* Get Voice Speed - Default is 120 */ -value = gconf_client_get(gconf_client, GCONF_KEY_VOICE_SPEED, NULL); -if (value) { - _voice_speed = gconf_value_get_int(value); - gconf_value_free(value); -} else { - _voice_speed = 120; -} -BOUND(_voice_speed, 80, 370); - -/* Get Voice Speed - Default is 50. */ -value = gconf_client_get(gconf_client, GCONF_KEY_VOICE_PITCH, NULL); -if (value) { - _voice_pitch = gconf_value_get_int(value); - gconf_value_free(value); -} else { - _voice_pitch = 50; -} -BOUND(_voice_pitch, 10, 99); +_voice_pitch=mapper_gconf_get_bound_int(GCONF_KEY_VOICE_PITCH, 50, 10, 99); +_voice_speed=mapper_gconf_get_bound_int(GCONF_KEY_VOICE_SPEED, 120, 80, 370); /* Get Always Keep On flag. Default is FALSE. */ _always_keep_on = gconf_client_get_bool(gconf_client,GCONF_KEY_ALWAYS_KEEP_ON, NULL); @@ -654,14 +598,7 @@ _route_dir_uri = gconf_client_get_string(gconf_client, GCONF_KEY_ROUTEDIR, NULL) /* Get Last Track File. Default is NULL. */ _track_file_uri = gconf_client_get_string(gconf_client, GCONF_KEY_TRACKFILE, NULL); -/* Get Auto-Center Mode. Default is CENTER_LEAD. */ -value = gconf_client_get(gconf_client, GCONF_KEY_AUTOCENTER_MODE, NULL); -if (value) { - _center_mode = gconf_value_get_int(value); - gconf_value_free(value); -} else { - _center_mode = CENTER_LEAD; -} +_center_mode=mapper_gconf_get_int(GCONF_KEY_AUTOCENTER_MODE, CENTER_LEAD); /* Get Show Scale flag. Default is TRUE. */ value = gconf_client_get(gconf_client, GCONF_KEY_SHOWSCALE, NULL); @@ -739,9 +676,7 @@ if (_mapper_db == NULL) { g_free(db_base); } -_poi_zoom = gconf_client_get_int(gconf_client, GCONF_KEY_POI_ZOOM, NULL); -if (!_poi_zoom) - _poi_zoom = 6; +_poi_zoom = mapper_gconf_get_int(GCONF_KEY_POI_ZOOM, 6); /* Get GPS Info flag. Default is FALSE. */ value = gconf_client_get(gconf_client, GCONF_KEY_GPS_INFO, NULL); @@ -759,13 +694,7 @@ if (_route_dl_url == NULL) _route_dl_url = g_strdup("http://www.gnuite.com/cgi-bin/gpx.cgi?saddr=%s&daddr=%s"); /* Get Route Download Radius. Default is 4. */ -value = gconf_client_get(gconf_client, GCONF_KEY_ROUTE_DL_RADIUS, NULL); -if (value) { - _route_dl_radius = gconf_value_get_int(value); - gconf_value_free(value); -} else { - _route_dl_radius = 4; -} +_route_dl_radius = mapper_gconf_get_int(GCONF_KEY_ROUTE_DL_RADIUS, 4); /* Get Colors. */ for (i = 0; i < COLORABLE_ENUM_COUNT; i++) { @@ -780,3 +709,53 @@ config_update_proxy(); gconf_client_clear_cache(gconf_client); g_free(config_dir); } + +/** + * + */ +gint +mapper_gconf_get_int(const gchar *path, gint def) +{ +GConfValue *value; +gint v; + +g_assert(gconf_client); +value=gconf_client_get(gconf_client, path, NULL); +if (value) { + v=gconf_value_get_int(value); + gconf_value_free(value); +} else { + v=def; +} +return v; +} + +/** + * + */ +gint +mapper_gconf_get_bound_int(const gchar *path, gint def, gint min, gint max) +{ +gint r; + +r=mapper_gconf_get_int(path, def); +BOUND(r, min, max); +return r; +} + +gfloat +mapper_gconf_get_float(const gchar *path, gfloat def) +{ +GConfValue *value; +gfloat v; + +g_assert(gconf_client); +value=gconf_client_get(gconf_client, path, NULL); +if (value) { + v=gconf_value_get_float(value); + gconf_value_free(value); +} else { + v=def; +} +return v; +}