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))
71 (defvar elmo-dop-queue-method-name-alist
72 '((elmo-folder-append-buffer-dop-delayed . "Append")
73 (elmo-folder-delete-messages-dop-delayed . "Delete")
74 (elmo-message-encache . "Encache")
75 (elmo-folder-create-dop-delayed . "Create")
76 (elmo-folder-mark-as-read . "Read")
77 (elmo-folder-unmark-read . "Unread")
78 (elmo-folder-mark-as-important . "Important")
79 (elmo-folder-unmark-important . "Unimportant")))
81 (defmacro elmo-dop-queue-method-name (queue)
82 `(cdr (assq (elmo-dop-queue-method ,queue)
83 elmo-dop-queue-method-name-alist)))
85 (defun elmo-dop-queue-flush ()
86 "Flush disconnected operations that consern plugged folders."
88 (unless (or (null elmo-dop-queue)
89 (vectorp (car elmo-dop-queue)))
91 Saved queue is old version(2.6). Clear all pending operations? ")
93 (setq elmo-dop-queue nil)
94 (message "All pending operations are cleared.")
95 (elmo-dop-queue-save))
96 (error "Please use 2.6 or earlier.")))
97 (elmo-dop-queue-merge)
98 (let ((queue-all elmo-dop-queue)
103 (if (elmo-folder-plugged-p
104 (elmo-make-folder (elmo-dop-queue-fname (car queue-all))))
105 (setq queue (append queue (list (car queue-all)))))
106 (setq queue-all (cdr queue-all)))
107 (setq count (length queue))
110 (format "%d pending operation(s) exists. Perform now? " count)
111 (not elmo-dop-flush-confirm) t)
115 (let ((queue elmo-dop-queue)
118 (num (length elmo-dop-queue))
121 ;; now perform pending processes.
124 (message "Flushing queue....%d/%d." i num)
127 (apply (elmo-dop-queue-method (car queue))
131 (elmo-dop-queue-fname (car queue))))
132 (elmo-folder-open folder))
133 (elmo-dop-queue-arguments (car queue)))
134 (elmo-folder-close folder))
135 (quit (setq failure t))
136 (error (setq failure err)))
139 (setq elmo-dop-queue (delq (car queue) elmo-dop-queue))
140 (setq performed (+ 1 performed)))
141 (setq queue (cdr queue)))
142 (message "%d/%d operation(s) are performed successfully."
145 (elmo-dop-queue-save)))
146 ;; when answer=NO against performing dop
147 (if (elmo-y-or-n-p "Clear these pending operations? "
148 (not elmo-dop-flush-confirm) t)
151 (setq elmo-dop-queue (delq (car queue) elmo-dop-queue))
152 (setq queue (cdr queue)))
153 (message "Pending operations are cleared.")
154 (elmo-dop-queue-save))
158 (defun elmo-dop-queue-merge ()
159 (let ((queue elmo-dop-queue)
160 new-queue match-queue que)
161 (while (setq que (car queue))
163 (memq (elmo-dop-queue-method que)
164 elmo-dop-queue-merge-method-list)
171 (string= (elmo-dop-queue-fname que)
172 (elmo-dop-queue-fname nqueue))
173 (string= (elmo-dop-queue-method que)
174 (elmo-dop-queue-method nqueue)))
177 (setcar (elmo-dop-queue-arguments match-queue)
178 (append (car (elmo-dop-queue-arguments match-queue))
179 (car (elmo-dop-queue-arguments que))))
180 (setq new-queue (nconc new-queue (list que))))
181 (setq queue (cdr queue)) )
182 (setq elmo-dop-queue new-queue)))
185 (defmacro elmo-dop-spool-folder (folder)
186 "Return a spool folder for disconnected operations
187 which is corresponded to the FOLDER."
189 (concat "+" (expand-file-name "spool" (elmo-folder-msgdb-path
192 (defun elmo-dop-spool-folder-append-buffer (folder)
193 "Append current buffer content to the dop spool folder.
194 FOLDER is the folder structure.
195 Return a message number."
196 (setq folder (elmo-dop-spool-folder folder))
197 (unless (elmo-folder-exists-p folder)
198 (elmo-folder-create folder))
199 (let ((new-number (1+ (car (elmo-folder-status folder)))))
200 ;; dop folder is a localdir folder.
201 (write-region-as-binary (point-min) (point-max)
203 (int-to-string new-number)
204 (elmo-localdir-folder-directory-internal folder))
209 (defun elmo-dop-spool-folder-list-messages (folder)
210 "List messages in the dop spool folder.
211 FOLDER is the folder structure."
212 (setq folder (elmo-dop-spool-folder folder))
213 (if (elmo-folder-exists-p folder)
214 (elmo-folder-list-messages folder)))
216 (defun elmo-dop-list-deleting-messages (folder)
217 "List messages which are on the deleting queue for the folder.
218 FOLDER is the folder structure."
220 (dolist (queue elmo-dop-queue)
221 (if (and (string= (elmo-dop-queue-fname queue)
222 (elmo-folder-name-internal folder))
223 (eq (elmo-dop-queue-method queue)
224 'elmo-folder-delete-messages-dop-delayed))
225 (setq messages (nconc messages
228 (car (elmo-dop-queue-arguments queue)))))))))
231 (defsubst elmo-folder-append-buffer-dop (folder unread &optional number)
232 (elmo-dop-queue-append
233 folder 'elmo-folder-append-buffer-dop-delayed
235 (elmo-dop-spool-folder-append-buffer
239 (defsubst elmo-folder-delete-messages-dop (folder numbers)
240 (let ((spool-folder (elmo-dop-spool-folder folder))
242 (dolist (number numbers)
244 (elmo-folder-delete-messages spool-folder
245 (list (abs number))) ; delete from queue
246 (setq queue (cons number queue))))
248 (elmo-dop-queue-append folder 'elmo-folder-delete-messages-dop-delayed
252 (cons number (elmo-message-field
253 folder number 'message-id)))
257 (defsubst elmo-message-encache-dop (folder number &optional read)
258 (elmo-dop-queue-append folder 'elmo-message-encache (list number read)))
260 (defsubst elmo-folder-create-dop (folder)
261 (elmo-dop-queue-append folder 'elmo-folder-create-dop-delayed nil))
263 (defsubst elmo-folder-mark-as-read-dop (folder numbers)
264 (elmo-dop-queue-append folder 'elmo-folder-mark-as-read (list numbers)))
266 (defsubst elmo-folder-unmark-read-dop (folder numbers)
267 (elmo-dop-queue-append folder 'elmo-folder-unmark-read (list numbers)))
269 (defsubst elmo-folder-mark-as-important-dop (folder numbers)
270 (elmo-dop-queue-append folder 'elmo-folder-mark-as-important (list numbers)))
272 (defsubst elmo-folder-unmark-important-dop (folder numbers)
273 (elmo-dop-queue-append folder 'elmo-folder-unmark-important (list numbers)))
275 ;;; Execute as subsutitute for plugged operation.
276 (defun elmo-folder-status-dop (folder)
277 (let* ((number-alist (elmo-msgdb-number-load
278 (elmo-folder-msgdb-path folder)))
279 (number-list (mapcar 'car number-alist))
280 (spool-folder (elmo-dop-spool-folder folder))
285 (or (car (if (elmo-folder-exists-p spool-folder)
286 (elmo-folder-status spool-folder)))
289 (or (nth (max (- (length number-list) 1) 0) number-list)
291 (cons (+ max-num spool-length) (+ (length number-list) spool-length))))
293 ;;; Delayed operation (executed at online status).
294 (defun elmo-folder-append-buffer-dop-delayed (folder unread number set-number)
295 (let ((spool-folder (elmo-dop-spool-folder folder))
296 failure saved dequeued)
298 (if (elmo-message-fetch spool-folder number
299 (elmo-make-fetch-strategy 'entire)
300 nil (current-buffer) 'unread)
303 (elmo-folder-append-buffer
304 folder unread set-number)))
305 (error (setq failure t)))
306 (setq dequeued t)) ; Already deletef from queue.
309 (setq saved (elmo-folder-append-buffer
310 (elmo-make-folder elmo-lost+found-folder)
312 (if (and (not dequeued) ; if dequeued, no need to delete.
313 (or (not failure) ; succeed
314 saved)) ; in lost+found
315 (elmo-folder-delete-messages spool-folder (list number)))
318 (defun elmo-folder-delete-messages-dop-delayed (folder number-alist)
319 (elmo-folder-delete-messages
321 ;; messages are deleted only if message-id is not changed.
327 (elmo-message-fetch-field folder (car pair)
331 (defun elmo-folder-create-dop-delayed (folder)
332 (unless (elmo-folder-exists-p folder)
333 (elmo-folder-create folder)))
336 (defun elmo-dop-msgdb (msgdb)
337 (list (mapcar (function
339 (elmo-msgdb-overview-entity-set-number
342 (elmo-msgdb-overview-entity-get-number x)))))
352 (cdr x)))) (nth 2 msgdb))))
355 (product-provide (provide 'elmo-dop) (require 'elmo-version))
357 ;;; elmo-dop.el ends here