From 3647ebd76c6f54d7ed593fbcda10389ddc782485 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Sat, 30 Jan 2021 22:38:07 +0100
Subject: [PATCH] MPD: Add the playlistid command

---
 CHANGELOG.md          |  3 ++-
 README.md             |  2 +-
 inc/lektor/commands.h |  3 ++-
 inc/lektor/database.h |  1 +
 src/base/commands.c   |  9 +++++++++
 src/database/queue.c  | 15 +++++++++++++++
 src/net/listen.c      |  2 ++
 7 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 405f9f40..d6774a62 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,8 +12,9 @@
 - Add SDL2-Image lib to the dependencies (--with-depends)
 - Add klkt autolaunch option to config file
 - Add the \_\_flat command to set all priorities to 1 in the queue
+- Add MPD commands: seek, seekid, seekcur, delete (partial), playlistid
+- Alias list\* commands as find
 - Remove archlinux package support (replaced by AppImage)
-- Add MPD commands: seek, seekid, seekcur, delete (partial)
 - Fix dl process with the repo module
 - Fix: the moveid was in fact a move command, no range supported
 - Bug fix: reduce libs to link to lkt (dear autoconf...)
diff --git a/README.md b/README.md
index 4b0fa999..915d9f5e 100644
--- a/README.md
+++ b/README.md
@@ -217,7 +217,7 @@ For the compatibility column, the possible values are the following:
 | `moveid {from} {to}`                      | `moveid {from} {to}`                  | ~     | `to` is always an absolute position               |
 | `playlist`                                | `playlist`                            | +     |                                                   |
 | `playlistfind {tag} {needle}`             | `playlistfind {uri}`                  | ~     | uses lektord URIs                                 |
-| `playlistid {songid}`                     |                                       | TODO  | not implemented                                   |
+| `playlistid {songid}`                     | `playlistid {id}                      | ~     | the uri is only an id                             |
 | `playlistinfo [[songpos]/[start:end]]`    | `playlistinfo`                        | -     | is an alias to `playlist`                         |
 | `playlistsearch {taf} {needle}`           | `playlistsearch {uri}`                | ~     | uses lektord URIs                                 |
 | `plchanges {version} [start:end]`         |                                       |       | not implemented                                   |
diff --git a/inc/lektor/commands.h b/inc/lektor/commands.h
index 8c1da8c2..e5b45e4e 100644
--- a/inc/lektor/commands.h
+++ b/inc/lektor/commands.h
@@ -46,7 +46,8 @@ bool command_dump   (struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]);
 bool command_swap   (struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]);
 bool command_flat   (struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]);
 
-bool command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_MAX]);
+bool command_queue_list  (struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_MAX]);
+bool command_queue_listid(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_MAX]);
 
 /* The playlists */
 bool command_plt_create(struct lkt_state *srv, char *args[LKT_MESSAGE_ARGS_MAX]);
diff --git a/inc/lektor/database.h b/inc/lektor/database.h
index fa74abee..68b93cc5 100644
--- a/inc/lektor/database.h
+++ b/inc/lektor/database.h
@@ -87,6 +87,7 @@ struct lkt_callback {
 /* List the content of the queue */
 bool database_queue_list(volatile sqlite3 *db, size_t from, size_t to,
                          struct lkt_callback *callback);
+bool database_queue_probe_id(volatile sqlite3 *db, int id);
 
 /* Search the database */
 struct lkt_search {
diff --git a/src/base/commands.c b/src/base/commands.c
index 4b6d3aeb..0651f6b7 100644
--- a/src/base/commands.c
+++ b/src/base/commands.c
@@ -959,6 +959,15 @@ command_shuffle(struct lkt_state *srv, char UNUSED *args[LKT_MESSAGE_ARGS_MAX])
     return true;
 }
 
+bool
+command_queue_listid(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_MAX])
+{
+    RETURN_UNLESS(args && args[0], "Invalid arguments", false);
+    long id = strtol(args[0], NULL, 0);
+    return database_queue_probe_id(srv->db, id) &&
+           command_findid(srv, c, args[0]);
+}
+
 bool
 command_queue_list(struct lkt_state *srv, size_t c, char *args[LKT_MESSAGE_ARGS_MAX])
 {
diff --git a/src/database/queue.c b/src/database/queue.c
index 61bb64a1..dd0a4151 100644
--- a/src/database/queue.c
+++ b/src/database/queue.c
@@ -802,6 +802,21 @@ error:
     return false;
 }
 
+bool
+database_queue_probe_id(volatile sqlite3 *db, int id)
+{
+    bool sta               = false;
+    sqlite3_stmt *stmt     = NULL;
+    static const char *SQL = "SELECT * FROM queue WHERE kara_id = ?;";
+    SQLITE_PREPARE(db, stmt, SQL, error);
+    SQLITE_BIND_INT(db, stmt, 1, id, error);
+    SQLITE_STEP_ROW(db, stmt, error);
+    sta = true;
+error:
+    sqlite3_finalize(stmt);
+    return sta;
+}
+
 bool
 database_queue_shuffle(volatile sqlite3 *db)
 {
diff --git a/src/net/listen.c b/src/net/listen.c
index 2f3fcbf1..a0ecab1b 100644
--- a/src/net/listen.c
+++ b/src/net/listen.c
@@ -217,6 +217,8 @@ handle_simple_command(struct lkt_state *srv, size_t c, struct lkt_command cmd)
         else if (STR_MATCH(cmd.name, "seekcur"))
             err = ! command_seekcur(srv, cmd.args);
 
+        else if (STR_MATCH(cmd.name, "playlistid"))
+            err = ! command_queue_listid(srv, c, cmd.args);
         else if (STR_MATCH(cmd.name, "playlist") || STR_MATCH(cmd.name, "playlistinfo"))
             err = ! command_queue_list(srv, c, cmd.args);
         else if (STR_MATCH(cmd.name, "playlistfind") || STR_MATCH(cmd.name, "playlistsearch"))
-- 
GitLab