From 9b9d2d0fb71a6f00d769bd0836b3eb97b80cd4ed Mon Sep 17 00:00:00 2001 From: ejurgensen Date: Sat, 22 Feb 2020 16:46:07 +0100 Subject: [PATCH] [settings] Add handlers to set default settings, e.g. from the cfg file --- src/settings.c | 90 ++++++++++++++++++++++++++++++++++++++++++++------ src/settings.h | 4 ++- 2 files changed, 83 insertions(+), 11 deletions(-) diff --git a/src/settings.c b/src/settings.c index a3989dcb..d3f9bae6 100644 --- a/src/settings.c +++ b/src/settings.c @@ -22,8 +22,12 @@ #include #include "db.h" +#include "conffile.h" - +// Forward - setting initializers +static bool artwork_spotify_default_getbool(struct settings_option *option); +static bool artwork_discogs_default_getbool(struct settings_option *option); +static bool artwork_coverartarchive_default_getbool(struct settings_option *option); static struct settings_option webinterface_options[] = { @@ -33,9 +37,9 @@ static struct settings_option webinterface_options[] = static struct settings_option artwork_options[] = { - { "use_artwork_source_spotify", SETTINGS_TYPE_BOOL }, - { "use_artwork_source_discogs", SETTINGS_TYPE_BOOL }, - { "use_artwork_source_coverartarchive", SETTINGS_TYPE_BOOL }, + { "use_artwork_source_spotify", SETTINGS_TYPE_BOOL, NULL, artwork_spotify_default_getbool, NULL }, + { "use_artwork_source_discogs", SETTINGS_TYPE_BOOL, NULL, artwork_discogs_default_getbool, NULL }, + { "use_artwork_source_coverartarchive", SETTINGS_TYPE_BOOL, NULL, artwork_coverartarchive_default_getbool, NULL }, }; static struct settings_category categories[] = @@ -45,6 +49,54 @@ static struct settings_category categories[] = }; +/* ---------------------------- DEFAULT SETTERS ------------------------------*/ + +static bool +artwork_default_getbool(bool no_cfg_default, const char *cfg_name) +{ + cfg_t *lib = cfg_getsec(cfg, "library"); + const char *name; + int n_cfg; + int i; + + n_cfg = cfg_size(lib, "artwork_online_sources"); + if (n_cfg == 0) + return no_cfg_default; + + for (i = 0; i < n_cfg; i++) + { + name = cfg_getnstr(lib, "artwork_online_sources", i); + if (strcasecmp(name, cfg_name) == 0) + return true; + } + + return false; +} + +static bool +artwork_spotify_default_getbool(struct settings_option *option) +{ + // Enabled by default, it will only work for premium users anyway. So Spotify + // probably won't mind, and the user probably also won't mind that we share + // data with Spotify, since he is already doing it. + return artwork_default_getbool(true, "spotify"); +} + +static bool +artwork_discogs_default_getbool(struct settings_option *option) +{ + return artwork_default_getbool(false, "discogs"); +} + +static bool +artwork_coverartarchive_default_getbool(struct settings_option *option) +{ + return artwork_default_getbool(false, "coverartarchive"); +} + + +/* ------------------------------ IMPLEMENTATION -----------------------------*/ + int settings_categories_count() { @@ -110,39 +162,57 @@ int settings_option_getint(struct settings_option *option) { int intval = 0; + int ret; if (!option || option->type != SETTINGS_TYPE_INT) return 0; - db_admin_getint(&intval, option->name); + ret = db_admin_getint(&intval, option->name); + if (ret == 0) + return intval; - return intval; + if (option->default_getint) + return option->default_getint(option); + + return 0; } bool settings_option_getbool(struct settings_option *option) { int intval = 0; + int ret; if (!option || option->type != SETTINGS_TYPE_BOOL) return false; - db_admin_getint(&intval, option->name); + ret = db_admin_getint(&intval, option->name); + if (ret == 0) + return (intval != 0); - return (intval != 0); + if (option->default_getbool) + return option->default_getbool(option); + + return false; } char * settings_option_getstr(struct settings_option *option) { char *s = NULL; + int ret; if (!option || option->type != SETTINGS_TYPE_STR) return NULL; - db_admin_get(&s, option->name); + ret = db_admin_get(&s, option->name); + if (ret == 0) + return s; - return s; + if (option->default_getstr) + return option->default_getstr(option); + + return NULL; } int diff --git a/src/settings.h b/src/settings.h index 0d972608..80d1a18f 100644 --- a/src/settings.h +++ b/src/settings.h @@ -15,6 +15,9 @@ enum settings_type { struct settings_option { const char *name; enum settings_type type; + int (*default_getint)(struct settings_option *option); + bool (*default_getbool)(struct settings_option *option); + char *(*default_getstr)(struct settings_option *option); }; struct settings_category { @@ -61,5 +64,4 @@ settings_option_setbool(struct settings_option *option, bool value); int settings_option_setstr(struct settings_option *option, const char *value); - #endif /* __SETTINGS_H__ */