Patch for user configuration saving

Michal Nazarewicz <[email protected]>
Newsgroups gmane.comp.gnome.apps.gkrellm
Message-ID <[email protected]>
Hello,

It recently occurred to me that the way GKrellM treats user
configuration file isn't really the proper way.  First of all, if you
temporary disable a plugin or run some different version of GkrellM
where one of the plugins does not work (seems like 64-bit GKrellM and
32-bit plugins don't like each other) you loose your configuration for
given plugin.  That's, of course, because GKrellM calls all active
plugins to save their configuration so if a plugin is disabled it has no
chance to save any data.

The other thing is that if GKrellM crashes somewhere at the beginning of
user configuration saving process (ie. bug in a plugin) most of the
configuration is lost (in fact, I somehow managed to loose my *whole*
config file).  That's, of course, because GKrellM simply overwrites
configuration file (ie. the file is first cleared and then filled up
with data).

Thank God GKrellM is an Free Software/Open Source project and I don't
need to report any bugs and wait like 42 months until someone tells me
that I'm not considered to be an important customer and the bug won't be
investigated.  Instead, I've written bug fix myself which I attach to
the post hoping that someone will find it useful and maybe even someone
will think that it may be nice to merge it to the software.


-- 
Best regards,                                         _     _
 .o. | Liege of Serenly Enlightened Majesty of      o' \,=./ `o
 ..o | Computer Science,  Michal "mina86" Nazarewicz   (o o)
 ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--

_______________________________________________
Gkrellm mailing list
[email protected]
http://lists.jutley.org/cgi-bin/mailman/listinfo/gkrellm
gkrellm-2.2.10.patch (text/x-patch, 4.1 KB)
diff -Naur gkrellm-2.2.10.old/src/config.c gkrellm-2.2.10/src/config.c
--- gkrellm-2.2.10.old/src/config.c	2007-06-10 16:00:02.000000000 +0000
+++ gkrellm-2.2.10/src/config.c	2007-06-10 16:26:38.000000000 +0000
@@ -14,7 +14,7 @@
 |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 |  GNU General Public License for more details.  Version 2 is in the
 |  COPYRIGHT file in the top level directory of this distribution.
-| 
+|
 |  To get a copy of the GNU General Puplic License, write to the Free Software
 |  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
@@ -1283,7 +1283,7 @@
 		{
 		mon = (GkrellmMonitor *) list->data;
 		destroy_piximage(&mon->privat->top_spacer.piximage);
-		destroy_piximage(&mon->privat->bottom_spacer.piximage);	
+		destroy_piximage(&mon->privat->bottom_spacer.piximage);
 		}
 	}
 
@@ -2272,7 +2272,7 @@
 gkrellm_load_user_config(gboolean monitor_values)
 	{
 	FILE	*f;
-	gchar	*s, *ss, *config, *config_2_1_14 = NULL;
+	gchar	*s, *ss, *config; /* , *config_2_1_14 = NULL; */
 	gchar	buf[CFG_BUFSIZE];
 
 	if (!monitor_values)
@@ -2289,13 +2289,19 @@
 	config = gkrellm_make_config_file_name(gkrellm_homedir(),
 				GKRELLM_USER_CONFIG);
 	f = fopen(config, "r");
+	g_free(config);
 
+#if 0
+	/* GKRELLM_2_1_14_CONFIG is defined to the same thing
+	GKRELLM_CONFIG is anyway. */
 	if (!f)		/* >= 2.1.15 configs are not backward compatible so there is */
 		{		/* a new config file name.  < 2.1.15 is forward compatible   */
 		config_2_1_14 = gkrellm_make_config_file_name(gkrellm_homedir(),
 					GKRELLM_2_1_14_CONFIG);
 		f = fopen(config_2_1_14, "r");
 		}
+#endif
+
 	if (f)
 		{
 		while (fgets(buf, sizeof(buf), f))
@@ -2322,10 +2328,12 @@
 		_GK.chart_width = CHART_WIDTH_MIN;
 	if (_GK.chart_width > CHART_WIDTH_MAX)
 		_GK.chart_width = CHART_WIDTH_MAX;
-//	if (config_2_1_14)
-//		rename(config_2_1_14, config);
+	/*
+	if (config_2_1_14)
+		rename(config_2_1_14, config);
 	g_free(config);
 	g_free(config_2_1_14);
+	*/
 	}
 
 void
@@ -2405,24 +2413,25 @@
 void
 gkrellm_save_user_config(void)
 	{
-	FILE	*f;
+	FILE	*f, *ff;
 	GList	*list;
 	GkrellmMonitor	*mon;
 	gint	i;
 	mode_t	mode;
-	gchar	*config;
+	gchar	*config, *config_new;
 
 	if (_GK.demo)
 		return;
-	config = gkrellm_make_config_file_name(gkrellm_homedir(),
-								GKRELLM_USER_CONFIG);
-	f = fopen(config, "w");
-	g_free(config);
+	config_new = gkrellm_make_config_file_name(gkrellm_homedir(),
+	                                           GKRELLM_USER_CONFIG ".new");
+	f = fopen(config_new, "w");
 	if (f == NULL)
 		{
-		printf(_("Cannot open config file %s for writing.\n"), config);
+		printf(_("Cannot open config file %s for writing.\n"), config_new);
+		g_free(config_new);
 		return;
 		}
+
 	fprintf(f,
 		"### GKrellM user config.  Auto written, do not edit (usually) ###\n");
 	fprintf(f, "### Version %d.%d.%d ###\n",
@@ -2447,6 +2456,43 @@
 			gkrellm_record_state(INTERNAL, NULL);
 			}
 		}
+
+	config = g_strndup(config_new, strlen(config_new) - 4); /* strip .new */
+	ff = fopen(config, "r");
+	if (ff)
+		{
+			gchar	buf[CFG_BUFSIZE], *keyword, *ch, tmp;
+			struct _config *cf, *const end = user_config +
+				sizeof user_config / sizeof *user_config;
+
+		while (fgets(buf, sizeof(buf), ff))
+			{
+			for (ch = buf; *ch == ' ' || *ch == '\t'; ++ch);
+			if (*ch == '\n' || *ch == '#' || !*ch) continue;
+			keyword = ch;
+			while (*ch && *ch != ' ' && *ch != '\t' && *ch != '\n') ++ch;
+			tmp = *ch;
+			*ch = 0;
+
+			for (list = gkrellm_monitor_list; list; list = list->next)
+				{
+				mon = (GkrellmMonitor *) list->data;
+				if (mon->save_user_config && mon->privat->enabled &&
+					mon->config_keyword && !strcmp(mon->config_keyword, keyword))
+					break;
+				}
+			if (list) continue;
+
+			cf = user_config;
+			while (cf != end && strcmp(cf->option, keyword)) ++cf;
+			if (cf!=end) continue;
+
+			*ch = tmp;
+			fputs(buf, f);
+			}
+		fclose(ff);
+		}
+
 #if defined (S_IRUSR)
 	mode = (S_IRUSR | S_IWUSR);
 #elif defined (S_IREAD)
@@ -2457,6 +2503,9 @@
 	fchmod(fileno(f), mode);
 	fclose(f);
 
+	rename(config_new, config);
+	g_free(config);
+	g_free(config_new);
+
 	_GK.config_modified = FALSE;
 	}
-
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.