lib-src/emacsclient.c (min): Undef before redefining it.
[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
MA
36;;; Code:
37(eval-when-compile
38 (require 'cl))
39
41a86354
MA
40(require 'dbus)
41
e43042fe 42(defconst notifications-specification-version "1.2"
7ea2d383
MA
43 "The version of the Desktop Notifications Specification implemented.")
44
41a86354
MA
45(defconst notifications-application-name "Emacs"
46 "Default application name.")
47
48(defconst notifications-application-icon
49 (expand-file-name
50 "images/icons/hicolor/scalable/apps/emacs.svg"
51 data-directory)
52 "Default application icon.")
53
54(defconst notifications-service "org.freedesktop.Notifications"
55 "D-Bus notifications service name.")
56
57(defconst notifications-path "/org/freedesktop/Notifications"
58 "D-Bus notifications service path.")
59
60(defconst notifications-interface "org.freedesktop.Notifications"
b613912b 61 "D-Bus notifications service interface.")
41a86354
MA
62
63(defconst notifications-notify-method "Notify"
b613912b 64 "D-Bus notifications notify method.")
41a86354
MA
65
66(defconst notifications-close-notification-method "CloseNotification"
b613912b
MA
67 "D-Bus notifications close notification method.")
68
69(defconst notifications-get-capabilities-method "GetCapabilities"
70 "D-Bus notifications get capabilities method.")
41a86354
MA
71
72(defconst notifications-action-signal "ActionInvoked"
73 "D-Bus notifications action signal.")
74
75(defconst notifications-closed-signal "NotificationClosed"
76 "D-Bus notifications closed signal.")
77
78(defconst notifications-closed-reason
79 '((1 expired)
80 (2 dismissed)
81 (3 close-notification)
82 (4 undefined))
83 "List of reasons why a notification has been closed.")
84
85(defvar notifications-on-action-map nil
86 "Mapping between notification and action callback functions.")
87
6612a284
MA
88(defvar notifications-on-action-object nil
89 "Object for registered on-action signal.")
90
41a86354
MA
91(defvar notifications-on-close-map nil
92 "Mapping between notification and close callback functions.")
93
6612a284
MA
94(defvar notifications-on-close-object nil
95 "Object for registered on-close signal.")
96
41a86354 97(defun notifications-on-action-signal (id action)
fa4003da 98 "Dispatch signals to callback functions from `notifications-on-action-map'."
a41a6cf4
MA
99 (let* ((unique-name (dbus-event-service-name last-input-event))
100 (entry (assoc (cons unique-name id) notifications-on-action-map)))
101 (when entry
fa4003da 102 (funcall (cadr entry) id action)
6612a284
MA
103 (when (and (not (setq notifications-on-action-map
104 (remove entry notifications-on-action-map)))
105 notifications-on-action-object)
106 (dbus-unregister-object notifications-on-action-object)
107 (setq notifications-on-action-object nil)))))
41a86354 108
5b77774d 109(defun notifications-on-closed-signal (id &optional reason)
fa4003da 110 "Dispatch signals to callback functions from `notifications-on-closed-map'."
5b77774d
MA
111 ;; notification-daemon prior 0.4.0 does not send a reason. So we
112 ;; make it optional, and assume `undefined' as default.
a41a6cf4
MA
113 (let* ((unique-name (dbus-event-service-name last-input-event))
114 (entry (assoc (cons unique-name id) notifications-on-close-map))
115 (reason (or reason 4)))
116 (when entry
fa4003da
MA
117 (funcall (cadr entry)
118 id (cadr (assoc reason notifications-closed-reason)))
6612a284
MA
119 (when (and (not (setq notifications-on-close-map
120 (remove entry notifications-on-close-map)))
121 notifications-on-close-object)
122 (dbus-unregister-object notifications-on-close-object)
123 (setq notifications-on-close-object nil)))))
41a86354
MA
124
125(defun notifications-notify (&rest params)
126 "Send notification via D-Bus using the Freedesktop notification protocol.
127Various PARAMS can be set:
128
129 :title The notification title.
130 :body The notification body text.
131 :app-name The name of the application sending the notification.
132 Default to `notifications-application-name'.
133 :replaces-id The notification ID that this notification replaces.
134 :app-icon The notification icon.
135 Default is `notifications-application-icon'.
136 Set to nil if you do not want any icon displayed.
137 :actions A list of actions in the form:
138 (KEY TITLE KEY TITLE ...)
139 where KEY and TITLE are both strings.
b978141d
JB
140 The default action (usually invoked by clicking the
141 notification) should have a key named \"default\".
890a18d6 142 The title can be anything, though implementations are free
b978141d 143 not to display it.
41a86354
MA
144 :timeout The timeout time in milliseconds since the display
145 of the notification at which the notification should
146 automatically close.
147 If -1, the notification's expiration time is dependent
148 on the notification server's settings, and may vary for
149 the type of notification.
150 If 0, the notification never expires.
151 Default value is -1.
152 :urgency The urgency level.
153 Either `low', `normal' or `critical'.
e43042fe
MA
154 :action-items Whether the TITLE of the actions is interpreted as
155 a named icon.
41a86354
MA
156 :category The type of notification this is.
157 :desktop-entry This specifies the name of the desktop filename representing
158 the calling program.
159 :image-data This is a raw data image format which describes the width,
160 height, rowstride, has alpha, bits per sample, channels and
161 image data respectively.
7ea2d383
MA
162 :image-path This is represented either as a URI (file:// is the
163 only URI schema supported right now) or a name
164 in a freedesktop.org-compliant icon theme.
41a86354 165 :sound-file The path to a sound file to play when the notification pops up.
40ba43b4 166 :sound-name A themable named sound from the freedesktop.org sound naming
7ea2d383
MA
167 specification to play when the notification pops up.
168 Similar to icon-name,only for sounds. An example would
169 be \"message-new-instant\".
41a86354
MA
170 :suppress-sound Causes the server to suppress playing any sounds, if it has
171 that ability.
e43042fe
MA
172 :resident When set the server will not automatically remove the
173 notification when an action has been invoked.
174 :transient When set the server will treat the notification as transient
175 and by-pass the server's persistence capability, if it
176 should exist.
41a86354 177 :x Specifies the X location on the screen that the notification
b978141d 178 should point to. The \"y\" hint must also be specified.
41a86354 179 :y Specifies the Y location on the screen that the notification
b978141d 180 should point to. The \"x\" hint must also be specified.
fa4003da
MA
181 :on-action Function to call when an action is invoked.
182 The notification id and the key of the action are passed
183 as arguments to the function.
41a86354
MA
184 :on-close Function to call when the notification has been closed
185 by timeout or by the user.
fa4003da
MA
186 The function receive the notification id and the closing
187 reason as arguments:
41a86354
MA
188 - `expired' if the notification has expired
189 - `dismissed' if the notification was dismissed by the user
190 - `close-notification' if the notification was closed
191 by a call to CloseNotification
66b907dc
MA
192 - `undefined' if the notification server hasn't provided
193 a reason
41a86354 194
b613912b
MA
195Which parameters are accepted by the notification server can be
196checked via `notifications-get-capabilities'.
197
41a86354
MA
198This function returns a notification id, an integer, which can be
199used to manipulate the notification item with
66b907dc
MA
200`notifications-close-notification' or the `:replaces-id' argument
201of another `notifications-notify' call."
41a86354
MA
202 (let ((title (plist-get params :title))
203 (body (plist-get params :body))
204 (app-name (plist-get params :app-name))
205 (replaces-id (plist-get params :replaces-id))
206 (app-icon (plist-get params :app-icon))
207 (actions (plist-get params :actions))
208 (timeout (plist-get params :timeout))
209 ;; Hints
210 (hints '())
211 (urgency (plist-get params :urgency))
212 (category (plist-get params :category))
213 (desktop-entry (plist-get params :desktop-entry))
214 (image-data (plist-get params :image-data))
7ea2d383 215 (image-path (plist-get params :image-path))
e43042fe 216 (action-items (plist-get params :action-items))
41a86354 217 (sound-file (plist-get params :sound-file))
7ea2d383 218 (sound-name (plist-get params :sound-name))
41a86354 219 (suppress-sound (plist-get params :suppress-sound))
e43042fe
MA
220 (resident (plist-get params :resident))
221 (transient (plist-get params :transient))
41a86354
MA
222 (x (plist-get params :x))
223 (y (plist-get params :y))
224 id)
225 ;; Build hints array
226 (when urgency
227 (add-to-list 'hints `(:dict-entry
228 "urgency"
229 (:variant :byte ,(case urgency
0adf5618
SM
230 (low 0)
231 (critical 2)
41a86354
MA
232 (t 1)))) t))
233 (when category
234 (add-to-list 'hints `(:dict-entry
235 "category"
236 (:variant :string ,category)) t))
237 (when desktop-entry
238 (add-to-list 'hints `(:dict-entry
239 "desktop-entry"
240 (:variant :string ,desktop-entry)) t))
241 (when image-data
242 (add-to-list 'hints `(:dict-entry
e43042fe 243 "image-data"
41a86354 244 (:variant :struct ,image-data)) t))
7ea2d383
MA
245 (when image-path
246 (add-to-list 'hints `(:dict-entry
e43042fe 247 "image-path"
7ea2d383 248 (:variant :string ,image-path)) t))
e43042fe
MA
249 (when action-items
250 (add-to-list 'hints `(:dict-entry
251 "action-items"
252 (:variant :boolean ,action-items)) t))
41a86354
MA
253 (when sound-file
254 (add-to-list 'hints `(:dict-entry
255 "sound-file"
256 (:variant :string ,sound-file)) t))
7ea2d383
MA
257 (when sound-name
258 (add-to-list 'hints `(:dict-entry
259 "sound-name"
260 (:variant :string ,sound-name)) t))
41a86354
MA
261 (when suppress-sound
262 (add-to-list 'hints `(:dict-entry
263 "suppress-sound"
264 (:variant :boolean ,suppress-sound)) t))
e43042fe
MA
265 (when resident
266 (add-to-list 'hints `(:dict-entry
267 "resident"
268 (:variant :boolean ,resident)) t))
269 (when transient
270 (add-to-list 'hints `(:dict-entry
271 "transient"
272 (:variant :boolean ,transient)) t))
41a86354
MA
273 (when x
274 (add-to-list 'hints `(:dict-entry "x" (:variant :int32 ,x)) t))
275 (when y
276 (add-to-list 'hints `(:dict-entry "y" (:variant :int32 ,y)) t))
277
278 ;; Call Notify method
279 (setq id
280 (dbus-call-method :session
281 notifications-service
282 notifications-path
283 notifications-interface
284 notifications-notify-method
285 :string (or app-name
286 notifications-application-name)
287 :uint32 (or replaces-id 0)
288 :string (if app-icon
289 (expand-file-name app-icon)
290 ;; If app-icon is nil because user
291 ;; requested it to be so, send the
292 ;; empty string
293 (if (plist-member params :app-icon)
294 ""
295 ;; Otherwise send the default icon path
296 notifications-application-icon))
297 :string (or title "")
298 :string (or body "")
299 `(:array ,@actions)
300 (or hints '(:array :signature "{sv}"))
301 :int32 (or timeout -1)))
302
db976e3c
MA
303 ;; Register close/action callback function. We must also remember
304 ;; the daemon's unique name, because the daemon could have
305 ;; restarted.
41a86354 306 (let ((on-action (plist-get params :on-action))
a41a6cf4
MA
307 (on-close (plist-get params :on-close))
308 (unique-name (dbus-get-name-owner :session notifications-service)))
41a86354 309 (when on-action
a41a6cf4 310 (add-to-list 'notifications-on-action-map
6612a284
MA
311 (list (cons unique-name id) on-action))
312 (unless notifications-on-action-object
313 (setq notifications-on-action-object
314 (dbus-register-signal
315 :session
316 nil
317 notifications-path
318 notifications-interface
319 notifications-action-signal
320 'notifications-on-action-signal))))
321
41a86354 322 (when on-close
a41a6cf4 323 (add-to-list 'notifications-on-close-map
6612a284
MA
324 (list (cons unique-name id) on-close))
325 (unless notifications-on-close-object
326 (setq notifications-on-close-object
327 (dbus-register-signal
328 :session
329 nil
330 notifications-path
331 notifications-interface
332 notifications-closed-signal
333 'notifications-on-closed-signal)))))
41a86354
MA
334
335 ;; Return notification id
336 id))
337
338(defun notifications-close-notification (id)
339 "Close a notification with identifier ID."
340 (dbus-call-method :session
341 notifications-service
342 notifications-path
343 notifications-interface
344 notifications-close-notification-method
345 :int32 id))
346
b613912b
MA
347(defun notifications-get-capabilities ()
348 "Return the capabilities of the notification server, a list of strings.
349The following capabilities can be expected:
350
400386db
MA
351 :actions The server will provide the specified actions
352 to the user.
353 :action-icons Supports using icons instead of text for
354 displaying actions.
355 :body Supports body text.
356 :body-hyperlinks The server supports hyperlinks in the notifications.
357 :body-images The server supports images in the notifications.
358 :body-markup Supports markup in the body text.
359 :icon-multi The server will render an animation of all the
360 frames in a given image array.
361 :icon-static Supports display of exactly 1 frame of any
362 given image array. This value is mutually exclusive
363 with `:icon-multi'.
364 :persistence The server supports persistence of notifications.
365 :sound The server supports sounds on notifications.
e43042fe
MA
366
367Further vendor-specific caps start with `:x-vendor', like `:x-gnome-foo-cap'."
368 (dbus-ignore-errors
369 (mapcar
370 (lambda (x) (intern (concat ":" x)))
371 (dbus-call-method :session
372 notifications-service
373 notifications-path
374 notifications-interface
375 notifications-get-capabilities-method))))
b613912b 376
41a86354 377(provide 'notifications)