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