diff --git a/doc/lkt.1 b/doc/lkt.1 index 153952886d9b2be3df10003ebaae9e221a8f783f..d1ce04e5380170d681ef25c6670d445a445a2354 100644 --- a/doc/lkt.1 +++ b/doc/lkt.1 @@ -92,6 +92,9 @@ This can work only if the currently playong kara is not the last \fBqueue add\fP <query> Add karas to the queue at the end of it with a valid query .TP +\fBqueue insert\fP <query> +Add karas to the queue just after the currently playing kara +.TP \fBqueue seek\fP <id> Goto to the kara with the specified id in the queue .TP @@ -115,12 +118,6 @@ playing one \fBsearch get\fP <query> Search and prints the kara that correspond to the query in the database .TP -\fBsearch add\fP <query> -Search, prints and add to the queue the karas that match the query -.TP -\fBsearch insert\fP <query> -Search, prints and insert into the queue the karas that match the query -.TP \fBsearch plt\fP <plt-name> <query> Search, prints and add to an existing playlist the karas that match the query diff --git a/src/commands.c b/src/commands.c index 3f0ea07f8c678f3c4802c2af56b99f910e29cfc8..22294dc4bcdb10b79164a704c5dfc3805d91baf2 100644 --- a/src/commands.c +++ b/src/commands.c @@ -234,6 +234,8 @@ command_add(volatile sqlite3 *db, struct lkt_win *win, char *args[LKT_MESSAGE_AR RETURN_UNLESS(lkt_uri_from(&uri, query), "Failed to parse query", false); ret = database_queue_add_uri(db, &uri, priority); lkt_uri_free(&uri); + if (!ret) + LOG_ERROR_SCT("COMMAND", "Failed to add with priority %d in queue", priority); return ret; } @@ -372,7 +374,7 @@ command_noidle(struct lkt_state *srv, size_t c) } /* Functions for the searchadd and the search mpd commands */ -static bool +bool lkt_callback_send_row_v1(void *_args, int id, int id_len, const char *sql_row) { struct lkt_callback *args = (struct lkt_callback *) _args; @@ -383,12 +385,13 @@ lkt_callback_send_row_v1(void *_args, int id, int id_len, const char *sql_row) return true; } -static bool +bool lkt_callback_send_row_v2(struct lkt_state *srv, size_t c, int id, int id_len, const char *sql_row) { struct lkt_message *out = lkt_message_new(); out->data_len = snprintf(out->data, LKT_MESSAGE_MAX, "%*d %s\n", id_len, id, sql_row); lkt_state_send(srv, c, out); + printf("TOTO\n"); return true; } @@ -687,9 +690,11 @@ only_one: /* The command is used with a range specifier. */ is_a_range: if (to - from + 1 < lkt_remaining_msg(srv, c) - 2) { + LOG_INFO_SCT("COMMAND", "Got range %d:%d, no continuation needed", from, to); lkt_set_continuation(srv, c, 0); return database_queue_list(srv->db, from, to, &callback); } else { + LOG_INFO_SCT("COMMAND", "Got range %d:%d, continuation needed", from, to); to = from + lkt_remaining_msg(srv, c) - 3; lkt_set_continuation(srv, c, to + 1); return database_queue_list(srv->db, from, to, &callback); diff --git a/src/database/queue.c b/src/database/queue.c index fae9861766a7005210ef13fb4c23a0f60a250cc9..a6371ad6cd69db3e5dddc93a96869b0275bb3f68 100644 --- a/src/database/queue.c +++ b/src/database/queue.c @@ -609,7 +609,6 @@ database_queue_list(volatile sqlite3 *db, size_t from, size_t to, struct lkt_cal for (;;) { code = sqlite3_step(stmt); - if (code == SQLITE_ROW) { id = sqlite3_column_int(stmt, 0); row = (const char *) sqlite3_column_text(stmt, 1); @@ -624,8 +623,10 @@ database_queue_list(volatile sqlite3 *db, size_t from, size_t to, struct lkt_cal else if (code == SQLITE_OK || code == SQLITE_DONE) goto done; - else + else { + LOG_ERROR_SCT("DB", "Failed: %s", sqlite3_errmsg((sqlite3 *) db)); break; + } } done: diff --git a/src/main/lkt.c b/src/main/lkt.c index 75e5a7206bc543a387884d4e4ebc9f420ed419ac..ea03894d8bd2574d6c0eb9acca7b65c1d0bafae6 100644 --- a/src/main/lkt.c +++ b/src/main/lkt.c @@ -511,32 +511,39 @@ queue_delete__(struct lkt_cmd_args *args) fail("Invalid argument"); } +static inline void +send_cmd_with_uri(FILE *sock, char *cmd, int argc, const char **argv) +{ + int i; + char buf[LKT_MESSAGE_MAX] = {0}; + for (i = 1; i < argc - 1; ++i) { + strncat(buf, argv[i], LKT_MESSAGE_MAX - 1); + strncat(buf, " ", LKT_MESSAGE_MAX - 1); + } + strncat(buf, argv[i], LKT_MESSAGE_MAX - 1); + strncat(buf, "\n", LKT_MESSAGE_MAX - 1); + (void) write_socket_format(sock, "%s %s://%s", cmd, argv[0], buf); +} + noreturn void queue_add__(struct lkt_cmd_args *args) { char buff[3]; - int i; - - if (args->argc < 1) - fail("Invalid argument, the add command should takes at least two arguments: queue add <query>"); - - if (!lkt_valid_type(args->argv[0])) - fail("Invalid argument, the type given to the add command is invalid"); - + fail_if(args->argc < 1, "Invalid arguments"); + fail_if(!lkt_valid_type(args->argv[0]), "Invalid query type"); FILE *sock = lkt_connect(); + send_cmd_with_uri(sock, "add", args->argc, args->argv); + exit_with_status(sock, buff); +} - (void) write_socket(sock, "add ", 4 * sizeof(char)); - (void) write_socket(sock, args->argv[0], strlen(args->argv[0])); - (void) write_socket(sock, "://", 3 * sizeof(char)); - - for (i = 1; i < args->argc - 1; ++i) { - (void) write_socket(sock, args->argv[i], strlen(args->argv[i])); - (void) write_socket(sock, " ", sizeof(char)); - } - - /* Here we have `i == argc - 1`. */ - (void) write_socket(sock, args->argv[i], strlen(args->argv[i])); - (void) write_socket(sock, "\n", sizeof(char)); +noreturn void +queue_insert__(struct lkt_cmd_args *args) +{ + char buff[3]; + fail_if(args->argc < 1, "Invalid arguments"); + fail_if(!lkt_valid_type(args->argv[0]), "Invalid query type"); + FILE *sock = lkt_connect(); + send_cmd_with_uri(sock, "__insert", args->argc, args->argv); exit_with_status(sock, buff); } @@ -792,8 +799,6 @@ redo: #define search_with_cmd(func, cmd) /* I don't want to write always the same things */ \ noreturn void func (struct lkt_cmd_args *args) { search_with_cmd__(args, #cmd); } search_with_cmd(search_get__, search) -search_with_cmd(search_add__, searchadd) -search_with_cmd(search_insert__, __insert) search_with_cmd(search_plt__, listplaylist) search_with_cmd(search_count__, count) search_with_cmd(search_queue__, playlistfind) @@ -802,6 +807,7 @@ search_with_cmd(search_queue__, playlistfind) /* Parsing stuff. */ static struct lkt_cmd_opt options_queue[] = { + { .name = "insert", .call = queue_insert__ }, { .name = "pos", .call = queue_pos__ }, { .name = "pop", .call = queue_pop__ }, { .name = "add", .call = queue_add__ }, @@ -822,8 +828,6 @@ static struct lkt_cmd_opt options_plt[] = { static struct lkt_cmd_opt options_search[] = { { .name = "get", .call = search_get__ }, - { .name = "add", .call = search_add__ }, - { .name = "insert", .call = search_insert__ }, { .name = "plt", .call = search_plt__ }, { .name = "count", .call = search_count__ }, { .name = "queue", .call = search_queue__ },