* mixi.el (mixi-entity-alist): New constant.
[elisp/mixi.git] / mixi-ticker.el
1 ;; mixi-ticker.el --- ticker for mixi
2
3 ;; Copyright (C) 2007 OHASHI Akira
4
5 ;; Author: OHASHI Akira <bg66@koka-in.org>
6 ;; Keywords: hypermedia
7
8 ;; This file is *NOT* a part of Emacs.
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 this program; if not, you can either send email to this
22 ;; program's maintainer or write to: The Free Software Foundation,
23 ;; Inc.; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; To use, add the following line to your ~/.emacs:
28 ;;
29 ;; (autoload 'mixi-ticker-start "mixi-ticker")
30 ;;
31 ;; then M-x mixi-ticker-start will start mixi-ticker.
32
33 ;; Bug reports:
34 ;;
35 ;; If you have bug reports and/or suggestions for improvement, please
36 ;; send them via <URL:http://mixi.jp/view_community.pl?id=1596390>.
37
38 ;;; Code:
39
40 (require 'mixi)
41 (require 'mixi-utils)
42 (require 'timer)
43
44 (defcustom mixi-ticker-check-list nil
45   "*A list for checking to detect new articles.
46 Each element looks like URL or FUNCTION.
47 URL is the URL for mixi access point of the channel.  If URL is friend's, get
48 his/her diaries as article.  If community's, get its BBSes.  If diary's or
49 BBS's, get its comments.
50 FUNCTION is the function which has one `range' argument and returns the list
51 of mixi object."
52   :type '(repeat (radio (string :tag "URL")
53                         (const :tag "New diaries" mixi-get-new-diaries)
54                         (const :tag "New comments" mixi-get-new-comments)
55                         (const :tag "New BBSes" mixi-get-new-bbses)
56                         (const :tag "Messages" mixi-get-messages)
57                         (const :tag "Logs" mixi-get-logs)
58                         (function :tag "Other function")))
59   :group 'mixi)
60
61 (defcustom mixi-ticker-check-range 3
62   "*The number of ranges that should be checked to detect new articles."
63   :type 'integer
64   :group 'mixi)
65
66 (defcustom mixi-ticker-interval 3600
67   "*Time interval for checking to detect new articles."
68   :type 'integer
69   :group 'mixi)
70
71 (defcustom mixi-ticker-display-interval 0.3
72   "*Time interval for displaying new articles."
73   :type 'number
74   :group 'mixi)
75
76 (defvar mixi-ticker-timer nil)
77 (defvar mixi-ticker-display-timer nil)
78 (defvar mixi-ticker-objects nil)
79 (defvar mixi-ticker-message nil)
80 (defvar mixi-ticker-last-check nil)
81
82 (defun mixi-ticker-display ()
83   (when (not (or (active-minibuffer-window)
84                  (and (current-message)
85                       (not (string= (current-message)
86                                     mixi-ticker-message)))))
87     (if (and (stringp mixi-ticker-message)
88              (> (length mixi-ticker-message) 1))
89         (setq mixi-ticker-message
90               (substring mixi-ticker-message 1))
91       (setq mixi-ticker-message nil))
92     (unless mixi-ticker-message
93       (while mixi-ticker-objects
94         (condition-case nil
95             (let ((string (mixi-make-title (car mixi-ticker-objects) t)))
96               (setq mixi-ticker-message (concat mixi-ticker-message " "
97                                                 (mixi-message string))))
98           (error nil))
99         (setq mixi-ticker-objects (cdr mixi-ticker-objects))))
100     (message mixi-ticker-message)))
101
102 (defun mixi-ticker-check ()
103   "Check to detect new articles."
104   (setq mixi-ticker-objects nil)
105   (mapc (lambda (url-or-function)
106           (let ((objects (mixi-make-objects url-or-function
107                                             mixi-ticker-check-range)))
108             (while objects
109               (let ((object (car objects)))
110                 (when (mixi-parent-p object)
111                   (let ((comments (mixi-get-comments
112                                    object mixi-ticker-check-range)))
113                     (while comments
114                       (let ((time (mixi-object-time (car comments))))
115                         (when (mixi-time-less-p mixi-ticker-last-check time)
116                           (setq mixi-ticker-objects
117                                 (cons (car comments) mixi-ticker-objects))))
118                       (setq comments (cdr comments)))))
119                 (let ((time (mixi-object-time object)))
120                   (when (mixi-time-less-p mixi-ticker-last-check time)
121                     (setq mixi-ticker-objects (cons object
122                                                     mixi-ticker-objects)))))
123               (setq objects (cdr objects)))))
124         mixi-ticker-check-list)
125   (setq mixi-ticker-objects (reverse mixi-ticker-objects))
126   (setq mixi-ticker-last-check (current-time)))
127
128 ;;;###autoload
129 (defun mixi-ticker-start ()
130   (interactive)
131   (when mixi-ticker-check-list
132     (setq mixi-ticker-timer
133           (run-at-time mixi-ticker-interval mixi-ticker-interval
134                        'mixi-ticker-check))
135     (setq mixi-ticker-display-timer
136           (run-at-time mixi-ticker-display-interval
137                        mixi-ticker-display-interval
138                        'mixi-ticker-display))
139     (unless mixi-ticker-last-check
140       (setq mixi-ticker-last-check (current-time)))))
141
142 ;;;###autoload
143 (defun mixi-ticker-stop ()
144   (interactive)
145   (when (timerp mixi-ticker-timer)
146     (cancel-timer mixi-ticker-timer)
147     (setq mixi-ticker-timer nil))
148   (when (timerp mixi-ticker-display-timer)
149     (cancel-timer mixi-ticker-display-timer)
150     (setq mixi-ticker-display-timer nil)))
151
152 (provide 'mixi-ticker)
153
154 ;;; mixi-ticker.el ends here