1 ;;; elmo-localdir.el --- Localdir Interface for ELMO.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4 ;; Copyright (C) 1998,1999,2000 Masahiro MURATA <muse@ba2.so-net.ne.jp>
5 ;; Copyright (C) 1999,2000 Kenichi OKADA <okada@opaopa.org>
7 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
8 ;; Masahiro MURATA <muse@ba2.so-net.ne.jp>
9 ;; Kenichi OKADA <okada@opaopa.org>
10 ;; Keywords: mail, net news
12 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
14 ;; This program is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
35 (eval-when-compile (require 'cl))
40 (defcustom elmo-localdir-folder-path "~/Mail"
41 "*Local mail directory (MH format) path."
45 (defvar elmo-localdir-lockfile-list nil)
47 ;;; ELMO Local directory folder
49 (luna-define-class elmo-localdir-folder (elmo-folder)
51 (luna-define-internal-accessors 'elmo-localdir-folder))
53 ;;; elmo-localdir specific methods.
54 (luna-define-generic elmo-localdir-folder-path (folder)
55 "Return local directory path of the FOLDER.")
57 (luna-define-generic elmo-localdir-folder-name (folder name)
58 "Return directory NAME for FOLDER.")
60 (luna-define-method elmo-localdir-folder-path ((folder elmo-localdir-folder))
61 elmo-localdir-folder-path)
63 (luna-define-method elmo-localdir-folder-name ((folder elmo-localdir-folder)
67 (luna-define-method elmo-folder-initialize ((folder
70 (elmo-localdir-folder-set-dir-name-internal folder name)
71 (if (file-name-absolute-p name)
72 (elmo-localdir-folder-set-directory-internal
74 (expand-file-name name))
75 (elmo-localdir-folder-set-directory-internal
78 (elmo-localdir-folder-name folder name)
79 (elmo-localdir-folder-path folder))))
82 ;; open, check, commit, and close are generic.
84 (luna-define-method elmo-folder-exists-p ((folder elmo-localdir-folder))
85 (file-directory-p (elmo-localdir-folder-directory-internal folder)))
87 (luna-define-method elmo-folder-expand-msgdb-path ((folder
88 elmo-localdir-folder))
93 'elmo-replace-string-as-filename
94 (split-string (elmo-localdir-folder-dir-name-internal folder)
97 (expand-file-name ;;"localdir"
98 (symbol-name (elmo-folder-type-internal folder))
99 elmo-msgdb-directory)))
101 (luna-define-method elmo-message-file-name ((folder
102 elmo-localdir-folder)
104 (expand-file-name (int-to-string number)
105 (elmo-localdir-folder-directory-internal folder)))
107 (luna-define-method elmo-folder-message-file-number-p ((folder
108 elmo-localdir-folder))
111 (luna-define-method elmo-folder-message-file-directory ((folder
112 elmo-localdir-folder))
113 (elmo-localdir-folder-directory-internal folder))
115 (luna-define-method elmo-folder-message-make-temp-file-p
116 ((folder elmo-localdir-folder))
119 (luna-define-method elmo-folder-message-make-temp-files ((folder
120 elmo-localdir-folder)
124 (let ((temp-dir (elmo-folder-make-temporary-directory folder))
125 (cur-number (or start-number 0)))
126 (dolist (number numbers)
129 (int-to-string number)
130 (elmo-localdir-folder-directory-internal folder))
132 (int-to-string (if start-number cur-number number))
137 (defun elmo-localdir-msgdb-create-entity (dir number)
138 (elmo-msgdb-create-overview-entity-from-file
139 number (expand-file-name (int-to-string number) dir)))
141 (luna-define-method elmo-folder-msgdb-create ((folder elmo-localdir-folder)
149 (let ((dir (elmo-localdir-folder-directory-internal folder))
150 overview number-alist mark-alist entity message-id
153 (len (length numbers)))
154 (message "Creating msgdb...")
157 (elmo-localdir-msgdb-create-entity
161 (setq num (elmo-msgdb-overview-entity-get-number entity))
163 (elmo-msgdb-append-element
165 (setq message-id (elmo-msgdb-overview-entity-get-id entity))
167 (elmo-msgdb-number-add number-alist
170 (setq seen (member message-id seen-list))
171 (if (setq gmark (or (elmo-msgdb-global-mark-get message-id)
172 (if (elmo-file-cache-exists-p message-id) ; XXX
180 (elmo-msgdb-mark-append
184 (when (> len elmo-display-progress-threshold)
186 (elmo-display-progress
187 'elmo-localdir-msgdb-create-as-numbers "Creating msgdb..."
189 (setq numbers (cdr numbers)))
190 (message "Creating msgdb...done")
191 (list overview number-alist mark-alist))))
193 (luna-define-method elmo-folder-list-subfolders ((folder elmo-localdir-folder)
195 (elmo-mapcar-list-of-list
196 (lambda (x) (concat (elmo-folder-prefix-internal folder) x))
197 (elmo-list-subdirectories
198 (elmo-localdir-folder-path folder)
199 (or (elmo-localdir-folder-dir-name-internal folder) "")
202 (defsubst elmo-localdir-list-subr (folder &optional nonsort)
203 (let ((flist (mapcar 'string-to-int
205 (elmo-localdir-folder-directory-internal folder)
207 (killed (elmo-msgdb-killed-list-load (elmo-folder-msgdb-path folder))))
209 (cons (or (elmo-max-of-list flist) 0)
212 (elmo-msgdb-killed-list-length killed))
216 (luna-define-method elmo-folder-append-buffer ((folder elmo-localdir-folder)
219 (let ((filename (elmo-message-file-name
222 (1+ (car (elmo-folder-status folder)))))))
223 (when (file-writable-p filename)
224 (write-region-as-binary
225 (point-min) (point-max) filename nil 'no-msg)
228 (luna-define-method elmo-folder-append-messages :around
229 ((folder elmo-localdir-folder)
230 src-folder numbers unread-marks &optional same-number)
231 (if (elmo-folder-message-file-p src-folder)
232 (let ((dir (elmo-localdir-folder-directory-internal folder))
234 (next-num (1+ (car (elmo-folder-status folder)))))
237 (elmo-message-file-name src-folder (car numbers))
240 (if same-number (car numbers) next-num))
242 (elmo-progress-notify 'elmo-folder-move-messages)
243 (if (and (setq numbers (cdr numbers))
246 (if (elmo-localdir-locked-p)
248 (1+ (car (elmo-folder-status folder)))
251 (luna-call-next-method)))
253 (luna-define-method elmo-folder-delete-messages ((folder elmo-localdir-folder)
255 (dolist (number numbers)
256 (elmo-localdir-delete-message folder number))
259 (defun elmo-localdir-delete-message (folder number)
260 "Delete message in the FOLDER with NUMBER."
261 (let ((filename (elmo-message-file-name folder number)))
262 (when (and (string-match "[0-9]+" filename) ; for safety.
263 (file-exists-p filename)
264 (file-writable-p filename)
265 (not (file-directory-p filename)))
266 (delete-file filename)
269 (luna-define-method elmo-message-fetch-internal ((folder elmo-localdir-folder)
271 &optional section unread)
272 (when (file-exists-p (elmo-message-file-name folder number))
273 (insert-file-contents-as-binary
274 (elmo-message-file-name folder number))))
276 (luna-define-method elmo-folder-list-messages-internal
277 ((folder elmo-localdir-folder) &optional nohide)
278 (elmo-localdir-list-subr folder))
280 (luna-define-method elmo-folder-status ((folder elmo-localdir-folder))
281 (elmo-localdir-list-subr folder t))
283 (luna-define-method elmo-folder-creatable-p ((folder elmo-localdir-folder))
286 (luna-define-method elmo-folder-writable-p ((folder elmo-localdir-folder))
289 (luna-define-method elmo-folder-create ((folder elmo-localdir-folder))
290 (let ((dir (elmo-localdir-folder-directory-internal folder)))
291 (if (file-directory-p dir)
293 (if (file-exists-p dir)
294 (error "Create folder failed")
295 (elmo-make-directory dir))
298 (luna-define-method elmo-folder-delete :before ((folder elmo-localdir-folder))
299 (let ((dir (elmo-localdir-folder-directory-internal folder)))
300 (if (not (file-directory-p dir))
301 (error "No such directory: %s" dir)
302 (elmo-delete-match-files dir "[0-9]+" t)
305 (luna-define-method elmo-folder-rename-internal ((folder elmo-localdir-folder)
307 (let* ((old (elmo-localdir-folder-directory-internal folder))
308 (new (elmo-localdir-folder-directory-internal new-folder))
309 (new-dir (directory-file-name (file-name-directory new))))
310 (if (not (file-directory-p old))
311 (error "No such directory: %s" old)
312 (if (file-exists-p new)
313 (error "Already exists directory: %s" new)
314 (if (not (file-exists-p new-dir))
315 (elmo-make-directory new-dir))
316 (rename-file old new)
319 (defsubst elmo-localdir-field-condition-match (folder condition
321 (elmo-file-field-condition-match
322 (expand-file-name (int-to-string number)
323 (elmo-localdir-folder-directory-internal folder))
324 condition number number-list))
326 (luna-define-method elmo-folder-pack-numbers ((folder elmo-localdir-folder))
327 (let* ((dir (elmo-localdir-folder-directory-internal folder))
328 (msgdb (elmo-folder-msgdb folder))
329 (onum-alist (elmo-msgdb-get-number-alist msgdb))
330 (omark-alist (elmo-msgdb-get-mark-alist msgdb))
331 (new-number 1) ; first ordinal position in localdir
332 flist onum mark new-mark-alist total)
334 (if elmo-pack-number-check-strict
335 (elmo-folder-list-messages folder) ; allow localnews
336 (mapcar 'car onum-alist)))
337 (setq total (length flist))
339 (when (> total elmo-display-progress-threshold)
340 (elmo-display-progress
341 'elmo-folder-pack-numbers "Packing..."
342 (/ (* new-number 100) total)))
343 (setq onum (car flist))
344 (when (not (eq onum new-number)) ; why \=() is wrong..
348 (rename-file (int-to-string onum) (int-to-string new-number) t))
350 (elmo-msgdb-overview-entity-set-number
351 (elmo-msgdb-overview-get-entity onum msgdb)
353 ;; update number-alist
354 (setcar (assq onum onum-alist) new-number))
356 (when (setq mark (cadr (assq onum omark-alist)))
358 (elmo-msgdb-mark-append
361 (setq new-number (1+ new-number))
362 (setq flist (cdr flist)))
363 (message "Packing...done")
364 (elmo-folder-set-msgdb-internal
367 (elmo-msgdb-get-overview msgdb)
371 (luna-define-method elmo-folder-message-file-p ((folder elmo-localdir-folder))
374 (luna-define-method elmo-message-file-name ((folder elmo-localdir-folder)
377 (int-to-string number)
378 (elmo-localdir-folder-directory-internal folder)))
380 (defun elmo-localdir-locked-p ()
381 (if elmo-localdir-lockfile-list
382 (let ((lock elmo-localdir-lockfile-list))
385 (if (file-exists-p (car lock))
387 (setq lock (cdr lock)))))))
390 (product-provide (provide 'elmo-localdir) (require 'elmo-version))
392 ;;; elmo-localdir.el ends here