* elmo.el (elmo-folder-list-flagged): New generic function.
[elisp/wanderlust.git] / elmo / elmo-pipe.el
1 ;;; elmo-pipe.el --- PIPE 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
34 (defvar elmo-pipe-folder-copied-filename "copied"
35   "Copied messages number set.")
36
37 ;;; ELMO pipe folder
38 (eval-and-compile
39   (luna-define-class elmo-pipe-folder (elmo-folder)
40                      (src dst copy))
41   (luna-define-internal-accessors 'elmo-pipe-folder))
42
43 (luna-define-method elmo-folder-initialize ((folder elmo-pipe-folder)
44                                             name)
45   (when (string-match "^\\([^|]*\\)|\\(:?\\)\\(.*\\)$" name)
46     (elmo-pipe-folder-set-src-internal folder
47                                        (elmo-make-folder
48                                         (elmo-match-string 1 name)))
49     (elmo-pipe-folder-set-dst-internal folder
50                                        (elmo-make-folder
51                                         (elmo-match-string 3 name)))
52     (elmo-pipe-folder-set-copy-internal folder
53                                         (string= ":"
54                                                  (elmo-match-string 2 name))))
55   folder)
56
57 (luna-define-method elmo-folder-get-primitive-list ((folder elmo-pipe-folder))
58   (elmo-flatten
59    (mapcar
60     'elmo-folder-get-primitive-list
61     (list (elmo-pipe-folder-src-internal folder)
62           (elmo-pipe-folder-dst-internal folder)))))
63
64 (luna-define-method elmo-folder-contains-type ((folder elmo-pipe-folder)
65                                                type)
66   (or (elmo-folder-contains-type (elmo-pipe-folder-src-internal folder) type)
67       (elmo-folder-contains-type (elmo-pipe-folder-dst-internal folder) type)))
68
69 (luna-define-method elmo-folder-msgdb-create ((folder elmo-pipe-folder)
70                                               numlist seen-list)
71   (elmo-folder-msgdb-create (elmo-pipe-folder-dst-internal folder)
72                             numlist seen-list))
73
74 (luna-define-method elmo-folder-append-messages ((folder elmo-pipe-folder)
75                                                  src-folder numbers
76                                                  &optional same-number)
77   (elmo-folder-append-messages (elmo-pipe-folder-dst-internal folder)
78                                src-folder numbers
79                                same-number))
80
81 (luna-define-method elmo-folder-append-buffer ((folder elmo-pipe-folder)
82                                                unread &optional number)
83   (elmo-folder-append-buffer (elmo-pipe-folder-dst-internal folder)
84                              unread number))
85
86 (luna-define-method elmo-message-fetch ((folder elmo-pipe-folder)
87                                         number strategy
88                                         &optional section outbuf unseen)
89   (elmo-message-fetch (elmo-pipe-folder-dst-internal folder)
90                       number strategy section outbuf unseen))
91
92 (luna-define-method elmo-folder-delete-messages ((folder elmo-pipe-folder)
93                                                  numbers)
94   (elmo-folder-delete-messages (elmo-pipe-folder-dst-internal folder)
95                                numbers))
96
97 (defvar elmo-pipe-drained-hook nil "A hook called when the pipe is flushed.")
98
99 (defsubst elmo-pipe-folder-list-target-messages (src &optional ignore-list)
100   (unwind-protect
101       (progn
102         (elmo-folder-set-killed-list-internal src ignore-list)
103         (elmo-folder-list-messages src))
104     (elmo-folder-set-killed-list-internal src nil)))
105
106 (defun elmo-pipe-drain (src dst &optional copy ignore-list)
107   "Move or copy all messages of SRC to DST."
108   (let ((elmo-inhibit-number-mapping (not copy)) ; No need to use UIDL
109         msgs len)
110     (message "Checking %s..." (elmo-folder-name-internal src))
111     ;; Warnnig: some function requires msgdb
112     ;;  but elmo-folder-open-internal do not load msgdb.
113     (elmo-folder-open-internal src)
114     (setq msgs (elmo-pipe-folder-list-target-messages src ignore-list)
115           len (length msgs))
116     (when (> len elmo-display-progress-threshold)
117       (elmo-progress-set 'elmo-folder-move-messages
118                          len
119                          (if copy
120                              "Copying messages..."
121                            "Moving messages...")))
122     (unwind-protect
123         (elmo-folder-move-messages src msgs dst
124                                    nil nil copy)
125       (elmo-progress-clear 'elmo-folder-move-messages))
126     (when (and copy msgs)
127       (setq ignore-list (elmo-number-set-append-list ignore-list
128                                                      msgs)))
129     ;; Don't save msgdb here.
130     ;; Because summary view of original folder is not updated yet.
131     (elmo-folder-close-internal src)
132     (run-hooks 'elmo-pipe-drained-hook)
133     ignore-list))
134
135 (defun elmo-pipe-folder-copied-list-load (folder)
136   (elmo-object-load
137    (expand-file-name elmo-pipe-folder-copied-filename
138                      (expand-file-name
139                       (elmo-replace-string-as-filename
140                        (elmo-folder-name-internal folder))
141                       (expand-file-name "pipe" elmo-msgdb-directory)))
142    nil t))
143
144 (defun elmo-pipe-folder-copied-list-save (folder copied-list)
145   (elmo-object-save
146    (expand-file-name elmo-pipe-folder-copied-filename
147                      (expand-file-name
148                       (elmo-replace-string-as-filename
149                        (elmo-folder-name-internal folder))
150                       (expand-file-name "pipe" elmo-msgdb-directory)))
151    copied-list))
152
153 (luna-define-method elmo-folder-open-internal ((folder elmo-pipe-folder))
154   (elmo-folder-open-internal (elmo-pipe-folder-dst-internal folder))
155   (let ((src-folder (elmo-pipe-folder-src-internal folder))
156         (dst-folder (elmo-pipe-folder-dst-internal folder)))
157     (when (and (elmo-folder-plugged-p src-folder)
158                (elmo-folder-plugged-p dst-folder))
159       (if (elmo-pipe-folder-copy-internal folder)
160           (elmo-pipe-folder-copied-list-save
161            folder
162            (elmo-pipe-drain src-folder
163                             dst-folder
164                             'copy
165                             (elmo-pipe-folder-copied-list-load folder)))
166         (elmo-pipe-drain src-folder dst-folder)))))
167
168 (luna-define-method elmo-folder-close-internal ((folder elmo-pipe-folder))
169   (elmo-folder-close-internal(elmo-pipe-folder-dst-internal folder)))
170
171 (luna-define-method elmo-folder-list-messages-internal
172   ((folder elmo-pipe-folder) &optional nohide)
173   (elmo-folder-list-messages-internal (elmo-pipe-folder-dst-internal
174                                        folder) nohide))
175
176 (luna-define-method elmo-folder-list-unreads ((folder elmo-pipe-folder))
177   (elmo-folder-list-unreads (elmo-pipe-folder-dst-internal folder)))
178
179 (luna-define-method elmo-folder-list-importants ((folder elmo-pipe-folder))
180   (elmo-folder-list-importants (elmo-pipe-folder-dst-internal folder)))
181
182 (luna-define-method elmo-folder-list-answereds ((folder elmo-pipe-folder))
183   (elmo-folder-list-answereds (elmo-pipe-folder-dst-internal folder)))
184
185 (luna-define-method elmo-folder-status ((folder elmo-pipe-folder))
186   (elmo-folder-open-internal (elmo-pipe-folder-src-internal folder))
187   (elmo-folder-open-internal (elmo-pipe-folder-dst-internal folder))
188   (let* ((elmo-inhibit-number-mapping
189           (not (elmo-pipe-folder-copy-internal folder)))
190          (src-length (length (elmo-pipe-folder-list-target-messages
191                               (elmo-pipe-folder-src-internal folder)
192                               (elmo-pipe-folder-copied-list-load folder))))
193          (dst-list (elmo-folder-list-messages
194                     (elmo-pipe-folder-dst-internal folder))))
195     (prog1 (cons (+ src-length (elmo-max-of-list dst-list))
196                  (+ src-length (length dst-list)))
197       ;; No save.
198       (elmo-folder-close-internal (elmo-pipe-folder-src-internal folder))
199       (elmo-folder-close-internal (elmo-pipe-folder-dst-internal folder)))))
200
201 (luna-define-method elmo-folder-exists-p ((folder elmo-pipe-folder))
202   (and (elmo-folder-exists-p (elmo-pipe-folder-src-internal folder))
203        (elmo-folder-exists-p (elmo-pipe-folder-dst-internal folder))))
204
205 (luna-define-method elmo-folder-expand-msgdb-path ((folder
206                                                     elmo-pipe-folder))
207   ;; Share with destination...OK?
208   (elmo-folder-expand-msgdb-path (elmo-pipe-folder-dst-internal folder)))
209
210 (luna-define-method elmo-folder-newsgroups ((folder elmo-pipe-folder))
211   (elmo-folder-newsgroups (elmo-pipe-folder-src-internal folder)))
212
213 (luna-define-method elmo-folder-creatable-p ((folder elmo-pipe-folder))
214   (and (elmo-folder-creatable-p (elmo-pipe-folder-src-internal folder))
215        (elmo-folder-creatable-p (elmo-pipe-folder-dst-internal folder))))
216
217 (luna-define-method elmo-folder-writable-p ((folder elmo-pipe-folder))
218   (elmo-folder-writable-p (elmo-pipe-folder-dst-internal folder)))
219
220 (luna-define-method elmo-folder-create ((folder elmo-pipe-folder))
221   (if (and (not (elmo-folder-exists-p (elmo-pipe-folder-src-internal folder)))
222            (elmo-folder-creatable-p (elmo-pipe-folder-src-internal folder)))
223       (elmo-folder-create (elmo-pipe-folder-src-internal folder)))
224   (if (and (not (elmo-folder-exists-p (elmo-pipe-folder-dst-internal folder)))
225            (elmo-folder-creatable-p (elmo-pipe-folder-dst-internal folder)))
226       (elmo-folder-create (elmo-pipe-folder-dst-internal folder))))
227
228 (luna-define-method elmo-folder-search ((folder elmo-pipe-folder)
229                                         condition &optional numlist)
230   (elmo-folder-search (elmo-pipe-folder-dst-internal folder)
231                       condition numlist))
232
233 (luna-define-method elmo-message-use-cache-p ((folder elmo-pipe-folder) number)
234   (elmo-message-use-cache-p (elmo-pipe-folder-dst-internal folder) number))
235
236 (luna-define-method elmo-folder-check ((folder elmo-pipe-folder))
237   (elmo-folder-close-internal folder)
238   (elmo-folder-open-internal folder))
239
240 (luna-define-method elmo-folder-plugged-p ((folder elmo-pipe-folder))
241   (and (elmo-folder-plugged-p (elmo-pipe-folder-src-internal folder))
242        (elmo-folder-plugged-p (elmo-pipe-folder-dst-internal folder))))
243
244 (luna-define-method elmo-folder-message-file-p ((folder elmo-pipe-folder))
245   (elmo-folder-message-file-p (elmo-pipe-folder-dst-internal folder)))
246
247 (luna-define-method elmo-message-file-p ((folder elmo-pipe-folder) number)
248   (elmo-message-file-p (elmo-pipe-folder-dst-internal folder) number))
249
250 (luna-define-method elmo-message-file-name ((folder elmo-pipe-folder) number)
251   (elmo-message-file-name (elmo-pipe-folder-dst-internal folder) number))
252
253 (luna-define-method elmo-folder-message-file-number-p ((folder
254                                                         elmo-pipe-folder))
255   (elmo-folder-message-file-number-p (elmo-pipe-folder-dst-internal folder)))
256
257 (luna-define-method elmo-folder-message-file-directory ((folder
258                                                          elmo-pipe-folder))
259   (elmo-folder-message-file-directory
260    (elmo-pipe-folder-dst-internal folder)))
261
262 (luna-define-method elmo-folder-message-make-temp-file-p
263   ((folder elmo-pipe-folder))
264   (elmo-folder-message-make-temp-file-p
265    (elmo-pipe-folder-dst-internal folder)))
266
267 (luna-define-method elmo-folder-message-make-temp-files ((folder
268                                                           elmo-pipe-folder)
269                                                          numbers
270                                                          &optional
271                                                          start-number)
272   (elmo-folder-message-make-temp-files
273    (elmo-pipe-folder-dst-internal folder) numbers start-number))
274
275 (luna-define-method elmo-folder-mark-as-read ((folder elmo-pipe-folder)
276                                               numbers &optional ignore-flag)
277   (elmo-folder-mark-as-read (elmo-pipe-folder-dst-internal folder)
278                             numbers ignore-flag))
279
280 (luna-define-method elmo-folder-unmark-read ((folder elmo-pipe-folder)
281                                              numbers
282                                              &optional ignore-flag)
283   (elmo-folder-unmark-read (elmo-pipe-folder-dst-internal folder)
284                            numbers ignore-flag))
285
286 (luna-define-method elmo-folder-unmark-important ((folder elmo-pipe-folder)
287                                                   numbers)
288   (elmo-folder-unmark-important (elmo-pipe-folder-dst-internal folder)
289                                 numbers))
290
291 (luna-define-method elmo-folder-mark-as-important ((folder elmo-pipe-folder)
292                                                    numbers)
293   (elmo-folder-mark-as-important (elmo-pipe-folder-dst-internal folder)
294                                  numbers))
295
296 (luna-define-method elmo-folder-pack-numbers ((folder elmo-pipe-folder))
297   (elmo-folder-pack-numbers (elmo-pipe-folder-dst-internal folder)))
298
299 (require 'product)
300 (product-provide (provide 'elmo-pipe) (require 'elmo-version))
301
302 ;;; elmo-pipe.el ends here