]> git.rkrishnan.org Git - .emacs.d.git/blob - emacs/notify.el
submodulized .emacs.d setup
[.emacs.d.git] / emacs / notify.el
1 ;;; notify.el --- notification frontend
2
3 ;; Copyright (C) 2008  Mark A. Hershberger
4
5 ;; Original Author: Mark A. Hershberger <mhersberger@intrahealth.org>
6 ;; Keywords: extensions, convenience, lisp
7
8 ;; This file is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; This file is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to
20 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;; This provides a single function, notify, that will produce a notify
26 ;; pop-up via DBus.
27
28 ;;; Code:
29
30 (defvar notify-last '(0 0 0))
31
32 (defvar notify-defaults
33   (list :app "GNU Emacs"
34         :icon "/usr/share/icons/emacs22/emacs_48.png"
35         :timeout 10000
36         :urgency "low"
37         :category "emacs.message"))
38
39 (defvar notify-id 0)
40
41 (defvar notify-delay '(0 5 0))
42
43 (defvar notify-last-notification '(0 5 0))
44
45 ;; We could set up other notification methods like notify-via-shell or
46 ;; notify-via-pointer
47 (defvar notify-method 'notify-via-dbus)
48
49 (defun notify-next-id ()
50   "Return the next notification id."
51   (setq notify-id (+ notify-id 1)))
52
53 (defun notify-via-dbus (title body params)
54   "Send notification via DBus."
55   (when (and (fboundp 'dbus-ping)
56              (dbus-ping :session "org.freedesktop.Notifications"))
57     (dbus-call-method :session "org.freedesktop.Notifications"
58                       "/org/freedesktop/Notifications"
59                       "org.freedesktop.Notifications" "Notify"
60                       (get 'params :app)
61                       (notify-next-id)
62                       (get 'params :icon)
63                       title
64                       body
65                       '(:array)
66                       '(:array :signature "{sv}")
67                       ':int32 (get 'params :timeout))))
68
69 (defun notify (title body &rest args)
70   "Use pop-up notifications for events."
71   (when (and
72          (time-less-p notify-delay
73                       (time-since notify-last-notification))
74          (let ((params))
75            (keywords-to-properties 'params args notify-defaults)
76            (setq notify-last-notification (current-time))
77            (funcall notify-method title body params)))))
78
79 (defun keywords-to-properties (symbol args &optional defaults)
80   "Convert a list in the form (:keywordA valueA
81                                :keywordB valueB ...)
82 to a list of propertys with the given values"
83   (when (car-safe defaults)  ; probably need to avoid recursion
84     (keywords-to-properties symbol defaults))
85   (while args
86       (let ((arg (car args)))
87         (setq args (cdr args))
88         (unless (symbolp arg)
89           (error "Junk in args %S" args))
90         (let ((keyword arg)
91               (value (car args)))
92           (unless args
93             (error "Keyword %s is missing an argument" keyword))
94           (setq args (cdr args))
95           (put symbol keyword value)))))
96
97 (provide 'notify)
98
99 ;;; notify.el ends here