From 58d1782cf7542b977bb8ebcd7442817427b309f7 Mon Sep 17 00:00:00 2001 From: jcorporation Date: Thu, 25 Oct 2018 21:03:33 +0100 Subject: [PATCH] Feat: disable playlists and browse database feature if not enabled in mpd #68 #69 --- htdocs/index.html | 30 +++++------ htdocs/js/mympd.js | 123 +++++++++++++++++++++++++++++++-------------- src/mpd_client.c | 91 ++++++++++++++++++++++++--------- src/mpd_client.h | 14 ++++-- 4 files changed, 178 insertions(+), 80 deletions(-) diff --git a/htdocs/index.html b/htdocs/index.html index 898c507..24f94b4 100644 --- a/htdocs/index.html +++ b/htdocs/index.html @@ -28,7 +28,7 @@ Local Player Settings @@ -81,7 +81,7 @@
Playback -
+
@@ -90,10 +90,12 @@

- Artist -

- Album -

+
+ Artist +

+ Album +

+
-
+
@@ -199,10 +201,10 @@
@@ -375,7 +377,7 @@
@@ -460,8 +462,8 @@
@@ -580,7 +582,7 @@
Invalid uri
-
+
diff --git a/htdocs/js/mympd.js b/htdocs/js/mympd.js index 3b083c3..c42f913 100644 --- a/htdocs/js/mympd.js +++ b/htdocs/js/mympd.js @@ -943,6 +943,24 @@ function toggleBtn(btn, state) { b.classList.remove('active'); } +function filterCols(x) { + var tags = settings.tags.slice(); + if (settings.featTags == false) + tags.push('Title'); + tags.push('Duration'); + if (x == 'colsQueue' || x == 'colsBrowsePlaylistsDetail') + tags.push('Pos'); + if (x == 'colsBrowseFilesystem') + tags.push('Type'); + + var cols = []; + for (var i = 0; i < settings[x].length; i++) { + if (tags.includes(settings[x][i])) + cols.push(settings[x][i]); + } + settings[x] = cols; +} + function parseSettings(obj) { toggleBtn('btnRandom', obj.data.random); toggleBtn('btnConsume', obj.data.consume); @@ -995,18 +1013,21 @@ function parseSettings(obj) { toggleBtn('btnnotifyPage', obj.data.notificationPage); - var stickerEls = document.getElementsByClassName('stickers'); - var stickerElsLen = stickerEls.length; - var displayStickers = obj.data.stickers == true ? '' : 'none'; - for (var i = 0; i < stickerElsLen; i++) - stickerEls[i].style.display = displayStickers; - - var smartplsEls = document.getElementsByClassName('smartpls'); - var smartplsElsLen = smartplsEls.length; - var displaySmartpls = obj.data.smartpls == true ? '' : 'none'; - for (var i = 0; i < smartplsElsLen; i++) - smartplsEls[i].style.display = displaySmartpls; + var features = ["featStickers", "featSmartpls", "featPlaylists", "featTags"]; + for (var j = 0; j < features.length; j++) { + var Els = document.getElementsByClassName(features[j]); + var ElsLen = Els.length; + var displayEl = obj.data[features[j]] == true ? '' : 'none'; + for (var i = 0; i < ElsLen; i++) + Els[i].style.display = displayEl; + } + + if (obj.data.featTags == false) { + app.apps.Browse.active = 'Filesystem'; + app.apps.Search.state = '0/filename/'; + } + if (obj.data.mixramp == true) document.getElementsByClassName('mixramp')[0].style.display = ''; else @@ -1025,8 +1046,17 @@ function parseSettings(obj) { settings = obj.data; - playlistEl = 'selectJukeboxPlaylist'; - sendAPI({"cmd": "MPD_API_PLAYLIST_LIST", "data": {"offset": 0, "filter": "-"}}, getAllPlaylists); + if (obj.data.featPlaylists) { + playlistEl = 'selectJukeboxPlaylist'; + sendAPI({"cmd": "MPD_API_PLAYLIST_LIST", "data": {"offset": 0, "filter": "-"}}, getAllPlaylists); + } else { + document.getElementById('selectJukeboxPlaylist').innerHTML = ''; + } + + filterCols('colsSearch'); + filterCols('colsQueue'); + filterCols('colsBrowsePlaylistsDetail'); + filterCols('colsBrowseFilesystem'); settings.mpdstream = 'http://'; if (settings.mpdhost == '127.0.0.1' || settings.mpdhost == 'localhost') @@ -1069,9 +1099,13 @@ function parseSettings(obj) { function setCols(table) { var tagChks = ''; var tags = settings.tags.slice(); + if (settings.featTags == false) + tags.push('Title'); tags.push('Duration'); if (table == 'Queue' || table == 'BrowsePlaylistsDetail') tags.push('Pos'); + if (table == 'BrowseFilesystem') + tags.push('Type'); for (var i = 0; i < tags.length; i++) { tagChks += '
' + @@ -1086,11 +1120,13 @@ function setCols(table) { var heading = ''; for (var i = 0; i < settings['cols' + table].length; i++) { - var h = settings['cols' + table][i]; - heading += ''; - if (h == 'Track' || h == 'Pos') - h = '#'; - heading += h + ''; +// if (tags.includes(settings['cols' + table][i])) { + var h = settings['cols' + table][i]; + heading += ''; + if (h == 'Track' || h == 'Pos') + h = '#'; + heading += h + ''; +// } } heading += ''; document.getElementById(table + 'List').getElementsByTagName('tr')[0].innerHTML = heading; @@ -1183,7 +1219,6 @@ function setCounter(currentSongId, totalTime, elapsedTime) { if (!posTd.classList.contains('material-icons')) { posTd.classList.add('material-icons'); posTd.innerText = 'play_arrow'; - console.log('A'); } } tr.classList.add('font-weight-bold'); @@ -1377,9 +1412,23 @@ function parseFilesystem(obj) { switch(obj.data[i].Type) { case 'dir': - row.innerHTML = 'folder_open' + - '' + obj.data[i].name + '' + - 'playlist_add'; + case 'smartpls': + case 'plist': + var tds = ''; + for (var c = 0; c < settings['cols' + list].length; c++) { + tds += ''; + if (settings['cols' + list][c] == 'Type') { + if (obj.data[i].Type == 'dir') + tds += 'folder_open'; + else + tds += 'list'; + } + else if (settings['cols' + list][c] == 'Title') + tds += obj.data[i].name; + tds += ''; + } + tds += 'playlist_add'; + row.innerHTML = tds; break; case 'song': var minutes = Math.floor(obj.data[i].Duration / 60); @@ -1397,12 +1446,6 @@ function parseFilesystem(obj) { tds += 'playlist_add'; row.innerHTML = tds; break; - case 'smartpls': - case 'plist': - row.innerHTML = 'list' + - '' + obj.data[i].name + '' + - 'playlist_add'; - break; } if (i < tr.length) tr[i].replaceWith(row); @@ -1801,7 +1844,7 @@ function parseSongDetails(obj) { songDetails += 'Uri' + obj.data.uri + ''; - if (settings.stickers == true) { + if (settings.featStickers == true) { var like = 'not voted'; if (obj.data.like == 0) like = 'thumb_down_alt'; @@ -1943,7 +1986,7 @@ function parseSmartPlaylist(obj) { } else if (obj.data.type == 'sticker') { document.getElementById('saveSmartPlaylistSticker').classList.remove('hide'); - document.getElementById('selectSaveSmartPlaylistSticker').value = obj.data.sticker; + document.getElementById('selectSaveSmartPlaylistSticker').value = obj.data.feat_sticker; document.getElementById('inputSaveSmartPlaylistStickerMaxentries').value = obj.data.maxentries; } else if (obj.data.type == 'newest') { @@ -2033,8 +2076,10 @@ function showAddToPlaylist(uri) { document.getElementById('addToPlaylistLabel').innerText = 'Add Stream'; } modalAddToPlaylist.show(); - playlistEl = 'addToPlaylistPlaylist'; - sendAPI({"cmd": "MPD_API_PLAYLIST_LIST","data": {"offset": 0, "filter": "-"}}, getAllPlaylists); + if (settings.featPlaylists) { + playlistEl = 'addToPlaylistPlaylist'; + sendAPI({"cmd": "MPD_API_PLAYLIST_LIST","data": {"offset": 0, "filter": "-"}}, getAllPlaylists); + } } function addToPlaylist() { @@ -2172,7 +2217,7 @@ function showMenu(el, event) { menu += addMenuItem({"cmd": "appendQueue", "options": [type, uri, name]}, 'Append to queue') + (type == 'song' ? addMenuItem({"cmd": "appendAfterQueue", "options": [type, uri, nextsongpos, name]}, 'Add after current playing song') : '') + addMenuItem({"cmd": "replaceQueue", "options": [type, uri, name]}, 'Replace queue') + - (type != 'plist' && type != 'smartpls' ? addMenuItem({"cmd": "showAddToPlaylist", "options": [uri]}, 'Add to playlist') : '') + + (type != 'plist' && type != 'smartpls' && settings.featPlaylists ? addMenuItem({"cmd": "showAddToPlaylist", "options": [uri]}, 'Add to playlist') : '') + (type == 'song' ? addMenuItem({"cmd": "songDetails", "options": [uri]}, 'Songdetails') : '') + (type == 'plist' || type == 'smartpls' ? addMenuItem({"cmd": "playlistDetails", "options": [uri]}, 'View playlist') : ''); if (app.current.app == 'Search') { @@ -2183,7 +2228,7 @@ function showMenu(el, event) { addMenuItem({"cmd": "appendQueue", "options": [type, baseuri, name]}, 'Append to queue') + addMenuItem({"cmd": "appendAfterQueue", "options": [type, baseuri, nextsongpos, name]}, 'Add after current playing song') + addMenuItem({"cmd": "replaceQueue", "options": [type, baseuri, name]}, 'Replace queue') + - addMenuItem({"cmd": "showAddToPlaylist", "options": [baseuri]}, 'Add to playlist') + + (settings.featPlaylists ? addMenuItem({"cmd": "showAddToPlaylist", "options": [baseuri]}, 'Add to playlist') : '') + '
'; } } @@ -2582,7 +2627,7 @@ function songChange(obj) { document.title = pageTitle; - if (settings.stickers == true) { + if (settings.featStickers == true) { setVoteSongBtns(obj.data.like, obj.data.uri); } @@ -2656,8 +2701,12 @@ function selectTag(btnsEl, desc, setTo) { function addTagList(x, any) { var tagList = ''; var tagBlacklist = ["Title", "MUSICBRAINZ_TRACKID", "Count", "Disc", "Comment", "Name"]; - if (any == true) - tagList += ''; + if (any == true) { + if (settings.featTags == true) + tagList += ''; + else + tagList += ''; + } for (var i = 0; i < settings.tags.length; i++) { if (settings.tags[i] == 'Track') continue; diff --git a/src/mpd_client.c b/src/mpd_client.c index 1a3ca49..9a86674 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -726,11 +726,15 @@ void mympd_mpd_features() { // Defaults mpd.feat_sticker = false; + mpd.feat_playlists = false; + mpd.feat_tags = false; mpd_send_allowed_commands(mpd.conn); while ((pair = mpd_recv_command_pair(mpd.conn)) != NULL) { if (strcmp(pair->value, "sticker") == 0) mpd.feat_sticker = true; + if (strcmp(pair->value, "listplaylists") == 0) + mpd.feat_playlists = true; mpd_return_pair(mpd.conn, pair); } mpd_response_finish(mpd.conn); @@ -743,7 +747,11 @@ void mympd_mpd_features() { printf("Stickers are disabled, disabling smartplaylists\n"); config.smartpls = false; } - + if (mpd.feat_playlists == false && config.smartpls == true) { + printf("Playlists are disabled, disabling smartplaylists\n"); + config.smartpls = false; + } + printf("MPD supported tags: "); list_free(&mpd_tags); mpd_send_list_tag_types(mpd.conn); @@ -753,19 +761,25 @@ void mympd_mpd_features() { mpd_return_pair(mpd.conn, pair); } mpd_response_finish(mpd.conn); - printf("\nmyMPD enabled tags: "); - list_free(&mympd_tags); - token = strtok(str, s); - while (token != NULL) { - if (list_get_value(&mpd_tags, token) == 1) { - list_push(&mympd_tags, token, 1); - printf("%s ", token); + if (mpd_tags.length == 0) { + printf("none\nTags are disabled\n"); + mpd.feat_tags = false; + } + else { + mpd.feat_tags = true; + printf("\nmyMPD enabled tags: "); + token = strtok(str, s); + while (token != NULL) { + if (list_get_value(&mpd_tags, token) == 1) { + list_push(&mympd_tags, token, 1); + printf("%s ", token); + } + token = strtok(NULL, s); } - token = strtok(NULL, s); - } - printf("\n"); - free(str); + printf("\n"); + } + free(str); } void mympd_idle(struct mg_mgr *s, int timeout) { @@ -1015,6 +1029,11 @@ void mympd_jukebox() { if (addSongs < 1) return; + + if (mpd.feat_playlists == false && strcmp(mympd_state.jukeboxPlaylist, "Database") != 0) { + printf("Jukebox: Playlists are disabled\n"); + return; + } srand((unsigned int)time(NULL)); @@ -1025,7 +1044,7 @@ void mympd_jukebox() { //add songs if (strcmp(mympd_state.jukeboxPlaylist, "Database") == 0) { if (!mpd_send_list_all(mpd.conn, "/")) { - LOG_ERROR_AND_RECOVER("mpd_send_list_playlist"); + LOG_ERROR_AND_RECOVER("mpd_send_list_all"); list_free(&add_list); return; } @@ -1067,12 +1086,12 @@ void mympd_jukebox() { else if (mympd_state.jukeboxMode == 2) { //add album if (!mpd_search_db_tags(mpd.conn, MPD_TAG_ALBUM)) { - LOG_ERROR_AND_RECOVER("mpd_send_list_playlist"); + LOG_ERROR_AND_RECOVER("mpd_search_db_tags"); list_free(&add_list); return; } if (!mpd_search_commit(mpd.conn)) { - LOG_ERROR_AND_RECOVER("mpd_send_list_playlist"); + LOG_ERROR_AND_RECOVER("mpd_search_commit"); list_free(&add_list); return; } @@ -1305,8 +1324,8 @@ int mympd_put_settings(char *buffer) { len = json_printf(&out, "{type: settings, data: {" "repeat: %d, single: %d, crossfade: %d, consume: %d, random: %d, " - "mixrampdb: %f, mixrampdelay: %f, mpdhost: %Q, mpdport: %d, passwort_set: %B, " - "streamport: %d, coverimage: %Q, stickers: %B, mixramp: %B, smartpls: %B, maxElementsPerPage: %d, " + "mixrampdb: %f, mixrampdelay: %f, mpdhost: %Q, mpdport: %d, passwort_set: %B, featPlaylists: %B, featTags: %B, " + "streamport: %d, coverimage: %Q, featStickers: %B, mixramp: %B, featSmartpls: %B, maxElementsPerPage: %d, " "replaygain: %Q, notificationWeb: %B, notificationPage: %B, jukeboxMode: %d, jukeboxPlaylist: %Q, jukeboxQueueLength: %d, " "tags: [", mpd_status_get_repeat(status), @@ -1319,6 +1338,8 @@ int mympd_put_settings(char *buffer) { config.mpdhost, config.mpdport, config.mpdpass ? "true" : "false", + mpd.feat_playlists, + mpd.feat_tags, config.streamport, config.coverimage, config.stickers, @@ -1457,7 +1478,10 @@ int mympd_put_current_song(char *buffer) { mpd.song_id, cover ); - PUT_SONG_TAGS(); + if (mpd.feat_tags == true) + PUT_SONG_TAGS(); + else + PUT_MIN_SONG_TAGS(); mpd_response_finish(mpd.conn); @@ -1493,7 +1517,10 @@ int mympd_put_songdetails(char *buffer, char *uri) { song = mpd_entity_get_song(entity); mympd_get_cover(uri, cover, 500); len += json_printf(&out, "cover: %Q, ", cover); - PUT_SONG_TAGS(); + if (mpd.feat_tags == true) + PUT_SONG_TAGS(); + else + PUT_MIN_SONG_TAGS(); mpd_entity_free(entity); } mpd_response_finish(mpd.conn); @@ -1546,7 +1573,10 @@ int mympd_put_queue(char *buffer, unsigned int offset, unsigned *queue_version, mpd_song_get_id(song), mpd_song_get_pos(song) ); - PUT_SONG_TAGS(); + if (mpd.feat_tags == true) + PUT_SONG_TAGS(); + else + PUT_MIN_SONG_TAGS(); len += json_printf(&out, "}"); } mpd_entity_free(entity); @@ -1602,7 +1632,11 @@ int mympd_put_browse(char *buffer, char *path, unsigned int offset, char *filter if (entities_returned++) len += json_printf(&out, ","); len += json_printf(&out, "{Type: song, "); - PUT_SONG_TAGS(); + if (mpd.feat_tags == true) + PUT_SONG_TAGS(); + else + PUT_MIN_SONG_TAGS(); + len += json_printf(&out, "}"); } else { entity_count--; @@ -1876,7 +1910,10 @@ int mympd_put_playlist_list(char *buffer, char *uri, unsigned int offset, char * if (entities_returned++) len += json_printf(&out, ","); len += json_printf(&out, "{Type: song, "); - PUT_SONG_TAGS(); + if (mpd.feat_tags == true) + PUT_SONG_TAGS(); + else + PUT_MIN_SONG_TAGS(); len += json_printf(&out, ", Pos: %d", entity_count); len += json_printf(&out, "}"); } else { @@ -1931,7 +1968,10 @@ int mympd_search(char *buffer, char *searchstr, char *filter, char *plist, unsig if (entities_returned++) len += json_printf(&out, ", "); len += json_printf(&out, "{Type: song, "); - PUT_SONG_TAGS(); + if (mpd.feat_tags == true) + PUT_SONG_TAGS(); + else + PUT_MIN_SONG_TAGS(); len += json_printf(&out, "}"); } mpd_song_free(song); @@ -2017,7 +2057,10 @@ int mympd_search_queue(char *buffer, char *mpdtagtype, unsigned int offset, char mpd_song_get_id(song), mpd_song_get_pos(song) ); - PUT_SONG_TAGS(); + if (mpd.feat_tags == true) + PUT_SONG_TAGS(); + else + PUT_MIN_SONG_TAGS(); len += json_printf(&out, "}"); mpd_song_free(song); } diff --git a/src/mpd_client.h b/src/mpd_client.h index 8e72849..a88c2d6 100644 --- a/src/mpd_client.h +++ b/src/mpd_client.h @@ -29,17 +29,15 @@ #include "list.h" #define RETURN_ERROR_AND_RECOVER(X) do { \ - printf("MPD X: %s\n", mpd_connection_get_error_message(mpd.conn)); \ - len = json_printf(&out, "{ type:error, data : %Q }", \ - mpd_connection_get_error_message(mpd.conn) \ - ); \ + printf("MPD %s: %s\n", X, mpd_connection_get_error_message(mpd.conn)); \ + len = json_printf(&out, "{ type:error, data : %Q }", mpd_connection_get_error_message(mpd.conn)); \ if (!mpd_connection_clear_error(mpd.conn)) \ mpd.conn_state = MPD_FAILURE; \ return len; \ } while (0) #define LOG_ERROR_AND_RECOVER(X) do { \ - printf("MPD X: %s\n", mpd_connection_get_error_message(mpd.conn)); \ + printf("MPD %s: %s\n", X, mpd_connection_get_error_message(mpd.conn)); \ if (!mpd_connection_clear_error(mpd.conn)) \ mpd.conn_state = MPD_FAILURE; \ } while (0) @@ -62,6 +60,10 @@ len += json_printf(&out, ", Duration: %d, uri: %Q", mpd_song_get_duration(song), mpd_song_get_uri(song)); \ } while (0) +#define PUT_MIN_SONG_TAGS() do { \ + len += json_printf(&out, "Title: %Q, Duration: %d, uri: %Q", mympd_get_tag(song, MPD_TAG_TITLE), mpd_song_get_duration(song), mpd_song_get_uri(song)); \ +} while (0) + #define MAX_SIZE 2048 * 400 #define MAX_ELEMENTS_PER_PAGE 400 @@ -160,6 +162,8 @@ struct t_mpd { const unsigned* protocol; // Supported tags bool feat_sticker; + bool feat_playlists; + bool feat_tags; } mpd; struct list mpd_tags;