* elmo-imap4.el (elmo-imap4-decode-folder-string): Call `utf7-decode'.
[elisp/wanderlust.git] / elmo / elmo-maildir.el
index 0a91115..29de0b7 100644 (file)
 (require 'elmo)
 (require 'elmo-map)
 
+(defcustom elmo-maildir-folder-path "~/Maildir"
+  "*Maildir folder path."
+  :type 'directory
+  :group 'elmo)
+
+(defconst elmo-maildir-flag-specs '((important ?F)
+                                   (read ?S)
+                                   (unread ?S 'remove)
+                                   (answered ?R)))
+
+;; Decided at compile time.
+(defcustom elmo-maildir-separator
+  (if (memq system-type '(windows-nt)) ?\- ?:)
+  "Character separating the id section from the flags section.
+According to the maildir specification, this should be a colon (?:),
+but some file systems don't support colons in filenames."
+  :type 'character
+  :group 'elmo)
+
+(defmacro elmo-maildir-adjust-separator (string)
+  `(if (= elmo-maildir-separator ?:)
+       ,string
+     (elmo-replace-in-string
+      ,string ":" (char-to-string elmo-maildir-separator))))
+
 ;;; ELMO Maildir folder
 (eval-and-compile
   (luna-define-class elmo-maildir-folder
                     (elmo-map-folder)
-                    (directory unread-locations flagged-locations))
+                    (directory unread-locations
+                               flagged-locations
+                               answered-locations))
   (luna-define-internal-accessors 'elmo-maildir-folder))
 
 (luna-define-method elmo-folder-initialize ((folder
@@ -84,97 +111,136 @@ LOCATION."
   (let* ((cur-dir (expand-file-name (or child-dir "cur") dir))
         (cur (directory-files cur-dir
                               nil "^[^.].*$" t))
-        unread-locations flagged-locations seen flagged sym
-        locations)
+        unread-locations flagged-locations answered-locations
+        sym locations flag-list)
     (setq locations
          (mapcar
           (lambda (x)
-            (if (string-match "^\\([^:]+\\):\\([^:]+\\)$" x)
+            (if (string-match
+                 (elmo-maildir-adjust-separator "^\\([^:]+\\):\\([^:]+\\)$")
+                 x)
                 (progn
-                  (setq seen nil)
-                  (save-match-data
-                    (cond
-                     ((string-match "S" (elmo-match-string 2 x))
-                      (setq seen t))
-                     ((string-match "F" (elmo-match-string 2 x))
-                      (setq flagged t))))
-                  (setq sym (elmo-match-string 1 x))
-                  (unless seen (setq unread-locations
-                                     (cons sym unread-locations)))
-                  (if flagged (setq flagged-locations
-                                    (cons sym flagged-locations)))
+                  (setq sym (elmo-match-string 1 x)
+                        flag-list (string-to-char-list
+                                   (elmo-match-string 2 x)))
+                  (when (memq ?F flag-list)
+                    (setq flagged-locations
+                          (cons sym flagged-locations)))
+                  (when (memq ?R flag-list)
+                    (setq answered-locations
+                          (cons sym answered-locations)))
+                  (unless (memq ?S flag-list)
+                    (setq unread-locations
+                          (cons sym unread-locations)))
                   sym)
               x))
           cur))
-    (list locations unread-locations flagged-locations)))
+    (list locations unread-locations flagged-locations answered-locations)))
 
 (luna-define-method elmo-map-folder-list-message-locations
   ((folder elmo-maildir-folder))
   (elmo-maildir-update-current folder)
   (let ((locs (elmo-maildir-list-location
               (elmo-maildir-folder-directory-internal folder))))
-    ;; 0: locations, 1: unread-locations, 2: flagged-locations
+    ;; 0: locations, 1: unread-locs, 2: flagged-locs 3: answered-locs
     (elmo-maildir-folder-set-unread-locations-internal folder (nth 1 locs))
     (elmo-maildir-folder-set-flagged-locations-internal folder (nth 2 locs))
+    (elmo-maildir-folder-set-answered-locations-internal folder (nth 3 locs))
     (nth 0 locs)))
 
-(luna-define-method elmo-map-folder-list-unreads
-  ((folder elmo-maildir-folder))
-  (elmo-maildir-folder-unread-locations-internal folder))
-
-(luna-define-method elmo-map-folder-list-importants
-  ((folder elmo-maildir-folder))
-  (elmo-maildir-folder-flagged-locations-internal folder))
-
-(luna-define-method elmo-folder-msgdb-create 
-  ((folder elmo-maildir-folder)
-   numbers new-mark already-mark seen-mark important-mark seen-list)
+(luna-define-method elmo-map-folder-list-flagged ((folder elmo-maildir-folder)
+                                                 flag)
+  (case flag
+    (unread
+     (elmo-maildir-folder-unread-locations-internal folder))
+    (important
+     (elmo-maildir-folder-flagged-locations-internal folder))
+    (answered
+     (elmo-maildir-folder-answered-locations-internal folder))
+    (otherwise
+     t)))
+
+(luna-define-method elmo-folder-msgdb-create ((folder elmo-maildir-folder)
+                                             numbers flag-table)
   (let* ((unread-list (elmo-maildir-folder-unread-locations-internal folder))
         (flagged-list (elmo-maildir-folder-flagged-locations-internal folder))
+        (answered-list (elmo-maildir-folder-answered-locations-internal
+                        folder))
         (len (length numbers))
+        (new-msgdb (elmo-make-msgdb))
         (i 0)
-        overview number-alist mark-alist entity
-        location pair mark)
+        entity message-id flags location)
     (message "Creating msgdb...")
-    (dolist
-       (number numbers)
+    (dolist (number numbers)
       (setq location (elmo-map-message-location folder number))
       (setq entity
-           (elmo-msgdb-create-overview-entity-from-file
+           (elmo-msgdb-create-message-entity-from-file
+            (elmo-msgdb-message-entity-handler new-msgdb)
             number
             (elmo-maildir-message-file-name folder location)))
       (when entity
-       (setq overview
-             (elmo-msgdb-append-element overview entity))
-       (setq number-alist
-             (elmo-msgdb-number-add number-alist
-                                    (elmo-msgdb-overview-entity-get-number
-                                     entity)
-                                    (elmo-msgdb-overview-entity-get-id
-                                     entity)))
-       (cond 
-        ((member location unread-list)
-         (setq mark new-mark)) ; unread!
-        ((member location flagged-list)
-         (setq mark important-mark)))
-       (if (setq mark (or (elmo-msgdb-global-mark-get
-                           (elmo-msgdb-overview-entity-get-id
-                            entity))
-                          mark))
-           (setq mark-alist
-                 (elmo-msgdb-mark-append
-                  mark-alist
-                  (elmo-msgdb-overview-entity-get-number
-                   entity)
-                  mark)))
+       (setq message-id (elmo-message-entity-field entity 'message-id)
+             ;; Precede flag-table to file-info.
+             flags (copy-sequence
+                    (elmo-flag-table-get flag-table message-id)))
+
+       ;; Already flagged on filename (precede it to flag-table).
+       (when (member location flagged-list)
+         (or (memq 'important flags)
+             (setq flags (cons 'important flags))))
+       (when (member location answered-list)
+         (or (memq 'answered flags)
+             (setq flags (cons 'answered flags))))
+       (unless (member location unread-list)
+         (and (memq 'unread flags)
+              (setq flags (delq 'unread flags))))
+
+       ;; Update filename's info portion according to the flag-table.
+       (when (and (memq 'important flags)
+                  (not (member location flagged-list)))
+         (elmo-maildir-set-mark
+          (elmo-maildir-message-file-name folder location)
+          ?F)
+         ;; Append to flagged location list.
+         (elmo-maildir-folder-set-flagged-locations-internal
+          folder
+          (cons location
+                (elmo-maildir-folder-flagged-locations-internal
+                 folder)))
+         (setq flags (delq 'unread flags)))
+       (when (and (memq 'answered flags)
+                  (not (member location answered-list)))
+         (elmo-maildir-set-mark
+          (elmo-maildir-message-file-name folder location)
+          ?R)
+         ;; Append to answered location list.
+         (elmo-maildir-folder-set-answered-locations-internal
+          folder
+          (cons location
+                (elmo-maildir-folder-answered-locations-internal folder)))
+         (setq flags (delq 'unread flags)))
+       (when (and (not (memq 'unread flags))
+                  (member location unread-list))
+         (elmo-maildir-set-mark
+          (elmo-maildir-message-file-name folder location)
+          ?S)
+         ;; Delete from unread locations.
+         (elmo-maildir-folder-set-unread-locations-internal
+          folder
+          (delete location
+                  (elmo-maildir-folder-unread-locations-internal
+                   folder))))
+       (unless (memq 'unread flags)
+         (setq flags (delq 'new flags)))
+       (elmo-global-flags-set flags folder number message-id)
+       (elmo-msgdb-append-entity new-msgdb entity flags)
        (when (> len elmo-display-progress-threshold)
          (setq i (1+ i))
          (elmo-display-progress
           'elmo-maildir-msgdb-create "Creating msgdb..."
           (/ (* i 100) len)))))
     (message "Creating msgdb...done")
-    (elmo-msgdb-sort-by-date
-     (list overview number-alist mark-alist))))
+    (elmo-msgdb-sort-by-date new-msgdb)))
 
 (defun elmo-maildir-cleanup-temporal (dir)
   ;; Delete files in the tmp dir which are not accessed
@@ -209,13 +275,20 @@ LOCATION."
     (while news
       (rename-file
        (expand-file-name (car news) (expand-file-name "new" maildir))
-       (expand-file-name (concat (car news) ":2,")
+       (expand-file-name (concat
+                         (car news)
+                         (unless (string-match
+                                  (elmo-maildir-adjust-separator ":2,[A-Z]*$")
+                                  (car news))
+                           (elmo-maildir-adjust-separator  ":2,")))
                         (expand-file-name "cur" maildir)))
       (setq news (cdr news)))))
 
 (defun elmo-maildir-set-mark (filename mark)
   "Mark the FILENAME file in the maildir.  MARK is a character."
-  (if (string-match "^\\([^:]+:[12],\\)\\(.*\\)$" filename)
+  (if (string-match
+       (elmo-maildir-adjust-separator "^\\([^:]+:[12],\\)\\(.*\\)$")
+       filename)
       (let ((flaglist (string-to-char-list (elmo-match-string
                                            2 filename))))
        (unless (memq mark flaglist)
@@ -225,12 +298,15 @@ LOCATION."
                               (char-list-to-string flaglist)))))
     ;; Rescue no info file in maildir.
     (rename-file filename
-                (concat filename ":2," (char-to-string mark))))
+                (concat filename
+                        (elmo-maildir-adjust-separator ":2,")
+                        (char-to-string mark))))
   t)
 
 (defun elmo-maildir-delete-mark (filename mark)
   "Mark the FILENAME file in the maildir.  MARK is a character."
-  (if (string-match "^\\([^:]+:2,\\)\\(.*\\)$" filename)
+  (if (string-match (elmo-maildir-adjust-separator "^\\([^:]+:2,\\)\\(.*\\)$")
+                   filename)
       (let ((flaglist (string-to-char-list (elmo-match-string
                                            2 filename))))
        (when (memq mark flaglist)
@@ -254,21 +330,25 @@ LOCATION."
      mark))
   t)
 
-(luna-define-method elmo-map-folder-mark-as-important ((folder elmo-maildir-folder)
-                                                      locs)
-  (elmo-maildir-set-mark-msgs folder locs ?F))
-  
-(luna-define-method elmo-map-folder-unmark-important ((folder elmo-maildir-folder)
-                                                     locs)
-  (elmo-maildir-delete-mark-msgs folder locs ?F))
-
-(luna-define-method elmo-map-folder-mark-as-read ((folder elmo-maildir-folder)
-                                                 locs)
-  (elmo-maildir-set-mark-msgs folder locs ?S))
-
-(luna-define-method elmo-map-folder-unmark-read ((folder elmo-maildir-folder)
-                                                locs)
-  (elmo-maildir-delete-mark-msgs folder locs ?S))
+(defsubst elmo-maildir-set-mark-messages (folder locations mark remove)
+  (when mark
+    (if remove
+       (elmo-maildir-delete-mark-msgs folder locations mark)
+      (elmo-maildir-set-mark-msgs folder locations mark))))
+
+(luna-define-method elmo-map-folder-set-flag ((folder elmo-maildir-folder)
+                                             locations flag)
+  (let ((spec (cdr (assq flag elmo-maildir-flag-specs))))
+    (when spec
+      (elmo-maildir-set-mark-messages folder locations
+                                     (car spec) (nth 1 spec)))))
+
+(luna-define-method elmo-map-folder-unset-flag ((folder elmo-maildir-folder)
+                                               locations flag)
+  (let ((spec (cdr (assq flag elmo-maildir-flag-specs))))
+    (when spec
+      (elmo-maildir-set-mark-messages folder locations
+                                     (car spec) (not (nth 1 spec))))))
 
 (luna-define-method elmo-folder-list-subfolders
   ((folder elmo-maildir-folder) &optional one-level)
@@ -337,7 +417,7 @@ file name for maildir directories."
     filename))
 
 (luna-define-method elmo-folder-append-buffer ((folder elmo-maildir-folder)
-                                              unread &optional number)
+                                              &optional flags number)
   (let ((basedir (elmo-maildir-folder-directory-internal folder))
        (src-buf (current-buffer))
        dst-buf filename)
@@ -350,11 +430,15 @@ file name for maildir directories."
          (as-binary-output-file
           (write-region (point-min) (point-max) filename nil 'no-msg))
          ;; add link from new.
-         (elmo-add-name-to-file
+         ;; Some filesystem (like AFS) does not have hard-link.
+         ;; So we use elmo-copy-file instead of elmo-add-name-to-file here.
+         (elmo-copy-file
           filename
           (expand-file-name
            (concat "new/" (file-name-nondirectory filename))
            basedir))
+         (elmo-folder-preserve-flags
+          folder (elmo-msgdb-get-message-id-from-buffer) flags)
          t)
       ;; If an error occured, return nil.
       (error))))
@@ -389,22 +473,34 @@ file name for maildir directories."
 
 (luna-define-method elmo-folder-append-messages :around
   ((folder elmo-maildir-folder)
-   src-folder numbers unread-marks &optional same-number)
+   src-folder numbers &optional same-number)
   (if (elmo-folder-message-file-p src-folder)
-      (let ((dir (elmo-maildir-folder-directory-internal folder))
+      (let ((src-msgdb-exists (not (zerop (elmo-folder-length src-folder))))
+           (dir (elmo-maildir-folder-directory-internal folder))
+           (table (elmo-folder-flag-table folder))
            (succeeds numbers)
-           filename)
+           filename flags id)
        (dolist (number numbers)
-         (setq filename (elmo-maildir-temporal-filename dir))
+         (setq flags (elmo-message-flags src-folder (car numbers))
+               filename (elmo-maildir-temporal-filename dir))
          (elmo-copy-file
           (elmo-message-file-name src-folder number)
           filename)
-         (elmo-add-name-to-file
+         ;; Some filesystem (like AFS) does not have hard-link.
+         ;; So we use elmo-copy-file instead of elmo-add-name-to-file here.
+         (elmo-copy-file
           filename
           (expand-file-name
            (concat "new/" (file-name-nondirectory filename))
            dir))
+         ;; src folder's msgdb is loaded.
+         (when (setq id (and src-msgdb-exists
+                             (elmo-message-field src-folder (car numbers)
+                                                 'message-id)))
+           (elmo-flag-table-set table id flags))
          (elmo-progress-notify 'elmo-folder-move-messages))
+       (when (elmo-folder-persistent-p folder)
+         (elmo-folder-close-flag-table folder))
        succeeds)
     (luna-call-next-method)))
 
@@ -416,14 +512,18 @@ file name for maildir directories."
       (if (and file
               (file-writable-p file)
               (not (file-directory-p file)))
-         (delete-file file)))))
+         (delete-file file))))
+  t)
 
 (luna-define-method elmo-map-message-fetch ((folder elmo-maildir-folder)
                                            location strategy
                                            &optional section unseen)
   (let ((file (elmo-maildir-message-file-name folder location)))
     (when (file-exists-p file)
-      (insert-file-contents-as-binary file))))
+      (insert-file-contents-as-binary file)
+      (unless unseen
+       (elmo-map-folder-set-flag folder (list location) 'read))
+      t)))
 
 (luna-define-method elmo-folder-exists-p ((folder elmo-maildir-folder))
   (let ((basedir (elmo-maildir-folder-directory-internal folder)))
@@ -431,8 +531,7 @@ file name for maildir directories."
         (file-directory-p (expand-file-name "cur" basedir))
         (file-directory-p (expand-file-name "tmp" basedir)))))
 
-(luna-define-method elmo-folder-diff ((folder elmo-maildir-folder)
-                                     &optional numbers)
+(luna-define-method elmo-folder-diff ((folder elmo-maildir-folder))
   (let* ((dir (elmo-maildir-folder-directory-internal folder))
         (new-len (length (car (elmo-maildir-list-location dir "new"))))
         (cur-len (length (car (elmo-maildir-list-location dir "cur")))))
@@ -457,22 +556,44 @@ file name for maildir directories."
          t)
       (error))))
 
-(luna-define-method elmo-folder-delete :before ((folder elmo-maildir-folder))
-  (let ((basedir (elmo-maildir-folder-directory-internal folder)))
-    (condition-case nil
-       (let ((tmp-files (directory-files
-                         (expand-file-name "tmp" basedir)
-                         t "[^.].*")))
-         ;; Delete files in tmp.
-         (dolist (file tmp-files)
-           (delete-file file))
-         (dolist (dir '("new" "cur" "tmp" "."))
-           (setq dir (expand-file-name dir basedir))
-           (if (not (file-directory-p dir))
-               (error nil)
-             (elmo-delete-directory dir t)))
-         t)
-      (error nil))))
+(luna-define-method elmo-folder-delete ((folder elmo-maildir-folder))
+  (let ((msgs (and (elmo-folder-exists-p folder)
+                  (elmo-folder-list-messages folder))))
+    (when (yes-or-no-p (format "%sDelete msgdb and substance of \"%s\"? "
+                              (if (> (length msgs) 0)
+                                  (format "%d msg(s) exists. " (length msgs))
+                                "")
+                              (elmo-folder-name-internal folder)))
+      (let ((basedir (elmo-maildir-folder-directory-internal folder)))
+       (condition-case nil
+           (let ((tmp-files (directory-files
+                             (expand-file-name "tmp" basedir)
+                             t "[^.].*")))
+             ;; Delete files in tmp.
+             (dolist (file tmp-files)
+               (delete-file file))
+             (dolist (dir '("new" "cur" "tmp" "."))
+               (setq dir (expand-file-name dir basedir))
+               (if (not (file-directory-p dir))
+                   (error nil)
+                 (elmo-delete-directory dir t))))
+         (error nil)))
+      (elmo-msgdb-delete-path folder)
+      t)))
+
+(luna-define-method elmo-folder-rename-internal ((folder elmo-maildir-folder)
+                                                new-folder)
+  (let* ((old (elmo-maildir-folder-directory-internal folder))
+        (new (elmo-maildir-folder-directory-internal new-folder))
+        (new-dir (directory-file-name (file-name-directory new))))
+    (unless (file-directory-p old)
+      (error "No such directory: %s" old))
+    (when (file-exists-p new)
+      (error "Already exists directory: %s" new))
+    (unless (file-directory-p new-dir)
+      (elmo-make-directory new-dir))
+    (rename-file old new)
+    t))
 
 (require 'product)
 (product-provide (provide 'elmo-maildir) (require 'elmo-version))