More CL cleanups and reduction of use of cl.el.
[bpt/emacs.git] / lisp / notifications.el
CommitLineData
41a86354
MA
1;;; notifications.el --- Client interface to desktop notifications.
2
acaf905b 3;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
41a86354
MA
4
5;; Author: Julien Danjou <julien@danjou.info>
6;; Keywords: comm desktop notifications
7
8;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23;;; Commentary:
24
25;; This package provides an implementation of the Desktop Notifications
e43042fe 26;; <http://developer.gnome.org/notification-spec/>.
41a86354
MA
27
28;; In order to activate this package, you must add the following code
29;; into your .emacs:
30;;
31;; (require 'notifications)
32
7f5c46c7
MA
33;; For proper usage, Emacs must be started in an environment with an
34;; active D-Bus session bus.
35
41a86354 36;;; Code:
41a86354
MA
37(require 'dbus)
38
e43042fe 39(defconst notifications-specification-version "1.2"
7ea2d383
MA
40 "The version of the Desktop Notifications Specification implemented.")
41
41a86354
MA
42(defconst notifications-application-name "Emacs"
43 "Default application name.")
44
45(defconst notifications-application-icon
46 (expand-file-name
47 "images/icons/hicolor/scalable/apps/emacs.svg"
48 data-directory)
49 "Default application icon.")
50
51(defconst notifications-service "org.freedesktop.Notifications"
52 "D-Bus notifications service name.")
53
54(defconst notifications-path "/org/freedesktop/Notifications"
55 "D-Bus notifications service path.")
56
57(defconst notifications-interface "org.freedesktop.Notifications"
b613912b 58 "D-Bus notifications service interface.")
41a86354
MA
59
60(defconst notifications-notify-method "Notify"
b613912b 61 "D-Bus notifications notify method.")
41a86354
MA
62
63(defconst notifications-close-notification-method "CloseNotification"
b613912b
MA
64 "D-Bus notifications close notification method.")
65
66(defconst notifications-get-capabilities-method "GetCapabilities"
67 "D-Bus notifications get capabilities method.")
41a86354
MA
68
69(defconst notifications-action-signal "ActionInvoked"
70 "D-Bus notifications action signal.")
71
72(defconst notifications-closed-signal "NotificationClosed"
73 "D-Bus notifications closed signal.")
74
75(defconst notifications-closed-reason
76 '((1 expired)
77 (2 dismissed)
78 (3 close-notification)
79 (4 undefined))
80 "List of reasons why a notification has been closed.")
81
82(defvar notifications-on-action-map nil
83 "Mapping between notification and action callback functions.")
84
6612a284
MA
85(defvar notifications-on-action-object nil
86 "Object for registered on-action signal.")
87
41a86354
MA
88(defvar notifications-on-close-map nil
89 "Mapping between notification and close callback functions.")
90
6612a284
MA
91(defvar notifications-on-close-object nil
92 "Object for registered on-close signal.")
93
41a86354 94(defun notifications-on-action-signal (id action)
fa4003da 95 "Dispatch signals to callback functions from `notifications-on-action-map'."
a41a6cf4
MA
96 (let* ((unique-name (dbus-event-service-name last-input-event))
97 (entry (assoc (cons unique-name id) notifications-on-action-map)))
98 (when entry
fa4003da 99 (funcall (cadr entry) id action)
6612a284
MA
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)))))
41a86354 105
5b77774d 106(defun notifications-on-closed-signal (id &optional reason)
fa4003da 107 "Dispatch signals to callback functions from `notifications-on-closed-map'."
5b77774d
MA
108 ;; notification-daemon prior 0.4.0 does not send a reason. So we
109 ;; make it optional, and assume `undefined' as default.
a41a6cf4
MA
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)))
113 (when entry
fa4003da
MA
114 (funcall (cadr entry)
115 id (cadr (assoc reason notifications-closed-reason)))
6612a284
MA
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)))))
41a86354
MA
121
122(defun notifications-notify (&rest params)
123 "Send notification via D-Bus using the Freedesktop notification protocol.
124Various PARAMS can be set:
125
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.
b978141d
JB
137 The default action (usually invoked by clicking the
138 notification) should have a key named \"default\".
890a18d6 139 The title can be anything, though implementations are free
b978141d 140 not to display it.
41a86354
MA
141 :timeout The timeout time in milliseconds since the display
142 of the notification at which the notification should
143 automatically close.
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.
148 Default value is -1.
149 :urgency The urgency level.
150 Either `low', `normal' or `critical'.
e43042fe
MA
151 :action-items Whether the TITLE of the actions is interpreted as
152 a named icon.
41a86354
MA
153 :category The type of notification this is.
154 :desktop-entry This specifies the name of the desktop filename representing
155 the calling program.
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.
7ea2d383
MA
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.
41a86354 162 :sound-file The path to a sound file to play when the notification pops up.
40ba43b4 163 :sound-name A themable named sound from the freedesktop.org sound naming
7ea2d383
MA
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\".
41a86354
MA
167 :suppress-sound Causes the server to suppress playing any sounds, if it has
168 that ability.
e43042fe
MA
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
55a71479 173 should exist.
41a86354 174 :x Specifies the X location on the screen that the notification
b978141d 175 should point to. The \"y\" hint must also be specified.
41a86354 176 :y Specifies the Y location on the screen that the notification
b978141d 177 should point to. The \"x\" hint must also be specified.
fa4003da
MA
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.
41a86354
MA
181 :on-close Function to call when the notification has been closed
182 by timeout or by the user.
fa4003da
MA
183 The function receive the notification id and the closing
184 reason as arguments:
41a86354
MA
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
66b907dc
MA
189 - `undefined' if the notification server hasn't provided
190 a reason
41a86354 191
b613912b
MA
192Which parameters are accepted by the notification server can be
193checked via `notifications-get-capabilities'.
194
41a86354
MA
195This function returns a notification id, an integer, which can be
196used to manipulate the notification item with
66b907dc
MA
197`notifications-close-notification' or the `:replaces-id' argument
198of another `notifications-notify' call."
41a86354
MA
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))
206 ;; Hints
207 (hints '())
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))
7ea2d383 212 (image-path (plist-get params :image-path))
e43042fe 213 (action-items (plist-get params :action-items))
41a86354 214 (sound-file (plist-get params :sound-file))
7ea2d383 215 (sound-name (plist-get params :sound-name))
41a86354 216 (suppress-sound (plist-get params :suppress-sound))
e43042fe
MA
217 (resident (plist-get params :resident))
218 (transient (plist-get params :transient))
41a86354
MA
219 (x (plist-get params :x))
220 (y (plist-get params :y))
221 id)
222 ;; Build hints array
223 (when urgency
224 (add-to-list 'hints `(:dict-entry
225 "urgency"
a464a6c7
SM
226 (:variant :byte ,(pcase urgency
227 (`low 0)
228 (`critical 2)
229 (_ 1)))) t))
41a86354
MA
230 (when category
231 (add-to-list 'hints `(:dict-entry
232 "category"
233 (:variant :string ,category)) t))
234 (when desktop-entry
235 (add-to-list 'hints `(:dict-entry
236 "desktop-entry"
237 (:variant :string ,desktop-entry)) t))
238 (when image-data
239 (add-to-list 'hints `(:dict-entry
e43042fe 240 "image-data"
41a86354 241 (:variant :struct ,image-data)) t))
7ea2d383
MA
242 (when image-path
243 (add-to-list 'hints `(:dict-entry
e43042fe 244 "image-path"
7ea2d383 245 (:variant :string ,image-path)) t))
e43042fe
MA
246 (when action-items
247 (add-to-list 'hints `(:dict-entry
248 "action-items"
249 (:variant :boolean ,action-items)) t))
41a86354
MA
250 (when sound-file
251 (add-to-list 'hints `(:dict-entry
252 "sound-file"
253 (:variant :string ,sound-file)) t))
7ea2d383
MA
254 (when sound-name
255 (add-to-list 'hints `(:dict-entry
256 "sound-name"
257 (:variant :string ,sound-name)) t))
41a86354
MA
258 (when suppress-sound
259 (add-to-list 'hints `(:dict-entry
260 "suppress-sound"
261 (:variant :boolean ,suppress-sound)) t))
e43042fe
MA
262 (when resident
263 (add-to-list 'hints `(:dict-entry
264 "resident"
265 (:variant :boolean ,resident)) t))
266 (when transient
267 (add-to-list 'hints `(:dict-entry
268 "transient"
269 (:variant :boolean ,transient)) t))
41a86354
MA
270 (when x
271 (add-to-list 'hints `(:dict-entry "x" (:variant :int32 ,x)) t))
272 (when y
273 (add-to-list 'hints `(:dict-entry "y" (:variant :int32 ,y)) t))
274
275 ;; Call Notify method
276 (setq id
277 (dbus-call-method :session
278 notifications-service
279 notifications-path
280 notifications-interface
281 notifications-notify-method
282 :string (or app-name
283 notifications-application-name)
284 :uint32 (or replaces-id 0)
285 :string (if app-icon
286 (expand-file-name app-icon)
287 ;; If app-icon is nil because user
288 ;; requested it to be so, send the
289 ;; empty string
290 (if (plist-member params :app-icon)
291 ""
292 ;; Otherwise send the default icon path
293 notifications-application-icon))
294 :string (or title "")
295 :string (or body "")
296 `(:array ,@actions)
297 (or hints '(:array :signature "{sv}"))
298 :int32 (or timeout -1)))
299
db976e3c
MA
300 ;; Register close/action callback function. We must also remember
301 ;; the daemon's unique name, because the daemon could have
302 ;; restarted.
41a86354 303 (let ((on-action (plist-get params :on-action))
a41a6cf4
MA
304 (on-close (plist-get params :on-close))
305 (unique-name (dbus-get-name-owner :session notifications-service)))
41a86354 306 (when on-action
a41a6cf4 307 (add-to-list 'notifications-on-action-map
6612a284
MA
308 (list (cons unique-name id) on-action))
309 (unless notifications-on-action-object
310 (setq notifications-on-action-object
311 (dbus-register-signal
312 :session
313 nil
314 notifications-path
315 notifications-interface
316 notifications-action-signal
317 'notifications-on-action-signal))))
318
41a86354 319 (when on-close
a41a6cf4 320 (add-to-list 'notifications-on-close-map
6612a284
MA
321 (list (cons unique-name id) on-close))
322 (unless notifications-on-close-object
323 (setq notifications-on-close-object
324 (dbus-register-signal
325 :session
326 nil
327 notifications-path
328 notifications-interface
329 notifications-closed-signal
330 'notifications-on-closed-signal)))))
41a86354
MA
331
332 ;; Return notification id
333 id))
334
335(defun notifications-close-notification (id)
336 "Close a notification with identifier ID."
337 (dbus-call-method :session
338 notifications-service
339 notifications-path
340 notifications-interface
341 notifications-close-notification-method
342 :int32 id))
343
55a71479
JB
344(defvar dbus-debug) ; used in the macroexpansion of dbus-ignore-errors
345
b613912b
MA
346(defun notifications-get-capabilities ()
347 "Return the capabilities of the notification server, a list of strings.
348The following capabilities can be expected:
349
400386db
MA
350 :actions The server will provide the specified actions
351 to the user.
352 :action-icons Supports using icons instead of text for
353 displaying actions.
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
362 with `:icon-multi'.
363 :persistence The server supports persistence of notifications.
364 :sound The server supports sounds on notifications.
e43042fe
MA
365
366Further vendor-specific caps start with `:x-vendor', like `:x-gnome-foo-cap'."
367 (dbus-ignore-errors
368 (mapcar
369 (lambda (x) (intern (concat ":" x)))
370 (dbus-call-method :session
371 notifications-service
372 notifications-path
373 notifications-interface
374 notifications-get-capabilities-method))))
b613912b 375
41a86354 376(provide 'notifications)