From 44dd0e2985672ddcb222110d3aa8297d38e07003 Mon Sep 17 00:00:00 2001 From: jcorporation Date: Wed, 26 Sep 2018 23:10:51 +0100 Subject: [PATCH] Feat: add backend code for manage searches as smart playlists #38 --- src/mpd_client.c | 57 +++++++++++++++++++++++++++++++++++++++++++++--- src/mpd_client.h | 5 +++++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/mpd_client.c b/src/mpd_client.c index e0a06cb..c69077b 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -191,6 +191,24 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) { else n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"Smart Playlists update failed\"}"); break; + case MPD_API_SMARTPLS_SAVE: + je = json_scanf(msg.p, msg.len, "{data: {playlist: %Q, tag: %Q, searchstr: %Q}}", &p_charbuf1, &p_charbuf2, &p_charbuf3); + if (je == 3) { + mympd_smartpls_save(p_charbuf1, p_charbuf2, p_charbuf3); + free(p_charbuf1); + free(p_charbuf2); + free(p_charbuf3); + n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}"); + } + break; + case MPD_API_SMARTPLS_RM: + je = json_scanf(msg.p, msg.len, "{data: {playlist: %Q}}", &p_charbuf1); + if (je == 3) { + mympd_smartpls_rm(p_charbuf1); + free(p_charbuf1); + n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}"); + } + break; case MPD_API_PLAYER_PAUSE: mpd_run_toggle_pause(mpd.conn); n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}"); @@ -1882,13 +1900,32 @@ void mympd_disconnect() { mympd_idle(NULL, 0); } +int mympd_smartpls_save(char *playlist, char *tag, char *searchstr) { + char tmp_file[400]; + char pl_file[400]; + snprintf(tmp_file, 400, "/var/lib/mympd/tmp/%s", playlist); + snprintf(pl_file, 400, "/var/lib/mympd/smartpls/%s", playlist); + json_fprintf(tmp_file, "{type: search, tag: %Q, searchstr: %Q}", tag, searchstr); + rename(tmp_file, pl_file); + mympd_smartpls_update_search(playlist, tag, searchstr); + return 0; +} + +int mympd_smartpls_rm(char *playlist) { + char pl_file[400]; + snprintf(pl_file, 400, "/var/lib/mympd/smartpls/%s", playlist); + unlink(pl_file); + mpd_run_rm(mpd.conn, playlist); + return 0; +} + int mympd_smartpls_update_all() { DIR *dir; struct dirent *ent; char *smartpltype; char filename[400]; int je; - char *p_charbuf1; + char *p_charbuf1, *p_charbuf2; int int_buf1, int_buf2; if (!config.smartpls) @@ -1896,7 +1933,7 @@ int mympd_smartpls_update_all() { if ((dir = opendir ("/var/lib/mympd/smartpls")) != NULL) { while ((ent = readdir(dir)) != NULL) { - if (strncmp (ent->d_name, ".", 1) == 0) + if (strncmp(ent->d_name, ".", 1) == 0) continue; snprintf(filename, 400, "/var/lib/mympd/smartpls/%s", ent->d_name); char *content = json_fread(filename); @@ -1921,7 +1958,14 @@ int mympd_smartpls_update_all() { printf("Can't parse file %s\n", filename); } else if (strcmp(smartpltype, "search") == 0) { - //to be implemented + je = json_scanf(content, strlen(content), "{tag: %Q, searchstr: %Q}", &p_charbuf1, &p_charbuf2); + if (je == 2) { + mympd_smartpls_update_search(ent->d_name, p_charbuf1, p_charbuf2); + free(p_charbuf1); + free(p_charbuf2); + } + else + printf("Can't parse file %s\n", filename); } free(smartpltype); } @@ -1960,6 +2004,13 @@ int mympd_smartpls_clear(char *playlist) { return 0; } +int mympd_smartpls_update_search(char *playlist, char *tag, char *searchstr) { + mympd_smartpls_clear(playlist); + mympd_search(mpd.buf, searchstr, tag, playlist, 0); + printf("Updated %s\n", playlist); + return 0; +} + int mympd_smartpls_update(char *sticker, char *playlist, int maxentries) { struct mpd_pair *pair; char *uri = NULL; diff --git a/src/mpd_client.h b/src/mpd_client.h index 4607281..195cd9f 100644 --- a/src/mpd_client.h +++ b/src/mpd_client.h @@ -73,6 +73,8 @@ X(MPD_API_PLAYLIST_LIST) \ X(MPD_API_PLAYLIST_CONTENT_LIST) \ X(MPD_API_SMARTPLS_UPDATE) \ + X(MPD_API_SMARTPLS_SAVE) \ + X(MPD_API_SMARTPLS_RM) \ X(MPD_API_DATABASE_SEARCH) \ X(MPD_API_DATABASE_UPDATE) \ X(MPD_API_DATABASE_RESCAN) \ @@ -198,10 +200,13 @@ void mympd_last_played_song_uri(const char *uri); void mympd_last_played_song_id(int song_id); void mympd_get_sticker(const char *uri, t_sticker *sticker); void mympd_jukebox(); +int mympd_smartpls_save(char *playlist, char *tag, char *searchstr); +int mympd_smartpls_rm(char *playlist); int mympd_smartpls_update_all(); int mympd_smartpls_clear(char *playlist); int mympd_smartpls_update(char *sticker, char *playlist, int maxentries); int mympd_smartpls_update_newest(char *playlist, int timerange, int maxentries); +int mympd_smartpls_update_search(char *playlist, char *tag, char *searchstr); int mympd_get_updatedb_state(char *buffer); int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, int *last_song_id, unsigned *queue_version, unsigned *queue_length); int mympd_put_outputs(char *buffer);