* Makefile.in (install-package-ja): Compile and install lisp files first.
[elisp/gnus.git-] / lisp / gnus-picon.el
1 ;;; gnus-picon.el --- displaying pretty icons in Gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Wes Hardaker <hardaker@ece.ucdavis.edu>
5 ;; Keywords: news xpm annotation glyph faces
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 ;; (require 'xpm)
32 (require 'annotations)
33 (require 'custom)
34 (require 'gnus-art)
35 (require 'gnus-win)
36
37 ;;; User variables:
38
39 (defgroup picons nil
40   "Show pictures of people, domains, and newsgroups (XEmacs).
41 For this to work, you must switch on the `gnus-treat-display-picons'
42 variable."
43   :group 'gnus-visual)
44
45 (defcustom gnus-picons-display-where 'picons
46   "Where to display the group and article icons.
47 Valid values are `article' and `picons'."
48   :type '(choice symbol string)
49   :group 'picons)
50
51 (defcustom gnus-picons-has-modeline-p t
52   "*Whether the picons window should have a modeline.
53 This is only useful if `gnus-picons-display-where' is `picons'."
54   :type 'boolean
55   :group 'picons)
56
57 (defcustom gnus-picons-database "/usr/local/faces"
58   "*Defines the location of the faces database.
59 For information on obtaining this database of pretty pictures, please
60 see http://www.cs.indiana.edu/picons/ftp/index.html"
61   :type 'directory
62   :group 'picons)
63
64 (defcustom gnus-picons-news-directories '("news")
65   "*List of directories to search for newsgroups faces."
66   :type '(repeat string)
67   :group 'picons)
68 (define-obsolete-variable-alias 'gnus-picons-news-directory
69   'gnus-picons-news-directories)
70
71 (defcustom gnus-picons-user-directories '("local" "users" "usenix" "misc")
72   "*List of directories to search for user faces."
73   :type '(repeat string)
74   :group 'picons)
75
76 (defcustom gnus-picons-domain-directories '("domains")
77   "*List of directories to search for domain faces.
78 Some people may want to add \"unknown\" to this list."
79   :type '(repeat string)
80   :group 'picons)
81
82 (defcustom gnus-picons-refresh-before-display nil
83   "*If non-nil, display the article buffer before computing the picons."
84   :type 'boolean
85   :group 'picons)
86
87 (defcustom gnus-picons-group-excluded-groups nil
88   "*If this regexp matches the group name, group picons will be disabled."
89   :type 'regexp
90   :group 'picons)
91
92 (defcustom gnus-picons-display-as-address t
93   "*If t display textual email addresses along with pictures."
94   :type 'boolean
95   :group 'picons)
96
97 (defcustom gnus-picons-file-suffixes
98   (when (featurep 'x)
99     (let ((types (list "xbm")))
100       (when (featurep 'gif)
101         (setq types (cons "gif" types)))
102       (when (featurep 'xpm)
103         (setq types (cons "xpm" types)))
104       types))
105   "*List of suffixes on picon file names to try."
106   :type '(repeat string)
107   :group 'picons)
108
109 (defcustom gnus-picons-display-article-move-p nil
110   "*Whether to move point to first empty line when displaying picons.
111 This has only an effect if `gnus-picons-display-where' has value `article'."
112   :type 'boolean
113   :group 'picons)
114
115 (defcustom gnus-picons-clear-cache-on-shutdown t
116   "*Whether to clear the picons cache when exiting gnus.
117 Gnus caches every picons it finds while it is running.  This saves
118 some time in the search process but eats some memory.  If this
119 variable is set to nil, Gnus will never clear the cache itself; you
120 will have to manually call `gnus-picons-clear-cache' to clear it.
121 Otherwise the cache will be cleared every time you exit Gnus."
122   :type 'boolean
123   :group 'picons)
124
125 (defcustom gnus-picons-piconsearch-url nil
126   "*The url to query for picons.  Setting this to nil will disable it.
127 The only publicly available address currently known is
128 http://www.cs.indiana.edu:800/piconsearch.  If you know of any other,
129 please tell me so that we can list it."
130   :type '(choice (const :tag "Disable" :value nil)
131                  (const :tag "www.cs.indiana.edu"
132                         :value "http://www.cs.indiana.edu:800/piconsearch")
133                  (string))
134   :group 'picons)
135
136 (defface gnus-picons-xbm-face '((t (:foreground "black" :background "white")))
137   "Face to show xbm picons in."
138   :group 'picons)
139
140 (defface gnus-picons-face '((t (:foreground "black" :background "white")))
141   "Face to show picons in."
142   :group 'picons)
143
144 (defcustom gnus-picons-setup-hook nil
145   "Hook run in Picons buffers."
146   :group 'picons
147   :type 'hook)
148
149 ;;; Internal variables:
150
151 (defvar gnus-picons-setup-p nil)
152 (defvar gnus-picons-processes-alist nil
153   "Picons processes currently running and their environment.")
154 (defvar gnus-picons-glyph-alist nil
155   "Picons glyphs cache.
156 List of pairs (KEY . GLYPH) where KEY is either a filename or an URL.")
157 (defvar gnus-picons-url-alist nil
158   "Picons file names cache.
159 List of pairs (KEY . NAME) where KEY is (USER HOST DBS) and NAME is an URL.")
160
161 (defvar gnus-picons-jobs-alist nil
162   "List of jobs that still need be done.
163 This is a list of (SYM-ANN TAG ARGS...) where SYM-ANN three annotations list,
164 TAG is one of `picon' or `search' indicating that the job should query a
165 picon or do a search for picons file names, and ARGS is some additionnal
166 arguments necessary for the job.")
167
168 (defvar gnus-picons-job-already-running nil
169   "Lock to ensure only one stream of http requests is running.")
170
171 ;;; Functions:
172
173 (defun gnus-picons-remove-all ()
174   "Removes all picons from the Gnus display(s)."
175   (interactive)
176   (map-extents (function (lambda (ext unused) (delete-annotation ext) nil))
177                nil nil nil nil nil 'gnus-picon)
178   (setq gnus-picons-jobs-alist '())
179   ;; notify running job that it may have been preempted
180   (if (and (listp gnus-picons-job-already-running)
181            gnus-picons-job-already-running)
182       (setq gnus-picons-job-already-running t)))
183
184 (defun gnus-get-buffer-name (variable)
185   "Returns the buffer name associated with the contents of a variable."
186   (let ((buf (gnus-get-buffer-create
187               (gnus-window-to-buffer-helper
188                (cdr (assq variable gnus-window-to-buffer))))))
189     (and buf
190          (buffer-name buf))))
191
192 (defun gnus-picons-buffer-name ()
193   (cond ((or (stringp gnus-picons-display-where)
194              (bufferp gnus-picons-display-where))
195          gnus-picons-display-where)
196         ((eq gnus-picons-display-where 'picons)
197          (if gnus-single-article-buffer
198              "*Picons*"
199            (concat "*Picons " gnus-newsgroup-name "*")))
200         (t
201          (gnus-get-buffer-name gnus-picons-display-where))))
202
203 (defun gnus-picons-kill-buffer ()
204   (let ((buf (get-buffer (gnus-picons-buffer-name))))
205     (when (and (buffer-live-p buf)
206                (string-match "Picons" (buffer-name buf)))
207       (kill-buffer buf))))
208
209 (defun gnus-picons-setup-buffer ()
210   (let ((name (gnus-picons-buffer-name)))
211     (save-excursion
212       (if (and (get-buffer name)
213                (with-current-buffer name
214                  gnus-picons-setup-p))
215           (set-buffer name)
216         (set-buffer (gnus-get-buffer-create name))
217         (buffer-disable-undo)
218         (setq buffer-read-only t)
219         (run-hooks 'gnus-picons-setup-hook)
220         (set (make-local-variable 'gnus-picons-setup-p) t)
221         (add-hook 'gnus-summary-prepare-exit-hook 'gnus-picons-kill-buffer))
222       (current-buffer))))
223
224 (defun gnus-picons-set-buffer ()
225   (set-buffer (gnus-picons-setup-buffer))
226   (goto-char (point-min))
227   (if (and (eq gnus-picons-display-where 'article)
228            gnus-picons-display-article-move-p)
229       (if (search-forward "\n\n" nil t)
230           (forward-line -1)
231         (goto-char (point-max)))
232     (setq buffer-read-only t)
233     (unless gnus-picons-has-modeline-p
234       (set-specifier has-modeline-p
235                      (list (list (current-buffer)
236                                  (cons nil gnus-picons-has-modeline-p)))))))
237
238 (defun gnus-picons-prepare-for-annotations ()
239   "Prepare picons buffer for putting annotations."
240   ;; let drawing catch up
241   (when gnus-picons-refresh-before-display
242     (sit-for 0))
243   (gnus-picons-set-buffer)
244   (gnus-picons-remove-all))
245
246 (defun gnus-picons-make-annotation (&rest args)
247   (let ((annot (apply 'make-annotation args)))
248     (set-extent-property annot 'gnus-picon t)
249     (set-extent-property annot 'duplicable t)
250     annot))
251
252 (defun gnus-article-display-picons ()
253   "Display faces for an author and her domain in gnus-picons-display-where."
254   (interactive)
255   (let (from at-idx)
256     (when (and (featurep 'xpm)
257                (or (not (fboundp 'device-type)) (equal (device-type) 'x))
258                (setq from (mail-fetch-field "from"))
259                (setq from (downcase (or (cadr
260                                          (funcall gnus-extract-address-components
261                                                   from))
262                                         "")))
263                (or (setq at-idx (string-match "@" from))
264                    (setq at-idx (length from))))
265       (save-excursion
266         (let ((username (downcase (substring from 0 at-idx)))
267               (addrs (if (eq at-idx (length from))
268                          (if gnus-local-domain
269                              (message-tokenize-header gnus-local-domain "."))
270                        (message-tokenize-header (substring from (1+ at-idx))
271                                                 "."))))
272           (gnus-picons-prepare-for-annotations)
273           (gnus-group-display-picons)
274           (unless gnus-picons-display-article-move-p
275             (let ((buffer-read-only nil)
276                   (case-fold-search t))
277               (when (re-search-forward "^From *: *" nil t)
278                 (when (search-forward from (gnus-point-at-eol) t)
279                   (gnus-put-text-property
280                    (match-beginning 0) (match-end 0)
281                    'invisible t)))))
282           (if (null gnus-picons-piconsearch-url)
283               (progn
284                 (gnus-picons-display-pairs (gnus-picons-lookup-pairs
285                                             addrs
286                                             gnus-picons-domain-directories)
287                                            gnus-picons-display-as-address
288                                            "." t)
289                 (if (and gnus-picons-display-as-address addrs)
290                     (gnus-picons-make-annotation
291                      [string :data "@"] nil 'text nil nil nil t))
292                 (gnus-picons-display-picon-or-name
293                  (gnus-picons-lookup-user username addrs)
294                  username t))
295             (push (list 'gnus-article-annotations 'search username addrs
296                         gnus-picons-domain-directories t (point-marker))
297                   gnus-picons-jobs-alist)
298             (gnus-picons-next-job)))))))
299
300 (defun gnus-group-display-picons ()
301   "Display icons for the group in the `gnus-picons-display-where' buffer."
302   (interactive)
303   (when (and (featurep 'xpm)
304              (or (not (fboundp 'device-type)) (equal (device-type) 'x))
305              (or (null gnus-picons-group-excluded-groups)
306                  (not (string-match gnus-picons-group-excluded-groups
307                                     gnus-newsgroup-name))))
308     (let* ((newsgroups (mail-fetch-field "newsgroups"))
309            (groups
310             (if (or gnus-picons-display-article-move-p
311                     (not newsgroups))
312                 (list (gnus-group-real-name gnus-newsgroup-name))
313               (split-string newsgroups ",")))
314            group)
315       (save-excursion
316         (gnus-picons-prepare-for-annotations)
317         (while (setq group (pop groups))
318           (unless gnus-picons-display-article-move-p
319             (let ((buffer-read-only nil)
320                   (case-fold-search t))
321               (goto-char (point-min))
322               (if (and (re-search-forward "^Newsgroups *: *" nil t)
323                        (search-forward group (gnus-point-at-eol) t))
324                   (gnus-put-text-property
325                    (match-beginning 0) (match-end 0)
326                    'invisible t)
327                 (let ((article-goto-body-goes-to-point-min-p nil))
328                   (article-goto-body))
329                 (unless (bobp)
330                   (backward-char 1)))))
331           (if (null gnus-picons-piconsearch-url)
332               (gnus-picons-display-pairs
333                (gnus-picons-lookup-pairs
334                 (reverse (split-string group "\\."))
335                 gnus-picons-news-directories)
336                t ".")
337             (push (list 'gnus-group-annotations 'search nil
338                         (split-string group "\\.")
339                         (if (listp gnus-picons-news-directories)
340                             gnus-picons-news-directories
341                           (list gnus-picons-news-directories))
342                         nil (point-marker))
343                   gnus-picons-jobs-alist)
344             (gnus-picons-next-job))
345
346           (add-hook 'gnus-summary-exit-hook 'gnus-picons-remove-all))))))
347
348 (defun gnus-picons-lookup-internal (addrs dir)
349   (setq dir (expand-file-name dir gnus-picons-database))
350   (gnus-picons-try-face (dolist (part (reverse addrs) dir)
351                           (setq dir (expand-file-name part dir)))))
352
353 (defun gnus-picons-lookup (addrs dirs)
354   "Lookup the picon for ADDRS in databases DIRS.
355 Returns the picon filename or NIL if none found."
356   (let (result)
357     (while (and dirs (null result))
358       (setq result (gnus-picons-lookup-internal addrs (pop dirs))))
359     result))
360
361 (defun gnus-picons-lookup-user-internal (user domains)
362   (let ((dirs gnus-picons-user-directories)
363         domains-tmp dir picon)
364     (while (and dirs (null picon))
365       (setq domains-tmp domains
366             dir (pop dirs))
367       (while (and domains-tmp
368                   (null (setq picon (gnus-picons-lookup-internal
369                                      (cons user domains-tmp) dir))))
370         (pop domains-tmp))
371       ;; Also make a try in MISC subdir
372       (unless picon
373         (setq picon (gnus-picons-lookup-internal (list user "MISC") dir))))
374     picon))
375
376 (defun gnus-picons-lookup-user (user domains)
377   "Lookup the picon for USER at DOMAINS.
378 USER is a string containing a name.
379 DOMAINS is a list of strings from the fully qualified domain name."
380   (or (gnus-picons-lookup-user-internal user domains)
381       (gnus-picons-lookup-user-internal "unknown" domains)))
382
383 (defun gnus-picons-lookup-pairs (domains directories)
384   "Lookup picons for DOMAINS and all its parents in DIRECTORIES.
385 Returns a list of PAIRS whose CAR is the picon filename or NIL if
386 none, and whose CDR is the corresponding element of DOMAINS."
387   (let (picons)
388     (setq directories (if (listp directories)
389                           directories
390                         (list directories)))
391     (while domains
392       (push (list (gnus-picons-lookup (cons "unknown" domains) directories)
393                   (pop domains))
394             picons))
395     picons))
396
397 (defun gnus-picons-display-picon-or-name (picon name &optional right-p)
398   (cond (picon (gnus-picons-display-glyph picon name right-p))
399         (gnus-picons-display-as-address (list (gnus-picons-make-annotation
400                                                (vector 'string :data name)
401                                                nil 'text
402                                                nil nil nil right-p)))))
403
404 (defun gnus-picons-display-pairs (pairs &optional bar-p dot-p right-p)
405   "Display picons in list PAIRS."
406   (let ((domain-p (and gnus-picons-display-as-address dot-p))
407         pair picons)
408     (when (and bar-p domain-p right-p
409                gnus-picons-display-article-move-p)
410       (setq picons (gnus-picons-display-glyph
411                     (let ((gnus-picons-file-suffixes '("xbm")))
412                       (gnus-picons-try-face
413                        gnus-xmas-glyph-directory "bar."))
414                     nil right-p)))
415     (while (setq pair (pop pairs))
416       (setq picons (nconc picons
417                           (gnus-picons-display-picon-or-name
418                            (car pair) (cadr pair) right-p)
419                           (if (and domain-p pairs)
420                               (list (gnus-picons-make-annotation
421                                      (vector 'string :data dot-p)
422                                      nil 'text nil nil nil right-p))))))
423     picons))
424
425 (defun gnus-picons-try-face (dir &optional filebase)
426   (let* ((dir (file-name-as-directory dir))
427          (filebase (or filebase "face."))
428          (key (concat dir filebase))
429          (glyph (cdr (assoc key gnus-picons-glyph-alist)))
430          (suffixes gnus-picons-file-suffixes)
431          f suf)
432     (while (setq suf (pop suffixes))
433       (when (file-exists-p (setq f (expand-file-name
434                                     (concat filebase suf)
435                                     dir)))
436         (setq suffixes nil
437               glyph (make-glyph f))
438         (if (equal suf "xbm")
439             (set-glyph-face glyph 'gnus-picons-xbm-face)
440           (set-glyph-face glyph 'gnus-picons-face))
441         (push (cons key glyph) gnus-picons-glyph-alist)))
442     glyph))
443
444 (defun gnus-picons-display-glyph (glyph &optional part rightp)
445   (set-glyph-baseline glyph 70)
446   (let ((new (gnus-picons-make-annotation
447               glyph (point) 'text nil nil nil rightp)))
448     (when (and part gnus-picons-display-as-address)
449       (set-annotation-data
450        new (cons new (make-glyph (vector 'string :data part))))
451       (set-annotation-action new 'gnus-picons-action-toggle))
452     (nconc
453      (list new)
454      (if (and (eq major-mode 'gnus-article-mode)
455               (not gnus-picons-display-as-address)
456               (not part))
457          (list (gnus-picons-make-annotation [string :data " "] (point)
458                                             'text nil nil nil rightp))))))
459
460 (defun gnus-picons-action-toggle (data)
461   "Toggle annotation."
462   (interactive "e")
463   (let* ((annot (car data))
464          (glyph (annotation-glyph annot)))
465     (set-annotation-glyph annot (cdr data))
466     (set-annotation-data annot (cons annot glyph))))
467
468 (defun gnus-picons-clear-cache ()
469   "Clear the picons cache."
470   (interactive)
471   (setq gnus-picons-glyph-alist nil
472         gnus-picons-url-alist nil))
473
474 (gnus-add-shutdown 'gnus-picons-close 'gnus)
475
476 (defun gnus-picons-close ()
477   "Shut down the picons."
478   (if gnus-picons-clear-cache-on-shutdown
479       (gnus-picons-clear-cache)))
480
481 ;;; Query a remote DB.  This requires some stuff from w3 !
482
483 (eval-and-compile
484   (ignore-errors
485     (require 'url)
486     (require 'w3-forms)))
487
488 (defun gnus-picons-url-retrieve (url fn arg)
489   (let ((old-asynch (default-value 'url-be-asynchronous))
490         (url-working-buffer (generate-new-buffer " *picons*"))
491         (url-package-name "Gnus")
492         (url-package-version gnus-version-number)
493         url-request-method)
494     (setq-default url-be-asynchronous t)
495     (save-excursion
496       (set-buffer url-working-buffer)
497       (setq url-be-asynchronous t
498             url-current-callback-data arg
499             url-current-callback-func fn)
500       (url-retrieve url t))
501     (setq-default url-be-asynchronous old-asynch)))
502
503 (defun gnus-picons-make-glyph (type)
504   "Make a TYPE glyph using current buffer as data.  Handles xbm nicely."
505   (cond ((null type) nil)
506         ((eq type 'xbm) (let ((fname (make-temp-name "/tmp/picon")))
507                           (write-region (point-min) (point-max) fname
508                                         nil 'quiet)
509                           (prog1 (make-glyph (vector 'xbm :file fname))
510                             (delete-file fname))))
511         (t (make-glyph (vector type :data (buffer-string))))))
512
513 ;;; Parsing of piconsearch result page.
514
515 ;; Assumes:
516 ;; 1 - each value field has the form: "<strong>key</strong> = <kbd>value</kbd>"
517 ;; 2 - a "<p>" separates the keywords from the results
518 ;; 3 - every results begins by the path within the database at the beginning
519 ;;     of the line in raw text.
520 ;; 3b - and the href following it is the preferred image type.
521
522 ;; if 1 or 2 is not met, it will probably cause an error.  The other
523 ;; will go undetected
524
525 (defun gnus-picons-parse-value (name)
526   (goto-char (point-min))
527   (if (re-search-forward (concat "<strong>"
528                                  (regexp-quote name)
529                                  "</strong> *= *<kbd> *\\([^ <][^<]*\\) *</kbd>")
530                          nil t)
531       (buffer-substring (match-beginning 1) (match-end 1))))
532
533 (defun gnus-picons-parse-filenames ()
534   ;; returns an alist of ((USER ADDRS DB) . URL)
535   (let ((case-fold-search t)
536         (user (gnus-picons-parse-value "user"))
537         (host (gnus-picons-parse-value "host"))
538         (dbs (message-tokenize-header (gnus-picons-parse-value "db") " "))
539         start-re cur-db cur-host cur-user types res)
540     ;; now point will be somewhere in the header.  Find beginning of
541     ;; entries
542     (when (and user host dbs)
543       (setq start-re
544             (concat
545              ;; dbs
546              "^\\(" (mapconcat 'regexp-quote dbs "\\|") "\\)/"
547              ;; host
548              "\\(\\(" (mapconcat 'regexp-quote
549                                  (message-tokenize-header host ".") "/\\|")
550              "/\\|MISC/\\)*\\)"
551              ;; user
552              "\\(" (regexp-quote user) "\\|unknown\\)/"
553              "face\\."))
554       (re-search-forward "<p>[ \t\n]*")
555       (while (re-search-forward start-re nil t)
556         (setq cur-db (buffer-substring (match-beginning 1) (match-end 1))
557               cur-host (buffer-substring (match-beginning 2) (match-end 2))
558               cur-user (buffer-substring (match-beginning 4) (match-end 4))
559               cur-host (nreverse (message-tokenize-header cur-host "/")))
560         ;; XXX - KLUDGE: there is a blank picon in news/MISC/unknown
561         (unless (and (string-equal cur-db "news")
562                      (string-equal cur-user "unknown")
563                      (equal cur-host '("MISC")))
564           ;; ok now we have found an entry (USER HOST DB), find the
565           ;; corresponding picon URL
566           (save-restriction
567             ;; restrict region to this entry
568             (narrow-to-region (point) (search-forward "<br>"))
569             (goto-char (point-min))
570             (setq types gnus-picons-file-suffixes)
571             (while (and types
572                         (not (re-search-forward
573                               (concat "<a[ \t\n]+href=\"\\([^\"]*\\."
574                                       (regexp-quote (car types)) "\\)\"")
575                               nil t)))
576               (pop types))
577             (push (cons (list cur-user cur-host cur-db)
578                         (buffer-substring (match-beginning 1) (match-end 1)))
579                   res))))
580       (nreverse res))))
581
582 ;;; picon network display functions :
583
584 (defun gnus-picons-network-display-internal (sym-ann glyph part right-p marker)
585   (let ((buf (marker-buffer marker))
586         (pos (marker-position marker)))
587     (if (and buf pos)
588         (save-excursion
589           (set-buffer buf)
590           (goto-char pos)
591           (gnus-picons-display-picon-or-name glyph part right-p))))
592   (gnus-picons-next-job-internal))
593
594 (defun gnus-picons-network-display-callback (url part sym-ann right-p marker)
595   (let ((glyph (gnus-picons-make-glyph (cdr (assoc url-current-mime-type
596                                                    w3-image-mappings)))))
597     (kill-buffer (current-buffer))
598     (push (cons url glyph) gnus-picons-glyph-alist)
599     ;; only do the job if it has not been preempted.
600     (if (equal gnus-picons-job-already-running
601                (list sym-ann 'picon url part right-p marker))
602         (gnus-picons-network-display-internal
603          sym-ann glyph part right-p marker)
604       (gnus-picons-next-job-internal))))
605
606 (defun gnus-picons-network-display (url part sym-ann right-p marker)
607   (let ((cache (assoc url gnus-picons-glyph-alist)))
608     (if (or cache (null url))
609         (gnus-picons-network-display-internal
610          sym-ann (cdr cache) part right-p marker)
611       (gnus-picons-url-retrieve url 'gnus-picons-network-display-callback
612                                 (list url part sym-ann right-p marker)))))
613
614 ;;; search job functions
615
616 (defun gnus-picons-display-bar-p ()
617   (if (eq gnus-picons-display-where 'article)
618       gnus-picons-display-article-move-p
619     gnus-picons-display-as-address))
620
621 (defun gnus-picons-network-search-internal (user addrs dbs sym-ann right-p
622                                                  marker &optional fnames)
623   (let (curkey dom pfx url dbs-tmp cache new-jobs)
624     ;; First do the domain search
625     (dolist (part (if right-p
626                       (reverse addrs)
627                     addrs))
628       (setq pfx (nconc (list part) pfx)
629             dom (cond ((and dom right-p) (concat part "." dom))
630                       (dom (concat dom "." part))
631                       (t part))
632             curkey (list "unknown" dom dbs))
633       (when (null (setq cache (assoc curkey gnus-picons-url-alist)))
634         ;; This one is not yet in the cache, create a new entry
635         ;; Search for an entry
636         (setq dbs-tmp dbs
637               url nil)
638         (while (and dbs-tmp (null url))
639           (setq url (or (cdr (assoc (list "unknown" pfx (car dbs-tmp)) fnames))
640                         (and (eq dom part)
641                              ;; This is the first component.  Try the
642                              ;; catch-all MISC component
643                              (cdr (assoc (list "unknown"
644                                                '("MISC")
645                                                (car dbs-tmp))
646                                          fnames)))))
647           (pop dbs-tmp))
648         (push (setq cache (cons curkey url)) gnus-picons-url-alist))
649       ;; Put this glyph in the job list
650       (if (and (not (eq dom part)) gnus-picons-display-as-address)
651           (push (list sym-ann "." right-p marker) new-jobs))
652       (push (list sym-ann 'picon (cdr cache) part right-p marker) new-jobs))
653     ;; next, the user search
654     (when user
655       (setq curkey (list user dom gnus-picons-user-directories))
656       (if (null (setq cache (assoc curkey gnus-picons-url-alist)))
657           (let ((users (list user "unknown"))
658                 dirs usr domains-tmp dir picon)
659             (while (and users (null picon))
660               (setq dirs gnus-picons-user-directories
661                     usr (pop users))
662               (while (and dirs (null picon))
663                 (setq domains-tmp addrs
664                       dir (pop dirs))
665                 (while (and domains-tmp
666                             (null (setq picon (assoc (list usr domains-tmp dir)
667                                                      fnames))))
668                   (pop domains-tmp))
669                 (unless picon
670                   (setq picon (assoc (list usr '("MISC") dir) fnames)))))
671             (push (setq cache (cons curkey (cdr picon)))
672                   gnus-picons-url-alist)))
673       (if (and gnus-picons-display-as-address new-jobs)
674           (push (list sym-ann "@" right-p marker) new-jobs))
675       (push (list sym-ann 'picon (cdr cache) user right-p marker) new-jobs))
676     (if (and (gnus-picons-display-bar-p) (not right-p))
677         (push (list sym-ann 'bar right-p marker) new-jobs))
678     ;; only put the jobs in the queue if this job has not been preempted.
679     (if (equal gnus-picons-job-already-running
680                (list sym-ann 'search user addrs dbs right-p marker))
681         (setq gnus-picons-jobs-alist
682               (nconc (if (and (gnus-picons-display-bar-p) right-p)
683                          (list (list sym-ann 'bar right-p marker)))
684                      (nreverse new-jobs)
685                      gnus-picons-jobs-alist)))
686     (gnus-picons-next-job-internal)))
687
688 (defun gnus-picons-network-search-callback (user addrs dbs sym-ann right-p
689                                                  marker)
690   (gnus-picons-network-search-internal
691    user addrs dbs sym-ann right-p marker
692    (prog1
693        (gnus-picons-parse-filenames)
694      (kill-buffer (current-buffer)))))
695
696 ;; Initiate a query on the picon database
697 (defun gnus-picons-network-search (user addrs dbs sym-ann right-p marker)
698   (let* ((host (mapconcat 'identity addrs "."))
699          (key (list (or user "unknown") host (if user
700                                                  gnus-picons-user-directories
701                                                dbs)))
702          (cache (assoc key gnus-picons-url-alist)))
703     (if (null cache)
704         (gnus-picons-url-retrieve
705          (concat gnus-picons-piconsearch-url
706                  "?user=" (w3-form-encode-xwfu (or user "unknown"))
707                  "&host=" (w3-form-encode-xwfu host)
708                  "&db=" (mapconcat 'w3-form-encode-xwfu
709                                    (if user
710                                        (append dbs
711                                                gnus-picons-user-directories)
712                                      dbs)
713                                    "+"))
714          'gnus-picons-network-search-callback
715          (list user addrs dbs sym-ann right-p marker))
716       (gnus-picons-network-search-internal
717        user addrs dbs sym-ann right-p marker))))
718
719 ;;; Main jobs dispatcher function
720
721 (defun gnus-picons-next-job-internal ()
722   (when (setq gnus-picons-job-already-running (pop gnus-picons-jobs-alist))
723     (let* ((job gnus-picons-job-already-running)
724            (sym-ann (pop job))
725            (tag (pop job)))
726       (when tag
727         (cond
728          ((stringp tag);; (SYM-ANN "..." RIGHT-P MARKER)
729           (gnus-picons-network-display-internal
730            sym-ann nil tag (pop job) (pop job)))
731          ((eq 'bar tag);; (SYM-ANN 'bar RIGHT-P MARKER)
732           (gnus-picons-network-display-internal
733            sym-ann
734            (let ((gnus-picons-file-suffixes '("xbm")))
735              (gnus-picons-try-face
736               gnus-xmas-glyph-directory "bar."))
737            nil (pop job) (pop job)))
738          ((eq 'search tag);; (SYM-ANN 'search USER ADDRS DBS RIGHT-P MARKER)
739           (gnus-picons-network-search
740            (pop job) (pop job) (pop job) sym-ann (pop job) (pop job)))
741          ((eq 'picon tag);; (SYM-ANN 'picon URL PART RIGHT-P MARKER)
742           (gnus-picons-network-display
743            (pop job) (pop job) sym-ann (pop job) (pop job)))
744          (t
745           (setq gnus-picons-job-already-running nil)
746           (error "Unknown picon job tag %s" tag)))))))
747
748 (defun gnus-picons-next-job ()
749   "Start processing the job queue if it is not in progress."
750   (unless gnus-picons-job-already-running
751     (gnus-picons-next-job-internal)))
752
753 (provide 'gnus-picon)
754
755 ;;; gnus-picon.el ends here