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)
145 (let ((dir (elmo-localdir-folder-directory-internal folder))
146 overview number-alist mark-alist entity message-id
149 (len (length numbers)))
150 (message "Creating msgdb...")
153 (elmo-localdir-msgdb-create-entity
157 (setq num (elmo-msgdb-overview-entity-get-number entity))
159 (elmo-msgdb-append-element
161 (setq message-id (elmo-msgdb-overview-entity-get-id entity))
163 (elmo-msgdb-number-add number-alist
166 (setq seen (member message-id seen-list))
167 (if (setq gmark (or (elmo-msgdb-global-mark-get message-id)
168 (if (elmo-file-cache-exists-p message-id) ; XXX
171 elmo-msgdb-unread-cached-mark)
174 elmo-msgdb-new-mark))))
176 (elmo-msgdb-mark-append
180 (when (> len elmo-display-progress-threshold)
182 (elmo-display-progress
183 'elmo-localdir-msgdb-create-as-numbers "Creating msgdb..."
185 (setq numbers (cdr numbers)))
186 (message "Creating msgdb...done")
187 (list overview number-alist mark-alist))))
189 (luna-define-method elmo-folder-list-subfolders ((folder elmo-localdir-folder)
191 (elmo-mapcar-list-of-list
192 (lambda (x) (concat (elmo-folder-prefix-internal folder) x))
193 (elmo-list-subdirectories
194 (elmo-localdir-folder-path folder)
195 (or (elmo-localdir-folder-dir-name-internal folder) "")
198 (defsubst elmo-localdir-list-subr (folder &optional nonsort)
199 (let ((flist (mapcar 'string-to-int
201 (elmo-localdir-folder-directory-internal folder)
203 (killed (elmo-msgdb-killed-list-load (elmo-folder-msgdb-path folder))))
205 (cons (or (elmo-max-of-list flist) 0)
208 (elmo-msgdb-killed-list-length killed))
212 (luna-define-method elmo-folder-append-buffer ((folder elmo-localdir-folder)
215 (let ((filename (elmo-message-file-name
218 (1+ (car (elmo-folder-status folder)))))))
219 (when (file-writable-p filename)
220 (write-region-as-binary
221 (point-min) (point-max) filename nil 'no-msg)
224 (luna-define-method elmo-folder-append-messages :around
225 ((folder elmo-localdir-folder)
226 src-folder numbers &optional same-number)
227 (if (elmo-folder-message-file-p src-folder)
228 (let ((dir (elmo-localdir-folder-directory-internal folder))
230 (next-num (1+ (car (elmo-folder-status folder)))))
233 (elmo-message-file-name src-folder (car numbers))
236 (if same-number (car numbers) next-num))
238 (elmo-progress-notify 'elmo-folder-move-messages)
239 (if (and (setq numbers (cdr numbers))
242 (if (elmo-localdir-locked-p)
244 (1+ (car (elmo-folder-status folder)))
247 (luna-call-next-method)))
249 (luna-define-method elmo-folder-delete-messages ((folder elmo-localdir-folder)
251 (dolist (number numbers)
252 (elmo-localdir-delete-message folder number))
255 (defun elmo-localdir-delete-message (folder number)
256 "Delete message in the FOLDER with NUMBER."
257 (let ((filename (elmo-message-file-name folder number)))
258 (when (and (string-match "[0-9]+" filename) ; for safety.
259 (file-exists-p filename)
260 (file-writable-p filename)
261 (not (file-directory-p filename)))
262 (delete-file filename)
265 (luna-define-method elmo-message-fetch-internal ((folder elmo-localdir-folder)
267 &optional section unread)
268 (when (file-exists-p (elmo-message-file-name folder number))
269 (insert-file-contents-as-binary
270 (elmo-message-file-name folder number))))
272 (luna-define-method elmo-folder-list-messages-internal
273 ((folder elmo-localdir-folder) &optional nohide)
274 (elmo-localdir-list-subr folder))
276 (luna-define-method elmo-folder-status ((folder elmo-localdir-folder))
277 (elmo-localdir-list-subr folder t))
279 (luna-define-method elmo-folder-creatable-p ((folder elmo-localdir-folder))
282 (luna-define-method elmo-folder-writable-p ((folder elmo-localdir-folder))
285 (luna-define-method elmo-folder-create ((folder elmo-localdir-folder))
286 (let ((dir (elmo-localdir-folder-directory-internal folder)))
287 (if (file-directory-p dir)
289 (if (file-exists-p dir)
290 (error "Create folder failed")
291 (elmo-make-directory dir))
294 (luna-define-method elmo-folder-delete :before ((folder elmo-localdir-folder))
295 (let ((dir (elmo-localdir-folder-directory-internal folder)))
296 (if (not (file-directory-p dir))
297 (error "No such directory: %s" dir)
298 (elmo-delete-match-files dir "[0-9]+" t)
301 (luna-define-method elmo-folder-rename-internal ((folder elmo-localdir-folder)
303 (let* ((old (elmo-localdir-folder-directory-internal folder))
304 (new (elmo-localdir-folder-directory-internal new-folder))
305 (new-dir (directory-file-name (file-name-directory new))))
306 (if (not (file-directory-p old))
307 (error "No such directory: %s" old)
308 (if (file-exists-p new)
309 (error "Already exists directory: %s" new)
310 (if (not (file-exists-p new-dir))
311 (elmo-make-directory new-dir))
312 (rename-file old new)
315 (defsubst elmo-localdir-field-condition-match (folder condition
317 (elmo-file-field-condition-match
318 (expand-file-name (int-to-string number)
319 (elmo-localdir-folder-directory-internal folder))
320 condition number number-list))
322 (luna-define-method elmo-folder-pack-numbers ((folder elmo-localdir-folder))
323 (let* ((dir (elmo-localdir-folder-directory-internal folder))
324 (msgdb (elmo-folder-msgdb folder))
325 (onum-alist (elmo-msgdb-get-number-alist msgdb))
326 (omark-alist (elmo-msgdb-get-mark-alist msgdb))
327 (new-number 1) ; first ordinal position in localdir
328 flist onum mark new-mark-alist total)
330 (if elmo-pack-number-check-strict
331 (elmo-folder-list-messages folder) ; allow localnews
332 (mapcar 'car onum-alist)))
333 (setq total (length flist))
335 (when (> total elmo-display-progress-threshold)
336 (elmo-display-progress
337 'elmo-folder-pack-numbers "Packing..."
338 (/ (* new-number 100) total)))
339 (setq onum (car flist))
340 (when (not (eq onum new-number)) ; why \=() is wrong..
344 (rename-file (int-to-string onum) (int-to-string new-number) t))
346 (elmo-msgdb-overview-entity-set-number
347 (elmo-msgdb-overview-get-entity onum msgdb)
349 ;; update number-alist
350 (setcar (assq onum onum-alist) new-number))
352 (when (setq mark (cadr (assq onum omark-alist)))
354 (elmo-msgdb-mark-append
357 (setq new-number (1+ new-number))
358 (setq flist (cdr flist)))
359 (message "Packing...done")
360 (elmo-folder-set-msgdb-internal
363 (elmo-msgdb-get-overview msgdb)
367 (luna-define-method elmo-folder-message-file-p ((folder elmo-localdir-folder))
370 (luna-define-method elmo-message-file-name ((folder elmo-localdir-folder)
373 (int-to-string number)
374 (elmo-localdir-folder-directory-internal folder)))
376 (defun elmo-localdir-locked-p ()
377 (if elmo-localdir-lockfile-list
378 (let ((lock elmo-localdir-lockfile-list))
381 (if (file-exists-p (car lock))
383 (setq lock (cdr lock)))))))
386 (product-provide (provide 'elmo-localdir) (require 'elmo-version))
388 ;;; elmo-localdir.el ends here