myMPD
Internal API documentation
Loading...
Searching...
No Matches
stickerdb.c File Reference

MPD sticker functions. More...

#include "compile_time.h"
#include "src/mympd_client/stickerdb.h"
#include "dist/rax/rax.h"
#include "src/lib/convert.h"
#include "src/lib/json/json_rpc.h"
#include "src/lib/log.h"
#include "src/lib/mympd_state.h"
#include "src/lib/sds_extras.h"
#include "src/lib/sticker.h"
#include "src/lib/utility.h"
#include "src/mympd_api/requests.h"
#include <inttypes.h>
#include <limits.h>
#include <poll.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for stickerdb.c:

Functions

static bool get_sticker_types (struct t_stickerdb_state *stickerdb)
 
static bool sticker_search_add_value_constraint (struct t_stickerdb_state *stickerdb, enum mpd_sticker_operator op, const char *value)
 
static bool sticker_search_add_sort (struct t_stickerdb_state *stickerdb, enum mpd_sticker_sort sort, bool desc)
 
static bool sticker_search_add_window (struct t_stickerdb_state *stickerdb, unsigned start, unsigned end)
 
static struct t_stickerget_sticker_all (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, struct t_sticker *sticker, bool user_defined)
 
static sds get_sticker_value (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, const char *name)
 
static int64_t get_sticker_int64 (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, const char *name)
 
static bool set_sticker_value (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, const char *name, const char *value)
 
static bool set_sticker_int64 (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, const char *name, int64_t value)
 
static bool dec_sticker (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, const char *name, unsigned value)
 
static bool inc_sticker (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, const char *name, unsigned value)
 
static bool remove_sticker (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, const char *name)
 
static bool stickerdb_connect_mpd (struct t_stickerdb_state *stickerdb)
 
static bool check_sticker_support (struct t_stickerdb_state *stickerdb)
 
bool stickerdb_connect (struct t_stickerdb_state *stickerdb)
 
void stickerdb_disconnect (struct t_stickerdb_state *stickerdb)
 
bool stickerdb_idle (struct t_stickerdb_state *stickerdb)
 
bool stickerdb_enter_idle (struct t_stickerdb_state *stickerdb)
 
bool stickerdb_exit_idle (struct t_stickerdb_state *stickerdb)
 
bool stickerdb_check_error_and_recover (struct t_stickerdb_state *stickerdb, const char *command)
 
sds stickerdb_get_batch (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, const char *name)
 
sds stickerdb_get (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, const char *name)
 
int64_t stickerdb_get_int64_batch (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, const char *name)
 
int64_t stickerdb_get_int64 (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, const char *name)
 
bool stickerdb_get_names (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, struct t_list *sticker_names)
 
struct t_stickerstickerdb_get_all_batch (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, struct t_sticker *sticker, bool user_defined)
 
struct t_stickerstickerdb_get_all (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, struct t_sticker *sticker, bool user_defined)
 
rax * stickerdb_find_stickers_by_name (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *name)
 
rax * stickerdb_find_stickers_by_name_value (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *name, enum mpd_sticker_operator op, const char *value)
 
struct t_liststickerdb_find_stickers_sorted (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *baseuri, const char *name, enum mpd_sticker_operator op, const char *value, enum mpd_sticker_sort sort, bool sort_desc, unsigned start, unsigned end)
 
void stickerdb_free_find_result (rax *stickers)
 
bool stickerdb_set (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, const char *name, const char *value)
 
bool stickerdb_set_int64 (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, const char *name, int64_t value)
 
bool stickerdb_dec (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, const char *name, unsigned value)
 
bool stickerdb_inc (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, const char *name, unsigned value)
 
bool stickerdb_set_elapsed (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, time_t elapsed)
 
bool stickerdb_inc_set (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, enum mympd_sticker_names name_inc, enum mympd_sticker_names name_timestamp, time_t timestamp)
 
bool stickerdb_inc_play_count (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, time_t timestamp)
 
bool stickerdb_inc_skip_count (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri)
 
bool stickerdb_set_like (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, enum sticker_like value)
 
bool stickerdb_set_rating (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, int value)
 
bool stickerdb_remove (struct t_stickerdb_state *stickerdb, enum mympd_sticker_type type, const char *uri, const char *name)
 
bool check_get_sticker (bool featSticker, const struct t_stickers *stickers)
 

Detailed Description

MPD sticker functions.

Function Documentation

◆ check_get_sticker()

bool check_get_sticker ( bool  featSticker,
const struct t_stickers stickers 
)

Checks if stickers should be fetched

Parameters
featStickerFlag indicating enabled sticker support
stickersPointer to t_stickers struct
Returns
bool true if stickers should be fetched, else false

◆ check_sticker_support()

static bool check_sticker_support ( struct t_stickerdb_state stickerdb)
static

Check for sticker support

Parameters
stickerdbpointer to the stickerdb state
Returns
true if stickers are supported, else false

◆ dec_sticker()

static bool dec_sticker ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
const char *  name,
unsigned  value 
)
static

Decrements a sticker

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
namesticker name
valuevalute to decrement
Returns
true on success, else false

◆ get_sticker_all()

static struct t_sticker * get_sticker_all ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
struct t_sticker sticker,
bool  user_defined 
)
static

Initializes the sticker struct and gets all stickers. You must manage the idle state manually.

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
stickerpointer to t_sticker struct to populate
user_definedget user defines stickers?
Returns
the initialized and populated sticker struct

◆ get_sticker_int64()

int64_t get_sticker_int64 ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
const char *  name 
)
static

Gets an int64t value from sticker

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
namesticker name
Returns
number

◆ get_sticker_types()

static bool get_sticker_types ( struct t_stickerdb_state stickerdb)
static

Populates the sticker_types list

Parameters
stickerdbpointer to the stickerdb state
Returns
true on success, else false

◆ get_sticker_value()

static sds get_sticker_value ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
const char *  name 
)
static

Gets a string value from sticker

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
namesticker name
Returns
string

◆ inc_sticker()

static bool inc_sticker ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
const char *  name,
unsigned  value 
)
static

Increments a sticker

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
namesticker name
valuevalute to increment
Returns
true on success, else false

◆ remove_sticker()

static bool remove_sticker ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
const char *  name 
)
static

Removes a sticker

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
namesticker name
Returns
true on success, else false

◆ set_sticker_int64()

static bool set_sticker_int64 ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
const char *  name,
int64_t  value 
)
static

Sets an int64_t sticker value

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
namesticker name
valuenumber to set
Returns
true on success, else false

◆ set_sticker_value()

static bool set_sticker_value ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
const char *  name,
const char *  value 
)
static

Sets a sticker string value

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
namesticker name
valuestring to set
Returns
true on success, else false

◆ sticker_search_add_sort()

static bool sticker_search_add_sort ( struct t_stickerdb_state stickerdb,
enum mpd_sticker_sort  sort,
bool  desc 
)
static

Adds a mpd sticker sort definition, if supported by MPD

Parameters
stickerdbpointer to the stickerdb state
sortmpd sticker sort type
descsort descending?
Returns
true on success, else false

◆ sticker_search_add_value_constraint()

static bool sticker_search_add_value_constraint ( struct t_stickerdb_state stickerdb,
enum mpd_sticker_operator  op,
const char *  value 
)
static

Adds a mpd sticker search value constraint if value is not NULL

Parameters
stickerdbpointer to the stickerdb state
opcompare operator
valuesticker value to search
Returns
true on success, else false

◆ sticker_search_add_window()

static bool sticker_search_add_window ( struct t_stickerdb_state stickerdb,
unsigned  start,
unsigned  end 
)
static

Adds a mpd sticker window definition, if supported by MPD

Parameters
stickerdbpointer to the stickerdb state
startwindow start (including)
endwindow end (excluding)
Returns
true on success, else false

◆ stickerdb_check_error_and_recover()

bool stickerdb_check_error_and_recover ( struct t_stickerdb_state stickerdb,
const char *  command 
)

Calls mpd_response_finish and checks for an mpd error and tries to recover.

Parameters
stickerdbpointer to the stickerdb state
commandcommand to check for the error
Returns
true on success, else false

◆ stickerdb_connect()

bool stickerdb_connect ( struct t_stickerdb_state stickerdb)

This function connects to mpd sticker instance on demand or exits the idle mode

Parameters
stickerdbpointer to the stickerdb state
Returns
true on success, else false

◆ stickerdb_connect_mpd()

static bool stickerdb_connect_mpd ( struct t_stickerdb_state stickerdb)
static

Creates the connection to MPD

Parameters
stickerdbpointer to the stickerdb state
Returns
true on success, else false

◆ stickerdb_dec()

bool stickerdb_dec ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
const char *  name,
unsigned  value 
)

Decrements a sticker

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
namesticker name
valuevalue to decrement
Returns
true on success, else false

◆ stickerdb_disconnect()

void stickerdb_disconnect ( struct t_stickerdb_state stickerdb)

Disconnects from MPD

Parameters
stickerdbpointer to stickerdb state

◆ stickerdb_enter_idle()

bool stickerdb_enter_idle ( struct t_stickerdb_state stickerdb)

Enters the idle mode

Parameters
stickerdbpointer to the stickerdb state
Returns
true on success, else false

◆ stickerdb_exit_idle()

bool stickerdb_exit_idle ( struct t_stickerdb_state stickerdb)

Exits the idle mode, ignoring all idle events

Parameters
stickerdbpointer to the stickerdb state
Returns
true on success, else false

◆ stickerdb_find_stickers_by_name()

rax * stickerdb_find_stickers_by_name ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  name 
)

Gets all stickers by name

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
namesticker name
Returns
newly allocated radix tree or NULL on error

◆ stickerdb_find_stickers_by_name_value()

rax * stickerdb_find_stickers_by_name_value ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  name,
enum mpd_sticker_operator  op,
const char *  value 
)

Gets all stickers by name and value

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
namesticker name
opcompare operator: MPD_STICKER_OP_EQ, MPD_STICKER_OP_GT, MPD_STICKER_OP_LT
valuesticker value
Returns
newly allocated radix tree or NULL on error

◆ stickerdb_find_stickers_sorted()

struct t_list * stickerdb_find_stickers_sorted ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  baseuri,
const char *  name,
enum mpd_sticker_operator  op,
const char *  value,
enum mpd_sticker_sort  sort,
bool  sort_desc,
unsigned  start,
unsigned  end 
)

Gets a sorted list of stickers by name and value

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
baseuribaseuri for search
namesticker name
opmpd sticker compare operator
valuesticker value or NULL to get all stickers with this name
sortsticker sort type
sort_descsort descending?
startwindow start (including)
endwindow end (excluding), use UINT_MAX for open end
Returns
new allocated struct t_list

◆ stickerdb_free_find_result()

void stickerdb_free_find_result ( rax *  stickers)

Frees the sticker find result

Parameters
stickerspointer to stickers rax tree

◆ stickerdb_get()

sds stickerdb_get ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
const char *  name 
)

Gets a sticker.

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
namesticker name
Returns
pointer to sticker value

◆ stickerdb_get_all()

struct t_sticker * stickerdb_get_all ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
struct t_sticker sticker,
bool  user_defined 
)

Gets all stickers

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
stickerpointer to t_sticker struct to populate
user_definedget user defines stickers?
Returns
Initialized and populated sticker struct or NULL on error

◆ stickerdb_get_all_batch()

struct t_sticker * stickerdb_get_all_batch ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
struct t_sticker sticker,
bool  user_defined 
)

Gets all stickers. You must manage the idle state manually.

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
stickerpointer to t_sticker struct to populate
user_definedget user defines stickers?
Returns
Initialized and populated sticker struct or NULL on error

◆ stickerdb_get_batch()

sds stickerdb_get_batch ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
const char *  name 
)

Gets a sticker. You must manage the idle state manually.

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
namesticker name
Returns
pointer to sticker value

◆ stickerdb_get_int64()

int64_t stickerdb_get_int64 ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
const char *  name 
)

Gets an int64_t value sticker.

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
namesticker name
Returns
sticker value or 0 on error

◆ stickerdb_get_int64_batch()

int64_t stickerdb_get_int64_batch ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
const char *  name 
)

Gets an int64_t value sticker. You must manage the idle state manually.

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
namesticker name
Returns
sticker value or 0 on error

◆ stickerdb_get_names()

bool stickerdb_get_names ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
struct t_list sticker_names 
)

Gets all sticker names by type

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
sticker_namesList to populate
Returns
true on success, else false

◆ stickerdb_idle()

bool stickerdb_idle ( struct t_stickerdb_state stickerdb)

Discards waiting idle events for the stickerdb connection. This prevents the connection to timeout.

Parameters
stickerdbpointer to the stickerdb state
Returns
true on success, else false

◆ stickerdb_inc()

bool stickerdb_inc ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
const char *  name,
unsigned  value 
)

Increments a sticker

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
namesticker name
valuevalue to increment
Returns
true on success, else false

◆ stickerdb_inc_play_count()

bool stickerdb_inc_play_count ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
time_t  timestamp 
)

Increments the myMPD play count and sets the last played time

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
timestamptimestamp to set
Returns
true on success, else false

◆ stickerdb_inc_set()

bool stickerdb_inc_set ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
enum mympd_sticker_names  name_inc,
enum mympd_sticker_names  name_timestamp,
time_t  timestamp 
)

Increments a counter and sets a timestamp

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
name_incsticker name for counter
name_timestampsticker name for timestamp
timestamptimestamp to set
Returns
true on success, else false

◆ stickerdb_inc_skip_count()

bool stickerdb_inc_skip_count ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri 
)

Increments the myMPD skip count and sets the last skipped time

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
Returns
true on success, else false

◆ stickerdb_remove()

bool stickerdb_remove ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
const char *  name 
)

Removes a sticker

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
namesticker name
Returns
bool true on success, else false

◆ stickerdb_set()

bool stickerdb_set ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
const char *  name,
const char *  value 
)

Sets a sticker

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
namesticker name
valuesticker value
Returns
true on success, else false

◆ stickerdb_set_elapsed()

bool stickerdb_set_elapsed ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
time_t  elapsed 
)

Sets the myMPD elapsed timestamp sticker

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
elapsedtimestamp
Returns
true on success, else false

◆ stickerdb_set_int64()

bool stickerdb_set_int64 ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
const char *  name,
int64_t  value 
)

Sets a sticker

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
namesticker name
valuesticker value
Returns
true on success, else false

◆ stickerdb_set_like()

bool stickerdb_set_like ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
enum sticker_like  value 
)

Sets the myMPD like sticker

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
value0 = hate, 1 = neutral, 2 = like
Returns
true on success, else false

◆ stickerdb_set_rating()

bool stickerdb_set_rating ( struct t_stickerdb_state stickerdb,
enum mympd_sticker_type  type,
const char *  uri,
int  value 
)

Sets the myMPD rating sticker

Parameters
stickerdbpointer to the stickerdb state
typeMPD sticker type
uristicker uri
value0 - 10 stars
Returns
true on success, else false