* wl-util.el (wl-current-message-buffer): Follow the API change.
[elisp/wanderlust.git] / elmo / elmo-multi.el
1 ;;; elmo-multi.el --- Multiple Folder Interface for ELMO.
2
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
7
8 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
9
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)
13 ;; any later version.
14 ;;
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.
19 ;;
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.
24 ;;
25
26 ;;; Commentary:
27 ;;
28
29 ;;; Code:
30 ;;
31
32 (require 'elmo)
33 (require 'luna)
34
35 (defvar elmo-multi-divide-number 100000
36   "*Multi divider number.")
37
38 ;;; ELMO Multi folder
39 (eval-and-compile
40   (luna-define-class elmo-multi-folder (elmo-folder)
41                      (children divide-number))
42   (luna-define-internal-accessors 'elmo-multi-folder))
43
44 (defmacro elmo-multi-real-folder-number (folder number)
45   "Returns a cons cell of real FOLDER and NUMBER."
46   (` (cons (nth (-
47                  (/ (, number)
48                     (elmo-multi-folder-divide-number-internal (, folder)))
49                  1) (elmo-multi-folder-children-internal (, folder)))
50            (% (, number) (elmo-multi-folder-divide-number-internal
51                           (, folder))))))
52
53 (luna-define-method elmo-folder-initialize ((folder
54                                              elmo-multi-folder)
55                                             name)
56   (while (> (length (car (setq name (elmo-parse-token name ",")))) 0)
57     (elmo-multi-folder-set-children-internal
58      folder
59      (nconc (elmo-multi-folder-children-internal
60              folder)
61             (list (elmo-make-folder (car name)))))
62     (setq name (cdr name))
63     (when (and (> (length name) 0)
64                (eq (aref name 0) ?,))
65       (setq name (substring name 1))))
66   (elmo-multi-folder-set-divide-number-internal
67    folder
68    elmo-multi-divide-number)
69   folder)
70
71 (luna-define-method elmo-folder-open-internal ((folder elmo-multi-folder))
72   (dolist (fld (elmo-multi-folder-children-internal folder))
73     (elmo-folder-open-internal fld)))
74
75 (luna-define-method elmo-folder-check ((folder elmo-multi-folder))
76   (dolist (fld (elmo-multi-folder-children-internal folder))
77     (elmo-folder-check fld)))
78
79 (luna-define-method elmo-folder-close-internal ((folder elmo-multi-folder))
80   (dolist (fld (elmo-multi-folder-children-internal folder))
81     (elmo-folder-close-internal fld)))
82
83 (luna-define-method elmo-folder-close :after ((folder elmo-multi-folder))
84   (dolist (fld (elmo-multi-folder-children-internal folder))
85     (elmo-folder-set-msgdb-internal fld nil)))
86
87 (luna-define-method elmo-folder-synchronize ((folder elmo-multi-folder)
88                                              &optional
89                                              disable-killed
90                                              ignore-msgdb
91                                              no-check
92                                              mask)
93   (if mask
94       (dolist (element (elmo-multi-split-numbers folder mask))
95         (when (cdr element)
96           (elmo-folder-synchronize (car element)
97                                    disable-killed
98                                    ignore-msgdb
99                                    no-check
100                                    (cdr element))))
101     (dolist (fld (elmo-multi-folder-children-internal folder))
102       (elmo-folder-synchronize fld disable-killed ignore-msgdb no-check)))
103   0)
104
105 (luna-define-method elmo-folder-expand-msgdb-path ((folder
106                                                     elmo-multi-folder))
107   (expand-file-name (elmo-replace-string-as-filename
108                      (elmo-folder-name-internal folder))
109                     (expand-file-name "multi"
110                                       elmo-msgdb-directory)))
111
112 (luna-define-method elmo-folder-newsgroups ((folder elmo-multi-folder))
113   (apply  #'nconc
114           (mapcar
115            'elmo-folder-newsgroups
116            (elmo-multi-folder-children-internal folder))))
117
118 (luna-define-method elmo-folder-get-primitive-list ((folder elmo-multi-folder))
119   (elmo-flatten
120    (mapcar
121     'elmo-folder-get-primitive-list
122     (elmo-multi-folder-children-internal folder))))
123
124 (luna-define-method elmo-folder-contains-type ((folder elmo-multi-folder) type)
125   (let ((children (elmo-multi-folder-children-internal folder))
126         match)
127     (while children
128       (when (elmo-folder-contains-type (car children) type)
129         (setq match t)
130         (setq children nil))
131       (setq children (cdr children)))
132     match))
133
134 (luna-define-method elmo-message-folder ((folder elmo-multi-folder)
135                                          number)
136   (nth (- (/ number (elmo-multi-folder-divide-number-internal folder)) 1)
137        (elmo-multi-folder-children-internal folder)))
138
139 (luna-define-method elmo-message-cached-p ((folder elmo-multi-folder) number)
140   (let ((pair (elmo-multi-real-folder-number folder number)))
141     (elmo-message-cached-p (car pair) (cdr pair))))
142
143 (luna-define-method elmo-message-set-cached ((folder elmo-multi-folder)
144                                              number cached)
145   (let ((pair (elmo-multi-real-folder-number folder number)))
146     (elmo-message-set-cached (car pair) (cdr pair) cached)))
147
148 (luna-define-method elmo-find-fetch-strategy ((folder elmo-multi-folder)
149                                               number
150                                               &optional
151                                               ignore-cache
152                                               require-entireness)
153   (let ((pair (elmo-multi-real-folder-number folder number)))
154     (elmo-find-fetch-strategy (car pair)
155                               (cdr pair)
156                               ignore-cache
157                               require-entireness)))
158
159 (luna-define-method elmo-message-number ((folder elmo-multi-folder)
160                                          message-id)
161   (let ((children (elmo-multi-folder-children-internal folder))
162         match)
163     (while children
164       (when (setq match (elmo-message-number (car children) message-id))
165         (setq children nil))
166       (setq children (cdr children)))
167     match))
168
169 (luna-define-method elmo-message-entity ((folder elmo-multi-folder) key)
170   (cond
171    ((numberp key)
172     (let* ((pair (elmo-multi-real-folder-number folder key))
173            (entity (elmo-message-entity (car pair) (cdr pair))))
174       (when entity
175         (elmo-message-entity-set-number (elmo-message-copy-entity entity)
176                                         key))))
177    ((stringp key)
178     (let ((children (elmo-multi-folder-children-internal folder))
179           (cur-number 0)
180           match)
181       (while children
182         (setq cur-number (+ cur-number 1))
183         (when (setq match (elmo-message-entity (car children) key))
184           (setq match (elmo-message-copy-entity match))
185           (elmo-message-entity-set-number
186            match
187            (+ (* (elmo-multi-folder-divide-number-internal folder)
188                  cur-number)
189               (elmo-message-entity-number match)))
190           (setq children nil))
191         (setq children (cdr children)))
192       match))))
193
194 (luna-define-method elmo-message-entity-parent ((folder
195                                                  elmo-multi-folder) entity)
196   (elmo-message-entity
197    folder
198    (elmo-message-entity-field entity 'references)))
199
200 (luna-define-method elmo-message-field ((folder elmo-multi-folder)
201                                         number field)
202   (let ((pair (elmo-multi-real-folder-number folder number)))
203     (elmo-message-field (car pair) (cdr pair) field)))
204
205 (luna-define-method elmo-message-flag-available-p ((folder
206                                                     elmo-multi-folder) number
207                                                     flag)
208   (let ((pair (elmo-multi-real-folder-number folder number)))
209     (elmo-message-flag-available-p (car pair) (cdr pair) flag)))
210
211 (luna-define-method elmo-message-flags ((folder elmo-multi-folder) number)
212   (let ((pair (elmo-multi-real-folder-number folder number)))
213     (elmo-message-flags (car pair) (cdr pair))))
214
215 (defun elmo-multi-split-numbers (folder numlist &optional as-is)
216   (let ((numbers (sort numlist '<))
217         (folders (elmo-multi-folder-children-internal folder))
218         (divider (elmo-multi-folder-divide-number-internal folder))
219         (cur-number 0)
220         one-list numbers-list)
221     (while numbers
222       (setq one-list (list (nth cur-number folders)))
223       (setq cur-number (+ cur-number 1))
224       (while (and numbers
225                   (eq 0
226                       (/ (- (car numbers)
227                             (* divider cur-number))
228                          divider)))
229         (setq one-list (nconc
230                         one-list
231                         (list
232                          (if as-is
233                              (car numbers)
234                            (% (car numbers)
235                               (* divider cur-number))))))
236         (setq numbers (cdr numbers)))
237       (setq numbers-list (nconc numbers-list (list one-list))))
238     numbers-list))
239
240 (luna-define-method elmo-folder-process-crosspost ((folder elmo-multi-folder))
241   (dolist (child (elmo-multi-folder-children-internal folder))
242     (elmo-folder-process-crosspost child)))
243
244 (luna-define-method elmo-message-fetch ((folder elmo-multi-folder)
245                                         number strategy
246                                         &optional section outbuf unseen)
247   (let ((pair (elmo-multi-real-folder-number folder number)))
248     (when (elmo-message-fetch (car pair) (cdr pair)
249                               strategy section outbuf unseen)
250       (unless unseen
251         (elmo-folder-notify-event folder 'flag-changed (list number)))
252       t)))
253
254 (luna-define-method elmo-folder-delete-messages ((folder elmo-multi-folder)
255                                                  numbers)
256   (dolist (element (elmo-multi-split-numbers folder numbers))
257     (when (cdr element)
258       (elmo-folder-delete-messages (car element) (cdr element))))
259   t)
260
261 (luna-define-method elmo-folder-detach-messages ((folder elmo-multi-folder)
262                                                  numbers)
263   (dolist (element (elmo-multi-split-numbers folder numbers))
264     (when (cdr element)
265       (elmo-folder-detach-messages (car element) (cdr element))))
266   t)
267
268 (luna-define-method elmo-folder-diff ((folder elmo-multi-folder))
269   (elmo-multi-folder-diff folder))
270
271 (defun elmo-multi-folder-diff (folder)
272   (let ((flds (elmo-multi-folder-children-internal folder))
273         (news 0)
274         (unreads 0)
275         (alls 0)
276         no-unreads diff)
277     (while flds
278       (setq diff (elmo-folder-diff (car flds)))
279       (cond
280        ((consp (cdr diff)) ; (new unread all)
281         (setq news    (+ news (nth 0 diff))
282               unreads (+ unreads (nth 1 diff))
283               alls    (+ alls (nth 2 diff))))
284        (t
285         (setq no-unreads t)
286         (setq news    (+ news (car diff))
287               alls    (+ alls (cdr diff)))))
288       (setq flds (cdr flds)))
289     (if no-unreads
290         (cons news alls)
291       (list news unreads alls))))
292
293 (luna-define-method elmo-folder-list-messages
294   ((folder elmo-multi-folder) &optional visible-only in-msgdb)
295   (let* ((flds (elmo-multi-folder-children-internal folder))
296          (cur-number 0)
297          list numbers)
298     (while flds
299       (setq cur-number (+ cur-number 1))
300       (setq list (elmo-folder-list-messages (car flds) visible-only in-msgdb))
301       (setq numbers
302             (nconc
303              numbers
304              (mapcar
305               (function
306                (lambda (x)
307                  (+
308                   (* (elmo-multi-folder-divide-number-internal
309                       folder) cur-number) x)))
310               list)))
311       (setq flds (cdr flds)))
312     numbers))
313
314 (luna-define-method elmo-folder-exists-p ((folder elmo-multi-folder))
315   (let ((flds (elmo-multi-folder-children-internal folder)))
316     (catch 'exists
317       (while flds
318         (unless (elmo-folder-exists-p (car flds))
319           (throw 'exists nil))
320         (setq flds (cdr flds)))
321       t)))
322
323 (luna-define-method elmo-folder-creatable-p ((folder elmo-multi-folder))
324   (let ((flds (elmo-multi-folder-children-internal folder)))
325     (catch 'creatable
326       (while flds
327         (when (and (elmo-folder-creatable-p (car flds))
328                    (not (elmo-folder-exists-p (car flds))))
329           ;; If folder already exists, don't to `creatable'.
330           ;; Because this function is called, when folder doesn't exists.
331           (throw 'creatable t))
332         (setq flds (cdr flds)))
333       nil)))
334
335 (luna-define-method elmo-folder-create ((folder elmo-multi-folder))
336   (let ((flds (elmo-multi-folder-children-internal folder)))
337     (catch 'create
338       (while flds
339         (unless (or (elmo-folder-exists-p (car flds))
340                     (elmo-folder-create (car flds)))
341           (throw 'create nil))
342         (setq flds (cdr flds)))
343       t)))
344
345 (luna-define-method elmo-folder-search ((folder elmo-multi-folder)
346                                         condition &optional numbers)
347   (let* ((flds (elmo-multi-folder-children-internal folder))
348          (cur-number 0)
349          numlist
350          matches)
351     (setq numbers (or numbers
352                       (elmo-folder-list-messages folder)))
353     (while flds
354       (setq cur-number (+ cur-number 1))
355       (setq matches (append matches
356                             (mapcar
357                              (function
358                               (lambda (x)
359                                 (+
360                                  (* (elmo-multi-folder-divide-number-internal
361                                      folder)
362                                     cur-number)
363                                  x)))
364                              (elmo-folder-search
365                               (car flds) condition))))
366       (setq flds (cdr flds)))
367     (elmo-list-filter numbers matches)))
368
369 (luna-define-method elmo-message-use-cache-p ((folder elmo-multi-folder)
370                                               number)
371   (let ((pair (elmo-multi-real-folder-number folder number)))
372     (elmo-message-use-cache-p (car pair) (cdr pair))))
373
374 (luna-define-method elmo-message-file-p ((folder elmo-multi-folder) number)
375   (let ((pair (elmo-multi-real-folder-number folder number)))
376     (elmo-message-file-p (car pair) (cdr pair))))
377
378 (luna-define-method elmo-message-file-name ((folder elmo-multi-folder) number)
379   (let ((pair (elmo-multi-real-folder-number folder number)))
380     (elmo-message-file-name (car pair) (cdr pair))))
381
382 (luna-define-method elmo-folder-plugged-p ((folder elmo-multi-folder))
383   (let ((flds (elmo-multi-folder-children-internal folder)))
384     (catch 'plugged
385       (while flds
386         (unless (elmo-folder-plugged-p (car flds))
387           (throw 'plugged nil))
388         (setq flds (cdr flds)))
389       t)))
390
391 (luna-define-method elmo-folder-set-plugged ((folder elmo-multi-folder)
392                                              plugged add)
393   (let ((flds  (elmo-multi-folder-children-internal folder)))
394     (dolist (fld flds)
395       (elmo-folder-set-plugged fld plugged add))))
396
397 (defun elmo-multi-folder-numbers-list-assoc (folder folder-numbers)
398   (let (ent)
399     (while folder-numbers
400       (when (string= (elmo-folder-name-internal (car (car folder-numbers)))
401                      (elmo-folder-name-internal folder))
402         (setq ent (car folder-numbers)
403               folder-numbers nil))
404       (setq folder-numbers (cdr folder-numbers)))
405     ent))
406
407 (defun elmo-multi-make-folder-numbers-list (folder msgs)
408   (let ((msg-list msgs)
409         pair fld-list
410         ret-val)
411     (while msg-list
412       (when (and (numberp (car msg-list))
413                  (> (car msg-list) 0))
414         (setq pair (elmo-multi-real-folder-number folder (car msg-list)))
415         (if (setq fld-list (elmo-multi-folder-numbers-list-assoc
416                             (car pair)
417                             ret-val))
418             (setcdr fld-list (cons (cdr pair) (cdr fld-list)))
419           (setq ret-val (cons (cons (car pair) (list (cdr pair))) ret-val))))
420       (setq msg-list (cdr msg-list)))
421     ret-val))
422
423 (luna-define-method elmo-folder-set-flag ((folder elmo-multi-folder)
424                                           numbers
425                                           flag
426                                           &optional is-local)
427   (dolist (pair (elmo-multi-make-folder-numbers-list folder numbers))
428     (elmo-folder-set-flag (car pair) (cdr pair) flag is-local))
429   (elmo-folder-notify-event folder 'flag-changed numbers))
430
431 (luna-define-method elmo-folder-unset-flag ((folder elmo-multi-folder)
432                                             numbers
433                                             flag
434                                             &optional is-local)
435   (dolist (pair (elmo-multi-make-folder-numbers-list folder numbers))
436     (ignore-errors
437      (elmo-folder-unset-flag (car pair) (cdr pair) flag is-local)))
438   (elmo-folder-notify-event folder 'flag-changed numbers))
439
440 (luna-define-method elmo-folder-list-flagged ((folder elmo-multi-folder)
441                                               flag
442                                               &optional in-msgdb)
443   (let ((cur-number 0)
444         numbers)
445     (dolist (child (elmo-multi-folder-children-internal folder))
446       (setq cur-number (+ cur-number 1)
447             numbers
448             (nconc
449              numbers
450              (mapcar
451               (function
452                (lambda (x)
453                  (+
454                   (* (elmo-multi-folder-divide-number-internal folder)
455                      cur-number) x)))
456               (elmo-folder-list-flagged child flag in-msgdb)))))
457     numbers))
458
459 (luna-define-method elmo-folder-commit ((folder elmo-multi-folder))
460   (dolist (child (elmo-multi-folder-children-internal folder))
461     (elmo-folder-commit child)))
462
463 (luna-define-method elmo-folder-length ((folder elmo-multi-folder))
464   (let ((sum 0))
465     (dolist (child (elmo-multi-folder-children-internal folder))
466       (setq sum (+ sum (elmo-folder-length child))))
467     sum))
468
469 (luna-define-method elmo-folder-count-flags ((folder elmo-multi-folder))
470   (let (flag-alist element)
471     (dolist (child (elmo-multi-folder-children-internal folder))
472       (dolist (pair (elmo-folder-count-flags child))
473         (if (setq element (assq (car pair) flag-alist))
474             (setcdr element (+ (cdr element) (cdr pair)))
475           (setq flag-alist (cons pair flag-alist)))))
476     flag-alist))
477
478 (require 'product)
479 (product-provide (provide 'elmo-multi) (require 'elmo-version))
480
481 ;;; elmo-multi.el ends here