changeset 5534:0aa4d089125c

[gaim-migrate @ 5934] prefs is finally starting to look the way I wanted it to. now there is no explicit saving of prefs (except on quit). prefs are saved automagically when a pref is changed. i also went in and finished the job of moving the debug window over to new prefs committer: Tailor Script <tailor@pidgin.im>
author Nathan Walp <nwalp@pidgin.im>
date Mon, 26 May 2003 15:44:21 +0000
parents b4c32b9a797d
children 933739f789f9
files src/gaimrc.c src/main.c src/prefs.c src/prefs.h
diffstat 4 files changed, 45 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/gaimrc.c	Mon May 26 14:39:03 2003 +0000
+++ b/src/gaimrc.c	Mon May 26 15:44:21 2003 +0000
@@ -1628,7 +1628,6 @@
 	g_free(filename_temp);
 
 	gaim_debug(GAIM_DEBUG_INFO, "gaimrc", "Exiting save_prefs\n");
-	gaim_prefs_save(); /* put this here for now */
 }
 
 
--- a/src/main.c	Mon May 26 14:39:03 2003 +0000
+++ b/src/main.c	Mon May 26 15:44:21 2003 +0000
@@ -123,6 +123,7 @@
 
 	/* record what we have before we blow it away... */
 	save_prefs();
+	gaim_prefs_sync();
 
 	gaim_debug(GAIM_DEBUG_INFO, "main", "Unloading all plugins\n");
 	gaim_plugins_destroy_all();
--- a/src/prefs.c	Mon May 26 14:39:03 2003 +0000
+++ b/src/prefs.c	Mon May 26 15:44:21 2003 +0000
@@ -63,9 +63,37 @@
 static struct gaim_pref prefs = { GAIM_PREF_NONE, NULL, {NULL}, NULL,
 	NULL, NULL, NULL };
 
+static guint prefs_save_timer = 0;
+static gboolean prefs_is_loaded = FALSE;
+
+
+static gboolean prefs_save_callback(gpointer who_cares) {
+	gaim_prefs_sync();
+	prefs_save_timer = 0;
+	return FALSE;
+}
+
+static void schedule_prefs_save() {
+	if(!prefs_save_timer)
+		prefs_save_timer = g_timeout_add(5000, prefs_save_callback, NULL);
+}
+
+static void prefs_save_cb(const char *name, GaimPrefType type, gpointer val,
+		gpointer user_data) {
+
+	if(!prefs_is_loaded)
+		return;
+
+	gaim_debug(GAIM_DEBUG_MISC, "prefs", "%s changed, scheduling save.\n", name);
+
+	schedule_prefs_save();
+}
+
 void gaim_prefs_init() {
 	prefs_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
 
+	gaim_prefs_connect_callback("/", prefs_save_cb, NULL);
+
 	gaim_prefs_add_none("/core");
 
 	/* XXX: this is where you would want to put prefs declarations */
@@ -444,20 +472,23 @@
 	}
 }
 
-void gaim_prefs_save() {
-	/* FIXME: do this with timers so we don't save so damn often */
-	gaim_prefs_sync();
-}
-
 void gaim_prefs_sync() {
 	FILE *file;
 	const char *user_dir = gaim_user_dir();
 	char *filename;
 	char *filename_real;
 
+	if(!prefs_is_loaded) {
+		gaim_debug(GAIM_DEBUG_WARNING, "prefs", "prefs saved before loading!  scheduling save.\n");
+		schedule_prefs_save(); /* schedule a save for after we read in */
+		return;
+	}
+
 	if(!user_dir)
 		return;
 
+	gaim_debug(GAIM_DEBUG_INFO, "prefs", "writing prefs out to disk.\n");
+
 	file = fopen(user_dir, "r");
 	if(!file)
 		mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR);
@@ -570,8 +601,11 @@
 	GMarkupParseContext *context;
 	GError *error = NULL;
 
-	if(!filename)
+
+	if(!filename) {
+		prefs_is_loaded = TRUE;
 		return;
+	}
 
 	gaim_debug(GAIM_DEBUG_INFO, "prefs", "Reading %s\n", filename);
 
@@ -579,6 +613,7 @@
 		gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error reading prefs: %s\n",
 				error->message);
 		g_error_free(error);
+		prefs_is_loaded = TRUE;
 		return;
 	}
 
@@ -587,6 +622,7 @@
 	if(!g_markup_parse_context_parse(context, contents, length, NULL)) {
 		g_markup_parse_context_free(context);
 		g_free(contents);
+		prefs_is_loaded = TRUE;
 		return;
 	}
 
@@ -594,6 +630,7 @@
 		gaim_debug(GAIM_DEBUG_ERROR, "prefs", "Error parsing %s\n", filename);
 		g_markup_parse_context_free(context);
 		g_free(contents);
+		prefs_is_loaded = TRUE;
 		return;
 	}
 
@@ -602,6 +639,7 @@
 
 	gaim_debug(GAIM_DEBUG_INFO, "prefs", "Finished reading %s\n", filename);
 	g_free(filename);
+	prefs_is_loaded = TRUE;
 }
 
 
--- a/src/prefs.h	Mon May 26 14:39:03 2003 +0000
+++ b/src/prefs.h	Mon May 26 15:44:21 2003 +0000
@@ -168,11 +168,6 @@
 void gaim_prefs_load();
 
 /**
- * Write preferences
- */
-void gaim_prefs_save();
-
-/**
  * Force an immediate write of preferences
  */
 void gaim_prefs_sync();