]> code.delx.au - gnu-emacs/blobdiff - lisp/notifications.el
Merge from emacs-24; up to 2012-05-02T11:38:01Z!lekktu@gmail.com
[gnu-emacs] / lisp / notifications.el
index b825eb65abfbdbda752f445bbc6226196130712a..792298c26b750c755753f67525be0b3d2565712c 100644 (file)
@@ -23,7 +23,7 @@
 ;;; Commentary:
 
 ;; This package provides an implementation of the Desktop Notifications
-;; <http://www.galago-project.org/specs/notification/>.
+;; <http://developer.gnome.org/notification-spec/>.
 
 ;; In order to activate this package, you must add the following code
 ;; into your .emacs:
 ;; active D-Bus session bus.
 
 ;;; Code:
-(eval-when-compile
-  (require 'cl))
-
-;; Pacify byte-compiler.  D-Bus support in the Emacs core can be
-;; disabled with configuration option "--without-dbus".  Declare used
-;; subroutines and variables of `dbus' therefore.
-(declare-function dbus-call-method "dbusbind.c")
-(declare-function dbus-register-signal "dbusbind.c")
-
 (require 'dbus)
 
-(defconst notifications-specification-version "1.1"
+(defconst notifications-specification-version "1.2"
   "The version of the Desktop Notifications Specification implemented.")
 
 (defconst notifications-application-name "Emacs"
   "D-Bus notifications service path.")
 
 (defconst notifications-interface "org.freedesktop.Notifications"
-  "D-Bus notifications service path.")
+  "D-Bus notifications service interface.")
 
 (defconst notifications-notify-method "Notify"
-  "D-Bus notifications service path.")
+  "D-Bus notifications notify method.")
 
 (defconst notifications-close-notification-method "CloseNotification"
-  "D-Bus notifications service path.")
+  "D-Bus notifications close notification method.")
+
+(defconst notifications-get-capabilities-method "GetCapabilities"
+  "D-Bus notifications get capabilities method.")
 
 (defconst notifications-action-signal "ActionInvoked"
   "D-Bus notifications action signal.")
@@ -99,8 +93,9 @@
 
 (defun notifications-on-action-signal (id action)
   "Dispatch signals to callback functions from `notifications-on-action-map'."
-  (let* ((unique-name (dbus-event-service-name last-input-event))
-        (entry (assoc (cons unique-name id) notifications-on-action-map)))
+  (let* ((bus (dbus-event-bus-name last-input-event))
+        (unique-name (dbus-event-service-name last-input-event))
+        (entry (assoc (list bus unique-name id) notifications-on-action-map)))
     (when entry
       (funcall (cadr entry) id action)
       (when (and (not (setq notifications-on-action-map
   "Dispatch signals to callback functions from `notifications-on-closed-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))
+  (let* ((bus (dbus-event-bus-name last-input-event))
+        (unique-name (dbus-event-service-name last-input-event))
+        (entry (assoc (list bus unique-name id) notifications-on-close-map))
         (reason (or reason 4)))
     (when entry
       (funcall (cadr entry)
   "Send notification via D-Bus using the Freedesktop notification protocol.
 Various PARAMS can be set:
 
+ :bus            The D-Bus bus, if different from `:session'.
  :title          The notification title.
  :body           The notification body text.
  :app-name       The name of the application sending the notification.
@@ -154,6 +151,8 @@ Various PARAMS can be set:
                  Default value is -1.
  :urgency        The urgency level.
                  Either `low', `normal' or `critical'.
+ :action-items   Whether the TITLE of the actions is interpreted as
+                 a named icon.
  :category       The type of notification this is.
  :desktop-entry  This specifies the name of the desktop filename representing
                  the calling program.
@@ -170,6 +169,11 @@ Various PARAMS can be set:
                  be \"message-new-instant\".
  :suppress-sound Causes the server to suppress playing any sounds, if it has
                  that ability.
+ :resident       When set the server will not automatically remove the
+                 notification when an action has been invoked.
+ :transient      When set the server will treat the notification as transient
+                 and by-pass the server's persistence capability, if it
+                 should exist.
  :x              Specifies the X location on the screen that the notification
                  should point to.  The \"y\" hint must also be specified.
  :y              Specifies the Y location on the screen that the notification
@@ -188,11 +192,15 @@ Various PARAMS can be set:
                    - `undefined' if the notification server hasn't provided
                      a reason
 
+Which parameters are accepted by the notification server can be
+checked via `notifications-get-capabilities'.
+
 This function returns a notification id, an integer, which can be
 used to manipulate the notification item with
 `notifications-close-notification' or the `:replaces-id' argument
 of another `notifications-notify' call."
-  (let ((title (plist-get params :title))
+  (let ((bus (or (plist-get params :bus) :session))
+       (title (plist-get params :title))
         (body (plist-get params :body))
         (app-name (plist-get params :app-name))
         (replaces-id (plist-get params :replaces-id))
@@ -206,9 +214,12 @@ of another `notifications-notify' call."
         (desktop-entry (plist-get params :desktop-entry))
         (image-data (plist-get params :image-data))
         (image-path (plist-get params :image-path))
+       (action-items (plist-get params :action-items))
         (sound-file (plist-get params :sound-file))
         (sound-name (plist-get params :sound-name))
         (suppress-sound (plist-get params :suppress-sound))
+       (resident (plist-get params :resident))
+       (transient (plist-get params :transient))
         (x (plist-get params :x))
         (y (plist-get params :y))
         id)
@@ -216,10 +227,10 @@ of another `notifications-notify' call."
     (when urgency
       (add-to-list 'hints `(:dict-entry
                             "urgency"
-                            (:variant :byte ,(case urgency
-                                               (low 0)
-                                               (critical 2)
-                                               (t 1)))) t))
+                            (:variant :byte ,(pcase urgency
+                                               (`low 0)
+                                               (`critical 2)
+                                               (_ 1)))) t))
     (when category
       (add-to-list 'hints `(:dict-entry
                             "category"
@@ -230,12 +241,16 @@ of another `notifications-notify' call."
                             (:variant :string ,desktop-entry)) t))
     (when image-data
       (add-to-list 'hints `(:dict-entry
-                            "image_data"
+                            "image-data"
                             (:variant :struct ,image-data)) t))
     (when image-path
       (add-to-list 'hints `(:dict-entry
-                            "image_path"
+                            "image-path"
                             (:variant :string ,image-path)) t))
+    (when action-items
+      (add-to-list 'hints `(:dict-entry
+                            "action-items"
+                            (:variant :boolean ,action-items)) t))
     (when sound-file
       (add-to-list 'hints `(:dict-entry
                             "sound-file"
@@ -248,14 +263,22 @@ of another `notifications-notify' call."
       (add-to-list 'hints `(:dict-entry
                             "suppress-sound"
                             (:variant :boolean ,suppress-sound)) t))
+    (when resident
+      (add-to-list 'hints `(:dict-entry
+                            "resident"
+                            (:variant :boolean ,resident)) t))
+    (when transient
+      (add-to-list 'hints `(:dict-entry
+                            "transient"
+                            (:variant :boolean ,transient)) t))
     (when x
       (add-to-list 'hints `(:dict-entry "x" (:variant :int32 ,x)) t))
     (when y
       (add-to-list 'hints `(:dict-entry "y" (:variant :int32 ,y)) t))
 
-    ;; Call Notify method
+    ;; Call Notify method.
     (setq id
-          (dbus-call-method :session
+          (dbus-call-method bus
                             notifications-service
                             notifications-path
                             notifications-interface
@@ -283,14 +306,14 @@ of another `notifications-notify' call."
     ;; restarted.
     (let ((on-action (plist-get params :on-action))
           (on-close (plist-get params :on-close))
-         (unique-name (dbus-get-name-owner :session notifications-service)))
+         (unique-name (dbus-get-name-owner bus notifications-service)))
       (when on-action
         (add-to-list 'notifications-on-action-map
-                    (list (cons unique-name id) on-action))
+                    (list (list bus unique-name id) on-action))
        (unless notifications-on-action-object
          (setq notifications-on-action-object
                (dbus-register-signal
-                :session
+                bus
                 nil
                 notifications-path
                 notifications-interface
@@ -299,11 +322,11 @@ of another `notifications-notify' call."
 
       (when on-close
         (add-to-list 'notifications-on-close-map
-                    (list (cons unique-name id) on-close))
+                    (list (list bus unique-name id) on-close))
        (unless notifications-on-close-object
          (setq notifications-on-close-object
                (dbus-register-signal
-                :session
+                bus
                 nil
                 notifications-path
                 notifications-interface
@@ -313,13 +336,47 @@ of another `notifications-notify' call."
     ;; Return notification id
     id))
 
-(defun notifications-close-notification (id)
-  "Close a notification with identifier ID."
-  (dbus-call-method :session
+(defun notifications-close-notification (id &optional bus)
+  "Close a notification with identifier ID.
+BUS can be a string denoting a D-Bus connection, the default is `:session'."
+  (dbus-call-method (or bus :session)
                     notifications-service
                     notifications-path
                     notifications-interface
                     notifications-close-notification-method
                     :int32 id))
 
+(defvar dbus-debug) ; used in the macroexpansion of dbus-ignore-errors
+
+(defun notifications-get-capabilities (&optional bus)
+  "Return the capabilities of the notification server, a list of strings.
+BUS can be a string denoting a D-Bus connection, the default is `:session'.
+The following capabilities can be expected:
+
+  :actions         The server will provide the specified actions
+                   to the user.
+  :action-icons    Supports using icons instead of text for
+                   displaying actions.
+  :body            Supports body text.
+  :body-hyperlinks The server supports hyperlinks in the notifications.
+  :body-images     The server supports images in the notifications.
+  :body-markup     Supports markup in the body text.
+  :icon-multi      The server will render an animation of all the
+                   frames in a given image array.
+  :icon-static     Supports display of exactly 1 frame of any
+                   given image array.  This value is mutually exclusive
+                   with `:icon-multi'.
+  :persistence     The server supports persistence of notifications.
+  :sound           The server supports sounds on notifications.
+
+Further vendor-specific caps start with `:x-vendor', like `:x-gnome-foo-cap'."
+  (dbus-ignore-errors
+   (mapcar
+    (lambda (x) (intern (concat ":" x)))
+    (dbus-call-method (or bus :session)
+                     notifications-service
+                     notifications-path
+                     notifications-interface
+                     notifications-get-capabilities-method))))
+
 (provide 'notifications)