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.
36 (require 'elmo-localdir)
39 (defvar elmo-dop-queue nil
40 "A list of (folder-name function-to-be-called argument-list).
41 Automatically loaded/saved.")
43 (defmacro elmo-make-dop-queue (fname method arguments)
45 (` (vector (, fname) (, method) (, arguments))))
47 (defmacro elmo-dop-queue-fname (queue)
48 "Return the folder name string of the QUEUE."
49 (` (aref (, queue) 0)))
51 (defmacro elmo-dop-queue-method (queue)
52 "Return the method symbol of the QUEUE."
53 (` (aref (, queue) 1)))
55 (defmacro elmo-dop-queue-arguments (queue)
56 "Return the arguments of the QUEUE."
57 (` (aref (, queue) 2)))
59 (defun elmo-dop-queue-append (folder method arguments)
60 "Append to disconnected operation queue."
61 (let ((queue (elmo-make-dop-queue (elmo-folder-name-internal folder)
63 (setq elmo-dop-queue (nconc elmo-dop-queue (list queue)))))
65 (defvar elmo-dop-queue-merge-method-list
66 '(elmo-folder-mark-as-read
67 elmo-folder-unmark-read
68 elmo-folder-mark-as-important
69 elmo-folder-unmark-important
70 elmo-folder-mark-as-answered
71 elmo-folder-unmark-answered))
73 (defvar elmo-dop-queue-method-name-alist
74 '((elmo-folder-append-buffer-dop-delayed . "Append")
75 (elmo-folder-delete-messages-dop-delayed . "Delete")
76 (elmo-message-encache . "Encache")
77 (elmo-folder-create-dop-delayed . "Create")
78 (elmo-folder-mark-as-read . "Read")
79 (elmo-folder-unmark-read . "Unread")
80 (elmo-folder-mark-as-answered . "Answered")
81 (elmo-folder-unmark-answered . "Unanswered")
82 (elmo-folder-mark-as-important . "Important")
83 (elmo-folder-unmark-important . "Unimportant")))
85 (defmacro elmo-dop-queue-method-name (queue)
86 `(cdr (assq (elmo-dop-queue-method ,queue)
87 elmo-dop-queue-method-name-alist)))
89 (defun elmo-dop-queue-flush ()
90 "Flush disconnected operations that consern plugged folders."
92 (unless (or (null elmo-dop-queue)
93 (vectorp (car elmo-dop-queue)))
95 Saved queue is old version(2.6). Clear all pending operations? ")
97 (setq elmo-dop-queue nil)
98 (message "All pending operations are cleared.")
99 (elmo-dop-queue-save))
100 (error "Please use 2.6 or earlier.")))
101 (elmo-dop-queue-merge)
102 (let ((queue-all elmo-dop-queue)
107 (if (elmo-folder-plugged-p
108 (elmo-make-folder (elmo-dop-queue-fname (car queue-all))))
109 (setq queue (append queue (list (car queue-all)))))
110 (setq queue-all (cdr queue-all)))
111 (setq count (length queue))
114 (format "%d pending operation(s) exists. Perform now? " count)
115 (not elmo-dop-flush-confirm) t)
119 (let ((queue elmo-dop-queue)
122 (num (length elmo-dop-queue))
125 ;; now perform pending processes.
128 (message "Flushing queue....%d/%d." i num)
131 (apply (elmo-dop-queue-method (car queue))
135 (elmo-dop-queue-fname (car queue))))
136 (elmo-folder-open folder))
137 (elmo-dop-queue-arguments (car queue)))
138 (elmo-folder-close folder))
139 (quit (setq failure t))
140 (error (setq failure err)))
143 (setq elmo-dop-queue (delq (car queue) elmo-dop-queue))
144 (setq performed (+ 1 performed)))
145 (setq queue (cdr queue)))
146 (message "%d/%d operation(s) are performed successfully."
149 (elmo-dop-queue-save)))
150 ;; when answer=NO against performing dop
151 (if (elmo-y-or-n-p "Clear these pending operations? "
152 (not elmo-dop-flush-confirm) t)
155 (setq elmo-dop-queue (delq (car queue) elmo-dop-queue))
156 (setq queue (cdr queue)))
157 (message "Pending operations are cleared.")
158 (elmo-dop-queue-save))
162 (defun elmo-dop-queue-merge ()
163 (let ((queue elmo-dop-queue)
164 new-queue match-queue que)
165 (while (setq que (car queue))
167 (memq (elmo-dop-queue-method que)
168 elmo-dop-queue-merge-method-list)
175 (string= (elmo-dop-queue-fname que)
176 (elmo-dop-queue-fname nqueue))
177 (string= (elmo-dop-queue-method que)
178 (elmo-dop-queue-method nqueue)))
181 (setcar (elmo-dop-queue-arguments match-queue)
182 (append (car (elmo-dop-queue-arguments match-queue))
183 (car (elmo-dop-queue-arguments que))))
184 (setq new-queue (nconc new-queue (list que))))
185 (setq queue (cdr queue)) )
186 (setq elmo-dop-queue new-queue)))
189 (defmacro elmo-dop-spool-folder (folder)
190 "Return a spool folder for disconnected operations
191 which is corresponded to the FOLDER."
193 (concat "+" (expand-file-name "spool" (elmo-folder-msgdb-path
196 (defun elmo-dop-spool-folder-append-buffer (folder)
197 "Append current buffer content to the dop spool folder.
198 FOLDER is the folder structure.
199 Return a message number."
200 (setq folder (elmo-dop-spool-folder folder))
201 (unless (elmo-folder-exists-p folder)
202 (elmo-folder-create folder))
203 (let ((new-number (1+ (car (elmo-folder-status folder)))))
204 ;; dop folder is a localdir folder.
205 (write-region-as-binary (point-min) (point-max)
207 (int-to-string new-number)
208 (elmo-localdir-folder-directory-internal folder))
213 (defun elmo-dop-spool-folder-list-messages (folder)
214 "List messages in the dop spool folder.
215 FOLDER is the folder structure."
216 (setq folder (elmo-dop-spool-folder folder))
217 (if (elmo-folder-exists-p folder)
218 (elmo-folder-list-messages folder)))
220 (defun elmo-dop-list-deleting-messages (folder)
221 "List messages which are on the deleting queue for the folder.
222 FOLDER is the folder structure."
224 (dolist (queue elmo-dop-queue)
225 (if (and (string= (elmo-dop-queue-fname queue)
226 (elmo-folder-name-internal folder))
227 (eq (elmo-dop-queue-method queue)
228 'elmo-folder-delete-messages-dop-delayed))
229 (setq messages (nconc messages
232 (car (elmo-dop-queue-arguments queue)))))))))
235 (defsubst elmo-folder-append-buffer-dop (folder unread &optional number)
236 (elmo-dop-queue-append
237 folder 'elmo-folder-append-buffer-dop-delayed
239 (elmo-dop-spool-folder-append-buffer
243 (defsubst elmo-folder-delete-messages-dop (folder numbers)
244 (let ((spool-folder (elmo-dop-spool-folder folder))
246 (dolist (number numbers)
248 (elmo-folder-delete-messages spool-folder
249 (list (abs number))) ; delete from queue
250 (setq queue (cons number queue))))
252 (elmo-dop-queue-append folder 'elmo-folder-delete-messages-dop-delayed
256 (cons number (elmo-message-field
257 folder number 'message-id)))
261 (defsubst elmo-message-encache-dop (folder number &optional read)
262 (elmo-dop-queue-append folder 'elmo-message-encache (list number read)))
264 (defsubst elmo-folder-create-dop (folder)
265 (elmo-dop-queue-append folder 'elmo-folder-create-dop-delayed nil))
267 (defsubst elmo-folder-mark-as-read-dop (folder numbers)
268 (elmo-dop-queue-append folder 'elmo-folder-mark-as-read (list numbers)))
270 (defsubst elmo-folder-unmark-read-dop (folder numbers)
271 (elmo-dop-queue-append folder 'elmo-folder-unmark-read (list numbers)))
273 (defsubst elmo-folder-mark-as-important-dop (folder numbers)
274 (elmo-dop-queue-append folder 'elmo-folder-mark-as-important (list numbers)))
276 (defsubst elmo-folder-unmark-important-dop (folder numbers)
277 (elmo-dop-queue-append folder 'elmo-folder-unmark-important (list numbers)))
279 (defsubst elmo-folder-mark-as-answered-dop (folder numbers)
280 (elmo-dop-queue-append folder 'elmo-folder-mark-as-answered (list numbers)))
282 (defsubst elmo-folder-unmark-answered-dop (folder numbers)
283 (elmo-dop-queue-append folder 'elmo-folder-unmark-answered (list numbers)))
285 ;;; Execute as subsutitute for plugged operation.
286 (defun elmo-folder-status-dop (folder)
287 (let* ((number-alist (elmo-msgdb-number-load
288 (elmo-folder-msgdb-path folder)))
289 (number-list (mapcar 'car number-alist))
290 (spool-folder (elmo-dop-spool-folder folder))
294 (setq spool-length (or (car (if (elmo-folder-exists-p spool-folder)
295 (elmo-folder-status spool-folder))) 0))
297 (or (nth (max (- (length number-list) 1) 0) number-list)
299 (cons (+ max-num spool-length) (+ (length number-list) spool-length))))
301 ;;; Delayed operation (executed at online status).
302 (defun elmo-folder-append-buffer-dop-delayed (folder unread number set-number)
303 (let ((spool-folder (elmo-dop-spool-folder folder))
304 failure saved dequeued)
306 (if (elmo-message-fetch spool-folder number
307 (elmo-make-fetch-strategy 'entire)
308 nil (current-buffer) 'unread)
311 (elmo-folder-append-buffer
312 folder unread set-number)))
313 (error (setq failure t)))
314 (setq dequeued t)) ; Already deletef from queue.
317 (setq saved (elmo-folder-append-buffer
318 (elmo-make-folder elmo-lost+found-folder)
320 (if (and (not dequeued) ; if dequeued, no need to delete.
321 (or (not failure) ; succeed
322 saved)) ; in lost+found
323 (elmo-folder-delete-messages spool-folder (list number)))
326 (defun elmo-folder-delete-messages-dop-delayed (folder number-alist)
327 (elmo-folder-delete-messages
329 ;; messages are deleted only if message-id is not changed.
335 (elmo-message-fetch-field folder (car pair)
339 (defun elmo-folder-create-dop-delayed (folder)
340 (unless (elmo-folder-exists-p folder)
341 (elmo-folder-create folder)))
344 (defun elmo-dop-msgdb (msgdb)
345 (list (mapcar (function
347 (elmo-msgdb-overview-entity-set-number
350 (elmo-msgdb-overview-entity-get-number x)))))
360 (cdr x)))) (nth 2 msgdb))))
363 (product-provide (provide 'elmo-dop) (require 'elmo-version))
365 ;;; elmo-dop.el ends here