* elmo.el (elmo-folder-list-flagged): New generic function.
[elisp/wanderlust.git] / elmo / elmo-localdir.el
1 ;;; elmo-localdir.el --- Localdir Interface for ELMO.
2
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>
6
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
11
12 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
13
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)
17 ;; any later version.
18 ;;
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.
23 ;;
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.
28 ;;
29
30 ;;; Commentary:
31 ;;
32
33 ;;; Code:
34 ;;
35 (eval-when-compile (require 'cl))
36
37 (require 'elmo-msgdb)
38 (require 'elmo)
39
40 (defcustom elmo-localdir-folder-path "~/Mail"
41   "*Local mail directory (MH format) path."
42   :type 'directory
43   :group 'elmo)
44
45 (defvar elmo-localdir-lockfile-list nil)
46
47 ;;; ELMO Local directory folder
48 (eval-and-compile
49   (luna-define-class elmo-localdir-folder (elmo-folder)
50                      (dir-name directory))
51   (luna-define-internal-accessors 'elmo-localdir-folder))
52
53 ;;; elmo-localdir specific methods.
54 (luna-define-generic elmo-localdir-folder-path (folder)
55   "Return local directory path of the FOLDER.")
56
57 (luna-define-generic elmo-localdir-folder-name (folder name)
58   "Return directory NAME for FOLDER.")
59
60 (luna-define-method elmo-localdir-folder-path ((folder elmo-localdir-folder))
61   elmo-localdir-folder-path)
62
63 (luna-define-method elmo-localdir-folder-name ((folder elmo-localdir-folder)
64                                                name)
65   name)
66
67 (luna-define-method elmo-folder-initialize ((folder
68                                              elmo-localdir-folder)
69                                             name)
70   (elmo-localdir-folder-set-dir-name-internal folder name)
71   (if (file-name-absolute-p name)
72       (elmo-localdir-folder-set-directory-internal
73        folder
74        (expand-file-name name))
75     (elmo-localdir-folder-set-directory-internal
76      folder
77      (expand-file-name
78       (elmo-localdir-folder-name folder name)
79       (elmo-localdir-folder-path folder))))
80   folder)
81
82 ;; open, check, commit, and close are generic.
83
84 (luna-define-method elmo-folder-exists-p ((folder elmo-localdir-folder))
85   (file-directory-p (elmo-localdir-folder-directory-internal folder)))
86
87 (luna-define-method elmo-folder-expand-msgdb-path ((folder
88                                                     elmo-localdir-folder))
89   (expand-file-name
90    (mapconcat
91     'identity
92     (mapcar
93      'elmo-replace-string-as-filename
94      (split-string (elmo-localdir-folder-dir-name-internal folder)
95                    "/"))
96     "/")
97    (expand-file-name ;;"localdir"
98     (symbol-name (elmo-folder-type-internal folder))
99     elmo-msgdb-directory)))
100
101 (luna-define-method elmo-message-file-name ((folder
102                                              elmo-localdir-folder)
103                                             number)
104   (expand-file-name (int-to-string number)
105                     (elmo-localdir-folder-directory-internal folder)))
106
107 (luna-define-method elmo-folder-message-file-number-p ((folder
108                                                         elmo-localdir-folder))
109   t)
110
111 (luna-define-method elmo-folder-message-file-directory ((folder
112                                                          elmo-localdir-folder))
113   (elmo-localdir-folder-directory-internal folder))
114
115 (luna-define-method elmo-folder-message-make-temp-file-p
116   ((folder elmo-localdir-folder))
117   t)
118
119 (luna-define-method elmo-folder-message-make-temp-files ((folder
120                                                           elmo-localdir-folder)
121                                                          numbers
122                                                          &optional
123                                                          start-number)
124   (let ((temp-dir (elmo-folder-make-temporary-directory folder))
125         (cur-number (or start-number 0)))
126     (dolist (number numbers)
127       (elmo-copy-file
128        (expand-file-name
129         (int-to-string number)
130         (elmo-localdir-folder-directory-internal folder))
131        (expand-file-name
132         (int-to-string (if start-number cur-number number))
133         temp-dir))
134       (incf cur-number))
135     temp-dir))
136
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)))
140
141 (luna-define-method elmo-folder-msgdb-create ((folder elmo-localdir-folder)
142                                               numbers
143                                               seen-list)
144   (when numbers
145     (let ((dir (elmo-localdir-folder-directory-internal folder))
146           overview number-alist mark-alist entity message-id
147           num seen gmark
148           (i 0)
149           (len (length numbers)))
150       (message "Creating msgdb...")
151       (while numbers
152         (setq entity
153               (elmo-localdir-msgdb-create-entity
154                dir (car numbers)))
155         (if (null entity)
156             ()
157           (setq num (elmo-msgdb-overview-entity-get-number entity))
158           (setq overview
159                 (elmo-msgdb-append-element
160                  overview entity))
161           (setq message-id (elmo-msgdb-overview-entity-get-id entity))
162           (setq number-alist
163                 (elmo-msgdb-number-add number-alist
164                                        num
165                                        message-id))
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
169                                   (if seen
170                                       nil
171                                     elmo-msgdb-unread-cached-mark)
172                                 (if seen
173                                     nil ;;seen-mark
174                                   elmo-msgdb-new-mark))))
175               (setq mark-alist
176                     (elmo-msgdb-mark-append
177                      mark-alist
178                      num
179                      gmark))))
180         (when (> len elmo-display-progress-threshold)
181           (setq i (1+ i))
182           (elmo-display-progress
183            'elmo-localdir-msgdb-create-as-numbers "Creating msgdb..."
184            (/ (* i 100) len)))
185         (setq numbers (cdr numbers)))
186       (message "Creating msgdb...done")
187       (list overview number-alist mark-alist))))
188
189 (luna-define-method elmo-folder-list-subfolders ((folder elmo-localdir-folder)
190                                                  &optional one-level)
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) "")
196     one-level)))
197
198 (defsubst elmo-localdir-list-subr (folder &optional nonsort)
199   (let ((flist (mapcar 'string-to-int
200                        (directory-files
201                         (elmo-localdir-folder-directory-internal folder)
202                         nil "^[0-9]+$" t)))
203         (killed (elmo-msgdb-killed-list-load (elmo-folder-msgdb-path folder))))
204     (if nonsort
205         (cons (or (elmo-max-of-list flist) 0)
206               (if killed
207                   (- (length flist)
208                      (elmo-msgdb-killed-list-length killed))
209                 (length flist)))
210       (sort flist '<))))
211
212 (luna-define-method elmo-folder-append-buffer ((folder elmo-localdir-folder)
213                                                unread
214                                                &optional number)
215   (let ((filename (elmo-message-file-name
216                    folder
217                    (or number
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)
222       t)))
223
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))
229             (succeeds numbers)
230             (next-num (1+ (car (elmo-folder-status folder)))))
231         (while numbers
232           (elmo-copy-file
233            (elmo-message-file-name src-folder (car numbers))
234            (expand-file-name
235             (int-to-string
236              (if same-number (car numbers) next-num))
237             dir))
238           (elmo-progress-notify 'elmo-folder-move-messages)
239           (if (and (setq numbers (cdr numbers))
240                    (not same-number))
241               (setq next-num
242                     (if (elmo-localdir-locked-p)
243                         ;; MDA is running.
244                         (1+ (car (elmo-folder-status folder)))
245                       (1+ next-num)))))
246         succeeds)
247     (luna-call-next-method)))
248
249 (luna-define-method elmo-folder-delete-messages ((folder elmo-localdir-folder)
250                                                  numbers)
251   (dolist (number numbers)
252     (elmo-localdir-delete-message folder number))
253   t)
254
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)
263       t)))
264
265 (luna-define-method elmo-message-fetch-internal ((folder elmo-localdir-folder)
266                                                  number strategy
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))))
271
272 (luna-define-method elmo-folder-list-messages-internal
273   ((folder elmo-localdir-folder) &optional nohide)
274   (elmo-localdir-list-subr folder))
275
276 (luna-define-method elmo-folder-status ((folder elmo-localdir-folder))
277   (elmo-localdir-list-subr folder t))
278
279 (luna-define-method elmo-folder-creatable-p ((folder elmo-localdir-folder))
280   t)
281
282 (luna-define-method elmo-folder-writable-p ((folder elmo-localdir-folder))
283   t)
284
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)
288         ()
289       (if (file-exists-p dir)
290           (error "Create folder failed")
291         (elmo-make-directory dir))
292       t)))
293
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)
299       t)))
300
301 (luna-define-method elmo-folder-rename-internal ((folder elmo-localdir-folder)
302                                                  new-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)
313         t))))
314
315 (defsubst elmo-localdir-field-condition-match (folder condition
316                                                       number number-list)
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))
321
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)
329     (setq flist
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))
334     (while 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..
341         (elmo-bind-directory
342          dir
343          ;; xxx  nfs,hardlink
344          (rename-file (int-to-string onum) (int-to-string new-number) t))
345         ;; update overview
346         (elmo-msgdb-overview-entity-set-number
347          (elmo-msgdb-overview-get-entity onum msgdb)
348          new-number)
349         ;; update number-alist
350         (setcar (assq onum onum-alist) new-number))
351       ;; update mark-alist
352       (when (setq mark (cadr (assq onum omark-alist)))
353         (setq new-mark-alist
354               (elmo-msgdb-mark-append
355                new-mark-alist
356                new-number mark)))
357       (setq new-number (1+ new-number))
358       (setq flist (cdr flist)))
359     (message "Packing...done")
360     (elmo-folder-set-msgdb-internal
361      folder
362      (elmo-make-msgdb
363       (elmo-msgdb-get-overview msgdb)
364       onum-alist
365       new-mark-alist))))
366
367 (luna-define-method elmo-folder-message-file-p ((folder elmo-localdir-folder))
368   t)
369
370 (luna-define-method elmo-message-file-name ((folder elmo-localdir-folder)
371                                             number)
372   (expand-file-name
373    (int-to-string number)
374    (elmo-localdir-folder-directory-internal folder)))
375
376 (defun elmo-localdir-locked-p ()
377   (if elmo-localdir-lockfile-list
378       (let ((lock elmo-localdir-lockfile-list))
379         (catch 'found
380           (while lock
381             (if (file-exists-p (car lock))
382                 (throw 'found t))
383             (setq lock (cdr lock)))))))
384
385 (require 'product)
386 (product-provide (provide 'elmo-localdir) (require 'elmo-version))
387
388 ;;; elmo-localdir.el ends here