1 ;;; notifications.el --- Client interface to desktop notifications.
3 ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: comm desktop notifications
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This package provides an implementation of the Desktop Notifications
26 ;; <http://developer.gnome.org/notification-spec/>.
28 ;; In order to activate this package, you must add the following code
31 ;; (require 'notifications)
33 ;; For proper usage, Emacs must be started in an environment with an
34 ;; active D-Bus session bus.
39 (defconst notifications-specification-version
"1.2"
40 "The version of the Desktop Notifications Specification implemented.")
42 (defconst notifications-application-name
"Emacs"
43 "Default application name.")
45 (defconst notifications-application-icon
47 "images/icons/hicolor/scalable/apps/emacs.svg"
49 "Default application icon.")
51 (defconst notifications-service
"org.freedesktop.Notifications"
52 "D-Bus notifications service name.")
54 (defconst notifications-path
"/org/freedesktop/Notifications"
55 "D-Bus notifications service path.")
57 (defconst notifications-interface
"org.freedesktop.Notifications"
58 "D-Bus notifications service interface.")
60 (defconst notifications-notify-method
"Notify"
61 "D-Bus notifications notify method.")
63 (defconst notifications-close-notification-method
"CloseNotification"
64 "D-Bus notifications close notification method.")
66 (defconst notifications-get-capabilities-method
"GetCapabilities"
67 "D-Bus notifications get capabilities method.")
69 (defconst notifications-action-signal
"ActionInvoked"
70 "D-Bus notifications action signal.")
72 (defconst notifications-closed-signal
"NotificationClosed"
73 "D-Bus notifications closed signal.")
75 (defconst notifications-closed-reason
78 (3 close-notification
)
80 "List of reasons why a notification has been closed.")
82 (defvar notifications-on-action-map nil
83 "Mapping between notification and action callback functions.")
85 (defvar notifications-on-action-object nil
86 "Object for registered on-action signal.")
88 (defvar notifications-on-close-map nil
89 "Mapping between notification and close callback functions.")
91 (defvar notifications-on-close-object nil
92 "Object for registered on-close signal.")
94 (defun notifications-on-action-signal (id action
)
95 "Dispatch signals to callback functions from `notifications-on-action-map'."
96 (let* ((unique-name (dbus-event-service-name last-input-event
))
97 (entry (assoc (cons unique-name id
) notifications-on-action-map
)))
99 (funcall (cadr entry
) id action
)
100 (when (and (not (setq notifications-on-action-map
101 (remove entry notifications-on-action-map
)))
102 notifications-on-action-object
)
103 (dbus-unregister-object notifications-on-action-object
)
104 (setq notifications-on-action-object nil
)))))
106 (defun notifications-on-closed-signal (id &optional reason
)
107 "Dispatch signals to callback functions from `notifications-on-closed-map'."
108 ;; notification-daemon prior 0.4.0 does not send a reason. So we
109 ;; make it optional, and assume `undefined' as default.
110 (let* ((unique-name (dbus-event-service-name last-input-event
))
111 (entry (assoc (cons unique-name id
) notifications-on-close-map
))
112 (reason (or reason
4)))
114 (funcall (cadr entry
)
115 id
(cadr (assoc reason notifications-closed-reason
)))
116 (when (and (not (setq notifications-on-close-map
117 (remove entry notifications-on-close-map
)))
118 notifications-on-close-object
)
119 (dbus-unregister-object notifications-on-close-object
)
120 (setq notifications-on-close-object nil
)))))
122 (defun notifications-notify (&rest params
)
123 "Send notification via D-Bus using the Freedesktop notification protocol.
124 Various PARAMS can be set:
126 :title The notification title.
127 :body The notification body text.
128 :app-name The name of the application sending the notification.
129 Default to `notifications-application-name'.
130 :replaces-id The notification ID that this notification replaces.
131 :app-icon The notification icon.
132 Default is `notifications-application-icon'.
133 Set to nil if you do not want any icon displayed.
134 :actions A list of actions in the form:
135 (KEY TITLE KEY TITLE ...)
136 where KEY and TITLE are both strings.
137 The default action (usually invoked by clicking the
138 notification) should have a key named \"default\".
139 The title can be anything, though implementations are free
141 :timeout The timeout time in milliseconds since the display
142 of the notification at which the notification should
144 If -1, the notification's expiration time is dependent
145 on the notification server's settings, and may vary for
146 the type of notification.
147 If 0, the notification never expires.
149 :urgency The urgency level.
150 Either `low', `normal' or `critical'.
151 :action-items Whether the TITLE of the actions is interpreted as
153 :category The type of notification this is.
154 :desktop-entry This specifies the name of the desktop filename representing
156 :image-data This is a raw data image format which describes the width,
157 height, rowstride, has alpha, bits per sample, channels and
158 image data respectively.
159 :image-path This is represented either as a URI (file:// is the
160 only URI schema supported right now) or a name
161 in a freedesktop.org-compliant icon theme.
162 :sound-file The path to a sound file to play when the notification pops up.
163 :sound-name A themable named sound from the freedesktop.org sound naming
164 specification to play when the notification pops up.
165 Similar to icon-name,only for sounds. An example would
166 be \"message-new-instant\".
167 :suppress-sound Causes the server to suppress playing any sounds, if it has
169 :resident When set the server will not automatically remove the
170 notification when an action has been invoked.
171 :transient When set the server will treat the notification as transient
172 and by-pass the server's persistence capability, if it
174 :x Specifies the X location on the screen that the notification
175 should point to. The \"y\" hint must also be specified.
176 :y Specifies the Y location on the screen that the notification
177 should point to. The \"x\" hint must also be specified.
178 :on-action Function to call when an action is invoked.
179 The notification id and the key of the action are passed
180 as arguments to the function.
181 :on-close Function to call when the notification has been closed
182 by timeout or by the user.
183 The function receive the notification id and the closing
185 - `expired' if the notification has expired
186 - `dismissed' if the notification was dismissed by the user
187 - `close-notification' if the notification was closed
188 by a call to CloseNotification
189 - `undefined' if the notification server hasn't provided
192 Which parameters are accepted by the notification server can be
193 checked via `notifications-get-capabilities'.
195 This function returns a notification id, an integer, which can be
196 used to manipulate the notification item with
197 `notifications-close-notification' or the `:replaces-id' argument
198 of another `notifications-notify' call."
199 (let ((title (plist-get params
:title
))
200 (body (plist-get params
:body
))
201 (app-name (plist-get params
:app-name
))
202 (replaces-id (plist-get params
:replaces-id
))
203 (app-icon (plist-get params
:app-icon
))
204 (actions (plist-get params
:actions
))
205 (timeout (plist-get params
:timeout
))
208 (urgency (plist-get params
:urgency
))
209 (category (plist-get params
:category
))
210 (desktop-entry (plist-get params
:desktop-entry
))
211 (image-data (plist-get params
:image-data
))
212 (image-path (plist-get params
:image-path
))
213 (action-items (plist-get params
:action-items
))
214 (sound-file (plist-get params
:sound-file
))
215 (sound-name (plist-get params
:sound-name
))
216 (suppress-sound (plist-get params
:suppress-sound
))
217 (resident (plist-get params
:resident
))
218 (transient (plist-get params
:transient
))
219 (x (plist-get params
:x
))
220 (y (plist-get params
:y
))
224 (add-to-list 'hints
`(:dict-entry
226 (:variant
:byte
,(pcase urgency
231 (add-to-list 'hints
`(:dict-entry
233 (:variant
:string
,category
)) t
))
235 (add-to-list 'hints
`(:dict-entry
237 (:variant
:string
,desktop-entry
)) t
))
239 (add-to-list 'hints
`(:dict-entry
241 (:variant
:struct
,image-data
)) t
))
243 (add-to-list 'hints
`(:dict-entry
245 (:variant
:string
,image-path
)) t
))
247 (add-to-list 'hints
`(:dict-entry
249 (:variant
:boolean
,action-items
)) t
))
251 (add-to-list 'hints
`(:dict-entry
253 (:variant
:string
,sound-file
)) t
))
255 (add-to-list 'hints
`(:dict-entry
257 (:variant
:string
,sound-name
)) t
))
259 (add-to-list 'hints
`(:dict-entry
261 (:variant
:boolean
,suppress-sound
)) t
))
263 (add-to-list 'hints
`(:dict-entry
265 (:variant
:boolean
,resident
)) t
))
267 (add-to-list 'hints
`(:dict-entry
269 (:variant
:boolean
,transient
)) t
))
271 (add-to-list 'hints
`(:dict-entry
"x" (:variant
:int32
,x
)) t
))
273 (add-to-list 'hints
`(:dict-entry
"y" (:variant
:int32
,y
)) t
))
275 ;; Call Notify method
277 (dbus-call-method :session
278 notifications-service
280 notifications-interface
281 notifications-notify-method
283 notifications-application-name
)
284 :uint32
(or replaces-id
0)
286 (expand-file-name app-icon
)
287 ;; If app-icon is nil because user
288 ;; requested it to be so, send the
290 (if (plist-member params
:app-icon
)
292 ;; Otherwise send the default icon path
293 notifications-application-icon
))
294 :string
(or title
"")
297 (or hints
'(:array
:signature
"{sv}"))
298 :int32
(or timeout -
1)))
300 ;; Register close/action callback function. We must also remember
301 ;; the daemon's unique name, because the daemon could have
303 (let ((on-action (plist-get params
:on-action
))
304 (on-close (plist-get params
:on-close
))
305 (unique-name (dbus-get-name-owner :session notifications-service
)))
307 (add-to-list 'notifications-on-action-map
308 (list (cons unique-name id
) on-action
))
309 (unless notifications-on-action-object
310 (setq notifications-on-action-object
311 (dbus-register-signal
315 notifications-interface
316 notifications-action-signal
317 'notifications-on-action-signal
))))
320 (add-to-list 'notifications-on-close-map
321 (list (cons unique-name id
) on-close
))
322 (unless notifications-on-close-object
323 (setq notifications-on-close-object
324 (dbus-register-signal
328 notifications-interface
329 notifications-closed-signal
330 'notifications-on-closed-signal
)))))
332 ;; Return notification id
335 (defun notifications-close-notification (id)
336 "Close a notification with identifier ID."
337 (dbus-call-method :session
338 notifications-service
340 notifications-interface
341 notifications-close-notification-method
344 (defvar dbus-debug
) ; used in the macroexpansion of dbus-ignore-errors
346 (defun notifications-get-capabilities ()
347 "Return the capabilities of the notification server, a list of strings.
348 The following capabilities can be expected:
350 :actions The server will provide the specified actions
352 :action-icons Supports using icons instead of text for
354 :body Supports body text.
355 :body-hyperlinks The server supports hyperlinks in the notifications.
356 :body-images The server supports images in the notifications.
357 :body-markup Supports markup in the body text.
358 :icon-multi The server will render an animation of all the
359 frames in a given image array.
360 :icon-static Supports display of exactly 1 frame of any
361 given image array. This value is mutually exclusive
363 :persistence The server supports persistence of notifications.
364 :sound The server supports sounds on notifications.
366 Further vendor-specific caps start with `:x-vendor', like `:x-gnome-foo-cap'."
369 (lambda (x) (intern (concat ":" x
)))
370 (dbus-call-method :session
371 notifications-service
373 notifications-interface
374 notifications-get-capabilities-method
))))
376 (provide 'notifications
)