[settings] Add settings component

This adds a new settings component for user configurable options that
can be changed through the JSON API.
The settings are stored in the admin db table and not in the conf-file.
This commit is contained in:
chme 2019-07-13 08:15:06 +02:00
parent f9bfec180f
commit 02dd0a9445
3 changed files with 219 additions and 0 deletions

View File

@ -122,6 +122,7 @@ forked_daapd_SOURCES = main.c \
smartpl_query.c smartpl_query.h \
player.c player.h \
worker.c worker.h \
settings.c settings.h \
input.h input.c \
inputs/file_http.c inputs/pipe.c \
outputs.h outputs.c \

153
src/settings.c Normal file
View File

@ -0,0 +1,153 @@
/*
* Copyright (C) 2019 Christian Meffert <christian.meffert@googlemail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "settings.h"
#include <stdbool.h>
#include <string.h>
#include "db.h"
static struct settings_option webinterface_options[] =
{
{ "show_composer_now_playing", SETTINGS_TYPE_BOOL },
{ "show_composer_for_genre", SETTINGS_TYPE_STR },
};
static struct settings_category categories[] =
{
{ "webinterface", webinterface_options, ARRAY_SIZE(webinterface_options) },
};
int
settings_categories_count()
{
return ARRAY_SIZE(categories);
}
struct settings_category *
settings_category_get_byindex(int index)
{
if (index < 0 || settings_categories_count() <= index)
return NULL;
return &categories[index];
}
struct settings_category *
settings_category_get(const char *name)
{
int i;
for (i = 0; i < settings_categories_count(); i++)
{
if (strcasecmp(name, categories[i].name) == 0)
return &categories[i];
}
return NULL;
}
int
settings_option_count(struct settings_category *category)
{
return category->count_options;
}
struct settings_option *
settings_option_get_byindex(struct settings_category *category, int index)
{
if (index < 0 || !category || category->count_options <= index)
return NULL;
return &category->options[index];
}
struct settings_option *
settings_option_get(struct settings_category *category, const char *name)
{
int i;
if (!category || !name)
return NULL;
for (i = 0; i < category->count_options; i++)
{
if (strcasecmp(name, category->options[i].name) == 0)
return &category->options[i];
}
return NULL;
}
int
settings_option_getint(struct settings_option *option)
{
if (!option || option->type != SETTINGS_TYPE_INT)
return 0;
return db_admin_getint(option->name);
}
bool
settings_option_getbool(struct settings_option *option)
{
if (!option || option->type != SETTINGS_TYPE_BOOL)
return false;
return db_admin_getint(option->name) > 0;
}
char *
settings_option_getstr(struct settings_option *option)
{
if (!option || option->type != SETTINGS_TYPE_STR)
return NULL;
return db_admin_get(option->name);
}
int
settings_option_setint(struct settings_option *option, int value)
{
if (!option || option->type != SETTINGS_TYPE_INT)
return -1;
return db_admin_setint(option->name, value);
}
int
settings_option_setbool(struct settings_option *option, bool value)
{
if (!option || option->type != SETTINGS_TYPE_BOOL)
return -1;
return db_admin_setint(option->name, value);
}
int
settings_option_setstr(struct settings_option *option, const char *value)
{
if (!option || option->type != SETTINGS_TYPE_STR)
return -1;
return db_admin_set(option->name, value);
}

65
src/settings.h Normal file
View File

@ -0,0 +1,65 @@
#ifndef __SETTINGS_H__
#define __SETTINGS_H__
#include <stdbool.h>
enum settings_type {
SETTINGS_TYPE_INT,
SETTINGS_TYPE_BOOL,
SETTINGS_TYPE_STR,
SETTINGS_TYPE_CATEGORY,
};
struct settings_option {
const char *name;
enum settings_type type;
};
struct settings_category {
const char *name;
struct settings_option *options;
int count_options;
};
int
settings_categories_count();
struct settings_category *
settings_category_get_byindex(int index);
struct settings_category *
settings_category_get(const char *name);
int
settings_option_count(struct settings_category *category);
struct settings_option *
settings_option_get_byindex(struct settings_category *category, int index);
struct settings_option *
settings_option_get(struct settings_category *category, const char *name);
int
settings_option_getint(struct settings_option *option);
bool
settings_option_getbool(struct settings_option *option);
char *
settings_option_getstr(struct settings_option *option);
int
settings_option_setint(struct settings_option *option, int value);
int
settings_option_setbool(struct settings_option *option, bool value);
int
settings_option_setstr(struct settings_option *option, const char *value);
#endif /* __SETTINGS_H__ */