1 ;;; elmo-multi.el --- Multiple Folder Interface for ELMO.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
8 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
35 (defvar elmo-multi-divide-number 100000
36 "*Multi divider number.")
40 (luna-define-class elmo-multi-folder (elmo-folder)
41 (children divide-number))
42 (luna-define-internal-accessors 'elmo-multi-folder))
44 (luna-define-method elmo-folder-initialize ((folder
47 (while (> (length (car (setq name (elmo-parse-token name ",")))) 0)
48 (elmo-multi-folder-set-children-internal
50 (nconc (elmo-multi-folder-children-internal
52 (list (elmo-make-folder (car name)))))
53 (setq name (cdr name))
54 (when (and (> (length name) 0)
55 (eq (aref name 0) ?,))
56 (setq name (substring name 1))))
57 (elmo-multi-folder-set-divide-number-internal
59 elmo-multi-divide-number)
62 (luna-define-method elmo-folder-open-internal ((folder elmo-multi-folder))
63 (dolist (fld (elmo-multi-folder-children-internal folder))
64 (elmo-folder-open-internal fld)))
66 (luna-define-method elmo-folder-check ((folder elmo-multi-folder))
67 (dolist (fld (elmo-multi-folder-children-internal folder))
68 (elmo-folder-synchronize fld)))
70 (luna-define-method elmo-folder-close-internal ((folder elmo-multi-folder))
71 (dolist (fld (elmo-multi-folder-children-internal folder))
72 (elmo-folder-close-internal fld)))
74 (luna-define-method elmo-folder-close :after ((folder elmo-multi-folder))
75 (dolist (fld (elmo-multi-folder-children-internal folder))
76 (elmo-folder-set-msgdb-internal fld nil)))
78 (luna-define-method elmo-folder-expand-msgdb-path ((folder
80 (expand-file-name (elmo-replace-string-as-filename
81 (elmo-folder-name-internal folder))
82 (expand-file-name "multi"
83 elmo-msgdb-directory)))
85 (luna-define-method elmo-folder-newsgroups ((folder elmo-multi-folder))
89 'elmo-folder-newsgroups
92 'elmo-folder-get-primitive-list
93 (elmo-multi-folder-children-internal folder)))))))
95 (luna-define-method elmo-folder-get-primitive-list ((folder elmo-multi-folder))
98 'elmo-folder-get-primitive-list
99 (elmo-multi-folder-children-internal folder))))
101 (luna-define-method elmo-folder-contains-type ((folder elmo-multi-folder) type)
102 (let ((children (elmo-multi-folder-children-internal folder))
105 (when (elmo-folder-contains-type (car children) type)
108 (setq children (cdr children)))
111 (luna-define-method elmo-message-folder ((folder elmo-multi-folder)
113 (nth (- (/ number (elmo-multi-folder-divide-number-internal folder)) 1)
114 (elmo-multi-folder-children-internal folder)))
116 (defun elmo-multi-msgdb (msgdb base)
117 (list (mapcar (function
119 (elmo-msgdb-overview-entity-set-number
122 (elmo-msgdb-overview-entity-get-number x)))))
132 (cdr x)))) (nth 2 msgdb))))
134 (defun elmo-multi-split-numbers (folder numlist &optional as-is)
135 (let ((numbers (sort numlist '<))
136 (divider (elmo-multi-folder-divide-number-internal folder))
138 one-list numbers-list)
140 (setq cur-number (+ cur-number 1))
145 (* divider cur-number))
147 (setq one-list (nconc
153 (* divider cur-number))))))
154 (setq numbers (cdr numbers)))
155 (setq numbers-list (nconc numbers-list (list one-list))))
158 (luna-define-method elmo-folder-msgdb-create ((folder elmo-multi-folder)
160 (let* ((folders (elmo-multi-folder-children-internal folder))
161 overview number-alist mark-alist entity
166 (setq numbers-list (elmo-multi-split-numbers folder numbers))
168 (while (< cur-number (length folders))
169 (if (nth cur-number numbers-list)
174 (elmo-folder-msgdb-create (nth cur-number folders)
175 (nth cur-number numbers-list)
177 (* (elmo-multi-folder-divide-number-internal folder)
179 (setq cur-number (1+ cur-number)))
180 (elmo-msgdb-sort-by-date msgdb)))
182 (luna-define-method elmo-folder-process-crosspost ((folder elmo-multi-folder)
185 (let ((number-alists (elmo-multi-split-number-alist
187 (elmo-msgdb-get-number-alist
188 (elmo-folder-msgdb folder))))
190 (dolist (child (elmo-multi-folder-children-internal folder))
191 (elmo-folder-process-crosspost child (car number-alists))
192 (setq cur-number (+ 1 cur-number)
193 number-alists (cdr number-alists)))))
195 (defsubst elmo-multi-folder-append-msgdb (folder append-msgdb)
197 (let* ((number-alist (elmo-msgdb-get-number-alist append-msgdb))
198 (all-alist (copy-sequence (append
199 (elmo-msgdb-get-number-alist
200 (elmo-folder-msgdb folder))
203 overview to-be-deleted
206 (setq all-alist (delq (car cur) all-alist))
207 ;; same message id exists.
208 (if (setq same (rassoc (cdr (car cur)) all-alist))
209 (unless (= (/ (car (car cur))
210 (elmo-multi-folder-divide-number-internal folder))
212 (elmo-multi-folder-divide-number-internal folder)))
213 ;; base is also same...delete it!
215 (append to-be-deleted (list (car (car cur)))))))
216 (setq cur (cdr cur)))
217 (cond ((eq (elmo-folder-process-duplicates-internal folder)
220 (elmo-msgdb-append-to-killed-list folder to-be-deleted)
221 (setq overview (elmo-delete-if
223 (memq (elmo-msgdb-overview-entity-get-number
226 (elmo-msgdb-get-overview append-msgdb)))
227 ;; Should be mark as read.
228 (elmo-folder-mark-as-read folder to-be-deleted)
229 (elmo-msgdb-set-overview append-msgdb overview))
230 ((eq (elmo-folder-process-duplicates-internal folder)
232 ;; Mark as read duplicates.
233 (elmo-folder-mark-as-read folder to-be-deleted))
236 (setq to-be-deleted nil)))
237 (elmo-folder-set-msgdb-internal folder
239 (elmo-folder-msgdb folder)
241 (length to-be-deleted))
244 (luna-define-method elmo-folder-append-msgdb ((folder elmo-multi-folder)
246 (elmo-multi-folder-append-msgdb folder append-msgdb))
248 (defmacro elmo-multi-real-folder-number (folder number)
249 "Returns a cons cell of real FOLDER and NUMBER."
252 (elmo-multi-folder-divide-number-internal (, folder)))
253 1) (elmo-multi-folder-children-internal (, folder)))
254 (% (, number) (elmo-multi-folder-divide-number-internal
257 (defsubst elmo-multi-find-fetch-strategy (folder entity ignore-cache)
259 (let ((pair (elmo-multi-real-folder-number
261 (elmo-msgdb-overview-entity-get-number entity)))
262 (new-entity (elmo-msgdb-copy-overview-entity entity)))
264 (elmo-msgdb-overview-entity-set-number new-entity (cdr pair)))
265 (elmo-find-fetch-strategy (car pair) new-entity ignore-cache))
266 (elmo-make-fetch-strategy 'entire)))
268 (luna-define-method elmo-find-fetch-strategy
269 ((folder elmo-multi-folder)
270 entity &optional ignore-cache)
271 (elmo-multi-find-fetch-strategy folder entity ignore-cache))
273 (luna-define-method elmo-message-fetch ((folder elmo-multi-folder)
275 &optional section outbuf unseen)
276 (let ((pair (elmo-multi-real-folder-number folder number)))
277 (elmo-message-fetch (car pair) (cdr pair) strategy section outbuf unseen)))
279 (luna-define-method elmo-folder-delete-messages ((folder elmo-multi-folder)
281 (let ((flds (elmo-multi-folder-children-internal folder))
284 (setq one-list-list (elmo-multi-split-numbers folder numbers))
285 (while (< cur-number (length flds))
286 (elmo-folder-delete-messages (nth cur-number flds)
287 (nth cur-number one-list-list))
288 (setq cur-number (+ 1 cur-number)))
291 (luna-define-method elmo-folder-diff ((folder elmo-multi-folder)
293 (elmo-multi-folder-diff folder numbers))
295 (defun elmo-multi-folder-diff (folder numbers)
296 (let ((flds (elmo-multi-folder-children-internal folder))
297 (numbers (mapcar 'car
298 (elmo-msgdb-number-load
299 (elmo-folder-msgdb-path folder))))
300 (killed (elmo-msgdb-killed-list-load (elmo-folder-msgdb-path folder)))
306 ;; If first time, dummy numbers is used as current number list.
309 (divider (elmo-multi-folder-divide-number-internal folder)))
310 (dolist (folder flds)
313 (cons (* i divider) numbers)))))
315 (elmo-multi-split-numbers folder
318 (elmo-number-set-to-number-list killed)
321 (setq nums (elmo-folder-diff (car flds) (car num-list))
322 nums (cons (or (elmo-diff-unread nums)
323 (elmo-diff-new nums))
324 (elmo-diff-all nums)))
325 (setq diffs (nconc diffs (list nums)))
326 (setq count (+ 1 count))
327 (setq num-list (cdr num-list))
328 (setq flds (cdr flds)))
330 (and (car (car diffs))
331 (setq unsync (+ unsync (car (car diffs)))))
332 (setq messages (+ messages (cdr (car diffs))))
333 (setq diffs (cdr diffs)))
334 (elmo-folder-set-info-hashtb folder nil messages)
335 (cons unsync messages)))
337 (defun elmo-multi-split-number-alist (folder number-alist)
338 (let ((alist (sort (copy-sequence number-alist)
339 (lambda (pair1 pair2)
340 (< (car pair1)(car pair2)))))
344 (setq cur-number (+ cur-number 1))
348 (/ (- (setq num (car (car alist)))
349 (* elmo-multi-divide-number cur-number))
350 (elmo-multi-folder-divide-number-internal folder))))
351 (setq one-alist (nconc
355 (% num (* (elmo-multi-folder-divide-number-internal
357 (cdr (car alist))))))
358 (setq alist (cdr alist)))
359 (setq split (nconc split (list one-alist))))
362 (defun elmo-multi-split-mark-alist (folder mark-alist)
364 (alist (sort (copy-sequence mark-alist)
365 (lambda (pair1 pair2)
366 (< (car pair1)(car pair2)))))
369 (setq cur-number (+ cur-number 1))
373 (/ (- (car (car alist))
374 (* (elmo-multi-folder-divide-number-internal
376 (elmo-multi-folder-divide-number-internal folder))))
377 (setq one-alist (nconc
380 (list (% (car (car alist))
381 (* (elmo-multi-folder-divide-number-internal
383 (cadr (car alist))))))
384 (setq alist (cdr alist)))
385 (setq result (nconc result (list one-alist))))
388 (luna-define-method elmo-folder-list-unreads ((folder elmo-multi-folder))
391 (dolist (child (elmo-multi-folder-children-internal folder))
392 (setq cur-number (+ cur-number 1))
398 (elmo-multi-folder-divide-number-internal
400 (elmo-folder-list-unreads child)))))
403 (luna-define-method elmo-folder-list-answereds ((folder elmo-multi-folder))
406 (dolist (child (elmo-multi-folder-children-internal folder))
407 (setq cur-number (+ cur-number 1))
413 (elmo-multi-folder-divide-number-internal
415 (elmo-folder-list-answereds child)))))
418 (luna-define-method elmo-folder-list-importants ((folder elmo-multi-folder))
421 (dolist (child (elmo-multi-folder-children-internal folder))
422 (setq cur-number (+ cur-number 1))
428 (elmo-multi-folder-divide-number-internal
430 (elmo-folder-list-importants child)))))
433 (elmo-folder-list-messages-with-global-mark
434 folder elmo-msgdb-important-mark)))))
436 (luna-define-method elmo-folder-list-messages-internal
437 ((folder elmo-multi-folder) &optional nohide)
438 (let* ((flds (elmo-multi-folder-children-internal folder))
442 (setq cur-number (+ cur-number 1))
443 (setq list (elmo-folder-list-messages-internal (car flds)))
452 (* (elmo-multi-folder-divide-number-internal
453 folder) cur-number) x)))
459 (eq cur-number (/ num
460 (elmo-multi-folder-divide-number-internal
464 (elmo-msgdb-get-number-alist
465 (elmo-folder-msgdb folder)))))))
466 (setq flds (cdr flds)))
469 (luna-define-method elmo-folder-exists-p ((folder elmo-multi-folder))
470 (let ((flds (elmo-multi-folder-children-internal folder)))
473 (unless (elmo-folder-exists-p (car flds))
475 (setq flds (cdr flds)))
478 (luna-define-method elmo-folder-creatable-p ((folder elmo-multi-folder))
479 (let ((flds (elmo-multi-folder-children-internal folder)))
482 (when (and (elmo-folder-creatable-p (car flds))
483 (not (elmo-folder-exists-p (car flds))))
484 ;; If folder already exists, don't to `creatable'.
485 ;; Because this function is called, when folder doesn't exists.
486 (throw 'creatable t))
487 (setq flds (cdr flds)))
490 (luna-define-method elmo-folder-create ((folder elmo-multi-folder))
491 (let ((flds (elmo-multi-folder-children-internal folder)))
494 (unless (or (elmo-folder-exists-p (car flds))
495 (elmo-folder-create (car flds)))
497 (setq flds (cdr flds)))
500 (luna-define-method elmo-folder-search ((folder elmo-multi-folder)
501 condition &optional numlist)
502 (let* ((flds (elmo-multi-folder-children-internal folder))
504 numlist-list cur-numlist ; for filtered search.
508 (elmo-multi-split-numbers folder numlist t)))
510 (setq cur-number (+ cur-number 1))
512 (setq cur-numlist (car numlist-list))
513 (if (null cur-numlist)
514 ;; t means filter all.
515 (setq cur-numlist t)))
516 (setq ret-val (append
524 (* (elmo-multi-folder-divide-number-internal
525 folder) cur-number) x)))
527 (car flds) condition)))))
529 (setq numlist-list (cdr numlist-list)))
530 (setq flds (cdr flds)))
533 (luna-define-method elmo-message-use-cache-p ((folder elmo-multi-folder)
535 (let ((pair (elmo-multi-real-folder-number folder number)))
536 (elmo-message-use-cache-p (car pair) (cdr pair))))
538 (luna-define-method elmo-message-file-p ((folder elmo-multi-folder) number)
539 (let ((pair (elmo-multi-real-folder-number folder number)))
540 (elmo-message-file-p (car pair) (cdr pair))))
542 (luna-define-method elmo-message-file-name ((folder elmo-multi-folder) number)
543 (let ((pair (elmo-multi-real-folder-number folder number)))
544 (elmo-message-file-name (car pair) (cdr pair))))
546 (luna-define-method elmo-folder-plugged-p ((folder elmo-multi-folder))
547 (let ((flds (elmo-multi-folder-children-internal folder)))
550 (unless (elmo-folder-plugged-p (car flds))
551 (throw 'plugged nil))
552 (setq flds (cdr flds)))
555 (luna-define-method elmo-folder-set-plugged ((folder elmo-multi-folder)
557 (let ((flds (elmo-multi-folder-children-internal folder)))
559 (elmo-folder-set-plugged fld plugged add))))
561 (defun elmo-multi-folder-numbers-list-assoc (folder folder-numbers)
563 (while folder-numbers
564 (when (string= (elmo-folder-name-internal (car (car folder-numbers)))
565 (elmo-folder-name-internal folder))
566 (setq ent (car folder-numbers)
568 (setq folder-numbers (cdr folder-numbers)))
571 (defun elmo-multi-make-folder-numbers-list (folder msgs)
572 (let ((msg-list msgs)
576 (when (and (numberp (car msg-list))
577 (> (car msg-list) 0))
578 (setq pair (elmo-multi-real-folder-number folder (car msg-list)))
579 (if (setq fld-list (elmo-multi-folder-numbers-list-assoc
582 (setcdr fld-list (cons (cdr pair) (cdr fld-list)))
583 (setq ret-val (cons (cons (car pair) (list (cdr pair))) ret-val))))
584 (setq msg-list (cdr msg-list)))
587 (luna-define-method elmo-folder-mark-as-important :before ((folder
590 (dolist (folder-numbers (elmo-multi-make-folder-numbers-list folder numbers))
591 (elmo-folder-mark-as-important (car folder-numbers)
592 (cdr folder-numbers))))
594 (luna-define-method elmo-folder-unmark-important :before ((folder
597 (dolist (folder-numbers (elmo-multi-make-folder-numbers-list folder numbers))
598 (elmo-folder-unmark-important (car folder-numbers)
599 (cdr folder-numbers))))
601 (luna-define-method elmo-folder-mark-as-read :before ((folder
604 &optional ignore-flag)
605 (dolist (folder-numbers (elmo-multi-make-folder-numbers-list folder numbers))
606 (elmo-folder-mark-as-read (car folder-numbers)
610 (luna-define-method elmo-folder-unmark-read :before ((folder
613 &optional ignore-flag)
614 (dolist (folder-numbers (elmo-multi-make-folder-numbers-list folder numbers))
615 (elmo-folder-unmark-read (car folder-numbers)
619 (luna-define-method elmo-folder-mark-as-answered :before ((folder
622 (dolist (folder-numbers (elmo-multi-make-folder-numbers-list folder numbers))
623 (elmo-folder-mark-as-answered (car folder-numbers)
624 (cdr folder-numbers))))
626 (luna-define-method elmo-folder-unmark-answered :before ((folder
629 (dolist (folder-numbers (elmo-multi-make-folder-numbers-list folder numbers))
630 (elmo-folder-unmark-answered (car folder-numbers)
631 (cdr folder-numbers))))
634 (product-provide (provide 'elmo-multi) (require 'elmo-version))
636 ;;; elmo-multi.el ends here