1 ;;; elmo-flag.el --- global flag handling.
3 ;; Copyright (C) 2003 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.
29 (require 'elmo-localdir)
30 (eval-when-compile (require 'cl))
33 (defcustom elmo-global-flags '(important)
34 "A list of flag symbol which is managed globally by the flag folder."
35 :type '(repeat symbol)
38 (defcustom elmo-local-flags '(unread any digest)
39 "A list of flag symbol which is not treated as global flag."
40 :type '(repeat symbol)
43 (defvar elmo-global-flag-folder-alist nil
44 "Internal variable to hold global-flag-folder structures.")
47 (luna-define-class elmo-flag-folder (elmo-localdir-folder)
48 (flag minfo minfo-hash max-number))
49 (luna-define-internal-accessors 'elmo-flag-folder))
51 (luna-define-method elmo-folder-initialize ((folder
54 (if (string-match "flag/\\([a-z]+\\)" name)
55 (setq name (match-string 1 name))
56 (setq name (symbol-name (car elmo-global-flags)))
57 (elmo-folder-set-name-internal
59 (concat (elmo-folder-name-internal folder) "/" name)))
60 (or (cdr (assq (intern name) elmo-global-flag-folder-alist))
61 (let ((flag (intern name))
63 (elmo-flag-folder-set-flag-internal folder flag)
64 (unless (elmo-global-flag-p flag)
65 (setq elmo-global-flags
66 (nconc elmo-global-flags (list flag))))
67 ;; must be AFTER set flag slot.
68 (setq msgdb-path (elmo-folder-msgdb-path folder))
69 (unless (file-directory-p msgdb-path)
70 (elmo-make-directory msgdb-path))
71 (elmo-localdir-folder-set-dir-name-internal
74 (elmo-localdir-folder-set-directory-internal
77 (if (file-exists-p (expand-file-name "max" msgdb-path))
78 (elmo-flag-folder-set-max-number-internal
80 (elmo-object-load (expand-file-name "max" msgdb-path))))
81 (if (file-exists-p (expand-file-name ".minfo" msgdb-path))
82 (elmo-flag-folder-set-minfo-internal
84 (elmo-object-load (expand-file-name ".minfo" msgdb-path))))
85 (elmo-flag-folder-set-minfo-hash-internal
87 (elmo-make-hash (length (elmo-flag-folder-minfo-internal folder))))
88 (dolist (elem (elmo-flag-folder-minfo-internal folder))
89 (elmo-set-hash-val (nth 1 elem) elem
90 (elmo-flag-folder-minfo-hash-internal folder))
91 (elmo-set-hash-val (concat "#" (number-to-string (nth 2 elem)))
93 (elmo-flag-folder-minfo-hash-internal folder))
94 (dolist (pair (car elem))
95 (elmo-set-hash-val (concat (number-to-string (cdr pair))
98 (elmo-flag-folder-minfo-hash-internal folder))))
99 (setq elmo-global-flag-folder-alist
100 (cons (cons flag folder) elmo-global-flag-folder-alist))
103 (luna-define-method elmo-folder-expand-msgdb-path ((folder elmo-flag-folder))
104 (expand-file-name (concat "flag/"
106 (elmo-flag-folder-flag-internal folder)))
107 elmo-msgdb-directory))
109 (luna-define-method elmo-folder-commit :after ((folder
112 (expand-file-name ".minfo" (elmo-folder-msgdb-path folder))
113 (elmo-flag-folder-minfo-internal folder))
114 (if (elmo-flag-folder-max-number-internal folder)
116 (expand-file-name "max" (elmo-folder-msgdb-path folder))
117 (elmo-flag-folder-max-number-internal folder))))
119 (luna-define-method elmo-folder-list-subfolders ((folder elmo-flag-folder)
121 (let ((dir (expand-file-name "flag" elmo-msgdb-directory)))
122 (mapcar (lambda (flag)
124 (elmo-folder-prefix-internal folder)
125 (symbol-name (elmo-folder-type-internal folder))
130 (mapcar 'intern (delete ".." (delete "." (directory-files dir))))
131 elmo-global-flags)))))
133 (defun elmo-flag-folder-delete-message (folder number
134 &optional keep-referrer)
135 (let* ((elem (elmo-get-hash-val (concat "#" (number-to-string number))
136 (elmo-flag-folder-minfo-hash-internal
139 (dolist (pair (car elem))
140 (when (and (car pair) (cdr pair))
141 (elmo-clear-hash-val (concat (number-to-string (cdr pair)) ":"
143 (elmo-flag-folder-minfo-hash-internal
145 (unless keep-referrer
146 (setq target-folder (elmo-make-folder (car pair)))
147 (elmo-folder-open target-folder 'load-msgdb)
148 ;; Unset the flag of the original folder.
149 ;; (XXX Should the message-id checked?)
150 (elmo-message-unset-flag target-folder (cdr pair)
151 (elmo-flag-folder-flag-internal folder))
152 (elmo-folder-close target-folder))))
153 (elmo-clear-hash-val (concat "#" (number-to-string number))
154 (elmo-flag-folder-minfo-hash-internal
156 (elmo-clear-hash-val (nth 1 elem) (elmo-flag-folder-minfo-hash-internal
158 (elmo-flag-folder-set-minfo-internal
160 (delq elem (elmo-flag-folder-minfo-internal folder))))
163 (luna-define-method elmo-folder-delete-messages-internal ((folder
166 (dolist (number numbers)
167 (elmo-flag-folder-delete-message folder number)
168 (elmo-localdir-delete-message folder number))
169 (elmo-folder-commit folder)
172 ;; Same as localdir except that the flag is always the flag.
173 (luna-define-method elmo-folder-msgdb-create ((folder elmo-flag-folder)
177 (let ((dir (elmo-localdir-folder-directory-internal folder))
178 (new-msgdb (elmo-make-msgdb))
180 (len (length numbers)))
181 (message "Creating msgdb...")
183 (when (setq entity (elmo-localdir-msgdb-create-entity
184 new-msgdb dir (car numbers)))
185 (elmo-msgdb-append-entity new-msgdb entity
186 (list (elmo-flag-folder-flag-internal
188 (when (> len elmo-display-progress-threshold)
190 (elmo-display-progress
191 'elmo-flag-folder-msgdb-create "Creating msgdb..."
193 (setq numbers (cdr numbers)))
194 (message "Creating msgdb...done")
197 (luna-define-method elmo-folder-append-messages ((folder elmo-flag-folder)
200 &optional same-number)
201 (dolist (number numbers)
202 (elmo-global-flag-set (elmo-flag-folder-flag-internal folder)
203 src-folder number (elmo-message-field
207 (elmo-folder-set-flag src-folder
209 (elmo-flag-folder-flag-internal folder))
212 (luna-define-method elmo-folder-append-buffer ((folder elmo-flag-folder)
213 &optional flag number)
214 (error "Cannot append to the flag folder"))
216 (luna-define-method elmo-folder-unset-flag :before ((folder elmo-flag-folder)
220 (when (eq flag (elmo-flag-folder-flag-internal folder))
221 (error "Cannot unset flag `%s' in this folder." flag)))
225 (defmacro elmo-flag-get-folder (flag)
226 "Get the flag folder structure for FLAG."
227 `(when (memq ,flag elmo-global-flags)
228 (elmo-make-folder (concat "'flag/" (symbol-name ,flag)))))
230 (defun elmo-flag-folder-referrer (folder number)
231 "Return a list of referrer message information.
232 Each element is a cons cell like following:
234 FNAME is the name of the folder which the message is contained.
235 NUMBER is the number of the message."
236 (when (eq (elmo-folder-type-internal folder) 'flag)
237 (car (elmo-get-hash-val (concat "#" (number-to-string number))
238 (elmo-flag-folder-minfo-hash-internal
242 (defun elmo-global-flag-p (flag)
243 "Return non-nil when FLAG is global."
244 (memq flag elmo-global-flags))
246 (defun elmo-global-flags (fname number)
247 "Return a list of global flags for the message.
248 FNAME is the name string of the folder.
249 NUMBER is the number of the message."
250 (let ((flag-list elmo-global-flags)
253 (setq folder (elmo-flag-get-folder (car flag-list)))
254 (when (elmo-get-hash-val
255 (concat (number-to-string number) ":" fname)
256 (elmo-flag-folder-minfo-hash-internal folder))
257 (setq matches (cons (elmo-flag-folder-flag-internal folder)
259 (setq flag-list (cdr flag-list)))
262 (defun elmo-folder-list-global-flag-messages (folder flag)
263 "List messages which have global flag.
264 FOLDER is the elmo folder structure.
265 FLAG is the symbol of the flag."
266 (when (elmo-global-flag-p flag)
267 (let ((flag-folder (elmo-flag-get-folder flag))
269 (dolist (elem (elmo-flag-folder-minfo-internal flag-folder))
270 (if (setq number (elmo-message-number folder (nth 1 elem)))
271 (setq result (cons number result))))
275 ;; minfo is a list of following cell.
276 ;; ((((FNAME . NUMBER)...(FNAME . NUMBER)) MESSAGE-ID NUMBER-IN-FLAG-FOLDER)
277 ;; minfo-index is the hash table of above with following indice;
278 (defun elmo-global-flags-set (flags folder number message-id)
279 "Set global flags to the message.
280 FLAGS is a list of symbol of the flag.
281 FOLDER is the elmo folder structure.
282 NUMBER is the message number."
284 (elmo-global-flag-set flag folder number message-id)))
286 (defun elmo-local-flag-p (flag)
287 "Return non-nil when flag is not appropriate for global flag."
288 (memq flag elmo-local-flags))
290 (defsubst elmo-global-flag-set-internal (flag folder number message-id)
291 (when (elmo-local-flag-p flag)
292 (error "Cannot treat `%s' as global flag." flag))
294 (let ((flag-folder (elmo-flag-get-folder flag))
295 cache new-file new-number elem)
296 (if (setq elem (elmo-get-hash-val
298 (elmo-flag-folder-minfo-hash-internal
300 ;; Same ID already exists.
301 (when (and folder number
302 (not (member (cons (elmo-folder-name-internal folder)
303 number) (car elem))))
305 (cons (cons (elmo-folder-name-internal folder)
307 (setq new-number (nth 2 elem))
308 (elmo-set-hash-val (concat (number-to-string number)
309 ":" (elmo-folder-name-internal
312 (elmo-flag-folder-minfo-hash-internal
314 ;; Append new element.
315 (elmo-flag-folder-set-max-number-internal
317 (+ (or (elmo-flag-folder-max-number-internal flag-folder)
318 ;; This is the first time.
319 (car (elmo-folder-status flag-folder)))
325 (elmo-flag-folder-max-number-internal flag-folder)))
326 (elmo-localdir-folder-directory-internal flag-folder)))
328 (setq cache (and message-id (elmo-file-cache-get message-id)))
329 (if (and cache (eq (elmo-file-cache-status cache) 'entire))
330 (elmo-copy-file (elmo-file-cache-path cache)
332 (when (and folder number)
333 (elmo-message-fetch folder number
334 (elmo-make-fetch-strategy 'entire))
335 (write-region-as-binary (point-min) (point-max) new-file nil
337 (elmo-flag-folder-set-minfo-internal
341 (when (and folder number)
342 (list (cons (elmo-folder-name-internal folder)
346 (elmo-flag-folder-minfo-internal flag-folder)))
347 (when (and folder number)
348 (elmo-set-hash-val (concat (number-to-string number)
349 ":" (elmo-folder-name-internal
352 (elmo-flag-folder-minfo-hash-internal
354 (elmo-set-hash-val message-id elem
355 (elmo-flag-folder-minfo-hash-internal
357 (elmo-set-hash-val (concat "#" (number-to-string new-number)) elem
358 (elmo-flag-folder-minfo-hash-internal
360 (elmo-folder-commit flag-folder)
363 (defun elmo-global-flag-set (flag folder number message-id)
364 "Set global flag to the message.
365 FLAG is a symbol of the flag.
366 FOLDER is the elmo folder structure.
367 NUMBER is the message number.
368 MESSAGE-ID is the message-id of the message."
369 (when (elmo-global-flag-p flag)
370 (elmo-global-flag-set-internal flag folder number message-id)))
372 (defun elmo-global-flag-detach (flag folder number &optional delete-if-none)
373 "Detach the message from the global flag.
374 FOLDER is the folder structure.
375 NUMBERS is the message number.
376 If optional DELETE-IF-NONE is non-nil, delete message from flag folder when
377 the message is not flagged in any folder.
378 If DELETE-IF-NONE is a symbol `always',
379 delete message without flagged in other folder."
380 (unless (and (eq (elmo-folder-type-internal folder) 'flag)
381 (eq (elmo-flag-folder-flag-internal folder) flag))
382 (let ((flag-folder (elmo-flag-get-folder flag))
385 (setq key (concat (number-to-string number) ":"
386 (elmo-folder-name-internal folder))
387 elem (elmo-get-hash-val
389 (elmo-flag-folder-minfo-hash-internal flag-folder)))
391 (setcar elem (delete (cons (elmo-folder-name-internal folder)
393 (elmo-clear-hash-val key (elmo-flag-folder-minfo-hash-internal
395 ;; Does not have any referrer, remove.
396 (when (and delete-if-none
397 (or (eq delete-if-none 'always)
399 (elmo-flag-folder-delete-message flag-folder (nth 2 elem)
401 (elmo-localdir-delete-message flag-folder (nth 2 elem))
402 (elmo-folder-commit flag-folder)))))))
404 (defun elmo-global-flag-detach-messages (folder numbers &optional
406 "Detach all messages specified from all global flags.
407 FOLDER is the folder structure.
408 NUMBERS is the message number list.
409 If optional DELETE-IF-NONE is non-nil, delete message from flag folder when
410 the message is not flagged in any folder."
411 (unless (eq (elmo-folder-type-internal folder) 'flag)
412 (dolist (flag elmo-global-flags)
413 (dolist (number numbers)
414 (elmo-global-flag-detach flag folder number delete-if-none)))))
416 (defun elmo-get-global-flags (&optional flags ignore-preserved)
418 Return value is a subset of optional argument FLAGS.
419 If FLAGS is `t', all global flags becomes candidates.
420 If optional IGNORE-PRESERVED is non-nil, preserved flags
421 \(answered, cached, new, unread\) are not included."
422 (let ((result (copy-sequence (if (eq flags t)
423 (setq flags elmo-global-flags)
426 (unless (elmo-global-flag-p (car flags))
427 (setq result (delq (car flags) result)))
428 (setq flags (cdr flags)))
429 (when ignore-preserved
430 (dolist (flag elmo-preserved-flags)
431 (setq result (delq flag result))))
434 ;;; To migrate from global mark folder
435 (defvar elmo-global-mark-filename "global-mark"
436 "Obsolete variable. (Just for migration)")
438 (defun elmo-global-mark-migrate ()
439 "Migrate from 'mark to 'flag. For automatic migration."
440 (when (and (file-exists-p (expand-file-name elmo-global-mark-filename
441 elmo-msgdb-directory))
442 (elmo-global-flag-p 'important)
443 (not (file-exists-p (elmo-folder-expand-msgdb-path
444 (elmo-flag-get-folder 'important)))))
445 (elmo-global-mark-upgrade)))
447 (defun elmo-global-mark-upgrade ()
448 "Upgrade old `global-mark' structure."
450 (when (file-exists-p (expand-file-name
451 elmo-global-mark-filename elmo-msgdb-directory))
452 (message "Upgrading flag structure...")
453 (when (elmo-global-flag-p 'important)
457 elmo-global-mark-filename elmo-msgdb-directory)))
458 (folder (elmo-flag-get-folder 'important))
460 (dolist (elem global-marks)
461 (setq file-cache (elmo-file-cache-get (car elem)))
462 (when (eq (elmo-file-cache-status file-cache) 'entire)
463 (elmo-global-flag-set 'important nil nil (car elem))))))
464 (message "Upgrading flag structure...done")))
466 (luna-define-method elmo-folder-delete :around ((folder elmo-flag-folder))
467 (let ((flag (elmo-flag-folder-flag-internal folder)))
468 (when (luna-call-next-method)
469 (setq elmo-global-flags (delq flag elmo-global-flags))
473 (product-provide (provide 'elmo-flag) (require 'elmo-version))
475 ;;; elmo-flag.el ends here