From f53c20296a003a25b59d82dd400ffa9f707d8d88 Mon Sep 17 00:00:00 2001 From: jcorporation Date: Sun, 14 Oct 2018 23:03:56 +0100 Subject: [PATCH] Feat: configurable max_elements_per_page #47 --- contrib/mympd.conf.dist | 3 ++ src/mpd_client.c | 79 +++++++++++++++-------------------------- src/mpd_client.h | 11 ++++-- src/mympd.c | 10 +++++- 4 files changed, 50 insertions(+), 53 deletions(-) diff --git a/contrib/mympd.conf.dist b/contrib/mympd.conf.dist index 89ead6e..9df4d1c 100644 --- a/contrib/mympd.conf.dist +++ b/contrib/mympd.conf.dist @@ -37,3 +37,6 @@ taglist = Artist,Album,AlbumArtist,Title,Track,Genre,Date,Composer,Performer #Enable smart playlists smartpls = true + +#Elements per page for pagination, max: 400 +max_elements_per_page = 100 diff --git a/src/mpd_client.c b/src/mpd_client.c index e2d4914..1b5e11a 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -814,11 +814,8 @@ int mympd_get_updatedb_state(char *buffer) { struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE); status = mpd_run_status(mpd.conn); - if (!status) { - printf("MPD mpd_run_status: %s\n", mpd_connection_get_error_message(mpd.conn)); - mpd.conn_state = MPD_FAILURE; - return 0; - } + if (!status) + RETURN_ERROR_AND_RECOVER("replay_gain_status"); update_id = mpd_status_get_update_id(status); #ifdef DEBUG fprintf(stderr, "Update database ID: %d\n", update_id); @@ -830,8 +827,7 @@ int mympd_get_updatedb_state(char *buffer) { mpd_status_free(status); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -1142,8 +1138,7 @@ int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, int * *queue_length = mpd_status_get_queue_length(status); mpd_status_free(status); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -1164,8 +1159,7 @@ int mympd_put_volume(char *buffer) { ); mpd_status_free(status); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -1175,8 +1169,7 @@ int mympd_put_welcome(char *buffer) { len = json_printf(&out, "{type: welcome, data: {version: %Q}}", MYMPD_VERSION); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -1264,7 +1257,7 @@ int mympd_put_settings(char *buffer) { config.stickers, config.mixramp, config.smartpls, - MAX_ELEMENTS_PER_PAGE, + config.max_elements_per_page, replaygain, mympd_state.notificationWeb, mympd_state.notificationPage, @@ -1285,8 +1278,7 @@ int mympd_put_settings(char *buffer) { len += json_printf(&out, "]}}"); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -1317,8 +1309,7 @@ int mympd_put_outputs(char *buffer) { len += json_printf(&out,"]}}"); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -1409,8 +1400,7 @@ int mympd_put_current_song(char *buffer) { len += json_printf(&out, "}}"); mpd_song_free(song); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -1459,8 +1449,7 @@ int mympd_put_songdetails(char *buffer, char *uri) { } len += json_printf(&out, "}}"); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -1476,7 +1465,7 @@ int mympd_put_queue(char *buffer, unsigned int offset, unsigned *queue_version, if (!status) RETURN_ERROR_AND_RECOVER("mpd_run_status"); - if (!mpd_send_list_queue_range_meta(mpd.conn, offset, offset + MAX_ELEMENTS_PER_PAGE)) + if (!mpd_send_list_queue_range_meta(mpd.conn, offset, offset + config.max_elements_per_page)) RETURN_ERROR_AND_RECOVER("mpd_send_list_queue_meta"); len = json_printf(&out, "{type: queue, data: ["); @@ -1516,8 +1505,7 @@ int mympd_put_queue(char *buffer, unsigned int offset, unsigned *queue_version, *queue_length = mpd_status_get_queue_length(status); mpd_status_free(status); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -1541,7 +1529,7 @@ int mympd_put_browse(char *buffer, char *path, unsigned int offset, char *filter const struct mpd_song *song; const struct mpd_directory *dir; entity_count ++; - if (entity_count > offset && entity_count <= offset + MAX_ELEMENTS_PER_PAGE) { + if (entity_count > offset && entity_count <= offset + config.max_elements_per_page) { switch (mpd_entity_get_type(entity)) { case MPD_ENTITY_TYPE_UNKNOWN: entity_count --; @@ -1637,8 +1625,7 @@ int mympd_put_browse(char *buffer, char *path, unsigned int offset, char *filter filter ); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -1663,7 +1650,7 @@ int mympd_put_db_tag(char *buffer, unsigned int offset, char *mpdtagtype, char * len = json_printf(&out, "{type: listDBtags, data: ["); while ((pair = mpd_recv_pair_tag(mpd.conn, mpd_tag_name_parse(mpdtagtype))) != NULL) { entity_count ++; - if (entity_count > offset && entity_count <= offset + MAX_ELEMENTS_PER_PAGE) { + if (entity_count > offset && entity_count <= offset + config.max_elements_per_page) { if (strcmp(pair->value, "") == 0) { entity_count --; } @@ -1694,8 +1681,7 @@ int mympd_put_db_tag(char *buffer, unsigned int offset, char *mpdtagtype, char * filter ); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -1724,7 +1710,7 @@ int mympd_put_songs_in_album(char *buffer, char *album, char *search, char *tag) while ((song = mpd_recv_song(mpd.conn)) != NULL) { entity_count ++; - if (entity_count <= MAX_ELEMENTS_PER_PAGE) { + if (entity_count <= config.max_elements_per_page) { if (entities_returned ++) len += json_printf(&out, ", "); else { @@ -1753,8 +1739,7 @@ int mympd_put_songs_in_album(char *buffer, char *album, char *search, char *tag) } free(albumartist); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -1775,7 +1760,7 @@ int mympd_put_playlists(char *buffer, unsigned int offset, char *filter) { while ((pl = mpd_recv_playlist(mpd.conn)) != NULL) { entity_count ++; - if (entity_count > offset && entity_count <= offset + MAX_ELEMENTS_PER_PAGE) { + if (entity_count > offset && entity_count <= offset + config.max_elements_per_page) { plpath = mpd_playlist_get_path(pl); if (strncmp(filter,"-",1) == 0 || strncasecmp(filter, plpath, 1) == 0 || (strncmp(filter, "0", 1) == 0 && isalpha(*plpath) == 0 ) @@ -1806,8 +1791,7 @@ int mympd_put_playlists(char *buffer, unsigned int offset, char *filter) { entities_returned ); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -1829,7 +1813,7 @@ int mympd_put_playlist_list(char *buffer, char *uri, unsigned int offset, char * while ((entity = mpd_recv_entity(mpd.conn)) != NULL) { const struct mpd_song *song; entity_count ++; - if (entity_count > offset && entity_count <= offset + MAX_ELEMENTS_PER_PAGE) { + if (entity_count > offset && entity_count <= offset + config.max_elements_per_page) { song = mpd_entity_get_song(entity); entityName = mympd_get_tag(song, MPD_TAG_TITLE); if (strncmp(filter, "-", 1) == 0 || strncasecmp(filter, entityName, 1) == 0 || @@ -1865,8 +1849,7 @@ int mympd_put_playlist_list(char *buffer, char *uri, unsigned int offset, char * smartpls ); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -1894,7 +1877,7 @@ int mympd_search(char *buffer, char *searchstr, char *filter, char *plist, unsig if (strcmp(plist, "") == 0) { while ((song = mpd_recv_song(mpd.conn)) != NULL) { entity_count ++; - if (entity_count > offset && entity_count <= offset + MAX_ELEMENTS_PER_PAGE) { + if (entity_count > offset && entity_count <= offset + config.max_elements_per_page) { if (entities_returned ++) len += json_printf(&out, ", "); len += json_printf(&out, "{type: song, uri: %Q, album: %Q, artist: %Q, duration: %d, title: %Q, name: %Q}", @@ -1923,8 +1906,7 @@ int mympd_search(char *buffer, char *searchstr, char *filter, char *plist, unsig else len = json_printf(&out, "{type: result, data: ok}"); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -1952,8 +1934,7 @@ int mympd_queue_crop(char *buffer) { mpd_status_free(status); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -1984,7 +1965,7 @@ int mympd_search_queue(char *buffer, char *mpdtagtype, unsigned int offset, char while ((song = mpd_recv_song(mpd.conn)) != NULL) { entity_count ++; - if (entity_count > offset && entity_count <= offset + MAX_ELEMENTS_PER_PAGE) { + if (entity_count > offset && entity_count <= offset + config.max_elements_per_page) { if (entities_returned ++) len += json_printf(&out, ", "); len += json_printf(&out, "{type: song, id: %d, pos: %d, album: %Q, artist: %Q, duration: %d, title: %Q}", @@ -2007,8 +1988,7 @@ int mympd_search_queue(char *buffer, char *mpdtagtype, unsigned int offset, char ); } - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } @@ -2036,8 +2016,7 @@ int mympd_put_stats(char *buffer) { ); mpd_stats_free(stats); - if (len > MAX_SIZE) - printf("Buffer truncated\n"); + CHECK_RETURN_LEN(); return len; } diff --git a/src/mpd_client.h b/src/mpd_client.h index 5508e06..0b5992d 100644 --- a/src/mpd_client.h +++ b/src/mpd_client.h @@ -44,8 +44,14 @@ mpd.conn_state = MPD_FAILURE; \ } while (0) -#define MAX_SIZE 1024 * 100 -#define MAX_ELEMENTS_PER_PAGE 100 +#define CHECK_RETURN_LEN() do { \ + if (len > MAX_SIZE) \ + printf("Buffer truncated: %d, %d\n", len, MAX_SIZE); \ + return len; \ +} while (0) + +#define MAX_SIZE 2048 * 400 +#define MAX_ELEMENTS_PER_PAGE 400 #define GEN_ENUM(X) X, #define GEN_STR(X) #X, @@ -161,6 +167,7 @@ typedef struct { const char* taglist; bool smartpls; const char* varlibdir; + long max_elements_per_page; } t_config; t_config config; diff --git a/src/mympd.c b/src/mympd.c index 94b9769..30d4eff 100644 --- a/src/mympd.c +++ b/src/mympd.c @@ -173,7 +173,14 @@ static int inihandler(void* user, const char* section, const char* name, const c else p_config->mixramp = false; else if (MATCH("taglist")) - p_config->taglist = strdup(value); + p_config->taglist = strdup(value); + else if (MATCH("max_elements_per_page")) { + p_config->max_elements_per_page = strtol(value, &crap, 10); + if (p_config->max_elements_per_page > MAX_ELEMENTS_PER_PAGE) { + printf("Setting max_elements_per_page to maximal value %d", MAX_ELEMENTS_PER_PAGE); + p_config->max_elements_per_page = MAX_ELEMENTS_PER_PAGE; + } + } else return 0; /* unknown section/name, error */ @@ -267,6 +274,7 @@ int main(int argc, char **argv) { config.mixramp = true; config.taglist = "Artist,Album,AlbumArtist,Title,Track,Genre,Date,Composer,Performer"; config.smartpls = true; + config.max_elements_per_page = 100; mpd.timeout = 3000; mpd.last_update_sticker_song_id = -1;