From 2025d3c6e2a5730a61bb8ae460f5ef9e8a344050 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 6 May 2020 19:33:27 +0200
Subject: [PATCH] Handle case to delete current kara (not if the kara is the
 last)

---
 src/commands.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/src/commands.c b/src/commands.c
index 22863455..f3337062 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -271,20 +271,30 @@ command_crop(volatile sqlite3 *db, mpd_idle_flag *watch_mask_ptr)
 bool
 command_delid(volatile sqlite3 *db, struct lkt_win *win, char *id_str, mpd_idle_flag *watch_mask_ptr)
 {
-    UNUSED(win);
     long id;
-    char *endptr = NULL, err = 0;
+    char *endptr = NULL, err = 0, filepath[PATH_MAX];
     int uri = 0;
 
-    *watch_mask_ptr |= MPD_IDLE_PLAYLIST;
     STRTOL(id, id_str, endptr, err);
     RETURN_IF(err, "STRTOL failed", false);
 
-    /* If one day we allow suppression of the current kara, will need the `win`
-       pointer to reload the kara in the same position (but the kara won't be
-       the same). */
     database_queue_current_kara(db, NULL, &uri);
-    RETURN_IF(id == (long) uri, "Can't delete current kara", false);
+
+    if (id == (long) uri) {
+        if (database_queue_skip_current(db, filepath)) {
+            if (!win->load_file(win, filepath)) {
+                LOG_ERROR_SCT("COMMAND", "Failed to skip current kara to delete id %ld", id);
+                return false;
+            }
+        }
+
+        else {
+            LOG_WARN_SCT("COMMAND", "Failed to skip current kara to delete id %ld, stop playback", id);
+            win->close(win);
+        }
+    }
+
+    *watch_mask_ptr |= MPD_IDLE_PLAYER;
     return database_queue_del_id(db, id);
 }
 
-- 
GitLab