1
0
mirror of https://github.com/SuperBFG7/ympd synced 2024-12-25 02:20:27 +00:00

Added search queue function

This commit is contained in:
jcorporation 2018-05-24 23:21:19 +01:00
parent 222ee460d6
commit d773dfb745
4 changed files with 122 additions and 25 deletions

View File

@ -122,8 +122,11 @@
<span class="material-icons">save</span>
</button>
</div>
<form id="searchqueue" role="search" class="btn btn-secondary">
<input type="text" class="form-control" placeholder="Search"/>
</form>
</div>
<table id="queueList" class="table table-hover">
<col class="tblnum"/>
<col class="tblartist"/>
@ -145,9 +148,9 @@
</tbody>
</table>
<ul class="pagination justify-content-center">
<li id="queuePrev" class="page-item hide"><a class="page-link text-secondary" href="">Previous</a></li>
<li id="queueNext" class="page-item hide"><a class="page-link text-secondary" href="">Next</a></li>
<ul id="queuePagination" class="pagination justify-content-center hide">
<li id="queuePrev" class="page-item disabled"><a class="page-link text-secondary" href="">Previous</a></li>
<li id="queueNext" class="page-item disabled"><a class="page-link text-secondary" href="">Next</a></li>
</ul>
</div>
</div>
@ -186,9 +189,9 @@
</tbody>
</table>
<ul class="pagination justify-content-center">
<li id="browsePrev" class="page-item hide"><a class="page-link text-secondary" href="">Previous</a></li>
<li id="browseNext" class="page-item hide"><a class="page-link text-secondary" href="">Next</a></li>
<ul id="browsePagination" class="pagination justify-content-center hide">
<li id="browsePrev" class="page-item disabled"><a class="page-link text-secondary" href="">Previous</a></li>
<li id="browseNext" class="page-item disabled"><a class="page-link text-secondary" href="">Next</a></li>
</ul>
</div>
</div>

View File

@ -45,7 +45,7 @@ var app = $.sammy(function() {
$('#cardQueue').addClass('hide');
$('#cardBrowse').addClass('hide');
$('#cardSearch').addClass('hide');
$('.page-item').addClass('hide');
$('.pagination').addClass('hide');
pagination = 0;
browsepath = '';
}
@ -200,6 +200,8 @@ function webSocketConnect() {
}
switch (obj.type) {
case 'queuesearch':
//Do the same as queue
case 'queue':
if(current_app !== 'queue')
break;
@ -220,6 +222,15 @@ function webSocketConnect() {
$('#queueList > tbody').empty();
for (var song in obj.data) {
if (obj.data[song].type == 'wrap') {
$('#'+current_app+'List > tbody').append(
"<tr><td><span class=\"material-icons\">error_outline</span></td>" +
"<td colspan=\"3\">Too many results, please refine your search!</td>" +
"<td></td><td></td></tr>"
);
continue;
}
var minutes = Math.floor(obj.data[song].duration / 60);
var seconds = obj.data[song].duration - minutes * 60;
@ -231,11 +242,17 @@ function webSocketConnect() {
"<td>"+ minutes + ":" + (seconds < 10 ? '0' : '') + seconds +
"</td><td></td></tr>");
}
if (obj.type == 'queue') {
if(obj.data.length && obj.data[obj.data.length-1].pos + 1 >= pagination + MAX_ELEMENTS_PER_PAGE) {
$('#queueNext').removeClass('disabled');
$('#queuePagination').removeClass('hide');
}
if(pagination > 0) {
$('#queuePrev').removeClass('disabled');
$('#queuePagination').removeClass('hide');
}
}
if(obj.data.length && obj.data[obj.data.length-1].pos + 1 >= pagination + MAX_ELEMENTS_PER_PAGE)
$('#queueNext').removeClass('hide');
if(pagination > 0)
$('#queuePrev').removeClass('hide');
if ( isTouch ) {
$('#queueList > tbody > tr > td:last-child').append(
"<a class=\"pull-right btn-group-hover color-darkgrey\" href=\"#/\" " +
@ -345,7 +362,8 @@ function webSocketConnect() {
break;
case 'wrap':
if(current_app == 'browse') {
$('#browseNext').removeClass('hide');
$('#browseNext').removeClass('disabled');
$('#browsePagination').removeClass('hide');
} else {
$('#'+current_app+'List > tbody').append(
"<tr><td><span class=\"material-icons\">error_outline</span></td>" +
@ -356,8 +374,10 @@ function webSocketConnect() {
break;
}
if(pagination > 0)
$('#browsePrev').removeClass('hide');
if(pagination > 0) {
$('#browsePrev').removeClass('disabled');
$('#browsePagination').removeClass('hide');
}
}
@ -705,6 +725,22 @@ $('#search').submit(function () {
return false;
});
$('#searchqueue > input').keyup(function (event) {
// if ( event.which == 13 ) {
var searchstr=$('#searchqueue > input').val();
if (searchstr.length > 3) {
socket.send('MPD_API_SEARCH_QUEUE,' + searchstr);
}
else if (searchstr.length == 0) {
socket.send('MPD_API_GET_QUEUE,0');
}
// }
});
$('#searchqueue').submit(function () {
return false;
});
$('.page-link').on('click', function (e) {
switch ($(this).text()) {

View File

@ -66,13 +66,11 @@ int callback_mpd(struct mg_connection *c)
int int_buf;
char *p_charbuf = NULL, *token;
// fprintf(stdout,"%s\n",c->content);
if(cmd_id == -1)
return MG_TRUE;
// if(mpd.conn_state != MPD_CONNECTED && cmd_id != MPD_API_SET_MPDHOST &&
// cmd_id != MPD_API_GET_MPDHOST && cmd_id != MPD_API_SET_MPDPASS)
// return MG_TRUE;
switch(cmd_id)
{
case MPD_API_UPDATE_DB:
@ -234,6 +232,20 @@ out_playlist:
out_save_queue:
free(p_charbuf);
break;
case MPD_API_SEARCH_QUEUE:
p_charbuf = strdup(c->content);
if(strcmp(strtok(p_charbuf, ","), "MPD_API_SEARCH_QUEUE"))
goto out_search_queue;
if((token = strtok(NULL, ",")) == NULL)
goto out_search_queue;
free(p_charbuf);
p_charbuf = strdup(c->content);
n = mpd_search_queue(mpd.buf, get_arg1(p_charbuf));
out_search_queue:
free(p_charbuf);
break;
case MPD_API_SEARCH:
p_charbuf = strdup(c->content);
if(strcmp(strtok(p_charbuf, ","), "MPD_API_SEARCH"))
@ -628,10 +640,6 @@ int mpd_put_queue(char *buffer, unsigned int offset)
cur += json_emit_quoted_str(cur, end - cur, mpd_get_album(song));
cur += json_emit_raw_str(cur, end - cur, ",\"title\":");
cur += json_emit_quoted_str(cur, end - cur, mpd_get_title(song));
cur += json_emit_raw_str(cur, end - cur, ",\"artist\":");
cur += json_emit_quoted_str(cur, end - cur, mpd_get_artist(song));
cur += json_emit_raw_str(cur, end - cur, ",\"album\":");
cur += json_emit_quoted_str(cur, end - cur, mpd_get_album(song));
cur += json_emit_raw_str(cur, end - cur, "},");
}
@ -764,10 +772,58 @@ int mpd_search(char *buffer, char *searchstr)
cur += json_emit_int(cur, end - cur, mpd_song_get_duration(song));
cur += json_emit_raw_str(cur, end - cur, ",\"title\":");
cur += json_emit_quoted_str(cur, end - cur, mpd_get_title(song));
cur += json_emit_raw_str(cur, end - cur, ",\"artist\":");
cur += json_emit_quoted_str(cur, end - cur, mpd_get_artist(song));
cur += json_emit_raw_str(cur, end - cur, "},");
mpd_song_free(song);
/* Maximum results */
if(i++ >= 300)
{
cur += json_emit_raw_str(cur, end - cur, "{\"type\":\"wrap\"},");
break;
}
}
/* remove last ',' */
cur--;
cur += json_emit_raw_str(cur, end - cur, "]}");
}
return cur - buffer;
}
int mpd_search_queue(char *buffer, char *searchstr)
{
int i = 0;
char *cur = buffer;
const char *end = buffer + MAX_SIZE;
struct mpd_song *song;
if(mpd_search_queue_songs(mpd.conn, false) == false)
RETURN_ERROR_AND_RECOVER("mpd_search_queue_songs");
else if(mpd_search_add_any_tag_constraint(mpd.conn, MPD_OPERATOR_DEFAULT, searchstr) == false)
RETURN_ERROR_AND_RECOVER("mpd_search_add_any_tag_constraint");
else if(mpd_search_commit(mpd.conn) == false)
RETURN_ERROR_AND_RECOVER("mpd_search_commit");
else {
cur += json_emit_raw_str(cur, end - cur, "{\"type\":\"queuesearch\",\"data\":[ ");
while((song = mpd_recv_song(mpd.conn)) != NULL) {
cur += json_emit_raw_str(cur, end - cur, "{\"type\":\"song\",\"uri\":");
cur += json_emit_quoted_str(cur, end - cur, mpd_song_get_uri(song));
cur += json_emit_raw_str(cur, end - cur, ",\"id\":");
cur += json_emit_int(cur, end - cur, mpd_song_get_id(song));
cur += json_emit_raw_str(cur, end - cur, ",\"pos\":");
cur += json_emit_int(cur, end - cur, mpd_song_get_pos(song));
cur += json_emit_raw_str(cur, end - cur, ",\"album\":");
cur += json_emit_quoted_str(cur, end - cur, mpd_get_album(song));
cur += json_emit_raw_str(cur, end - cur, ",\"artist\":");
cur += json_emit_quoted_str(cur, end - cur, mpd_get_artist(song));
// cur += json_emit_raw_str(cur, end - cur, ",\"album_artist\":");
// cur += json_emit_quoted_str(cur, end - cur, mpd_get_album_artist(song));
cur += json_emit_raw_str(cur, end - cur, ",\"duration\":");
cur += json_emit_int(cur, end - cur, mpd_song_get_duration(song));
cur += json_emit_raw_str(cur, end - cur, ",\"title\":");
cur += json_emit_quoted_str(cur, end - cur, mpd_get_title(song));
cur += json_emit_raw_str(cur, end - cur, "},");
mpd_song_free(song);

View File

@ -54,6 +54,7 @@
X(MPD_API_RM_RANGE) \
X(MPD_API_RM_ALL) \
X(MPD_API_MOVE_TRACK) \
X(MPD_API_SEARCH_QUEUE) \
X(MPD_API_SEARCH) \
X(MPD_API_SEND_MESSAGE) \
X(MPD_API_SET_VOLUME) \
@ -120,6 +121,7 @@ int mpd_put_current_song(char *buffer);
int mpd_put_queue(char *buffer, unsigned int offset);
int mpd_put_browse(char *buffer, char *path, unsigned int offset);
int mpd_search(char *buffer, char *searchstr);
int mpd_search_queue(char *buffer, char *searchstr);
void mpd_disconnect();
#endif