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