notifications.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. ;;; notifications.el --- Client interface to desktop notifications.
  2. ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
  3. ;; Author: Julien Danjou <julien@danjou.info>
  4. ;; Keywords: comm desktop notifications
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This package provides an implementation of the Desktop Notifications
  18. ;; <http://www.galago-project.org/specs/notification/>.
  19. ;; In order to activate this package, you must add the following code
  20. ;; into your .emacs:
  21. ;;
  22. ;; (require 'notifications)
  23. ;; For proper usage, Emacs must be started in an environment with an
  24. ;; active D-Bus session bus.
  25. ;;; Code:
  26. (eval-when-compile
  27. (require 'cl))
  28. ;; Pacify byte-compiler. D-Bus support in the Emacs core can be
  29. ;; disabled with configuration option "--without-dbus". Declare used
  30. ;; subroutines and variables of `dbus' therefore.
  31. (declare-function dbus-call-method "dbusbind.c")
  32. (declare-function dbus-register-signal "dbusbind.c")
  33. (require 'dbus)
  34. (defconst notifications-specification-version "1.1"
  35. "The version of the Desktop Notifications Specification implemented.")
  36. (defconst notifications-application-name "Emacs"
  37. "Default application name.")
  38. (defconst notifications-application-icon
  39. (expand-file-name
  40. "images/icons/hicolor/scalable/apps/emacs.svg"
  41. data-directory)
  42. "Default application icon.")
  43. (defconst notifications-service "org.freedesktop.Notifications"
  44. "D-Bus notifications service name.")
  45. (defconst notifications-path "/org/freedesktop/Notifications"
  46. "D-Bus notifications service path.")
  47. (defconst notifications-interface "org.freedesktop.Notifications"
  48. "D-Bus notifications service path.")
  49. (defconst notifications-notify-method "Notify"
  50. "D-Bus notifications service path.")
  51. (defconst notifications-close-notification-method "CloseNotification"
  52. "D-Bus notifications service path.")
  53. (defconst notifications-action-signal "ActionInvoked"
  54. "D-Bus notifications action signal.")
  55. (defconst notifications-closed-signal "NotificationClosed"
  56. "D-Bus notifications closed signal.")
  57. (defconst notifications-closed-reason
  58. '((1 expired)
  59. (2 dismissed)
  60. (3 close-notification)
  61. (4 undefined))
  62. "List of reasons why a notification has been closed.")
  63. (defvar notifications-on-action-map nil
  64. "Mapping between notification and action callback functions.")
  65. (defvar notifications-on-action-object nil
  66. "Object for registered on-action signal.")
  67. (defvar notifications-on-close-map nil
  68. "Mapping between notification and close callback functions.")
  69. (defvar notifications-on-close-object nil
  70. "Object for registered on-close signal.")
  71. (defun notifications-on-action-signal (id action)
  72. "Dispatch signals to callback functions from `notifications-on-action-map'."
  73. (let* ((unique-name (dbus-event-service-name last-input-event))
  74. (entry (assoc (cons unique-name id) notifications-on-action-map)))
  75. (when entry
  76. (funcall (cadr entry) id action)
  77. (when (and (not (setq notifications-on-action-map
  78. (remove entry notifications-on-action-map)))
  79. notifications-on-action-object)
  80. (dbus-unregister-object notifications-on-action-object)
  81. (setq notifications-on-action-object nil)))))
  82. (defun notifications-on-closed-signal (id &optional reason)
  83. "Dispatch signals to callback functions from `notifications-on-closed-map'."
  84. ;; notification-daemon prior 0.4.0 does not send a reason. So we
  85. ;; make it optional, and assume `undefined' as default.
  86. (let* ((unique-name (dbus-event-service-name last-input-event))
  87. (entry (assoc (cons unique-name id) notifications-on-close-map))
  88. (reason (or reason 4)))
  89. (when entry
  90. (funcall (cadr entry)
  91. id (cadr (assoc reason notifications-closed-reason)))
  92. (when (and (not (setq notifications-on-close-map
  93. (remove entry notifications-on-close-map)))
  94. notifications-on-close-object)
  95. (dbus-unregister-object notifications-on-close-object)
  96. (setq notifications-on-close-object nil)))))
  97. (defun notifications-notify (&rest params)
  98. "Send notification via D-Bus using the Freedesktop notification protocol.
  99. Various PARAMS can be set:
  100. :title The notification title.
  101. :body The notification body text.
  102. :app-name The name of the application sending the notification.
  103. Default to `notifications-application-name'.
  104. :replaces-id The notification ID that this notification replaces.
  105. :app-icon The notification icon.
  106. Default is `notifications-application-icon'.
  107. Set to nil if you do not want any icon displayed.
  108. :actions A list of actions in the form:
  109. (KEY TITLE KEY TITLE ...)
  110. where KEY and TITLE are both strings.
  111. The default action (usually invoked by clicking the
  112. notification) should have a key named \"default\".
  113. The title can be anything, though implementations are free
  114. not to display it.
  115. :timeout The timeout time in milliseconds since the display
  116. of the notification at which the notification should
  117. automatically close.
  118. If -1, the notification's expiration time is dependent
  119. on the notification server's settings, and may vary for
  120. the type of notification.
  121. If 0, the notification never expires.
  122. Default value is -1.
  123. :urgency The urgency level.
  124. Either `low', `normal' or `critical'.
  125. :category The type of notification this is.
  126. :desktop-entry This specifies the name of the desktop filename representing
  127. the calling program.
  128. :image-data This is a raw data image format which describes the width,
  129. height, rowstride, has alpha, bits per sample, channels and
  130. image data respectively.
  131. :image-path This is represented either as a URI (file:// is the
  132. only URI schema supported right now) or a name
  133. in a freedesktop.org-compliant icon theme.
  134. :sound-file The path to a sound file to play when the notification pops up.
  135. :sound-name A themable named sound from the freedesktop.org sound naming
  136. specification to play when the notification pops up.
  137. Similar to icon-name,only for sounds. An example would
  138. be \"message-new-instant\".
  139. :suppress-sound Causes the server to suppress playing any sounds, if it has
  140. that ability.
  141. :x Specifies the X location on the screen that the notification
  142. should point to. The \"y\" hint must also be specified.
  143. :y Specifies the Y location on the screen that the notification
  144. should point to. The \"x\" hint must also be specified.
  145. :on-action Function to call when an action is invoked.
  146. The notification id and the key of the action are passed
  147. as arguments to the function.
  148. :on-close Function to call when the notification has been closed
  149. by timeout or by the user.
  150. The function receive the notification id and the closing
  151. reason as arguments:
  152. - `expired' if the notification has expired
  153. - `dismissed' if the notification was dismissed by the user
  154. - `close-notification' if the notification was closed
  155. by a call to CloseNotification
  156. - `undefined' if the notification server hasn't provided
  157. a reason
  158. This function returns a notification id, an integer, which can be
  159. used to manipulate the notification item with
  160. `notifications-close-notification' or the `:replaces-id' argument
  161. of another `notifications-notify' call."
  162. (let ((title (plist-get params :title))
  163. (body (plist-get params :body))
  164. (app-name (plist-get params :app-name))
  165. (replaces-id (plist-get params :replaces-id))
  166. (app-icon (plist-get params :app-icon))
  167. (actions (plist-get params :actions))
  168. (timeout (plist-get params :timeout))
  169. ;; Hints
  170. (hints '())
  171. (urgency (plist-get params :urgency))
  172. (category (plist-get params :category))
  173. (desktop-entry (plist-get params :desktop-entry))
  174. (image-data (plist-get params :image-data))
  175. (image-path (plist-get params :image-path))
  176. (sound-file (plist-get params :sound-file))
  177. (sound-name (plist-get params :sound-name))
  178. (suppress-sound (plist-get params :suppress-sound))
  179. (x (plist-get params :x))
  180. (y (plist-get params :y))
  181. id)
  182. ;; Build hints array
  183. (when urgency
  184. (add-to-list 'hints `(:dict-entry
  185. "urgency"
  186. (:variant :byte ,(case urgency
  187. (low 0)
  188. (critical 2)
  189. (t 1)))) t))
  190. (when category
  191. (add-to-list 'hints `(:dict-entry
  192. "category"
  193. (:variant :string ,category)) t))
  194. (when desktop-entry
  195. (add-to-list 'hints `(:dict-entry
  196. "desktop-entry"
  197. (:variant :string ,desktop-entry)) t))
  198. (when image-data
  199. (add-to-list 'hints `(:dict-entry
  200. "image_data"
  201. (:variant :struct ,image-data)) t))
  202. (when image-path
  203. (add-to-list 'hints `(:dict-entry
  204. "image_path"
  205. (:variant :string ,image-path)) t))
  206. (when sound-file
  207. (add-to-list 'hints `(:dict-entry
  208. "sound-file"
  209. (:variant :string ,sound-file)) t))
  210. (when sound-name
  211. (add-to-list 'hints `(:dict-entry
  212. "sound-name"
  213. (:variant :string ,sound-name)) t))
  214. (when suppress-sound
  215. (add-to-list 'hints `(:dict-entry
  216. "suppress-sound"
  217. (:variant :boolean ,suppress-sound)) t))
  218. (when x
  219. (add-to-list 'hints `(:dict-entry "x" (:variant :int32 ,x)) t))
  220. (when y
  221. (add-to-list 'hints `(:dict-entry "y" (:variant :int32 ,y)) t))
  222. ;; Call Notify method
  223. (setq id
  224. (dbus-call-method :session
  225. notifications-service
  226. notifications-path
  227. notifications-interface
  228. notifications-notify-method
  229. :string (or app-name
  230. notifications-application-name)
  231. :uint32 (or replaces-id 0)
  232. :string (if app-icon
  233. (expand-file-name app-icon)
  234. ;; If app-icon is nil because user
  235. ;; requested it to be so, send the
  236. ;; empty string
  237. (if (plist-member params :app-icon)
  238. ""
  239. ;; Otherwise send the default icon path
  240. notifications-application-icon))
  241. :string (or title "")
  242. :string (or body "")
  243. `(:array ,@actions)
  244. (or hints '(:array :signature "{sv}"))
  245. :int32 (or timeout -1)))
  246. ;; Register close/action callback function. We must also remember
  247. ;; the daemon's unique name, because the daemon could have
  248. ;; restarted.
  249. (let ((on-action (plist-get params :on-action))
  250. (on-close (plist-get params :on-close))
  251. (unique-name (dbus-get-name-owner :session notifications-service)))
  252. (when on-action
  253. (add-to-list 'notifications-on-action-map
  254. (list (cons unique-name id) on-action))
  255. (unless notifications-on-action-object
  256. (setq notifications-on-action-object
  257. (dbus-register-signal
  258. :session
  259. nil
  260. notifications-path
  261. notifications-interface
  262. notifications-action-signal
  263. 'notifications-on-action-signal))))
  264. (when on-close
  265. (add-to-list 'notifications-on-close-map
  266. (list (cons unique-name id) on-close))
  267. (unless notifications-on-close-object
  268. (setq notifications-on-close-object
  269. (dbus-register-signal
  270. :session
  271. nil
  272. notifications-path
  273. notifications-interface
  274. notifications-closed-signal
  275. 'notifications-on-closed-signal)))))
  276. ;; Return notification id
  277. id))
  278. (defun notifications-close-notification (id)
  279. "Close a notification with identifier ID."
  280. (dbus-call-method :session
  281. notifications-service
  282. notifications-path
  283. notifications-interface
  284. notifications-close-notification-method
  285. :int32 id))
  286. (provide 'notifications)