Fix typo.
[riece] / lisp / riece-desktop-notify.el
1 ;;; riece-desktop-notify.el --- Display notification to desktop
2 ;; Copyright (C) 2009 OHASHI Akira
3
4 ;; Author: OHASHI Akira <bg66@koka-in.org>
5 ;; Created: 2009-03-29
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
10 ;; This program 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 2, or (at your option)
13 ;; any later version.
14
15 ;; This program 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; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; NOTE: This is an add-on module for Riece.
28
29 ;; riece-desktop-notify.el needs an external program to notify desktop.
30 ;; The setting prepared beforehand for most popular OS uses the
31 ;; following external programs.
32 ;;
33 ;; Mac OS X:
34 ;;   growlnotify <URL:http://growl.info/extras.php#growlnotify>
35 ;;   (of course you need Growl <URL:http://growl.info>)
36 ;;
37 ;; GNU/Linux:
38 ;;   notify-send (which is included in libnotify)
39 ;;    <URL:http://www.galago-project.org/news/index.php>
40 ;;
41 ;;  If you use Debian, you can install it by the following command:
42 ;;
43 ;;    % sudo aptitude install libnotify-bin
44 ;;
45 ;; Windows:
46 ;;   Snarl_CMD.exe <URL:http://tlhan-ghun.de/?q=node/59>
47 ;;   (of course you need Snarl <URL:http://www.fullphat.net/>)
48
49 ;;; Code:
50
51 (require 'riece-message)
52 (eval-when-compile (require 'riece-keyword))
53
54 (defconst riece-desktop-notify-description
55   "Display notification to desktop.")
56
57 (defgroup riece-desktop-notify nil
58   "Display notification to desktop."
59   :group 'riece)
60
61 (defcustom riece-desktop-notify-title-function
62   '(lambda (message)
63      (riece-identity-prefix (riece-message-target message)))
64   "*The function which make title.
65 This function must have only one message object as argument."
66   :type 'function
67   :group 'riece-desktop-notify)
68
69 (defcustom riece-desktop-notify-message-function 'riece-format-message
70   "*The function which make message.
71 This function must have only one message object as argument."
72   :type 'function
73   :group 'riece-desktop-notify)
74
75 (defcustom riece-desktop-notify-coding-system (terminal-coding-system)
76   "*Coding system used to notify desktop."
77   :type 'coding-system
78   :group 'riece-desktop-notify)
79
80 (defcustom riece-desktop-notify-type
81   (if (eq system-type 'linux) 'gnu/linux system-type)
82   "*The type to notify desktop."
83   :type '(radio (const :tag "Like Darwin" darwin)
84                 (const :tag "Like GNU/Linux" gnu/linux)
85                 (const :tag "Like Windows" windows-nt)
86                 (symbol :tag "The other type"))
87   :group 'riece-desktop-notify)
88
89 ;; for Darwin
90 (defcustom riece-desktop-notify-darwin-program "growlnotify"
91   "*The program name to notify for darwin."
92   :type 'file
93   :group 'riece-desktop-notify)
94
95 (defcustom riece-desktop-notify-darwin-args
96   '("-t" title "-m" message "-H" "localhost")
97   "*The Arguments to notify for darwin."
98   :type '(repeat (radio (string :tag "Argument")
99                         (const :tag "Title" title)
100                         (const :tag "Message" message)))
101   :group 'riece-desktop-notify)
102
103 ;; for GNU/Linux
104 (defcustom riece-desktop-notify-gnu/linux-program "notify-send"
105   "*The program name to notify for GNU/Linux."
106   :type 'file
107   :group 'riece-desktop-notify)
108
109 (defcustom riece-desktop-notify-gnu/linux-args '("-u" "low" title message)
110   "*The Arguments to notify for GNU/Linux."
111   :type '(repeat (radio (string :tag "Argument")
112                         (const :tag "Title" title)
113                         (const :tag "Message" message)))
114   :group 'riece-desktop-notify)
115
116 ;; for Windows
117 (defcustom riece-desktop-notify-windows-nt-program "snarl_cmd.exe"
118   "*The program name to notify for Windows."
119   :type 'file
120   :group 'riece-desktop-notify)
121
122 (defcustom riece-desktop-notify-windows-nt-args
123   '("snShowMessage" "-1" title message)
124   "*The Arguments string to notify for Windows."
125   :type '(repeat (radio (string :tag "Argument")
126                         (const :tag "Title" title)
127                         (const :tag "Message" message)))
128   :group 'riece-desktop-notify)
129
130 ;; stolen and modified from riece-ruby.el
131 (defun riece-desktop-notify-substitute-variables (args alist)
132   "Substitute symbols in ARGS by looking up ALIST."
133   (setq args (copy-sequence args))
134   (while alist
135     (let ((pointer args))
136       (while pointer
137         (setq pointer (memq (car (car alist)) args))
138         (if pointer
139             (setcar pointer (cdr (car alist))))))
140     (setq alist (cdr alist)))
141   args)
142
143 (defsubst riece-desktop-notify-make-symbol (symbol)
144   (intern (format "riece-desktop-notify-%s-%s"
145                   (symbol-name riece-desktop-notify-type)
146                   (symbol-name symbol))))
147
148 (defun riece-desktop-notify-keyword-notify-function (keyword message)
149   (let ((program-symbol (riece-desktop-notify-make-symbol 'program))
150         (args-symbol (riece-desktop-notify-make-symbol 'args)))
151     (when (and (boundp program-symbol) (boundp args-symbol))
152       (let ((program (eval program-symbol))
153             (args (eval args-symbol)))
154         (when (fboundp 'executable-find)
155           (setq program (executable-find program)))
156         (when (stringp program)
157           (let ((title (funcall riece-desktop-notify-title-function message))
158                 (message (funcall riece-desktop-notify-message-function
159                                   message)))
160             (condition-case nil
161                 (apply #'call-process program nil nil nil
162                        (riece-desktop-notify-substitute-variables
163                         args
164                         (list (cons 'title
165                                     (encode-coding-string
166                                      title
167                                      riece-desktop-notify-coding-system))
168                               (cons 'message
169                                     (encode-coding-string
170                                      message
171                                      riece-desktop-notify-coding-system)))))
172               (file-error nil))))))))
173
174 (defun riece-desktop-notify-requires ()
175   '(riece-keyword))
176
177 (defun riece-desktop-notify-insinuate ()
178   (add-hook 'riece-keyword-notify-functions
179             'riece-desktop-notify-keyword-notify-function))
180
181 (defun riece-desktop-notify-uninstall ()
182   (remove-hook 'riece-keyword-notify-functions
183                'riece-desktop-notify-keyword-notify-function))
184
185 (provide 'riece-desktop-notify)
186
187 ;;; riece-desktop-notify.el ends here