lispref/markers.texi small change
[bpt/emacs.git] / lisp / notifications.el
index 68db58e..1b75c2c 100644 (file)
@@ -1,6 +1,6 @@
 ;;; notifications.el --- Client interface to desktop notifications.
 
-;; Copyright (C) 2010 Free Software Foundation, Inc.
+;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
 
 ;; Author: Julien Danjou <julien@danjou.info>
 ;; Keywords: comm desktop notifications
@@ -30,6 +30,9 @@
 ;;
 ;;   (require 'notifications)
 
+;; For proper usage, Emacs must be started in an environment with an
+;; active D-Bus session bus.
+
 ;;; Code:
 (eval-when-compile
   (require 'cl))
 
 (defun notifications-on-action-signal (id action)
   "Dispatch signals to callback functions from `notifications-on-action-map'."
-  (let ((entry (assoc id notifications-on-action-map)))
+  (let* ((unique-name (dbus-event-service-name last-input-event))
+        (entry (assoc (cons unique-name id) notifications-on-action-map)))
     (when entry
       (funcall (cadr entry) id action)
-      (remove entry 'notifications-on-action-map))))
-
-(dbus-register-signal
- :session
- notifications-service
- notifications-path
- notifications-interface
- notifications-action-signal
- 'notifications-on-action-signal)
-
-(defun notifications-on-closed-signal (id reason)
+      (remove entry notifications-on-action-map))))
+
+(when (fboundp 'dbus-register-signal)
+  (dbus-register-signal
+   :session
+   nil
+   notifications-path
+   notifications-interface
+   notifications-action-signal
+   'notifications-on-action-signal))
+
+(defun notifications-on-closed-signal (id &optional reason)
   "Dispatch signals to callback functions from `notifications-on-closed-map'."
-  (let ((entry (assoc id notifications-on-close-map)))
+  ;; notification-daemon prior 0.4.0 does not send a reason.  So we
+  ;; make it optional, and assume `undefined' as default.
+  (let* ((unique-name (dbus-event-service-name last-input-event))
+        (entry (assoc (cons unique-name id) notifications-on-close-map))
+        (reason (or reason 4)))
     (when entry
       (funcall (cadr entry)
               id (cadr (assoc reason notifications-closed-reason)))
-      (remove entry 'notifications-on-close-map))))
+      (remove entry notifications-on-close-map))))
 
-(dbus-register-signal
- :session
- notifications-service
- notifications-path
- notifications-interface
- notifications-closed-signal
- 'notifications-on-closed-signal)
+(when (fboundp 'dbus-register-signal)
+  (dbus-register-signal
+   :session
+   nil
+   notifications-path
+   notifications-interface
+   notifications-closed-signal
+   'notifications-on-closed-signal))
 
 (defun notifications-notify (&rest params)
   "Send notification via D-Bus using the Freedesktop notification protocol.
@@ -158,7 +168,7 @@ Various PARAMS can be set:
                  only URI schema supported right now) or a name
                  in a freedesktop.org-compliant icon theme.
  :sound-file     The path to a sound file to play when the notification pops up.
- :sound-name     A themeable named sound from the freedesktop.org sound naming
+ :sound-name     A themable named sound from the freedesktop.org sound naming
                  specification to play when the notification pops up.
                  Similar to icon-name,only for sounds. An example would
                  be \"message-new-instant\".
@@ -182,7 +192,7 @@ Various PARAMS can be set:
 
 This function returns a notification id, an integer, which can be
 used to manipulate the notification item with
-`notifications-close'."
+`notifications-close-notification'."
   (let ((title (plist-get params :title))
         (body (plist-get params :body))
         (app-name (plist-get params :app-name))
@@ -208,8 +218,8 @@ used to manipulate the notification item with
       (add-to-list 'hints `(:dict-entry
                             "urgency"
                             (:variant :byte ,(case urgency
-                                               ('low 0)
-                                               ('critical 2)
+                                               (low 0)
+                                               (critical 2)
                                                (t 1)))) t))
     (when category
       (add-to-list 'hints `(:dict-entry
@@ -269,13 +279,18 @@ used to manipulate the notification item with
                             (or hints '(:array :signature "{sv}"))
                             :int32 (or timeout -1)))
 
-    ;; Register close/action callback function
+    ;; Register close/action callback function.  We must also remember
+    ;; the daemon's unique name, because the daemon could have
+    ;; restarted.
     (let ((on-action (plist-get params :on-action))
-          (on-close (plist-get params :on-close)))
+          (on-close (plist-get params :on-close))
+         (unique-name (dbus-get-name-owner :session notifications-service)))
       (when on-action
-        (add-to-list 'notifications-on-action-map (list id on-action)))
+        (add-to-list 'notifications-on-action-map
+                    (list (cons unique-name id) on-action)))
       (when on-close
-        (add-to-list 'notifications-on-close-map (list id on-close))))
+        (add-to-list 'notifications-on-close-map
+                    (list (cons unique-name id) on-close))))
 
     ;; Return notification id
     id))