4c5a0150424c75add3911850af4a4f72e53da9b3
[gnus] / 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 (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" "/usr/lib/picon")
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
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-picons-make-annotation (&rest args)
251   nil)
252
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 (or (not (fboundp 'device-type)) (equal (device-type) 'x))
259                (setq from (mail-fetch-field "from"))
260                (setq from (downcase (or (cadr (mail-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 directory)
349   (let ((dbs gnus-picons-database)
350         result db dir)
351     (unless (listp dbs)
352       (setq dbs (list dbs)))
353     (while (and (not result)
354                 (setq db (pop dbs)))
355       (setq dir (expand-file-name directory gnus-picons-database))
356       (setq result
357             (gnus-picons-try-face
358              (dolist (part (reverse addrs) dir)
359                (setq dir (expand-file-name part dir))))))
360     result))
361
362 (defun gnus-picons-lookup (addrs dirs)
363   "Lookup the picon for ADDRS in databases DIRS.
364 Returns the picon filename or nil if none found."
365   (let (result)
366     (while (and dirs (null result))
367       (setq result (gnus-picons-lookup-internal addrs (pop dirs))))
368     result))
369
370 (defun gnus-picons-lookup-user-internal (user domains)
371   (let ((dirs gnus-picons-user-directories)
372         domains-tmp dir picon)
373     (while (and dirs (null picon))
374       (setq domains-tmp domains
375             dir (pop dirs))
376       (while (and domains-tmp
377                   (null (setq picon (gnus-picons-lookup-internal
378                                      (cons user domains-tmp) dir))))
379         (pop domains-tmp))
380       ;; Also make a try in MISC subdir
381       (unless picon
382         (setq picon (gnus-picons-lookup-internal (list user "MISC") dir))))
383     picon))
384
385 (defun gnus-picons-lookup-user (user domains)
386   "Lookup the picon for USER at DOMAINS.
387 USER is a string containing a name.
388 DOMAINS is a list of strings from the fully qualified domain name."
389   (or (gnus-picons-lookup-user-internal user domains)
390       (gnus-picons-lookup-user-internal "unknown" domains)))
391
392 (defun gnus-picons-lookup-pairs (domains directories)
393   "Lookup picons for DOMAINS and all its parents in DIRECTORIES.
394 Returns a list of PAIRS whose CAR is the picon filename or nil if
395 none, and whose CDR is the corresponding element of DOMAINS."
396   (let (picons)
397     (setq directories (if (listp directories)
398                           directories
399                         (list directories)))
400     (while domains
401       (push (list (gnus-picons-lookup (cons "unknown" domains) directories)
402                   (pop domains))
403             picons))
404     picons))
405
406 (defun gnus-picons-display-picon-or-name (picon name &optional right-p)
407   (cond (picon (gnus-picons-display-glyph picon name right-p))
408         (gnus-picons-display-as-address (list (gnus-picons-make-annotation
409                                                (vector 'string :data name)
410                                                nil 'text
411                                                nil nil nil right-p)))))
412
413 (defun gnus-picons-display-pairs (pairs &optional bar-p dot-p right-p)
414   "Display picons in list PAIRS."
415   (let ((domain-p (and gnus-picons-display-as-address dot-p))
416         pair picons)
417     (when (and bar-p domain-p right-p
418                gnus-picons-display-article-move-p)
419       (setq picons (gnus-picons-display-glyph
420                     (let ((gnus-picons-file-suffixes '("xbm")))
421                       (gnus-picons-try-face
422                        gnus-xmas-glyph-directory "bar."))
423                     nil right-p)))
424     (while (setq pair (pop pairs))
425       (setq picons (nconc picons
426                           (gnus-picons-display-picon-or-name
427                            (car pair) (cadr pair) right-p)
428                           (if (and domain-p pairs)
429                               (list (gnus-picons-make-annotation
430                                      (vector 'string :data dot-p)
431                                      nil 'text nil nil nil right-p))))))
432     picons))
433
434 (defun gnus-picons-try-face (dir &optional filebase)
435   (let* ((dir (file-name-as-directory dir))
436          (filebase (or filebase "face."))
437          (key (concat dir filebase))
438          (glyph (cdr (assoc key gnus-picons-glyph-alist)))
439          (suffixes gnus-picons-file-suffixes)
440          f suf)
441     (while (setq suf (pop suffixes))
442       (when (file-exists-p (setq f (expand-file-name
443                                     (concat filebase suf)
444                                     dir)))
445         (setq suffixes nil
446               glyph (make-glyph f))
447         (if (equal suf "xbm")
448             (set-glyph-face glyph 'gnus-picons-xbm-face)
449           (set-glyph-face glyph 'gnus-picons-face))
450         (push (cons key glyph) gnus-picons-glyph-alist)))
451     glyph))
452
453 (defun gnus-picons-display-glyph (glyph &optional part rightp)
454   (set-glyph-baseline glyph 70)
455   (let ((new (gnus-picons-make-annotation
456               glyph (point) 'text nil nil nil rightp)))
457     (when (and part gnus-picons-display-as-address)
458       (set-annotation-data
459        new (cons new (make-glyph (vector 'string :data part))))
460       (set-annotation-action new 'gnus-picons-action-toggle))
461     (nconc
462      (list new)
463      (if (and (eq major-mode 'gnus-article-mode)
464               (not gnus-picons-display-as-address)
465               (not part))
466          (list (gnus-picons-make-annotation [string :data " "] (point)
467                                             'text nil nil nil rightp))))))
468
469 (defun gnus-picons-action-toggle (data)
470   "Toggle annotation."
471   (interactive "e")
472   (let* ((annot (car data))
473          (glyph (annotation-glyph annot)))
474     (set-annotation-glyph annot (cdr data))
475     (set-annotation-data annot (cons annot glyph))))
476
477 (defun gnus-picons-clear-cache ()
478   "Clear the picons cache."
479   (interactive)
480   (setq gnus-picons-glyph-alist nil
481         gnus-picons-url-alist nil))
482
483 (gnus-add-shutdown 'gnus-picons-close 'gnus)
484
485 (defun gnus-picons-close ()
486   "Shut down the picons."
487   (if gnus-picons-clear-cache-on-shutdown
488       (gnus-picons-clear-cache)))
489
490 ;;; Query a remote DB.  This requires some stuff from w3 !
491
492 (eval-and-compile
493   (ignore-errors
494     (require 'url)
495     (require 'w3-forms)))
496
497 (defun gnus-picons-url-retrieve (url fn arg)
498   (let ((old-asynch (default-value 'url-be-asynchronous))
499         (url-working-buffer (generate-new-buffer " *picons*"))
500         (url-package-name "Gnus")
501         (url-package-version gnus-version-number)
502         url-request-method)
503     (setq-default url-be-asynchronous t)
504     (save-excursion
505       (set-buffer url-working-buffer)
506       (setq url-be-asynchronous t
507             url-current-callback-data arg
508             url-current-callback-func fn)
509       (url-retrieve url t))
510     (setq-default url-be-asynchronous old-asynch)))
511
512 (defun gnus-picons-make-glyph (type)
513   "Make a TYPE glyph using current buffer as data.  Handles xbm nicely."
514   (cond ((null type) nil)
515         ((eq type 'xbm) (let ((fname (make-temp-name "/tmp/picon")))
516                           (write-region (point-min) (point-max) fname
517                                         nil 'quiet)
518                           (prog1 (make-glyph (vector 'xbm :file fname))
519                             (delete-file fname))))
520         (t (make-glyph (vector type :data (buffer-string))))))
521
522 ;;; Parsing of piconsearch result page.
523
524 ;; Assumes:
525 ;; 1 - each value field has the form: "<strong>key</strong> = <kbd>value</kbd>"
526 ;; 2 - a "<p>" separates the keywords from the results
527 ;; 3 - every results begins by the path within the database at the beginning
528 ;;     of the line in raw text.
529 ;; 3b - and the href following it is the preferred image type.
530
531 ;; if 1 or 2 is not met, it will probably cause an error.  The other
532 ;; will go undetected
533
534 (defun gnus-picons-parse-value (name)
535   (goto-char (point-min))
536   (if (re-search-forward (concat "<strong>"
537                                  (regexp-quote name)
538                                  "</strong> *= *<kbd> *\\([^ <][^<]*\\) *</kbd>")
539                          nil t)
540       (buffer-substring (match-beginning 1) (match-end 1))))
541
542 (defun gnus-picons-parse-filenames ()
543   ;; returns an alist of ((USER ADDRS DB) . URL)
544   (let ((case-fold-search t)
545         (user (gnus-picons-parse-value "user"))
546         (host (gnus-picons-parse-value "host"))
547         (dbs (message-tokenize-header (gnus-picons-parse-value "db") " "))
548         start-re cur-db cur-host cur-user types res)
549     ;; now point will be somewhere in the header.  Find beginning of
550     ;; entries
551     (when (and user host dbs)
552       (setq start-re
553             (concat
554              ;; dbs
555              "^\\(" (mapconcat 'regexp-quote dbs "\\|") "\\)/"
556              ;; host
557              "\\(\\(" (mapconcat 'regexp-quote
558                                  (message-tokenize-header host ".") "/\\|")
559              "/\\|MISC/\\)*\\)"
560              ;; user
561              "\\(" (regexp-quote user) "\\|unknown\\)/"
562              "face\\."))
563       (re-search-forward "<p>[ \t\n]*")
564       (while (re-search-forward start-re nil t)
565         (setq cur-db (buffer-substring (match-beginning 1) (match-end 1))
566               cur-host (buffer-substring (match-beginning 2) (match-end 2))
567               cur-user (buffer-substring (match-beginning 4) (match-end 4))
568               cur-host (nreverse (message-tokenize-header cur-host "/")))
569         ;; XXX - KLUDGE: there is a blank picon in news/MISC/unknown
570         (unless (and (string-equal cur-db "news")
571                      (string-equal cur-user "unknown")
572                      (equal cur-host '("MISC")))
573           ;; ok now we have found an entry (USER HOST DB), find the
574           ;; corresponding picon URL
575           (save-restriction
576             ;; restrict region to this entry
577             (narrow-to-region (point) (search-forward "<br>"))
578             (goto-char (point-min))
579             (setq types gnus-picons-file-suffixes)
580             (while (and types
581                         (not (re-search-forward
582                               (concat "<a[ \t\n]+href=\"\\([^\"]*\\."
583                                       (regexp-quote (car types)) "\\)\"")
584                               nil t)))
585               (pop types))
586             (push (cons (list cur-user cur-host cur-db)
587                         (buffer-substring (match-beginning 1) (match-end 1)))
588                   res))))
589       (nreverse res))))
590
591 ;;; picon network display functions :
592
593 (defun gnus-picons-network-display-internal (sym-ann glyph part right-p marker)
594   (let ((buf (marker-buffer marker))
595         (pos (marker-position marker)))
596     (if (and buf pos)
597         (save-excursion
598           (set-buffer buf)
599           (goto-char pos)
600           (gnus-picons-display-picon-or-name glyph part right-p))))
601   (gnus-picons-next-job-internal))
602
603 (defun gnus-picons-network-display-callback (url part sym-ann right-p marker)
604   (let ((glyph (gnus-picons-make-glyph (cdr (assoc url-current-mime-type
605                                                    w3-image-mappings)))))
606     (kill-buffer (current-buffer))
607     (push (cons url glyph) gnus-picons-glyph-alist)
608     ;; only do the job if it has not been preempted.
609     (if (equal gnus-picons-job-already-running
610                (list sym-ann 'picon url part right-p marker))
611         (gnus-picons-network-display-internal
612          sym-ann glyph part right-p marker)
613       (gnus-picons-next-job-internal))))
614
615 (defun gnus-picons-network-display (url part sym-ann right-p marker)
616   (let ((cache (assoc url gnus-picons-glyph-alist)))
617     (if (or cache (null url))
618         (gnus-picons-network-display-internal
619          sym-ann (cdr cache) part right-p marker)
620       (gnus-picons-url-retrieve url 'gnus-picons-network-display-callback
621                                 (list url part sym-ann right-p marker)))))
622
623 ;;; search job functions
624
625 (defun gnus-picons-display-bar-p ()
626   (if (eq gnus-picons-display-where 'article)
627       gnus-picons-display-article-move-p
628     gnus-picons-display-as-address))
629
630 (defun gnus-picons-network-search-internal (user addrs dbs sym-ann right-p
631                                                  marker &optional fnames)
632   (let (curkey dom pfx url dbs-tmp cache new-jobs)
633     ;; First do the domain search
634     (dolist (part (if right-p
635                       (reverse addrs)
636                     addrs))
637       (setq pfx (nconc (list part) pfx)
638             dom (cond ((and dom right-p) (concat part "." dom))
639                       (dom (concat dom "." part))
640                       (t part))
641             curkey (list "unknown" dom dbs))
642       (when (null (setq cache (assoc curkey gnus-picons-url-alist)))
643         ;; This one is not yet in the cache, create a new entry
644         ;; Search for an entry
645         (setq dbs-tmp dbs
646               url nil)
647         (while (and dbs-tmp (null url))
648           (setq url (or (cdr (assoc (list "unknown" pfx (car dbs-tmp)) fnames))
649                         (and (eq dom part)
650                              ;; This is the first component.  Try the
651                              ;; catch-all MISC component
652                              (cdr (assoc (list "unknown"
653                                                '("MISC")
654                                                (car dbs-tmp))
655                                          fnames)))))
656           (pop dbs-tmp))
657         (push (setq cache (cons curkey url)) gnus-picons-url-alist))
658       ;; Put this glyph in the job list
659       (if (and (not (eq dom part)) gnus-picons-display-as-address)
660           (push (list sym-ann "." right-p marker) new-jobs))
661       (push (list sym-ann 'picon (cdr cache) part right-p marker) new-jobs))
662     ;; next, the user search
663     (when user
664       (setq curkey (list user dom gnus-picons-user-directories))
665       (if (null (setq cache (assoc curkey gnus-picons-url-alist)))
666           (let ((users (list user "unknown"))
667                 dirs usr domains-tmp dir picon)
668             (while (and users (null picon))
669               (setq dirs gnus-picons-user-directories
670                     usr (pop users))
671               (while (and dirs (null picon))
672                 (setq domains-tmp addrs
673                       dir (pop dirs))
674                 (while (and domains-tmp
675                             (null (setq picon (assoc (list usr domains-tmp dir)
676                                                      fnames))))
677                   (pop domains-tmp))
678                 (unless picon
679                   (setq picon (assoc (list usr '("MISC") dir) fnames)))))
680             (push (setq cache (cons curkey (cdr picon)))
681                   gnus-picons-url-alist)))
682       (if (and gnus-picons-display-as-address new-jobs)
683           (push (list sym-ann "@" right-p marker) new-jobs))
684       (push (list sym-ann 'picon (cdr cache) user right-p marker) new-jobs))
685     (if (and (gnus-picons-display-bar-p) (not right-p))
686         (push (list sym-ann 'bar right-p marker) new-jobs))
687     ;; only put the jobs in the queue if this job has not been preempted.
688     (if (equal gnus-picons-job-already-running
689                (list sym-ann 'search user addrs dbs right-p marker))
690         (setq gnus-picons-jobs-alist
691               (nconc (if (and (gnus-picons-display-bar-p) right-p)
692                          (list (list sym-ann 'bar right-p marker)))
693                      (nreverse new-jobs)
694                      gnus-picons-jobs-alist)))
695     (gnus-picons-next-job-internal)))
696
697 (defun gnus-picons-network-search-callback (user addrs dbs sym-ann right-p
698                                                  marker)
699   (gnus-picons-network-search-internal
700    user addrs dbs sym-ann right-p marker
701    (prog1
702        (gnus-picons-parse-filenames)
703      (kill-buffer (current-buffer)))))
704
705 ;; Initiate a query on the picon database
706 (defun gnus-picons-network-search (user addrs dbs sym-ann right-p marker)
707   (let* ((host (mapconcat 'identity addrs "."))
708          (key (list (or user "unknown") host (if user
709                                                  gnus-picons-user-directories
710                                                dbs)))
711          (cache (assoc key gnus-picons-url-alist)))
712     (if (null cache)
713         (gnus-picons-url-retrieve
714          (concat gnus-picons-piconsearch-url
715                  "?user=" (w3-form-encode-xwfu (or user "unknown"))
716                  "&host=" (w3-form-encode-xwfu host)
717                  "&db=" (mapconcat 'w3-form-encode-xwfu
718                                    (if user
719                                        (append dbs
720                                                gnus-picons-user-directories)
721                                      dbs)
722                                    "+"))
723          'gnus-picons-network-search-callback
724          (list user addrs dbs sym-ann right-p marker))
725       (gnus-picons-network-search-internal
726        user addrs dbs sym-ann right-p marker))))
727
728 ;;; Main jobs dispatcher function
729
730 (defun gnus-picons-next-job-internal ()
731   (when (setq gnus-picons-job-already-running (pop gnus-picons-jobs-alist))
732     (let* ((job gnus-picons-job-already-running)
733            (sym-ann (pop job))
734            (tag (pop job)))
735       (when tag
736         (cond
737          ((stringp tag);; (SYM-ANN "..." RIGHT-P MARKER)
738           (gnus-picons-network-display-internal
739            sym-ann nil tag (pop job) (pop job)))
740          ((eq 'bar tag);; (SYM-ANN 'bar RIGHT-P MARKER)
741           (gnus-picons-network-display-internal
742            sym-ann
743            (let ((gnus-picons-file-suffixes '("xbm")))
744              (gnus-picons-try-face
745               gnus-xmas-glyph-directory "bar."))
746            nil (pop job) (pop job)))
747          ((eq 'search tag);; (SYM-ANN 'search USER ADDRS DBS RIGHT-P MARKER)
748           (gnus-picons-network-search
749            (pop job) (pop job) (pop job) sym-ann (pop job) (pop job)))
750          ((eq 'picon tag);; (SYM-ANN 'picon URL PART RIGHT-P MARKER)
751           (gnus-picons-network-display
752            (pop job) (pop job) sym-ann (pop job) (pop job)))
753          (t
754           (setq gnus-picons-job-already-running nil)
755           (error "Unknown picon job tag %s" tag)))))))
756
757 (defun gnus-picons-next-job ()
758   "Start processing the job queue if it is not in progress."
759   (unless gnus-picons-job-already-running
760     (gnus-picons-next-job-internal)))
761
762 (provide 'gnus-picon)
763
764 ;;; gnus-picon.el ends here