summaryrefslogtreecommitdiffstats
path: root/prefs.c
blob: d1073c88f3af9e52bad4b5f5c9f282cdace5a817 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <string.h>

#include "dive.h"

struct units *get_units()
{
	return &prefs.units;
}

static void set_bool_conf(char *name, gboolean value, gboolean def)
{
	if (value == def) {
		subsurface_unset_conf(name);
		return;
	}
	subsurface_set_conf_bool(name, value);
}
#define __SAVE_BOOLEAN(name, field, value) \
	set_bool_conf(name, prefs.field == value, default_prefs.field == value)
#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)
{
	int val = rint(_val * 100), neg, len;
	int def = rint(_def * 100);
	char string[16];

	if (val == def) {
		subsurface_unset_conf(name);
		return;
	}
	neg = 0;
	if (val < 0) {
		neg = 1;
		val = -val;
	}
	len = snprintf(string, sizeof(string), "%s%d.%02d",
		neg ? "-" : "", val / 100, val % 100);

	/* Save with 0-2 decimals */
	if (string[len-1] == '0') {
		len--;
		if (string[len-1] == '0')
			len -= 2;	/* Remove decimal point too */
		string[len] = 0;
	}

	subsurface_set_conf(name, string);
}

static void save_int_conf(char *name, int value, int def)
{
	if (value == def) {
		subsurface_unset_conf(name);
		return;
	}
	subsurface_set_conf_int(name, value);
}

#define SAVE_DOUBLE(name, field) save_double_conf(name, prefs.field, default_prefs.field)
#define SAVE_PERCENT(name, field) save_double_conf(name, prefs.field*100, default_prefs.field*100)
#define SAVE_INT(name, field) save_int_conf(name, prefs.field, default_prefs.field)

void save_preferences(void)
{
	SAVE_UNIT("feet", length, FEET);
	SAVE_UNIT("psi", pressure, PSI);
	SAVE_UNIT("cuft", volume, CUFT);
	SAVE_UNIT("fahrenheit", temperature, FAHRENHEIT);
	SAVE_UNIT("lbs", weight, LBS);

	SAVE_BOOL("TEMPERATURE", visible_cols.temperature);
	SAVE_BOOL("TOTALWEIGHT", visible_cols.totalweight);
	SAVE_BOOL("SUIT", visible_cols.suit);
	SAVE_BOOL("CYLINDER", visible_cols.cylinder);
	SAVE_BOOL("NITROX", visible_cols.nitrox);
	SAVE_BOOL("SAC", visible_cols.sac);
	SAVE_BOOL("OTU", visible_cols.otu);
	SAVE_BOOL("MAXCNS", visible_cols.maxcns);

	SAVE_STRING("divelist_font", divelist_font);

	SAVE_BOOL("po2graph", pp_graphs.po2);
	SAVE_BOOL("pn2graph", pp_graphs.pn2);
	SAVE_BOOL("phegraph", pp_graphs.phe);

	SAVE_DOUBLE("po2threshold", pp_graphs.po2_threshold);
	SAVE_DOUBLE("pn2threshold", pp_graphs.pn2_threshold);
	SAVE_DOUBLE("phethreshold", pp_graphs.phe_threshold);

	SAVE_BOOL("mod", mod);
	SAVE_DOUBLE("modppO2", mod_ppO2);
	SAVE_BOOL("ead", ead);
	SAVE_BOOL("redceiling", profile_red_ceiling);
	SAVE_BOOL("calcceiling", profile_calc_ceiling);
	SAVE_BOOL("calcceiling3m", calc_ceiling_3m_incr);

	SAVE_PERCENT("gflow", gflow);
	SAVE_PERCENT("gfhigh", gfhigh);

	SAVE_STRING("default_filename", default_filename);

	SAVE_INT("map_provider", map_provider);
        SAVE_INT("display_invalid_dives", display_invalid_dives);

	/* Flush the changes out to the system */
	subsurface_flush_conf();
}

static gboolean get_bool(char *name, gboolean def)
{
	int val = subsurface_get_conf_bool(name);
	if (val < 0)
		return def;
	return val;
}
#define GET_UNIT(name, field, f, t) \
	prefs.units.field = get_bool(name, default_prefs.units.field) ? (t) : (f)
#define GET_BOOL(name, field) \
	prefs.field = get_bool(name, default_prefs.field)


void load_preferences(void)
{
	const char *conf_value;
	int int_value;

	GET_UNIT("feet", length, METERS, FEET);
	GET_UNIT("psi", pressure, BAR, PSI);
	GET_UNIT("cuft", volume, LITER, CUFT);
	GET_UNIT("fahrenheit", temperature, CELSIUS, FAHRENHEIT);
	GET_UNIT("lbs", weight, KG, LBS);

	/* an unset key is 'default' */
	GET_BOOL("CYLINDER", visible_cols.cylinder);
	GET_BOOL("TEMPERATURE", visible_cols.temperature);
	GET_BOOL("TOTALWEIGHT", visible_cols.totalweight);
	GET_BOOL("SUIT", visible_cols.suit);
	GET_BOOL("NITROX", visible_cols.nitrox);
	GET_BOOL("OTU", visible_cols.otu);
	GET_BOOL("MAXCNS", visible_cols.maxcns);
	GET_BOOL("SAC", visible_cols.sac);
	GET_BOOL("po2graph", pp_graphs.po2);
	GET_BOOL("pn2graph", pp_graphs.pn2);
	GET_BOOL("phegraph", pp_graphs.phe);

	conf_value = subsurface_get_conf("po2threshold");
	if (conf_value) {
		sscanf(conf_value, "%lf", &prefs.pp_graphs.po2_threshold);
		free((void *)conf_value);
	}
	conf_value = subsurface_get_conf("pn2threshold");
	if (conf_value) {
		sscanf(conf_value, "%lf", &prefs.pp_graphs.pn2_threshold);
		free((void *)conf_value);
	}
	conf_value = subsurface_get_conf("phethreshold");
	if (conf_value) {
		sscanf(conf_value, "%lf", &prefs.pp_graphs.phe_threshold);
		free((void *)conf_value);
	}
	GET_BOOL("mod", mod);
	conf_value = subsurface_get_conf("modppO2");
	if (conf_value) {
		sscanf(conf_value, "%lf", &prefs.mod_ppO2);
		free((void *)conf_value);
	}
	GET_BOOL("ead", ead);
	GET_BOOL("redceiling", profile_red_ceiling);
	GET_BOOL("calcceiling", profile_calc_ceiling);
	GET_BOOL("calcceiling3m", calc_ceiling_3m_incr);
	conf_value = subsurface_get_conf("gflow");
	if (conf_value) {
		sscanf(conf_value, "%lf", &prefs.gflow);
		prefs.gflow /= 100.0;
		free((void *)conf_value);
	}
	conf_value = subsurface_get_conf("gfhigh");
	if (conf_value) {
		sscanf(conf_value, "%lf", &prefs.gfhigh);
		prefs.gfhigh /= 100.0;
		free((void *)conf_value);
	}
	set_gf(prefs.gflow, prefs.gfhigh);

	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;

	int_value = subsurface_get_conf_int("map_provider");
	if (int_value >= 0)
		prefs.map_provider = int_value;

	int_value = subsurface_get_conf_int("display_invalid_dives");
	if (int_value >= 0)
		prefs.display_invalid_dives = int_value;
}