[PATCH 2/3] Make the default preferences explicit

Linus Torvalds torvalds at linux-foundation.org
Thu Jan 10 20:03:11 PST 2013


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Thu, 10 Jan 2013 17:55:44 -0800
Subject: [PATCH 2/3] Make the default preferences explicit

This makes it explicit what the default preferences are, so that we can
more easily avoid unnecessarily saving default settings.  It also makes
imperial metrics the default for the US (Burma and Liberia always get
forgotten!)

Right now we tend to be somewhat confused about defaults.  We do have
them, but then even if something has a default value, we tend to write
it out to the config file.  Which is not just unnecessary, but makes it
really hard to see after-the-fact whether the user actually wanted that
*specific* value, or whether they just wanted the default behavior.

So this prepares for having explicit configuration for when we want
something different than the defaults.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---
 gtk-gui.c |  7 -------
 main.c    | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 51 insertions(+), 9 deletions(-)

diff --git a/gtk-gui.c b/gtk-gui.c
index 3c4c9906891d..b6904045bc2a 100644
--- a/gtk-gui.c
+++ b/gtk-gui.c
@@ -35,13 +35,6 @@ const char *existing_filename;
 const char *divelist_font;
 const char *default_filename;
 
-struct preferences prefs = {
-	SI_UNITS,
-	{ TRUE, FALSE, },
-	{ FALSE, FALSE, FALSE, 1.6, 4.0, 13.0},
-	FALSE, FALSE, FALSE, 0.30, 0.75
-};
-
 char *nicknamestring;
 
 static GtkWidget *dive_profile;
diff --git a/main.c b/main.c
index 71742d7c018a..6cba5d3d2134 100644
--- a/main.c
+++ b/main.c
@@ -15,7 +15,24 @@ char *debugfilename;
 FILE *debugfile;
 #endif
 
-struct units units;
+struct preferences prefs;
+struct preferences default_prefs = {
+	.units = SI_UNITS,
+	.visible_cols = { TRUE, FALSE, },
+	.pp_graphs = {
+		.po2 = FALSE,
+		.pn2 = FALSE,
+		.phe = FALSE,
+		.po2_threshold =  1.6,
+		.pn2_threshold =  4.0,
+		.phe_threshold = 13.0,
+	},
+	.profile_red_ceiling  = FALSE,
+	.profile_calc_ceiling = FALSE,
+	.calc_ceiling_3m_incr = FALSE,
+	.gflow = 0.30,
+	.gfhigh = 0.75,
+};
 
 /* random helper functions, used here or elsewhere */
 static int sortfn(const void *_a, const void *_b)
@@ -239,6 +256,36 @@ void renumber_dives(int nr)
 	mark_divelist_changed(TRUE);
 }
 
+/*
+ * Under a POSIX setup, the locale string should have a format
+ * like [language[_territory][.codeset][@modifier]].
+ *
+ * So search for the underscore, and see if the "territory" is
+ * US, and turn on imperial units by default.
+ *
+ * I guess Burma and Liberia should trigger this too. I'm too
+ * lazy to look up the territory names, though.
+ */
+static void setup_system_prefs(void)
+{
+	const char *env = getenv("LC_MEASUREMENT");
+
+	if (!env)
+		env = getenv("LC_ALL");
+	if (!env)
+		env = getenv("LANG");
+	if (!env)
+		return;
+	env = strchr(env, '_');
+	if (!env)
+		return;
+	env++;
+	if (strncmp(env, "US", 2))
+		return;
+
+	default_prefs.units = IMPERIAL_units;
+}
+
 int main(int argc, char **argv)
 {
 	int i;
@@ -253,7 +300,9 @@ int main(int argc, char **argv)
 	bindtextdomain("subsurface", path);
 	bind_textdomain_codeset("subsurface", "utf-8");
 	textdomain("subsurface");
-	units = SI_units;
+
+	setup_system_prefs();
+	prefs = default_prefs;
 
 #if DEBUGFILE > 1
 	debugfile = stderr;
-- 
1.8.1.rc2.6.g18499ba



More information about the subsurface mailing list