-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.el
140 lines (121 loc) · 5.31 KB
/
notify.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
;;; notify.el --- notification front-end
;; Copyright (C) 2008 Mark A. Hershberger
;; Original Author: Mark A. Hershberger <[email protected]>
;; Modified by Andrey Kotlarski <[email protected]>
;; Modified by Andrew Gwozdziewycz <[email protected]>
;; Modified by Aidan Gauland <[email protected]> October 2011
;; Keywords: extensions, convenience, lisp
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; This provides a single function, `notify', that will produce a notify
;; pop-up via D-Bus, libnotify, simple message or growl.
;; To use, just put (autoload 'notify "notify" "Notify TITLE, BODY.")
;; in your init file. You may override default chosen notification
;; method by assigning `notify-method' to one of 'notify-via-dbus
;; 'notify-via-libnotify or 'notify-via-message
;;; Code:
(defvar notify-defaults (list :app "Emacs" :icon "emacs" :timeout 5000
:urgency "low"
:category "emacs.message")
"Notification settings' defaults.
May be overridden with key-value additional arguments to `notify'.")
(defvar notify-delay '(0 5 0)
"Minimum time allowed between notifications in time format.")
(defvar notify-last-notification '(0 0 0) "Time of last notification.")
(defvar notify-method nil "Notification method among
'notify-via-dbus, 'notify-via-libnotify, 'notify-via-message or
'notify-via-growl")
;; determine notification method unless already set
;; prefer growl > D-Bus > libnotify > message
(cond
((null notify-method)
(setq notify-method
(cond
((executable-find "growlnotify") 'notify-via-growl)
((and (require 'dbus nil t)
(dbus-ping :session "org.freedesktop.Notifications"))
(defvar notify-id 0 "Current D-Bus notification id.")
'notify-via-dbus)
((executable-find "notify-send") 'notify-via-libnotify)
(t 'notify-via-message))))
((eq notify-method 'notify-via-dbus) ;housekeeping for pre-chosen DBus
(if (and (require 'dbus nil t)
(dbus-ping :session "org.freedesktop.Notifications"))
(defvar notify-id 0 "Current D-Bus notification id.")
(setq notify-method (if (executable-find "notify-send")
'notify-via-libnotify
'notify-via-message))))
((and (eq notify-method 'notify-via-libnotify)
(not (executable-find "notify-send"))) ;housekeeping for pre-chosen libnotify
(setq notify-method
(if (and (require 'dbus nil t)
(dbus-ping :session "org.freedesktop.Notifications"))
(progn
(defvar notify-id 0 "Current D-Bus notification id.")
'notify-via-dbus)
'notify-via-message)))
((and (eq notify-method 'notify-via-growl)
(not (executable-find "growlnotify")))
(setq notify-method 'notify-via-message)))
(defun notify-via-dbus (title body)
"Send notification with TITLE, BODY `D-Bus'."
(dbus-call-method :session "org.freedesktop.Notifications"
"/org/freedesktop/Notifications"
"org.freedesktop.Notifications" "Notify"
(get 'notify-defaults :app)
(setq notify-id (+ notify-id 1))
(get 'notify-defaults :icon) title body '(:array)
'(:array :signature "{sv}") ':int32
(get 'notify-defaults :timeout)))
(defun notify-via-libnotify (title body)
"Notify with TITLE, BODY via `libnotify'."
(call-process "notify-send" nil 0 nil
title body "-t"
(number-to-string (get 'notify-defaults :timeout))
"-i" (get 'notify-defaults :icon)
"-u" (get 'notify-defaults :urgency)
"-c" (get 'notify-defaults :category)))
(defun notify-via-message (title body)
"Notify TITLE, BODY with a simple message."
(message "%s: %s" title body))
(defun notify-via-growl (title body)
"Notify TITLE, BODY with a growl"
(call-process "growlnotify" nil 0 nil
"-a" (get 'notify-defaults :app)
"-t" (notify-via-growl-stringify title)
"-m" (notify-via-growl-stringify body)))
(defun notify-via-growl-stringify (thing)
(cond ((null thing) "")
((stringp thing) thing)
(t (format "%s" thing))))
(defun keywords-to-properties (symbol args &optional defaults)
"Add to SYMBOL's property list key-values from ARGS and DEFAULTS."
(when (consp defaults)
(keywords-to-properties symbol defaults))
(while args
(put symbol (car args) (cadr args))
(setq args (cddr args))))
;;;###autoload
(defun notify (title body &rest args)
"Notify TITLE, BODY via `notify-method'.
ARGS may be amongst :timeout, :icon, :urgency, :app and :category."
(when (time-less-p notify-delay
(time-since notify-last-notification))
(or (eq notify-method 'notify-via-message)
(keywords-to-properties 'notify-defaults args
notify-defaults))
(setq notify-last-notification (current-time))
(funcall notify-method title body)))
(provide 'notify)
;;; notify.el ends here