Skip to content

Commit

Permalink
Feat: use song ids for move in queue #1001
Browse files Browse the repository at this point in the history
  • Loading branch information
jcorporation committed May 21, 2023
1 parent b6f3db1 commit 2943d8b
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ https://github.com/jcorporation/myMPD/
### API changes

- MYMPD_API_QUEUE_RM_SONG -> MYMPD_API_QUEUE_RM_IDS
- MYMPD_API_QUEUE_MOVE_SONG -> MYMPD_API_QUEUE_MOVE_POSITION
- MYMPD_API_QUEUE_MOVE_SONG -> MYMPD_API_QUEUE_MOVE_IDS
- MYMPD_API_QUEUE_APPEND_URI -> MYMPD_API_QUEUE_APPEND_URIS
- MYMPD_API_QUEUE_INSERT_URI -> MYMPD_API_QUEUE_INSERT_URIS
- MYMPD_API_QUEUE_REPLACE_URI -> MYMPD_API_QUEUE_REPLACE_URIS
Expand Down
1 change: 1 addition & 0 deletions htdocs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2520,6 +2520,7 @@ <h5 class="modal-title"><span class="mi title-icon">trending_flat</span> <span d
</div>
<div class="modal-body">
<input type="hidden" id="inputSongPosOld"/>
<input type="hidden" id="inputSongId"/>
<input type="hidden" id="inputSongPosPlist"/>
<div class="mb-3 row">
<label class="col-sm-4 col-form-label" for="inputSongPosNew" data-phrase="Position"></label>
Expand Down
20 changes: 11 additions & 9 deletions htdocs/js/QueueCurrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,10 +708,11 @@ function saveQueueCheckError(obj) {
* @returns {void}
*/
//eslint-disable-next-line no-unused-vars
function showSetSongPos(plist, oldSongPos) {
function showSetSongPos(plist, oldSongPos, songId) {
cleanupModalId('modalSetSongPos');
document.getElementById('inputSongPosNew').value = '';
document.getElementById('inputSongPosOld').value = oldSongPos;
document.getElementById('inputSongId').value = songId;
document.getElementById('inputSongPosPlist').value = plist;
uiElements.modalSetSongPos.show();
}
Expand All @@ -725,15 +726,16 @@ function setSongPos() {
cleanupModalId('modalSetSongPos');
const plist = document.getElementById('inputSongPosPlist').value;
const oldSongPos = Number(document.getElementById('inputSongPosOld').value);
const songId = Number(document.getElementById('inputSongId').value);
const newSongPosEl = document.getElementById('inputSongPosNew');
if (validateIntRangeEl(newSongPosEl, 1, 99999) === true) {
let newSongPos = Number(newSongPosEl.value);
if (newSongPos < oldSongPos) {
newSongPos--;
}
if (plist === 'queue') {
sendAPI("MYMPD_API_QUEUE_MOVE_POSITION", {
"from": oldSongPos,
sendAPI("MYMPD_API_QUEUE_MOVE_ID", {
"songIds": [songId],
"to": newSongPos
}, setSongPosCheckError, true);
}
Expand All @@ -748,7 +750,7 @@ function setSongPos() {
}

/**
* Handles the MYMPD_API_QUEUE_MOVE_POSITION and jsonrpc response
* Handles the MYMPD_API_QUEUE_MOVE_ID and MYMPD_API_PLAYLIST_CONTENT_MOVE_POSITION jsonrpc response
* @param {object} obj jsonrpc response
* @returns {void}
*/
Expand Down Expand Up @@ -857,13 +859,13 @@ function gotoPlayingSong() {

/**
* Moves a entry in the queue
* @param {number} from from position
* @param {number} id song id
* @param {number} to to position
* @returns {void}
*/
function queueMovePosition(from, to) {
sendAPI("MYMPD_API_QUEUE_MOVE_POSITION", {
"from": from,
function queueMoveId(id, to) {
sendAPI("MYMPD_API_QUEUE_MOVE_IDS", {
"songIds": [id],
"to": to
}, null, false);
}
Expand All @@ -880,7 +882,7 @@ function playAfterCurrent(songId, songPos) {
if (settings.partition.random === false) {
//not in random mode - move song after current playing song
const newSongPos = currentState.songPos !== undefined ? currentState.songPos + 1 : 0;
queueMovePosition(songPos, newSongPos);
queueMoveId(songId, newSongPos);
}
else {
//in random mode - set song priority
Expand Down
6 changes: 3 additions & 3 deletions htdocs/js/apidoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,10 @@ const APImethods = {
}
}
},
"MYMPD_API_QUEUE_MOVE_POSITION": {
"desc": "Moves a song in the queue.",
"MYMPD_API_QUEUE_MOVE_IDS": {
"desc": "Moves song ids in the queue.",
"params": {
"from": APIparams.from,
"songIds": APIparams.songIds,
"to": APIparams.to
}
},
Expand Down
2 changes: 1 addition & 1 deletion htdocs/js/contextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ function createMenuLists(target, contextMenuTitle, contextMenuBody) {
addMenuItem(contextMenuBody, {"cmd": "showSetSongPriority", "options": [songid]}, 'Set priority');
}
else {
addMenuItem(contextMenuBody, {"cmd": "showSetSongPos", "options": ["queue", songpos]}, 'Move song');
addMenuItem(contextMenuBody, {"cmd": "showSetSongPos", "options": ["queue", songpos, songid]}, 'Move song');
}
if (songid === currentState.currentSongId) {
addMenuItemsSingleActions(contextMenuBody);
Expand Down
11 changes: 7 additions & 4 deletions htdocs/js/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ function dragAndDropTable(tableId) {
if (event.target.nodeName === 'TD') {
target = event.target.parentNode;
}
const oldSongPos = getDataId(event.dataTransfer.getData('Text'), 'songpos');
const newSongPos = getData(target, 'songpos');
document.getElementById(event.dataTransfer.getData('Text')).remove();
dragEl.classList.remove('opacity05');
Expand All @@ -90,12 +89,16 @@ function dragAndDropTable(tableId) {
}
document.getElementById(tableId).classList.add('opacity05');
switch(app.id) {
case 'QueueCurrent':
queueMovePosition(oldSongPos, newSongPos);
case 'QueueCurrent': {
const songId = getDataId(event.dataTransfer.getData('Text'), 'songid');
queueMoveId(songId, newSongPos);
break;
case 'BrowsePlaylistsDetail':
}
case 'BrowsePlaylistsDetail': {
const oldSongPos = getDataId(event.dataTransfer.getData('Text'), 'songpos');
playlistMoveSong(oldSongPos, newSongPos);
break;
}
}
}, false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
X(MYMPD_API_QUEUE_INSERT_SEARCH) \
X(MYMPD_API_QUEUE_INSERT_URIS) \
X(MYMPD_API_QUEUE_LIST) \
X(MYMPD_API_QUEUE_MOVE_POSITION) \
X(MYMPD_API_QUEUE_MOVE_IDS) \
X(MYMPD_API_QUEUE_PRIO_SET) \
X(MYMPD_API_QUEUE_PRIO_SET_HIGHEST) \
X(MYMPD_API_QUEUE_REPLACE_PLAYLISTS) \
Expand Down
15 changes: 8 additions & 7 deletions src/mympd_api/mympd_api_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -898,17 +898,18 @@ void mympd_api_handler(struct t_partition_state *partition_state, struct t_work_
response->data = mympd_respond_with_error_or_ok(partition_state, response->data, request->cmd_id, request->id, rc, "mpd_run_delete_range", &result);
}
break;
case MYMPD_API_QUEUE_MOVE_POSITION:
if (json_get_uint(request->data, "$.params.from", 0, MPD_PLAYLIST_LENGTH_MAX, &uint_buf1, &error) == true &&
json_get_uint(request->data, "$.params.to", 0, MPD_PLAYLIST_LENGTH_MAX, &uint_buf2, &error) == true)
case MYMPD_API_QUEUE_MOVE_IDS: {
struct t_list song_ids;
list_init(&song_ids);
if (json_get_array_llong(request->data, "$.params.songIds", &song_ids, MPD_COMMANDS_MAX, &error) == true &&
json_get_uint(request->data, "$.params.to", 0, MPD_PLAYLIST_LENGTH_MAX, &uint_buf1, &error) == true)
{
if (uint_buf1 < uint_buf2) {
uint_buf2--;
}
rc = mpd_run_move(partition_state->conn, uint_buf1, uint_buf2);
rc = mympd_api_queue_move_ids(partition_state, &song_ids, uint_buf1);
response->data = mympd_respond_with_error_or_ok(partition_state, response->data, request->cmd_id, request->id, rc, "mpd_run_move", &result);
}
list_clear(&song_ids);
break;
}
case MYMPD_API_QUEUE_PRIO_SET: {
struct t_list song_ids;
list_init(&song_ids);
Expand Down
25 changes: 25 additions & 0 deletions src/mympd_api/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,31 @@ bool mympd_api_queue_play_newly_inserted(struct t_partition_state *partition_sta
return mympd_check_rc_error_and_recover(partition_state, rc, "mpd_run_play_id");
}

/**
* Moves the song ids to pos in queue.
* @param partition_state pointer to partition state
* @param song_ids song ids in the queue
* @param to position to move
* @return true on success, else false
*/
bool mympd_api_queue_move_ids(struct t_partition_state *partition_state, struct t_list *song_ids, unsigned to) {
if (mpd_command_list_begin(partition_state->conn, false) == true) {
struct t_list_node *current = song_ids->head;
while (current != NULL) {
bool rc = mpd_send_move_id(partition_state->conn, (unsigned)current->value_i, to);
if (rc == false) {
MYMPD_LOG_ERROR("Error adding command to command list mpd_send_move_id");
break;
}
current = current->next;
to++;
}
return mpd_command_list_end(partition_state->conn) &&
mpd_response_finish(partition_state->conn);
}
return false;
}

/**
* Sets the priority of a song in the queue.
* The priority has only an effect in random mode.
Expand Down
1 change: 1 addition & 0 deletions src/mympd_api/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ bool mympd_api_queue_replace(struct t_partition_state *partition_state, struct t
bool mympd_api_queue_append_plist(struct t_partition_state *partition_state, struct t_list *plists);
bool mympd_api_queue_insert_plist(struct t_partition_state *partition_state, struct t_list *plists, unsigned to, unsigned whence);
bool mympd_api_queue_replace_plist(struct t_partition_state *partition_state, struct t_list *plists);
bool mympd_api_queue_move_ids(struct t_partition_state *partition_state, struct t_list *song_ids, unsigned to);
#endif

0 comments on commit 2943d8b

Please sign in to comment.