1 ;;; elmo-dop.el --- Modules for Disconnected Operations on 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.
31 (eval-when-compile (require 'cl
))
37 (require 'elmo-localdir
)
40 (defvar elmo-dop-queue nil
41 "A list of (folder-name function-to-be-called argument-list).
42 Automatically loaded/saved.")
44 (defmacro elmo-make-dop-queue
(fname method arguments
)
46 `(vector ,fname
,method
,arguments
))
48 (defmacro elmo-dop-queue-fname
(queue)
49 "Return the folder name string of the QUEUE."
52 (defmacro elmo-dop-queue-method
(queue)
53 "Return the method symbol of the QUEUE."
56 (defmacro elmo-dop-queue-arguments
(queue)
57 "Return the arguments of the QUEUE."
60 (defun elmo-dop-queue-append (folder method arguments
)
61 "Append to disconnected operation queue."
62 (let ((queue (elmo-make-dop-queue (elmo-folder-name-internal folder
)
64 (setq elmo-dop-queue
(nconc elmo-dop-queue
(list queue
)))))
66 (defvar elmo-dop-queue-merge-method-list
67 '(elmo-folder-set-read-delayed
68 elmo-folder-unset-read-delayed
69 elmo-folder-set-important-delayed
70 elmo-folder-unset-important-delayed
71 elmo-folder-set-answered-delayed
72 elmo-folder-unset-answered-delayed
))
74 (defvar elmo-dop-queue-method-name-alist
75 '((elmo-folder-append-buffer-dop-delayed .
"Append")
76 (elmo-folder-delete-messages-dop-delayed .
"Delete")
77 (elmo-message-encache .
"Encache")
78 (elmo-folder-create-dop-delayed .
"Create")
79 (elmo-folder-set-read-delayed .
"Read")
80 (elmo-folder-unset-read-delayed .
"Unread")
81 (elmo-folder-set-answered-delayed .
"Answered")
82 (elmo-folder-unset-answered-delayed .
"Unanswered")
83 (elmo-folder-set-important-delayed .
"Important")
84 (elmo-folder-unset-important-delayed .
"Unimportant")
85 (elmo-folder-set-flag .
"Set flag")
86 (elmo-folder-unset-flag .
"Unset flag")))
88 (defmacro elmo-dop-queue-method-name
(queue)
89 `(cdr (assq (elmo-dop-queue-method ,queue
)
90 elmo-dop-queue-method-name-alist
)))
92 (defun elmo-dop-queue-flush ()
93 "Flush disconnected operations that consern plugged folders."
95 (unless (or (null elmo-dop-queue
)
96 (vectorp (car elmo-dop-queue
)))
98 Saved queue is old version(2.6). Clear all pending operations? ")
100 (setq elmo-dop-queue nil
)
101 (message "All pending operations are cleared.")
102 (elmo-dop-queue-save))
103 (error "Please use 2.6 or earlier")))
104 (elmo-dop-queue-merge)
105 (let ((queue-all elmo-dop-queue
)
110 (if (elmo-folder-plugged-p
111 (elmo-get-folder (elmo-dop-queue-fname (car queue-all
))))
112 (setq queue
(append queue
(list (car queue-all
)))))
113 (setq queue-all
(cdr queue-all
)))
114 (setq count
(length queue
))
117 (format "%d pending operation(s) exists. Perform now? " count
)
118 (not elmo-dop-flush-confirm
) t
)
122 (let ((queue elmo-dop-queue
)
125 (num (length elmo-dop-queue
))
128 ;; now perform pending processes.
131 (message "Flushing queue....%d/%d." i num
)
134 (apply (elmo-dop-queue-method (car queue
))
138 (elmo-dop-queue-fname (car queue
))))
139 (elmo-folder-open folder
)
140 (unless (elmo-folder-plugged-p folder
)
141 (error "Unplugged")))
142 (elmo-dop-queue-arguments (car queue
)))
143 (elmo-folder-close folder
))
144 (quit (setq failure t
))
145 (error (setq failure err
)))
148 (setq elmo-dop-queue
(delq (car queue
) elmo-dop-queue
))
149 (setq performed
(+ 1 performed
)))
150 (setq queue
(cdr queue
)))
151 (message "%d/%d operation(s) are performed successfully."
154 (elmo-dop-queue-save)))
155 ;; when answer=NO against performing dop
156 (if (elmo-y-or-n-p "Clear these pending operations? "
157 (not elmo-dop-flush-confirm
) t
)
160 (when (eq (elmo-dop-queue-method (car queue
))
161 'elmo-folder-append-buffer-dop-delayed
)
162 (elmo-folder-delete-messages
163 (elmo-dop-spool-folder
164 (elmo-get-folder (elmo-dop-queue-fname (car queue
))))
165 (list (nth 1 (elmo-dop-queue-arguments (car queue
))))))
166 (setq elmo-dop-queue
(delq (car queue
) elmo-dop-queue
))
167 (setq queue
(cdr queue
)))
168 (message "Pending operations are cleared.")
169 (elmo-dop-queue-save))
173 (defun elmo-dop-queue-merge ()
174 (let ((queue elmo-dop-queue
)
175 new-queue match-queue que
)
176 (while (setq que
(car queue
))
178 (memq (elmo-dop-queue-method que
)
179 elmo-dop-queue-merge-method-list
)
186 (string= (elmo-dop-queue-fname que
)
187 (elmo-dop-queue-fname nqueue
))
188 (string= (elmo-dop-queue-method que
)
189 (elmo-dop-queue-method nqueue
)))
192 (setcar (elmo-dop-queue-arguments match-queue
)
193 (append (car (elmo-dop-queue-arguments match-queue
))
194 (car (elmo-dop-queue-arguments que
))))
195 (setq new-queue
(nconc new-queue
(list que
))))
196 (setq queue
(cdr queue
)) )
197 (setq elmo-dop-queue new-queue
)))
200 (defsubst elmo-dop-spool-folder
(folder)
201 "Return a spool folder for disconnected operations
202 which is corresponded to the FOLDER."
204 (concat "+" (expand-file-name "spool" (elmo-folder-msgdb-path folder
)))
206 (elmo-folder-mime-charset-internal folder
)))
208 (defun elmo-dop-spool-folder-append-buffer (folder flags
)
209 "Append current buffer content to the dop spool folder.
210 FOLDER is the folder structure.
211 Return a message number."
212 (let ((spool (elmo-dop-spool-folder folder
)))
213 (unless (elmo-folder-exists-p spool
)
214 (elmo-folder-create spool
))
215 (let ((new-number (1+ (car (elmo-folder-status spool
)))))
216 (elmo-folder-append-buffer spool flags new-number
)
220 (defun elmo-dop-spool-folder-list-messages (folder)
221 "List messages in the dop spool folder.
222 FOLDER is the folder structure."
223 (setq folder
(elmo-dop-spool-folder folder
))
224 (if (elmo-folder-exists-p folder
)
225 (elmo-folder-list-messages folder
)))
227 (defun elmo-dop-list-deleting-messages (folder)
228 "List messages which are on the deleting queue for the folder.
229 FOLDER is the folder structure."
231 (dolist (queue elmo-dop-queue
)
232 (if (and (string= (elmo-dop-queue-fname queue
)
233 (elmo-folder-name-internal folder
))
234 (eq (elmo-dop-queue-method queue
)
235 'elmo-folder-delete-messages-dop-delayed
))
236 (setq messages
(nconc messages
239 (car (elmo-dop-queue-arguments queue
)))))))))
241 (defun elmo-dop-filter-pending-messages (numbers)
242 (elmo-delete-if (lambda (number) (< number
0)) numbers
))
245 (defsubst elmo-folder-append-buffer-dop
(folder &optional flag number
)
246 (elmo-dop-queue-append
247 folder
'elmo-folder-append-buffer-dop-delayed
249 (elmo-dop-spool-folder-append-buffer folder flag
)
252 (defsubst elmo-folder-delete-messages-dop
(folder numbers
)
253 (let ((spool-folder (elmo-dop-spool-folder folder
))
255 (dolist (number numbers
)
259 (elmo-folder-delete-messages spool-folder
261 (dolist (queue elmo-dop-queue
)
262 (when (and (eq (elmo-dop-queue-fname queue
)
263 (elmo-folder-name-internal folder
))
264 (eq (elmo-dop-queue-method queue
)
265 'elmo-folder-append-buffer-dop-delayed
)
267 (nth 1 (elmo-dop-queue-arguments queue
))))
268 (setq elmo-dop-queue
(delq queue elmo-dop-queue
)))))
269 (setq queue
(cons number queue
))))
271 (elmo-dop-queue-append folder
'elmo-folder-delete-messages-dop-delayed
275 (cons number
(elmo-message-field
276 folder number
'message-id
)))
280 (defsubst elmo-message-encache-dop
(folder number
&optional read
)
281 (elmo-dop-queue-append folder
'elmo-message-encache
(list number read
)))
283 (defsubst elmo-folder-create-dop
(folder)
284 (elmo-dop-queue-append folder
'elmo-folder-create-dop-delayed nil
))
286 (defsubst elmo-folder-set-flag-dop
(folder numbers flag
)
287 (when (setq numbers
(elmo-dop-filter-pending-messages numbers
))
288 (let ((method (case flag
290 'elmo-folder-unset-read-delayed
)
292 'elmo-folder-set-read-delayed
)
294 'elmo-folder-set-important-delayed
)
296 'elmo-folder-set-answered-delayed
))))
298 (elmo-dop-queue-append folder method
(list numbers
))
299 (elmo-dop-queue-append folder
'elmo-folder-set-flag
300 (list numbers flag
))))))
302 (defsubst elmo-folder-unset-flag-dop
(folder numbers flag
)
303 (when (setq numbers
(elmo-dop-filter-pending-messages numbers
))
304 (let ((method (case flag
306 'elmo-folder-set-read-delayed
)
308 'elmo-folder-unset-read-delayed
)
310 'elmo-folder-unset-important-delayed
)
312 'elmo-folder-unset-answered-delayed
))))
314 (elmo-dop-queue-append folder method
(list numbers
))
315 (elmo-dop-queue-append folder
'elmo-folder-unset-flag
316 (list numbers flag
))))))
318 ;;; Execute as subsutitute for plugged operation.
319 (defun elmo-folder-status-dop (folder)
320 (let ((number-list (elmo-folder-list-messages folder nil
'in-msgdb
))
321 (spool-folder (elmo-dop-spool-folder folder
))
324 (setq spool-length
(or (if (elmo-folder-exists-p spool-folder
)
325 (car (elmo-folder-status spool-folder
)))
327 (setq max-num
(if number-list
(apply #'max number-list
) 0))
328 (cons (+ max-num spool-length
) (+ (length number-list
) spool-length
))))
330 (defun elmo-folder-next-message-number-dop (folder)
331 (let ((spool-folder (elmo-dop-spool-folder folder
)))
332 (- (+ 1 (elmo-max-of-list (or (elmo-folder-list-messages
336 ;;; Delayed operation (executed at online status).
337 (defun elmo-folder-append-buffer-dop-delayed (folder flag number set-number
)
338 (let ((spool-folder (elmo-dop-spool-folder folder
))
341 (when (elmo-message-fetch spool-folder number
342 (elmo-make-fetch-strategy 'entire
)
344 (setq flags
(or (elmo-message-flags-for-append folder
(* -
1 number
))
347 ;; for compatibility with 2.11.12 or earlier
352 (when (or (condition-case nil
353 (let ((new-num (elmo-folder-next-message-number folder
)))
355 (elmo-folder-append-buffer folder flags set-number
)
356 (elmo-emit-signal 'message-number-changed
357 folder
(- number
) new-num
)))
360 (elmo-folder-append-buffer
361 (elmo-get-folder elmo-lost
+found-folder
)
363 (elmo-folder-delete-messages spool-folder
(list number
)))))
364 ;; ignore failure (already dequed)
367 (defun elmo-folder-delete-messages-dop-delayed (folder number-alist
)
369 (elmo-folder-delete-messages
371 ;; messages are deleted only if message-id is not changed.
377 (elmo-message-fetch-field folder
(car pair
)
380 t
)) ; Always success (If failure, just remain)
382 (defun elmo-folder-create-dop-delayed (folder)
383 (unless (elmo-folder-exists-p folder
)
384 (elmo-folder-create folder
)))
386 (defun elmo-folder-set-important-delayed (folder numbers
)
387 (elmo-folder-set-flag folder numbers
'important
))
389 (defun elmo-folder-unset-important-delayed (folder numbers
)
390 (elmo-folder-unset-flag folder numbers
'important
))
392 (defun elmo-folder-set-read-delayed (folder numbers
)
393 (elmo-folder-unset-flag folder numbers
'unread
))
395 (defun elmo-folder-unset-read-delayed (folder numbers
)
396 (elmo-folder-set-flag folder numbers
'unread
))
398 (defun elmo-folder-set-answered-delayed (folder numbers
)
399 (elmo-folder-set-flag folder numbers
'answered
))
401 (defun elmo-folder-unset-answered-delayed (folder numbers
)
402 (elmo-folder-unset-flag folder numbers
'answered
))
405 (defun elmo-dop-msgdb (msgdb)
406 (let ((new-db (elmo-make-msgdb))
408 (dolist (dop-entity (mapcar
410 (setq entity
(elmo-msgdb-message-entity
412 (elmo-message-entity-set-number
414 (* -
1 (elmo-message-entity-number entity
)))
416 (elmo-msgdb-list-messages msgdb
)))
417 (elmo-msgdb-append-entity new-db
421 (abs (elmo-message-entity-number
426 (product-provide (provide 'elmo-dop
) (require 'elmo-version
))
428 ;;; elmo-dop.el ends here