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