New feature: toggle visibility of mime buttons.
[more-wl.git] / elmo / elmo-filter.el
blob700be31a8b3f149d5d42c3132cb3074218687dfa
1 ;;; elmo-filter.el --- Filtered Folder Interface for ELMO.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Hiroya Murata <lapis-lazuli@pop06.odn.ne.jp>
7 ;; Keywords: mail, net news
9 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
11 ;; This program 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.
16 ;; This program 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.
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.
27 ;;; Commentary:
30 ;;; Code:
32 (require 'elmo)
33 (require 'elmo-signal)
34 (require 'elmo-msgdb)
36 (defvar elmo-filter-number-filename "number-list"
37 "File name for message number database.")
39 ;;; ELMO filter folder
40 (eval-and-compile
41 (luna-define-class elmo-filter-folder (elmo-folder)
42 (condition target require-msgdb number-list flag-count))
43 (luna-define-internal-accessors 'elmo-filter-folder))
45 (luna-define-method elmo-folder-initialize ((folder elmo-filter-folder)
46 name)
47 (let (pair)
48 (setq pair (elmo-parse-search-condition name))
49 (elmo-filter-folder-set-condition-internal folder (car pair))
50 (if (string-match "^ */\\(.*\\)$" (cdr pair))
51 (elmo-filter-folder-set-target-internal
52 folder
53 (elmo-get-folder (elmo-match-string 1 (cdr pair))))
54 (error "Folder syntax error `%s'" (elmo-folder-name-internal folder)))
55 (elmo-filter-folder-set-require-msgdb-internal
56 folder
57 (elmo-folder-search-requires-msgdb-p
58 (elmo-filter-folder-target-internal folder)
59 (elmo-filter-folder-condition-internal folder)))
60 (elmo-filter-folder-set-number-list-internal folder 'not-loaded)
61 (elmo-filter-connect-signals
62 folder
63 (elmo-filter-folder-target-internal folder))
64 folder))
66 (defun elmo-filter-connect-signals (folder target)
67 (elmo-connect-signal
68 target 'flag-changing folder
69 (elmo-define-signal-handler (folder target number old-flags new-flags)
70 (elmo-filter-add-flag-count folder old-flags -1)
71 (elmo-filter-add-flag-count folder new-flags)
72 (elmo-emit-signal 'flag-changing folder number old-flags new-flags))
73 (elmo-define-signal-filter (folder target number)
74 (memq number (elmo-folder-list-messages folder nil t))))
75 (elmo-connect-signal
76 target 'flag-changed folder
77 (elmo-define-signal-handler (folder target numbers)
78 (let ((filterd (elmo-list-filter
79 (elmo-folder-list-messages folder nil t)
80 numbers)))
81 (when filterd
82 (elmo-emit-signal 'flag-changed folder filterd)))))
83 (elmo-connect-signal
84 target 'status-changed folder
85 (elmo-define-signal-handler (folder target numbers)
86 (let ((filterd (elmo-list-filter
87 (elmo-folder-list-messages folder nil t)
88 numbers)))
89 (when filterd
90 (elmo-emit-signal 'status-changed folder filterd)))))
91 (elmo-connect-signal
92 target 'update-overview folder
93 (elmo-define-signal-handler (folder target number)
94 (elmo-emit-signal 'update-overview folder number))
95 (elmo-define-signal-filter (folder target number)
96 (memq number (elmo-folder-list-messages folder nil t)))))
98 (defun elmo-filter-number-list-load (dir)
99 (elmo-object-load
100 (expand-file-name elmo-filter-number-filename dir)))
102 (defun elmo-filter-number-list-save (dir number-list)
103 (elmo-object-save
104 (expand-file-name elmo-filter-number-filename dir)
105 number-list))
107 (defun elmo-filter-folder-number-list-loaded-p (folder)
108 (listp (elmo-filter-folder-number-list-internal folder)))
110 (defun elmo-filter-folder-number-list (folder)
111 (let ((numbers (elmo-filter-folder-number-list-internal folder)))
112 (if (listp numbers)
113 numbers
114 (elmo-filter-folder-set-number-list-internal
115 folder
116 (elmo-filter-number-list-load (elmo-folder-msgdb-path folder))))))
118 (defsubst elmo-filter-folder-countup-message-flags (folder numbers
119 &optional delta)
120 (let ((flag-count (elmo-filter-folder-flag-count-internal folder))
121 (delta (or delta 1))
122 elem)
123 (dolist (number numbers)
124 (dolist (flag (elmo-message-flags folder number))
125 (if (setq elem (assq flag flag-count))
126 (setcdr elem (+ (cdr elem) delta))
127 (setq flag-count (cons (cons flag delta) flag-count)))))
128 (elmo-filter-folder-set-flag-count-internal folder flag-count)))
130 (defun elmo-filter-add-flag-count (folder flags &optional delta)
131 (let ((flag-count (elmo-filter-folder-flag-count-internal folder))
132 (delta (or delta 1))
133 elem)
134 (dolist (flag flags)
135 (if (setq elem (assq flag flag-count))
136 (setcdr elem (+ (cdr elem) delta))
137 (setq flag-count (cons (cons flag delta) flag-count))))
138 (elmo-filter-folder-set-flag-count-internal folder flag-count)))
140 (defun elmo-filter-folder-flag-count (folder)
141 (or (elmo-filter-folder-flag-count-internal folder)
142 (elmo-filter-folder-countup-message-flags
143 folder
144 (elmo-folder-list-messages folder t t))))
146 (defun elmo-filter-folder-copy-flag-count (flag-counts)
147 (mapcar (lambda (pair) (cons (car pair) (cdr pair))) flag-counts))
149 (luna-define-method elmo-folder-open :after ((folder elmo-filter-folder)
150 &optional load-msgdb)
151 (when load-msgdb
152 (elmo-filter-folder-number-list folder)
153 (elmo-filter-folder-flag-count folder)
154 (elmo-folder-msgdb (elmo-filter-folder-target-internal folder))))
156 (luna-define-method elmo-folder-open-internal ((folder elmo-filter-folder))
157 (elmo-folder-open-internal (elmo-filter-folder-target-internal folder)))
159 (luna-define-method elmo-folder-check ((folder elmo-filter-folder))
160 (when (elmo-filter-folder-require-msgdb-internal folder)
161 (elmo-folder-synchronize (elmo-filter-folder-target-internal folder))))
163 (luna-define-method elmo-folder-close-internal ((folder elmo-filter-folder))
164 (elmo-folder-close-internal (elmo-filter-folder-target-internal folder)))
166 (luna-define-method elmo-folder-close ((folder elmo-filter-folder))
167 (elmo-generic-folder-close folder)
168 (elmo-filter-folder-set-number-list-internal folder 'not-loaded)
169 (elmo-filter-folder-set-flag-count-internal folder nil)
170 (elmo-folder-close (elmo-filter-folder-target-internal folder)))
172 (luna-define-method elmo-folder-commit ((folder elmo-filter-folder))
173 (elmo-folder-commit (elmo-filter-folder-target-internal folder))
174 (when (elmo-folder-persistent-p folder)
175 (elmo-folder-set-info-max-by-numdb
176 folder
177 (elmo-folder-list-messages folder nil 'in-msgdb))
178 (elmo-msgdb-killed-list-save
179 (elmo-folder-msgdb-path folder)
180 (elmo-folder-killed-list-internal folder))
181 (when (elmo-filter-folder-number-list-loaded-p folder)
182 (elmo-filter-number-list-save
183 (elmo-folder-msgdb-path folder)
184 (elmo-filter-folder-number-list folder)))))
186 (luna-define-method elmo-folder-expand-msgdb-path ((folder
187 elmo-filter-folder))
188 (expand-file-name
189 (elmo-replace-string-as-filename (elmo-folder-name-internal folder))
190 (expand-file-name "filter" elmo-msgdb-directory)))
192 (luna-define-method elmo-folder-search-requires-msgdb-p ((folder
193 elmo-filter-folder)
194 condition)
195 (elmo-folder-search-requires-msgdb-p
196 (elmo-filter-folder-target-internal folder) condition))
198 (luna-define-method elmo-folder-newsgroups ((folder elmo-filter-folder))
199 (elmo-folder-newsgroups (elmo-filter-folder-target-internal folder)))
201 (luna-define-method elmo-find-fetch-strategy ((folder elmo-filter-folder)
202 number
203 &optional
204 ignore-cache
205 require-entireness)
206 (elmo-find-fetch-strategy
207 (elmo-filter-folder-target-internal folder)
208 number ignore-cache require-entireness))
210 (luna-define-method elmo-folder-get-primitive-list ((folder
211 elmo-filter-folder))
212 (elmo-folder-get-primitive-list (elmo-filter-folder-target-internal folder)))
214 (luna-define-method elmo-folder-contains-type ((folder elmo-filter-folder)
215 type)
216 (elmo-folder-contains-type
217 (elmo-filter-folder-target-internal folder)
218 type))
220 (luna-define-method elmo-folder-append-buffer ((folder elmo-filter-folder)
221 &optional flag number)
222 (elmo-folder-append-buffer
223 (elmo-filter-folder-target-internal folder)
224 flag number))
226 (defun elmo-folder-append-messages-filter-* (dst-folder
227 src-folder
228 numbers
229 same-number)
230 (elmo-folder-append-messages dst-folder
231 (elmo-filter-folder-target-internal src-folder)
232 numbers
233 same-number))
235 (defun elmo-folder-append-messages-*-filter (dst-folder
236 src-folder
237 numbers
238 same-number)
239 (elmo-folder-append-messages (elmo-filter-folder-target-internal dst-folder)
240 src-folder
241 numbers
242 same-number))
244 (luna-define-method elmo-message-fetch-bodystructure ((folder
245 elmo-filter-folder)
246 number strategy)
247 (elmo-message-fetch-bodystructure
248 (elmo-filter-folder-target-internal folder)
249 number strategy))
251 (luna-define-method elmo-message-fetch ((folder elmo-filter-folder)
252 number strategy
253 &optional unseen section)
254 (elmo-message-fetch (elmo-filter-folder-target-internal folder)
255 number strategy unseen section))
257 (luna-define-method elmo-folder-delete-messages ((folder elmo-filter-folder)
258 numbers)
259 (let ((flag-count (elmo-filter-folder-copy-flag-count
260 (elmo-filter-folder-flag-count-internal folder)))
261 (messages (copy-sequence
262 (elmo-filter-folder-number-list folder)))
263 success)
264 (elmo-folder-detach-messages folder numbers)
265 (unless (setq success
266 (elmo-folder-delete-messages
267 (elmo-filter-folder-target-internal folder) numbers))
268 (elmo-filter-folder-set-flag-count-internal folder flag-count)
269 (elmo-filter-folder-set-number-list-internal folder messages))
270 success))
272 (luna-define-method elmo-folder-list-messages ((folder elmo-filter-folder)
273 &optional visible-only in-msgdb)
274 (let ((list (if in-msgdb
276 (elmo-folder-list-messages-internal folder visible-only)))
277 (killed-list (elmo-folder-killed-list-internal folder)))
278 (unless (listp list)
279 ;; Use current list.
280 (setq list (elmo-filter-folder-number-list folder)))
281 (if visible-only
282 (elmo-living-messages list killed-list)
283 (if (and in-msgdb killed-list list)
284 (elmo-uniq-sorted-list
285 (sort (nconc (elmo-number-set-to-number-list killed-list) list) #'<)
286 #'eq)
287 list))))
289 (luna-define-method elmo-folder-list-messages-internal
290 ((folder elmo-filter-folder) &optional nohide)
291 (let ((target (elmo-filter-folder-target-internal folder)))
292 (if (or (elmo-folder-plugged-p target)
293 (not (elmo-folder-persistent-p folder)))
294 ;; search target folder
295 (elmo-folder-search
296 target
297 (elmo-filter-folder-condition-internal folder))
298 ;; not available
299 t)))
301 (luna-define-method elmo-folder-list-flagged ((folder elmo-filter-folder)
302 flag
303 &optional in-msgdb)
304 (elmo-list-filter
305 (elmo-folder-list-messages folder nil t)
306 (elmo-folder-list-flagged
307 (elmo-filter-folder-target-internal folder) flag in-msgdb)))
309 (luna-define-method elmo-folder-list-subfolders ((folder elmo-filter-folder)
310 &optional one-level)
311 (let* ((target (elmo-filter-folder-target-internal folder))
312 (prefix (and (string-match
313 (concat "^\\(.*\\)"
314 (regexp-quote
315 (elmo-folder-name-internal
316 target))
317 "$")
318 (elmo-folder-name-internal folder))
319 (match-string 1 (elmo-folder-name-internal
320 folder)))))
321 (elmo-mapcar-list-of-list
322 (lambda (x) (concat prefix x))
323 (elmo-folder-list-subfolders target one-level))))
325 (luna-define-method elmo-folder-diff :around ((folder elmo-filter-folder))
326 (let ((condition (elmo-filter-folder-condition-internal folder))
327 diff)
328 (if (vectorp condition)
329 (cond
330 ((and (string= (elmo-filter-key condition) "flag")
331 (or (string= (elmo-filter-value condition) "any")
332 (string= (elmo-filter-value condition) "digest")
333 (string= (elmo-filter-value condition) "unread")))
334 (setq diff (elmo-folder-diff (elmo-filter-folder-target-internal
335 folder)))
336 (if (consp (cdr diff))
337 ;; new unread unread
338 (list (car diff) (nth 1 diff) (nth 1 diff))
339 (cons (car diff) (car diff))))
340 ((string= "last" (elmo-filter-key condition))
341 (luna-call-next-method))
343 (cons nil (cdr (elmo-folder-diff (elmo-filter-folder-target-internal
344 folder))))))
345 (luna-call-next-method))))
347 (luna-define-method elmo-folder-status ((folder elmo-filter-folder))
348 (elmo-folder-status
349 (elmo-filter-folder-target-internal folder)))
351 (luna-define-method elmo-folder-exists-p ((folder elmo-filter-folder))
352 (elmo-folder-exists-p (elmo-filter-folder-target-internal folder)))
354 (luna-define-method elmo-folder-creatable-p ((folder elmo-filter-folder))
355 (elmo-folder-creatable-p (elmo-filter-folder-target-internal folder)))
357 (luna-define-method elmo-folder-writable-p ((folder elmo-filter-folder))
358 (elmo-folder-writable-p (elmo-filter-folder-target-internal folder)))
360 (luna-define-method elmo-folder-create ((folder elmo-filter-folder))
361 (elmo-folder-create (elmo-filter-folder-target-internal folder)))
363 (luna-define-method elmo-folder-search ((folder elmo-filter-folder)
364 condition &optional numbers)
365 ;; search from messages in this folder
366 (let ((result (elmo-folder-search
367 (elmo-filter-folder-target-internal folder)
368 condition
369 (elmo-folder-list-messages folder))))
370 (if numbers
371 (elmo-list-filter numbers result)
372 result)))
374 (luna-define-method elmo-message-use-cache-p ((folder elmo-filter-folder)
375 number)
376 (elmo-message-use-cache-p (elmo-filter-folder-target-internal folder)
377 number))
379 (luna-define-method elmo-folder-message-file-p ((folder elmo-filter-folder))
380 (elmo-folder-message-file-p (elmo-filter-folder-target-internal folder)))
382 (luna-define-method elmo-folder-local-p ((folder elmo-filter-folder))
383 (elmo-folder-local-p (elmo-filter-folder-target-internal folder)))
385 (luna-define-method elmo-folder-plugged-p ((folder elmo-filter-folder))
386 (elmo-folder-plugged-p (elmo-filter-folder-target-internal folder)))
388 (luna-define-method elmo-folder-set-plugged ((folder elmo-filter-folder)
389 plugged &optional add)
390 (elmo-folder-set-plugged (elmo-filter-folder-target-internal folder)
391 plugged add))
393 (luna-define-method elmo-message-file-name ((folder elmo-filter-folder)
394 number)
395 (elmo-message-file-name (elmo-filter-folder-target-internal folder)
396 number))
398 (luna-define-method elmo-message-flag-available-p ((folder
399 elmo-filter-folder) number
400 flag)
401 (elmo-message-flag-available-p
402 (elmo-filter-folder-target-internal folder)
403 number flag))
405 (luna-define-method elmo-message-flags ((folder elmo-filter-folder) number)
406 (elmo-message-flags (elmo-filter-folder-target-internal folder)
407 number))
409 (luna-define-method elmo-message-set-cached ((folder elmo-filter-folder)
410 number cached)
411 (elmo-message-set-cached
412 (elmo-filter-folder-target-internal folder) number cached))
414 (luna-define-method elmo-message-number ((folder elmo-filter-folder)
415 message-id)
416 (elmo-message-number (elmo-filter-folder-target-internal folder)
417 message-id))
419 (luna-define-method elmo-message-entity ((folder elmo-filter-folder) key)
420 (elmo-message-entity (elmo-filter-folder-target-internal folder) key))
422 (luna-define-method elmo-message-entity-parent ((folder elmo-filter-folder)
423 entity)
424 (let ((parent (elmo-message-entity-parent
425 (elmo-filter-folder-target-internal folder)
426 entity)))
427 (when (memq (elmo-message-entity-number parent)
428 (elmo-filter-folder-number-list folder))
429 parent)))
431 (luna-define-method elmo-folder-flag-table ((folder elmo-filter-folder)
432 &optional if-exists)
433 (elmo-folder-flag-table (elmo-filter-folder-target-internal folder)
434 if-exists))
436 (luna-define-method elmo-folder-close-flag-table ((folder elmo-filter-folder))
437 (elmo-folder-close-flag-table (elmo-filter-folder-target-internal folder)))
439 (luna-define-method elmo-folder-count-flags ((folder elmo-filter-folder))
440 (elmo-filter-folder-flag-count folder))
442 (luna-define-method elmo-folder-set-flag ((folder elmo-filter-folder)
443 numbers
444 flag
445 &optional is-local)
446 (elmo-folder-set-flag (elmo-filter-folder-target-internal folder)
447 numbers flag is-local))
449 (luna-define-method elmo-folder-unset-flag ((folder elmo-filter-folder)
450 numbers
451 flag
452 &optional is-local)
453 (elmo-folder-unset-flag (elmo-filter-folder-target-internal folder)
454 numbers flag is-local))
456 (luna-define-method elmo-message-folder ((folder elmo-filter-folder)
457 number)
458 (elmo-message-folder (elmo-filter-folder-target-internal folder) number))
460 (luna-define-method elmo-message-field ((folder elmo-filter-folder)
461 number field &optional type)
462 (elmo-message-field
463 (elmo-filter-folder-target-internal folder) number field type))
465 (luna-define-method elmo-message-set-field ((folder elmo-filter-folder)
466 number field value)
467 (elmo-message-set-field
468 (elmo-filter-folder-target-internal folder) number field value))
470 (luna-define-method elmo-folder-clear ((folder elmo-filter-folder)
471 &optional keep-killed)
472 (unless keep-killed
473 (elmo-folder-set-killed-list-internal folder nil))
474 (elmo-filter-folder-set-number-list-internal folder nil)
475 (elmo-filter-folder-set-flag-count-internal folder nil))
477 (luna-define-method elmo-folder-synchronize ((folder elmo-filter-folder)
478 &optional
479 disable-killed
480 ignore-msgdb
481 no-check
482 mask)
483 (let ((killed-list (elmo-folder-killed-list-internal folder))
484 numbers)
485 (unless no-check
486 (when (elmo-filter-folder-require-msgdb-internal folder)
487 (elmo-folder-synchronize (elmo-filter-folder-target-internal folder)
488 disable-killed
489 ignore-msgdb
490 no-check
491 mask)))
492 (setq numbers (elmo-folder-list-messages folder (not disable-killed)))
493 (when (and numbers
494 (not (elmo-filter-folder-require-msgdb-internal folder)))
495 (elmo-folder-synchronize (elmo-filter-folder-target-internal folder)
496 'disable-killed
497 ignore-msgdb
498 no-check
499 (if mask
500 (elmo-list-filter mask numbers)
501 numbers)))
502 (when (and disable-killed ignore-msgdb)
503 (elmo-folder-set-killed-list-internal folder nil))
504 (elmo-filter-folder-set-number-list-internal folder numbers)
505 (elmo-filter-folder-set-flag-count-internal folder nil)
508 (luna-define-method elmo-folder-detach-messages ((folder elmo-filter-folder)
509 numbers)
510 (elmo-filter-folder-countup-message-flags folder numbers -1)
511 (elmo-filter-folder-set-number-list-internal
512 folder
513 (elmo-list-delete numbers (elmo-filter-folder-number-list folder) #'delq))
516 (luna-define-method elmo-folder-length ((folder elmo-filter-folder))
517 (and (elmo-filter-folder-number-list-loaded-p folder)
518 (length (elmo-filter-folder-number-list-internal folder))))
520 (require 'product)
521 (product-provide (provide 'elmo-filter) (require 'elmo-version))
523 ;;; elmo-filter.el ends here