1 ;;; notifications.el --- Client interface to desktop notifications.
3 ;; Copyright (C) 2010-2011 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://www.galago-project.org/specs/notification/>.
28 ;; In order to activate this package, you must add the following code
31 ;; (require 'notifications)
37 ;; Pacify byte-compiler. D-Bus support in the Emacs core can be
38 ;; disabled with configuration option "--without-dbus". Declare used
39 ;; subroutines and variables of `dbus' therefore.
40 (declare-function dbus-call-method
"dbusbind.c")
41 (declare-function dbus-register-signal
"dbusbind.c")
45 (defconst notifications-specification-version
"1.1"
46 "The version of the Desktop Notifications Specification implemented.")
48 (defconst notifications-application-name
"Emacs"
49 "Default application name.")
51 (defconst notifications-application-icon
53 "images/icons/hicolor/scalable/apps/emacs.svg"
55 "Default application icon.")
57 (defconst notifications-service
"org.freedesktop.Notifications"
58 "D-Bus notifications service name.")
60 (defconst notifications-path
"/org/freedesktop/Notifications"
61 "D-Bus notifications service path.")
63 (defconst notifications-interface
"org.freedesktop.Notifications"
64 "D-Bus notifications service path.")
66 (defconst notifications-notify-method
"Notify"
67 "D-Bus notifications service path.")
69 (defconst notifications-close-notification-method
"CloseNotification"
70 "D-Bus notifications service path.")
72 (defconst notifications-action-signal
"ActionInvoked"
73 "D-Bus notifications action signal.")
75 (defconst notifications-closed-signal
"NotificationClosed"
76 "D-Bus notifications closed signal.")
78 (defconst notifications-closed-reason
81 (3 close-notification
)
83 "List of reasons why a notification has been closed.")
85 (defvar notifications-on-action-map nil
86 "Mapping between notification and action callback functions.")
88 (defvar notifications-on-close-map nil
89 "Mapping between notification and close callback functions.")
91 (defun notifications-on-action-signal (id action
)
92 "Dispatch signals to callback functions from `notifications-on-action-map'."
93 (let ((entry (assoc id notifications-on-action-map
)))
95 (funcall (cadr entry
) id action
)
96 (remove entry
'notifications-on-action-map
))))
98 (when (fboundp 'dbus-register-signal
)
101 notifications-service
103 notifications-interface
104 notifications-action-signal
105 'notifications-on-action-signal
))
107 (defun notifications-on-closed-signal (id reason
)
108 "Dispatch signals to callback functions from `notifications-on-closed-map'."
109 (let ((entry (assoc id notifications-on-close-map
)))
111 (funcall (cadr entry
)
112 id
(cadr (assoc reason notifications-closed-reason
)))
113 (remove entry
'notifications-on-close-map
))))
115 (when (fboundp 'dbus-register-signal
)
116 (dbus-register-signal
118 notifications-service
120 notifications-interface
121 notifications-closed-signal
122 'notifications-on-closed-signal
))
124 (defun notifications-notify (&rest params
)
125 "Send notification via D-Bus using the Freedesktop notification protocol.
126 Various PARAMS can be set:
128 :title The notification title.
129 :body The notification body text.
130 :app-name The name of the application sending the notification.
131 Default to `notifications-application-name'.
132 :replaces-id The notification ID that this notification replaces.
133 :app-icon The notification icon.
134 Default is `notifications-application-icon'.
135 Set to nil if you do not want any icon displayed.
136 :actions A list of actions in the form:
137 (KEY TITLE KEY TITLE ...)
138 where KEY and TITLE are both strings.
139 The default action (usually invoked by clicking the
140 notification) should have a key named \"default\".
141 The title can be anything, though implementations are free
143 :timeout The timeout time in milliseconds since the display
144 of the notification at which the notification should
146 If -1, the notification's expiration time is dependent
147 on the notification server's settings, and may vary for
148 the type of notification.
149 If 0, the notification never expires.
151 :urgency The urgency level.
152 Either `low', `normal' or `critical'.
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 themeable 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 :x Specifies the X location on the screen that the notification
170 should point to. The \"y\" hint must also be specified.
171 :y Specifies the Y location on the screen that the notification
172 should point to. The \"x\" hint must also be specified.
173 :on-action Function to call when an action is invoked.
174 The notification id and the key of the action are passed
175 as arguments to the function.
176 :on-close Function to call when the notification has been closed
177 by timeout or by the user.
178 The function receive the notification id and the closing
180 - `expired' if the notification has expired
181 - `dismissed' if the notification was dismissed by the user
182 - `close-notification' if the notification was closed
183 by a call to CloseNotification
185 This function returns a notification id, an integer, which can be
186 used to manipulate the notification item with
187 `notifications-close'."
188 (let ((title (plist-get params
:title
))
189 (body (plist-get params
:body
))
190 (app-name (plist-get params
:app-name
))
191 (replaces-id (plist-get params
:replaces-id
))
192 (app-icon (plist-get params
:app-icon
))
193 (actions (plist-get params
:actions
))
194 (timeout (plist-get params
:timeout
))
197 (urgency (plist-get params
:urgency
))
198 (category (plist-get params
:category
))
199 (desktop-entry (plist-get params
:desktop-entry
))
200 (image-data (plist-get params
:image-data
))
201 (image-path (plist-get params
:image-path
))
202 (sound-file (plist-get params
:sound-file
))
203 (sound-name (plist-get params
:sound-name
))
204 (suppress-sound (plist-get params
:suppress-sound
))
205 (x (plist-get params
:x
))
206 (y (plist-get params
:y
))
210 (add-to-list 'hints
`(:dict-entry
212 (:variant
:byte
,(case urgency
217 (add-to-list 'hints
`(:dict-entry
219 (:variant
:string
,category
)) t
))
221 (add-to-list 'hints
`(:dict-entry
223 (:variant
:string
,desktop-entry
)) t
))
225 (add-to-list 'hints
`(:dict-entry
227 (:variant
:struct
,image-data
)) t
))
229 (add-to-list 'hints
`(:dict-entry
231 (:variant
:string
,image-path
)) t
))
233 (add-to-list 'hints
`(:dict-entry
235 (:variant
:string
,sound-file
)) t
))
237 (add-to-list 'hints
`(:dict-entry
239 (:variant
:string
,sound-name
)) t
))
241 (add-to-list 'hints
`(:dict-entry
243 (:variant
:boolean
,suppress-sound
)) t
))
245 (add-to-list 'hints
`(:dict-entry
"x" (:variant
:int32
,x
)) t
))
247 (add-to-list 'hints
`(:dict-entry
"y" (:variant
:int32
,y
)) t
))
249 ;; Call Notify method
251 (dbus-call-method :session
252 notifications-service
254 notifications-interface
255 notifications-notify-method
257 notifications-application-name
)
258 :uint32
(or replaces-id
0)
260 (expand-file-name app-icon
)
261 ;; If app-icon is nil because user
262 ;; requested it to be so, send the
264 (if (plist-member params
:app-icon
)
266 ;; Otherwise send the default icon path
267 notifications-application-icon
))
268 :string
(or title
"")
271 (or hints
'(:array
:signature
"{sv}"))
272 :int32
(or timeout -
1)))
274 ;; Register close/action callback function
275 (let ((on-action (plist-get params
:on-action
))
276 (on-close (plist-get params
:on-close
)))
278 (add-to-list 'notifications-on-action-map
(list id on-action
)))
280 (add-to-list 'notifications-on-close-map
(list id on-close
))))
282 ;; Return notification id
285 (defun notifications-close-notification (id)
286 "Close a notification with identifier ID."
287 (dbus-call-method :session
288 notifications-service
290 notifications-interface
291 notifications-close-notification-method
294 (provide 'notifications
)