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