[PATCH] Add default filename and divelist font to prefs structure

Linus Torvalds torvalds at linux-foundation.org
Fri Jan 11 17:14:06 PST 2013


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Fri, 11 Jan 2013 17:07:22 -0800
Subject: [PATCH] Add default filename and divelist font to prefs structure

.. and add the usual logic to not save the default values.

This also simplifies the initial system-specific setup of both of these:
since we have defaults for all the preferences that get set up at
startup, we can just initialize those defaults to the system-specific
fonts then and there.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---

Again, this changes the windows and macos code without testing. Hopefully 
it "just works". The logic is a bit simpler, since now the system-specific 
code only gets run once to initialze the "default_prefs" fields. Or, in 
the case of the default font to use, it's now just a constant string 
variable.

The preference loading functions should have helpers for loading doubles 
and strings, right now that is still pointlessly duplicated. 

 dive.h       |  2 --
 divelist.c   |  2 +-
 file.c       |  2 +-
 gtk-gui.c    | 30 ++++++++++--------------------
 linux.c      | 30 ++++++++++++------------------
 macos.c      | 29 ++++++++++++-----------------
 main.c       | 11 +++++++----
 pref.h       |  6 ++++--
 prefs.c      | 26 ++++++++++++++++++++++----
 statistics.c |  2 +-
 windows.c    | 37 ++++++++++++++++---------------------
 11 files changed, 86 insertions(+), 91 deletions(-)

diff --git a/dive.h b/dive.h
index f746909ad0ea..b80e6bf5375f 100644
--- a/dive.h
+++ b/dive.h
@@ -569,9 +569,7 @@ typedef enum {
 	UTF8_FONT_WITH_STARS
 } os_feature_t;
 
-extern const char *default_filename;
 extern const char *existing_filename;
-extern const char *subsurface_default_filename(void);
 extern const char *subsurface_gettext_domainpath(char *);
 extern gboolean subsurface_os_feature_available(os_feature_t);
 extern void subsurface_command_line_init(gint *, gchar ***);
diff --git a/divelist.c b/divelist.c
index 831e2e19d7ce..e7eb5562d363 100644
--- a/divelist.c
+++ b/divelist.c
@@ -2659,7 +2659,7 @@ GtkWidget *dive_list_create(void)
 				);
 	dive_list.model = dive_list.treemodel;
 	dive_list.tree_view = gtk_tree_view_new_with_model(TREEMODEL(dive_list));
-	set_divelist_font(divelist_font);
+	set_divelist_font(prefs.divelist_font);
 
 	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
 
diff --git a/file.c b/file.c
index ecbc53df8f4d..2e40872a1692 100644
--- a/file.c
+++ b/file.c
@@ -255,7 +255,7 @@ void parse_file(const char *filename, GError **error, gboolean possible_default_
 
 	if (readfile(filename, &mem) < 0) {
 		/* we don't want to display an error if this was the default file */
-		if (default_filename && ! strcmp(filename, default_filename))
+		if (prefs.default_filename && ! strcmp(filename, prefs.default_filename))
 			return;
 
 		g_warning(_("Failed to read '%s'.\n"), filename);
diff --git a/gtk-gui.c b/gtk-gui.c
index d096e7e90ccf..1bc338811895 100644
--- a/gtk-gui.c
+++ b/gtk-gui.c
@@ -33,8 +33,6 @@ GtkWidget *notebook;
 
 int        error_count;
 const char *existing_filename;
-const char *divelist_font;
-const char *default_filename;
 
 char *nicknamestring;
 
@@ -129,7 +127,7 @@ static void file_save_as(GtkWidget *w, gpointer data)
 		current_dir = g_path_get_dirname(existing_filename);
 		current_file = g_path_get_basename(existing_filename);
 	} else {
-		const char *current_default = subsurface_default_filename();
+		const char *current_default = prefs.default_filename;
 		current_dir = g_path_get_dirname(current_default);
 		current_file = g_path_get_basename(current_default);
 	}
@@ -159,7 +157,7 @@ static void file_save(GtkWidget *w, gpointer data)
 	if (!existing_filename)
 		return file_save_as(w, data);
 
-	current_default = subsurface_default_filename();
+	current_default = prefs.default_filename;
 	if (strcmp(existing_filename, current_default) ==  0) {
 		/* if we are using the default filename the directory
 		 * that we are creating the file in may not exist */
@@ -255,7 +253,7 @@ static void file_open(GtkWidget *w, gpointer data)
 		GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 		GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
 		NULL);
-	current_default = subsurface_default_filename();
+	current_default = prefs.default_filename;
 	gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), current_default);
 	/* when opening the data file we should allow only one file to be chosen */
 	gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE);
@@ -496,7 +494,7 @@ static void pick_default_file(GtkWidget *w, GtkButton *button)
 	gtk_widget_set_sensitive(parent, FALSE);
 	gtk_window_set_transient_for(GTK_WINDOW(fs_dialog), GTK_WINDOW(parent));
 
-	current_default = subsurface_default_filename();
+	current_default = prefs.default_filename;
 	current_def_dir = g_path_get_dirname(current_default);
 	current_def_file = g_path_get_basename(current_default);
 
@@ -522,7 +520,6 @@ static void pick_default_file(GtkWidget *w, GtkButton *button)
 
 	free(current_def_dir);
 	free(current_def_file);
-	free((void *)current_default);
 	gtk_widget_destroy(fs_dialog);
 
 	gtk_widget_set_sensitive(parent, TRUE);
@@ -622,7 +619,7 @@ static void preferences_dialog(GtkWidget *w, gpointer data)
 	frame = gtk_frame_new(_("Divelist Font"));
 	gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 5);
 
-	font = gtk_font_button_new_with_font(divelist_font);
+	font = gtk_font_button_new_with_font(prefs.divelist_font);
 	gtk_container_add(GTK_CONTAINER(frame),font);
 
 	frame = gtk_frame_new(_("Misc. Options"));
@@ -635,7 +632,7 @@ static void preferences_dialog(GtkWidget *w, gpointer data)
 	gtk_box_pack_start(GTK_BOX(box), frame, FALSE, FALSE, 5);
 	box = gtk_hbox_new(FALSE, 6);
 	gtk_container_add(GTK_CONTAINER(frame), box);
-	current_default = subsurface_default_filename();
+	current_default = prefs.default_filename;
 	xmlfile_button = gtk_button_new_with_label(current_default);
 	g_signal_connect(G_OBJECT(xmlfile_button), "clicked",
 			 G_CALLBACK(pick_default_file), xmlfile_button);
@@ -773,10 +770,8 @@ static void preferences_dialog(GtkWidget *w, gpointer data)
 		/* Make sure to flush any modified old dive data with old units */
 		update_dive(NULL);
 
-		if (divelist_font)
-			free((void *)divelist_font);
-		divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
-		set_divelist_font(divelist_font);
+		prefs.divelist_font = strdup(gtk_font_button_get_font_name(GTK_FONT_BUTTON(font)));
+		set_divelist_font(prefs.divelist_font);
 		po2_threshold_text = gtk_entry_get_text(GTK_ENTRY(entry_po2));
 		sscanf(po2_threshold_text, "%lf", &prefs.pp_graphs.po2_threshold);
 		pn2_threshold_text = gtk_entry_get_text(GTK_ENTRY(entry_pn2));
@@ -805,8 +800,7 @@ static void preferences_dialog(GtkWidget *w, gpointer data)
 		}
 
 		if (strcmp(current_default, new_default)) {
-			free((void *)default_filename);
-			default_filename = new_default;
+			prefs.default_filename = new_default;
 		}
 
 		save_preferences();
@@ -815,7 +809,6 @@ static void preferences_dialog(GtkWidget *w, gpointer data)
 		set_gf(prefs.gflow, prefs.gfhigh);
 		update_screen();
 	}
-	free((void *)current_default);
 	gtk_widget_destroy(dialog);
 }
 
@@ -1319,8 +1312,6 @@ void run_ui(void)
 void exit_ui(void)
 {
 	subsurface_close_conf();
-	if (default_filename)
-		free((char *)default_filename);
 	if (existing_filename)
 		free((void *)existing_filename);
 	if (default_dive_computer_device)
@@ -1573,9 +1564,8 @@ void import_files(GtkWidget *w, gpointer data)
 	if (existing_filename) {
 		current_def_dir = g_path_get_dirname(existing_filename);
 	} else {
-		current_default = subsurface_default_filename();
+		current_default = prefs.default_filename;
 		current_def_dir = g_path_get_dirname(current_default);
-		free((void *)current_default);
 	}
 
 	/* it's possible that the directory doesn't exist (especially for the default)
diff --git a/linux.c b/linux.c
index 6e83a9f3751b..9281b434e22d 100644
--- a/linux.c
+++ b/linux.c
@@ -5,7 +5,7 @@
 #include <gconf/gconf-client.h>
 #include <string.h>
 
-#define DIVELIST_DEFAULT_FONT "Sans 8"
+const char system_divelist_default_font[] = "Sans 8";
 
 GConfClient *gconf;
 
@@ -131,22 +131,18 @@ const char *subsurface_icon_name()
 	return "subsurface.svg";
 }
 
-const char *subsurface_default_filename()
+const char *system_default_filename(void)
 {
-	if (default_filename) {
-		return strdup(default_filename);
-	} else {
-		const char *home, *user;
-		char *buffer;
-		int len;
-
-		home = g_get_home_dir();
-		user = g_get_user_name();
-		len = strlen(home) + strlen(user) + 17;
-		buffer = malloc(len);
-		snprintf(buffer, len, "%s/subsurface/%s.xml", home, user);
-		return buffer;
-	}
+	const char *home, *user;
+	char *buffer;
+	int len;
+
+	home = g_get_home_dir();
+	user = g_get_user_name();
+	len = strlen(home) + strlen(user) + 17;
+	buffer = malloc(len);
+	snprintf(buffer, len, "%s/subsurface/%s.xml", home, user);
+	return buffer;
 }
 
 const char *subsurface_gettext_domainpath(char *argv0)
@@ -163,8 +159,6 @@ const char *subsurface_gettext_domainpath(char *argv0)
 void subsurface_ui_setup(GtkSettings *settings, GtkWidget *menubar,
 		GtkWidget *vbox, GtkUIManager *ui_manager)
 {
-	if (!divelist_font)
-		divelist_font = strdup(DIVELIST_DEFAULT_FONT);
 	gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
 }
 
diff --git a/macos.c b/macos.c
index b2b96cc83f82..b8451cfd0826 100644
--- a/macos.c
+++ b/macos.c
@@ -19,7 +19,8 @@ static GtkOSXApplication *osx_app;
 #define SUBSURFACE_PREFERENCES CFSTR("org.hohndel.subsurface")
 #define ICON_NAME "Subsurface.icns"
 #define UI_FONT "Arial Unicode MS 12"
-#define DIVELIST_MAC_DEFAULT_FONT "Arial Unicode MS 9"
+
+const char system_divelist_default_font[] = "Arial Unicode MS 9";
 
 void subsurface_open_conf(void)
 {
@@ -134,22 +135,18 @@ const char *subsurface_icon_name()
 	return path;
 }
 
-const char *subsurface_default_filename()
+const char *system_default_filename(void)
 {
-	if (default_filename) {
-		return strdup(default_filename);
-	} else {
-		const char *home, *user;
-		char *buffer;
-		int len;
+	const char *home, *user;
+	char *buffer;
+	int len;
 
-		home = g_get_home_dir();
-		user = g_get_user_name();
-		len = strlen(home) + strlen(user) + 45;
-		buffer = malloc(len);
-		snprintf(buffer, len, "%s/Library/Application Support/Subsurface/%s.xml", home, user);
-		return buffer;
-	}
+	home = g_get_home_dir();
+	user = g_get_user_name();
+	len = strlen(home) + strlen(user) + 45;
+	buffer = malloc(len);
+	snprintf(buffer, len, "%s/Library/Application Support/Subsurface/%s.xml", home, user);
+	return buffer;
 }
 
 const char *subsurface_gettext_domainpath(char *argv0)
@@ -176,8 +173,6 @@ void subsurface_ui_setup(GtkSettings *settings, GtkWidget *menubar,
 {
 	GtkWidget *menu_item, *sep;
 
-	if (!divelist_font)
-		divelist_font = strdup(DIVELIST_MAC_DEFAULT_FONT);
 	g_object_set(G_OBJECT(settings), "gtk-font-name", UI_FONT, NULL);
 
 	osx_app = g_object_new(GTK_TYPE_OSX_APPLICATION, NULL);
diff --git a/main.c b/main.c
index 6cba5d3d2134..a0cd313d0664 100644
--- a/main.c
+++ b/main.c
@@ -268,8 +268,12 @@ void renumber_dives(int nr)
  */
 static void setup_system_prefs(void)
 {
-	const char *env = getenv("LC_MEASUREMENT");
+	const char *env;
 
+	default_prefs.divelist_font = strdup(system_divelist_default_font);
+	default_prefs.default_filename = strdup(system_default_filename());
+
+	env = getenv("LC_MEASUREMENT");
 	if (!env)
 		env = getenv("LC_ALL");
 	if (!env)
@@ -307,7 +311,7 @@ int main(int argc, char **argv)
 #if DEBUGFILE > 1
 	debugfile = stderr;
 #elif defined(DEBUGFILE)
-	debugfilename = (char *)subsurface_default_filename();
+	debugfilename = strdup(prefs.default_filename);
 	strncpy(debugfilename + strlen(debugfilename) - 3, "log", 3);
 	if (g_mkdir_with_parents(g_path_get_dirname(debugfilename), 0664) != 0 ||
 	    (debugfile = g_fopen(debugfilename, "w")) == NULL)
@@ -339,12 +343,11 @@ int main(int argc, char **argv)
 	}
 	if (no_filenames) {
 		GError *error = NULL;
-		const char *filename = subsurface_default_filename();
+		const char *filename = prefs.default_filename;
 		parse_file(filename, &error, TRUE);
 		/* don't report errors - this file may not exist, but make
 		   sure we remember this as the filename in use */
 		set_filename(filename, FALSE);
-		free((void *)filename);
 	}
 	report_dives(imported, FALSE);
 	if (dive_table.nr == 0)
diff --git a/pref.h b/pref.h
index 18a6480322f7..342c9f6a94e3 100644
--- a/pref.h
+++ b/pref.h
@@ -30,6 +30,8 @@ struct preferences {
 	gboolean calc_ceiling_3m_incr;
 	double gflow;
 	double gfhigh;
+	const char *divelist_font;
+	const char *default_filename;
 };
 
 extern struct preferences prefs, default_prefs;
@@ -45,8 +47,8 @@ extern int subsurface_get_conf_bool(char *name);
 extern void subsurface_flush_conf(void);
 extern void subsurface_close_conf(void);
 
-/* Misc preferences - should we have defaults for these too? */
-extern const char *divelist_font;
+extern const char system_divelist_default_font[];
+extern const char *system_default_filename();
 
 extern void load_preferences(void);
 extern void save_preferences(void);
diff --git a/prefs.c b/prefs.c
index d15a97dd9191..b6ae4ee6a81c 100644
--- a/prefs.c
+++ b/prefs.c
@@ -1,3 +1,5 @@
+#include <string.h>
+
 #include "dive.h"
 
 static void set_bool_conf(char *name, gboolean value, gboolean def)
@@ -13,6 +15,16 @@ static void set_bool_conf(char *name, gboolean value, gboolean def)
 #define SAVE_UNIT(name, field, value) __SAVE_BOOLEAN(name, units.field, value)
 #define SAVE_BOOL(name, field) __SAVE_BOOLEAN(name, field, TRUE)
 
+static void set_string_conf(char *name, const char *value, const char *def)
+{
+	if (!strcmp(value, def)) {
+		subsurface_unset_conf(name);
+		return;
+	}
+	subsurface_set_conf(name, value);
+}
+#define SAVE_STRING(name, field) set_string_conf(name, prefs.field, default_prefs.field)
+
 /* We don't really save doubles */
 static void save_double_conf(char *name, double _val, double _def)
 {
@@ -63,7 +75,7 @@ void save_preferences(void)
 	SAVE_BOOL("OTU", visible_cols.otu);
 	SAVE_BOOL("MAXCNS", visible_cols.maxcns);
 
-	subsurface_set_conf("divelist_font", divelist_font);
+	SAVE_STRING("divelist_font", divelist_font);
 
 	SAVE_BOOL("po2graph", pp_graphs.po2);
 	SAVE_BOOL("pn2graph", pp_graphs.pn2);
@@ -80,7 +92,7 @@ void save_preferences(void)
 	SAVE_PERCENT("gflow", gflow);
 	SAVE_PERCENT("gfhigh", gfhigh);
 
-	subsurface_set_conf("default_filename", default_filename);
+	SAVE_STRING("default_filename", default_filename);
 
 	/* Flush the changes out to the system */
 	subsurface_flush_conf();
@@ -153,7 +165,13 @@ void load_preferences(void)
 		free((void *)conf_value);
 	}
 	set_gf(prefs.gflow, prefs.gfhigh);
-	divelist_font = subsurface_get_conf("divelist_font");
 
-	default_filename = subsurface_get_conf("default_filename");
+	conf_value = subsurface_get_conf("divelist_font");
+	if (conf_value)
+		prefs.divelist_font = conf_value;
+
+
+	conf_value = subsurface_get_conf("default_filename");
+	if (conf_value)
+		prefs.default_filename = conf_value;
 }
diff --git a/statistics.c b/statistics.c
index b4427918d0f1..dc2304ec5a4e 100644
--- a/statistics.c
+++ b/statistics.c
@@ -154,7 +154,7 @@ static void init_tree()
 	GtkTreeViewColumn *column;
 	GtkTreeStore *store;
 	int i;
-	PangoFontDescription *font_desc = pango_font_description_from_string(divelist_font);
+	PangoFontDescription *font_desc = pango_font_description_from_string(prefs.divelist_font);
 
 	gtk_widget_modify_font(yearly_tree, font_desc);
 	pango_font_description_free(font_desc);
diff --git a/windows.c b/windows.c
index c3ea94c74ae5..f9b04ce031c9 100644
--- a/windows.c
+++ b/windows.c
@@ -4,7 +4,8 @@
 #include "display-gtk.h"
 #include <windows.h>
 #include <shlobj.h>
-#define DIVELIST_DEFAULT_FONT "Sans 8"
+
+const char system_divelist_default_font[] = "Sans 8";
 
 static HKEY hkey;
 
@@ -208,26 +209,22 @@ const char *subsurface_icon_name()
 	return "subsurface.ico";
 }
 
-const char *subsurface_default_filename()
+const char *system_default_filename(void)
 {
-	if (default_filename) {
-		return strdup(default_filename);
-	} else {
-		char datapath[MAX_PATH];
-		const char *user;
-		char *buffer;
-		int len;
-
-		user = g_get_user_name();
-		if (! SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, datapath))) {
-			datapath[0] = '.';
-			datapath[1] = '\0';
-		}
-		len = strlen(datapath) + strlen(user) + 17;
-		buffer = malloc(len);
-		snprintf(buffer, len, "%s\\Subsurface\\%s.xml", datapath, user);
-		return buffer;
+	char datapath[MAX_PATH];
+	const char *user;
+	char *buffer;
+	int len;
+
+	user = g_get_user_name();
+	if (! SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, datapath))) {
+		datapath[0] = '.';
+		datapath[1] = '\0';
 	}
+	len = strlen(datapath) + strlen(user) + 17;
+	buffer = malloc(len);
+	snprintf(buffer, len, "%s\\Subsurface\\%s.xml", datapath, user);
+	return buffer;
 }
 
 const char *subsurface_gettext_domainpath(char *argv0)
@@ -244,8 +241,6 @@ const char *subsurface_gettext_domainpath(char *argv0)
 void subsurface_ui_setup(GtkSettings *settings, GtkWidget *menubar,
 		GtkWidget *vbox, GtkUIManager *ui_manager)
 {
-	if (!divelist_font)
-		divelist_font = strdup(DIVELIST_DEFAULT_FONT);
 	gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
 }
 
-- 
1.8.1.rc2.6.g18499ba



More information about the subsurface mailing list