Store the user's unit preferences in git storage

Save and load a usually unused copy of the preferences with the units that
were active the last time the dive list was saved to git storage (this
isn't used in XML files); storing the unit preferences in the data file is
usually pointless (that's a setting of the software, not a property of the
data), but it's a great hint of what the user might expect to see when
creating a backend service that visualizes the dive list without
Subsurface running - so this is basically a functionality for the core
library that Subsurface itself doesn't use but that another consumer of
the library (like an HTML exporter) will need.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2015-06-16 20:28:42 -07:00
parent 7cf3ebc2f7
commit ff4b5478b5
6 changed files with 75 additions and 3 deletions

47
dive.c
View file

@ -2860,6 +2860,53 @@ void set_userid(char *rUserId)
prefs.userid[30]='\0'; prefs.userid[30]='\0';
} }
/* this sets a usually unused copy of the preferences with the units
* that were active the last time the dive list was saved to git storage
* (this isn't used in XML files); storing the unit preferences in the
* data file is usually pointless (that's a setting of the software,
* not a property of the data), but it's a great hint of what the user
* might expect to see when creating a backend service that visualizes
* the dive list without Subsurface running - so this is basically a
* functionality for the core library that Subsurface itself doesn't
* use but that another consumer of the library (like an HTML exporter)
* will need */
void set_informational_units(char *units)
{
if (strstr(units, "METRIC")) {
informational_prefs.unit_system = METRIC;
} else if (strstr(units, "IMPERIAL")) {
informational_prefs.unit_system = IMPERIAL;
} else if (strstr(units, "PERSONALIZE")) {
informational_prefs.unit_system = PERSONALIZE;
if (strstr(units, "METERS"))
informational_prefs.units.length = METERS;
if (strstr(units, "FEET"))
informational_prefs.units.length = FEET;
if (strstr(units, "LITER"))
informational_prefs.units.volume = LITER;
if (strstr(units, "CUFT"))
informational_prefs.units.volume = CUFT;
if (strstr(units, "BAR"))
informational_prefs.units.pressure = BAR;
if (strstr(units, "PSI"))
informational_prefs.units.pressure = PSI;
if (strstr(units, "PASCAL"))
informational_prefs.units.pressure = PASCAL;
if (strstr(units, "CELSIUS"))
informational_prefs.units.temperature = CELSIUS;
if (strstr(units, "FAHRENHEIT"))
informational_prefs.units.temperature = FAHRENHEIT;
if (strstr(units, "KG"))
informational_prefs.units.weight = KG;
if (strstr(units, "LBS"))
informational_prefs.units.weight = LBS;
if (strstr(units, "SECONDS"))
informational_prefs.units.vertical_speed_time = SECONDS;
if (strstr(units, "MINUTES"))
informational_prefs.units.vertical_speed_time = MINUTES;
}
}
void average_max_depth(struct diveplan *dive, int *avg_depth, int *max_depth) void average_max_depth(struct diveplan *dive, int *avg_depth, int *max_depth)
{ {
int integral = 0; int integral = 0;

1
dive.h
View file

@ -858,6 +858,7 @@ extern double strtod_flags(const char *str, const char **ptr, unsigned int flags
extern void set_save_userid_local(short value); extern void set_save_userid_local(short value);
extern void set_userid(char *user_id); extern void set_userid(char *user_id);
extern void set_informational_units(char *units);
extern const char *get_dive_date_c_string(timestamp_t when); extern const char *get_dive_date_c_string(timestamp_t when);
extern void update_setpoint_events(struct divecomputer *dc); extern void update_setpoint_events(struct divecomputer *dc);

View file

@ -727,6 +727,12 @@ static void parse_trip_notes(char *line, struct membuffer *str, void *_trip)
static void parse_settings_autogroup(char *line, struct membuffer *str, void *_unused) static void parse_settings_autogroup(char *line, struct membuffer *str, void *_unused)
{ set_autogroup(1); } { set_autogroup(1); }
static void parse_settings_units(char *line, struct membuffer *str, void *unused)
{
if (line)
set_informational_units(line);
}
static void parse_settings_userid(char *line, struct membuffer *str, void *_unused) static void parse_settings_userid(char *line, struct membuffer *str, void *_unused)
{ {
if (line) { if (line) {
@ -895,7 +901,7 @@ static void trip_parser(char *line, struct membuffer *str, void *_trip)
static struct keyword_action settings_action[] = { static struct keyword_action settings_action[] = {
#undef D #undef D
#define D(x) { #x, parse_settings_ ## x } #define D(x) { #x, parse_settings_ ## x }
D(autogroup), D(divecomputerid), D(subsurface), D(userid), D(version), D(autogroup), D(divecomputerid), D(subsurface), D(units), D(userid), D(version),
}; };
static void settings_parser(char *line, struct membuffer *str, void *_unused) static void settings_parser(char *line, struct membuffer *str, void *_unused)

2
pref.h
View file

@ -120,7 +120,7 @@ enum cloud_status {
CS_VERIFIED CS_VERIFIED
}; };
extern struct preferences prefs, default_prefs; extern struct preferences prefs, default_prefs, informational_prefs;
#define PP_GRAPHS_ENABLED (prefs.pp_graphs.po2 || prefs.pp_graphs.pn2 || prefs.pp_graphs.phe) #define PP_GRAPHS_ENABLED (prefs.pp_graphs.po2 || prefs.pp_graphs.pn2 || prefs.pp_graphs.phe)

View file

@ -786,6 +786,23 @@ static int save_one_trip(git_repository *repo, struct dir *tree, dive_trip_t *tr
return 0; return 0;
} }
static void save_units(void *_b)
{
struct membuffer *b =_b;
if (prefs.unit_system == METRIC)
put_string(b, "units METRIC\n");
else if (prefs.unit_system == IMPERIAL)
put_string(b, "units IMPERIAL\n");
else
put_format(b, "units PERSONALIZE %s %s %s %s %s %s",
prefs.units.length == METERS ? "METERS" : "FEET",
prefs.units.volume == LITER ? "LITER" : "CUFT",
prefs.units.pressure == BAR ? "BAR" : prefs.units.pressure == PSI ? "PSI" : "PASCAL",
prefs.units.temperature == CELSIUS ? "CELSIUS" : prefs.units.temperature == FAHRENHEIT ? "FAHRENHEIT" : "KELVIN",
prefs.units.weight == KG ? "KG" : "LBS",
prefs.units.vertical_speed_time == SECONDS ? "SECONDS" : "MINUTES");
}
static void save_userid(void *_b) static void save_userid(void *_b)
{ {
struct membuffer *b = _b; struct membuffer *b = _b;
@ -824,6 +841,7 @@ static void save_settings(git_repository *repo, struct dir *tree)
save_userid(&b); save_userid(&b);
call_for_each_dc(&b, save_one_device, false); call_for_each_dc(&b, save_one_device, false);
cond_put_format(autogroup, &b, "autogroup\n"); cond_put_format(autogroup, &b, "autogroup\n");
save_units(&b);
blob_insert(repo, tree, &b, "00-Subsurface"); blob_insert(repo, tree, &b, "00-Subsurface");
} }

View file

@ -3,7 +3,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include "gettext.h" #include "gettext.h"
struct preferences prefs; struct preferences prefs, informational_prefs;
struct preferences default_prefs = { struct preferences default_prefs = {
.cloud_base_url = "https://cloud.subsurface-divelog.org/", .cloud_base_url = "https://cloud.subsurface-divelog.org/",
.units = SI_UNITS, .units = SI_UNITS,