[monkey-bubble: 3/753] Added bool config items to libgnome. Updated several programs to take advantage of new bool support.



commit 2b3fcabf8409c592a19312f4cd7ba9038ea96a73
Author: Tom Tromey <tromey src gnome org>
Date:   Mon Dec 1 18:45:03 1997 +0000

    Added bool config items to libgnome.
    Updated several programs to take advantage of new bool support.
    Config code can now also tell caller if the default was used.

 libgnome/gnome-config.c |   67 ++++++++++++++++++++++++++++++++++++----------
 libgnome/gnome-config.h |   25 ++++++++++++++++-
 libgnome/gnome-dentry.c |    4 +-
 3 files changed, 77 insertions(+), 19 deletions(-)
---
diff --git a/libgnome/gnome-config.c b/libgnome/gnome-config.c
index 314f0a3..ef024e0 100644
--- a/libgnome/gnome-config.c
+++ b/libgnome/gnome-config.c
@@ -283,13 +283,15 @@ new_key (TSecHeader *section, char *key_name, char *value)
 
 static char *
 access_config (access_type mode, char *section_name, char *key_name, 
-		   char *def, char *filename)
+		   char *def, char *filename, int *def_used)
 {
     
 	TProfile   *New;
 	TSecHeader *section;
 	TKeys      *key;
 
+	if (def_used)
+		*def_used = 0;
 	if (!is_loaded (filename, &section)){
 		New = (TProfile *) g_malloc (sizeof (TProfile));
 		New->link = Base;
@@ -333,6 +335,8 @@ access_config (access_type mode, char *section_name, char *key_name,
 		section->link = Current->section;
 		Current->section = section;
 	} 
+	if (def_used)
+		*def_used = 1;
 	return def;
 }
 
@@ -567,44 +571,63 @@ gnome_config_drop_all (void)
 }
 
 int
-gnome_config_get_int (char *path)
+gnome_config_get_int_with_default (char *path, int *def)
 {
 	ParsedPath *pp;
 	char *r;
 	int  v;
 	
 	pp = parse_path (path);
-	r = access_config (LOOKUP, pp->section, pp->key, pp->def, pp->file);
+	r = access_config (LOOKUP, pp->section, pp->key, pp->def, pp->file,
+			   def);
 
 	g_return_val_if_fail(r != NULL, 0);
 
-	if (!strcasecmp (r, "true")){
-		release_path (pp);
-		return 1;
-	}
-	if (!strcasecmp (r, "false")){
-		release_path (pp);
-		return 0;
-	}
 	v = atoi (r);
 	release_path (pp);
 	return v;
 }
 
 char *
-gnome_config_get_string (char *path)
+gnome_config_get_string_with_default (char *path, int *def)
 {
 	ParsedPath *pp;
 	char *r;
 	
 	pp = parse_path (path);
-	r = access_config (LOOKUP, pp->section, pp->key, pp->def, pp->file);
+	r = access_config (LOOKUP, pp->section, pp->key, pp->def, pp->file,
+			   def);
 	if (r)
 		r = strdup (r);
 	release_path (pp);
 	return r;
 }
 
+int
+gnome_config_get_bool_with_default (char *path, int *def)
+{
+	ParsedPath *pp;
+	char *r;
+	int  v;
+	
+	pp = parse_path (path);
+	r = access_config (LOOKUP, pp->section, pp->key, pp->def, pp->file,
+			   def);
+
+	g_return_val_if_fail(r != NULL, 0);
+
+	if (!strcasecmp (r, "true")){
+		v = 1;
+	} else if (!strcasecmp (r, "false")){
+		v = 0;
+	} else {
+	        /* FIXME: what to return?  */
+	        v = 0;
+	}
+	release_path (pp);
+	return v;
+}
+
 void
 gnome_config_set_string (char *path, char *new_value)
 {
@@ -612,7 +635,8 @@ gnome_config_set_string (char *path, char *new_value)
 	char *r;
 	
 	pp = parse_path (path);
-	r = access_config (SET, pp->section, pp->key, new_value, pp->file);
+	r = access_config (SET, pp->section, pp->key, new_value, pp->file,
+			   NULL);
 	release_path (pp);
 }
 
@@ -625,7 +649,20 @@ gnome_config_set_int (char *path, int new_value)
 	
 	pp = parse_path (path);
 	sprintf (intbuf, "%d", new_value);
-	r = access_config (SET, pp->section, pp->key, intbuf, pp->file);
+	r = access_config (SET, pp->section, pp->key, intbuf, pp->file,
+			   NULL);
+	release_path (pp);
+}
+
+void
+gnome_config_set_bool (char *path, int new_value)
+{
+	ParsedPath *pp;
+	char *r;
+	
+	pp = parse_path (path);
+	r = access_config (SET, pp->section, pp->key,
+			   new_value ? "true" : "false", pp->file, NULL);
 	release_path (pp);
 }
 
diff --git a/libgnome/gnome-config.h b/libgnome/gnome-config.h
index 5bf8843..f0b21ab 100644
--- a/libgnome/gnome-config.h
+++ b/libgnome/gnome-config.h
@@ -5,10 +5,31 @@ BEGIN_GNOME_DECLS
 
 /* Prototypes for the profile management functions */
 
-char *gnome_config_get_string    (char *path);
-int  gnome_config_get_int        (char *path);
+/* These functions look for the config option named in PATH.  If the
+   option does not exist, and a default is specified in PATH, then the
+   default will be returned.  In all cases, *DEF is set to 1 if the
+   default was return, 0 otherwise.  If DEF is NULL then it will not
+   be set.  */
+
+char *gnome_config_get_string_with_default    (char *path, int *def);
+int  gnome_config_get_int_with_default        (char *path, int *def);
+int  gnome_config_get_bool_with_default       (char *path, int *def);
+
+/* Convenience wrappers for the case when you don't care if you see
+   the default.  */
+
+#define gnome_config_get_string(Path) \
+	(gnome_config_get_string_with_default ((Path), NULL))
+#define gnome_config_get_int(Path) \
+	(gnome_config_get_int_with_default ((Path), NULL))
+#define gnome_config_get_bool(Path) \
+	(gnome_config_get_bool_with_default ((Path), NULL))
+
+
+/* Set a config variable.  */
 void gnome_config_set_string     (char *path, char *value);
 void gnome_config_set_int        (char *path, int value);
+void gnome_config_set_bool       (char *path, int value);
 
 /* Returns true if /path/section is defined */
 int  gnome_config_has_section    (char *path);
diff --git a/libgnome/gnome-dentry.c b/libgnome/gnome-dentry.c
index 6d09d7f..d2bf395 100644
--- a/libgnome/gnome-dentry.c
+++ b/libgnome/gnome-dentry.c
@@ -92,7 +92,7 @@ gnome_desktop_entry_load (char *file)
 	newitem->icon_base = gnome_config_get_string ("Icon");
 	newitem->docpath   = gnome_config_get_string ("DocPath");
 	newitem->info      = gnome_config_get_string ("Info");
-	newitem->terminal  = gnome_config_get_int    ("Terminal");
+	newitem->terminal  = gnome_config_get_bool   ("Terminal");
 	newitem->type      = gnome_config_get_string ("Type");
 	newitem->location  = strdup (file);
 	
@@ -157,7 +157,7 @@ gnome_desktop_entry_save (GnomeDesktopEntry *dentry)
 	if (dentry->info)
 		gnome_config_set_string("Info", dentry->info);
 
-	gnome_config_set_int("Terminal", dentry->terminal);
+	gnome_config_set_bool("Terminal", dentry->terminal);
 
 	if (dentry->type)
 		gnome_config_set_string("Type", dentry->type);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]