Subject: [PATCH] Add notifications support to 'mpc'
From: john muhl <[email-masked]>
To: [email-masked]
Date: Sat, 28 Sep 2024 17:48:21 -0500
List-Id: <debbugs-submit.debbugs.gnu.org>

Tags: patch

This adds support for displaying a notification when the song
changes.

From eb495c851dafcf883b0c1cbcd22b2dedc074f226 Mon Sep 17 00:00:00 2001
From: john muhl <[email-masked]>
Date: Sun, 15 Sep 2024 19:52:25 -0500
Subject: [PATCH] Add notifications support to 'mpc'

* lisp/mpc.el (mpc-notifications):
(mpc-notifications-function): New option.
(mpc-notifications-id): New variable.
(mpc-notifications-notify):
(mpc-cover-image-find):
(mpc-cover-image-p): New function.
---
 lisp/mpc.el | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)

diff --git a/lisp/mpc.el b/lisp/mpc.el
index 768c70c2e3a..30e295d4810 100644
--- a/lisp/mpc.el
+++ b/lisp/mpc.el
@@ -95,6 +95,8 @@
   (require 'cl-lib)
   (require 'subr-x))
 
+(require 'notifications)
+
 (defgroup mpc ()
   "Client for the Music Player Daemon (mpd)."
   :prefix "mpc-"
@@ -2766,6 +2768,83 @@ mpc-drag-n-drop
       (t
        (error "Unsupported drag'n'drop gesture"))))))
 
+;;; Notifications ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(declare-function notifications-notify "notifications")
+(declare-function w32-notification-notify "notifications")
+(declare-function haiku-notifications-notify "notifications")
+(declare-function android-notifications-notify "notifications")
+
+(defcustom mpc-notifications-function 'mpc-notifications-notify
+  "Function called to display notification."
+  :version "31.1"
+  :type 'function)
+
+(defcustom mpc-notifications nil
+  "Non-nil means to display notifications when the song changes."
+  :version "31.1"
+  :type 'boolean
+  :set (lambda (symbol value)
+         (if-let ((cb (cons 'file mpc-notifications-function))
+                  value)
+             (add-to-list 'mpc-status-callbacks cb)
+           (setq mpc-status-callbacks (delete cb mpc-status-callbacks)))
+         (set-default-toplevel-value symbol value)))
+
+(defvar mpc-notifications-id nil)
+
+(defun mpc-notifications-notify ()
+  "Display a notification with information about the current song."
+  (interactive)
+  (mpc-status-refresh)
+  (when-let (((string= "play" (alist-get 'state mpc-status)))
+             (title (or (alist-get 'Title mpc-status) "Unknown Title"))
+             (body (or (alist-get 'Artist mpc-status)
+                       (alist-get 'AlbumArtist mpc-status)
+                       "Unknown Artist"))
+             (icon (or (mpc-cover-image-find (alist-get 'file mpc-status))
+                       notifications-application-icon)))
+    (pcase system-type
+      ("windows-nt"
+       (w32-notification-notify :title title :body body :icon icon))
+      ("haiku"
+       (setq mpc-notifications-id
+             (haiku-notifications-notify :title title
+                                         :body body
+                                         :app-icon icon
+                                         :replaces-id mpc-notifications-id)))
+      ("android"
+       (setq mpc-notifications-id
+             (android-notifications-notify :title title
+                                           :body body
+                                           :icon icon
+                                           :replaces-id mpc-notifications-id))
+       (android-notifications-notify :title title :body body :icon icon))
+      (_ (when (notifications-get-server-information)
+           (setq mpc-notifications-id
+                 (notifications-notify :title title
+                                       :body body
+                                       :app-icon icon
+                                       :replaces-id mpc-notifications-id)))))))
+
+(defun mpc-cover-image-find (file)
+  "Find cover image for FILE."
+  (and-let* ((default-directory mpc-mpd-music-directory)
+             (dir (file-name-directory file))
+             (files (directory-files (mpc-file-local-copy dir)))
+             (cover (seq-find #'mpc-cover-image-p files))
+             ((expand-file-name cover dir)))))
+
+(defun mpc-cover-image-p (file)
+  "Check if FILE is a cover image."
+  (let ((covers '(".folder.png" "folder.png" "cover.jpg" "folder.jpg")))
+    (or (seq-find (lambda (cover) (string= file cover)) covers)
+        (and mpc-cover-image-re (string-match-p mpc-cover-image-re file)))))
+
+(when mpc-notifications
+  (add-to-list 'mpc-status-callbacks
+               (cons 'file mpc-notifications-function)))
+
 ;;; Toplevel ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 (defcustom mpc-frame-alist '((name . "MPC") (tool-bar-lines . 1)
-- 
2.46.2