1 ;;; elmo-map.el --- A ELMO folder class with message number mapping.
3 ;; Copyright (C) 2000 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.
27 ;; Folders which do not have unique message numbers but unique message names
28 ;; should inherit this folder.
35 (eval-when-compile (require 'cl))
38 ;; location-hash: location->number mapping
39 ;; number-hash: number->location mapping
40 (luna-define-class elmo-map-folder (elmo-folder)
41 (location-alist number-max location-hash))
42 (luna-define-internal-accessors 'elmo-map-folder))
44 (defun elmo-map-folder-numbers-to-locations (folder numbers)
46 (dolist (number numbers)
47 (if (setq pair (elmo-get-hash-val
48 (concat "#" (int-to-string number))
49 (elmo-map-folder-location-hash-internal folder)))
50 (setq locations (cons (cdr pair) locations))))
51 (nreverse locations)))
53 (defun elmo-map-folder-locations-to-numbers (folder locations)
55 (dolist (location locations)
56 (if (setq pair (elmo-get-hash-val
58 (elmo-map-folder-location-hash-internal folder)))
59 (setq numbers (cons (car pair) numbers))))
62 (luna-define-generic elmo-map-folder-list-message-locations (folder)
63 "Return a location list of the FOLDER.")
65 (luna-define-generic elmo-map-folder-unflag-important (folder locations)
68 (luna-define-generic elmo-map-folder-flag-as-important (folder locations)
71 (luna-define-generic elmo-map-folder-unflag-read (folder locations)
74 (luna-define-generic elmo-map-folder-flag-as-read (folder locations)
77 (luna-define-generic elmo-map-folder-unflag-answered (folder locations)
80 (luna-define-generic elmo-map-folder-flag-as-answered (folder locations)
83 (luna-define-generic elmo-map-message-fetch (folder location
90 (luna-define-generic elmo-map-folder-list-unreads (folder)
93 (luna-define-method elmo-map-folder-list-unreads ((folder elmo-map-folder))
96 (luna-define-generic elmo-map-folder-list-importants (folder)
99 (luna-define-method elmo-map-folder-list-importants ((folder elmo-map-folder))
102 (luna-define-generic elmo-map-folder-list-answereds (folder)
105 (luna-define-method elmo-map-folder-list-answereds ((folder elmo-map-folder))
108 (luna-define-generic elmo-map-folder-delete-messages (folder locations)
111 (luna-define-method elmo-folder-status ((folder elmo-map-folder))
112 (elmo-folder-open-internal folder)
113 (elmo-folder-set-killed-list-internal
115 (elmo-msgdb-killed-list-load (elmo-folder-msgdb-path folder)))
116 (let ((numbers (mapcar
118 (elmo-map-folder-location-alist-internal folder))))
119 (setq numbers (elmo-living-messages numbers (elmo-folder-killed-list-internal folder)))
121 (cons (elmo-max-of-list numbers)
123 ;; Don't close after status.
124 (unless (elmo-folder-reserve-status-p folder)
125 (elmo-folder-close-internal folder)))))
127 (defun elmo-map-message-number (folder location)
128 "Return number of the message in the FOLDER with LOCATION."
129 (car (elmo-get-hash-val
131 (elmo-map-folder-location-hash-internal folder))))
133 (defun elmo-map-message-location (folder number)
134 "Return location of the message in the FOLDER with NUMBER."
135 (cdr (elmo-get-hash-val
136 (concat "#" (int-to-string number))
137 (elmo-map-folder-location-hash-internal folder))))
139 (luna-define-method elmo-folder-pack-numbers ((folder elmo-map-folder))
140 (let* ((msgdb (elmo-folder-msgdb folder))
141 (numbers (sort (elmo-folder-list-messages folder nil 'in-msgdb) '<))
142 (new-msgdb (elmo-make-msgdb (elmo-folder-msgdb-path folder)))
144 total location entity)
145 (setq total (length numbers))
146 (elmo-with-progress-display (> total elmo-display-progress-threshold)
147 (elmo-folder-pack-numbers total "Packing...")
148 (dolist (old-number numbers)
149 (setq entity (elmo-msgdb-message-entity msgdb old-number))
150 (elmo-msgdb-overview-entity-set-number entity number)
151 (elmo-msgdb-append-entity new-msgdb entity
152 (elmo-msgdb-flags msgdb old-number))
155 (elmo-map-message-location folder old-number))
157 (setq number (1+ number))))
158 (message "Packing...done")
159 (elmo-map-folder-location-setup folder (nreverse location))
160 (elmo-folder-set-msgdb-internal folder new-msgdb)))
162 (defun elmo-map-folder-location-setup (folder locations)
163 (elmo-map-folder-set-location-alist-internal
166 (elmo-map-folder-set-location-hash-internal
167 folder (elmo-make-hash
168 (* 2 (length locations))))
169 (elmo-map-folder-set-number-max-internal folder 0)
170 ;; Set number-max and hashtables.
171 (dolist (location-cons locations)
172 (if (< (elmo-map-folder-number-max-internal folder)
174 (elmo-map-folder-set-number-max-internal folder (car location-cons)))
175 (elmo-set-hash-val (cdr location-cons)
177 (elmo-map-folder-location-hash-internal folder))
178 (elmo-set-hash-val (concat "#" (int-to-string (car location-cons)))
180 (elmo-map-folder-location-hash-internal folder))))
182 (defun elmo-map-folder-update-locations (folder locations)
183 ;; A subroutine to make location-alist.
184 ;; location-alist is existing location-alist.
185 ;; locations is the newest locations.
186 (let* ((location-alist (elmo-map-folder-location-alist-internal folder))
187 (locations-in-db (mapcar 'cdr location-alist))
188 new-locs new-alist deleted-locs pair i)
190 (elmo-delete-if (function
191 (lambda (x) (member x locations-in-db)))
194 (elmo-delete-if (function
195 (lambda (x) (member x locations)))
197 (dolist (location deleted-locs)
202 (elmo-map-folder-location-hash-internal
206 (elmo-clear-hash-val (concat "#" (int-to-string (car pair)))
207 (elmo-map-folder-location-hash-internal
209 (elmo-clear-hash-val location
210 (elmo-map-folder-location-hash-internal
212 (setq i (elmo-map-folder-number-max-internal folder))
213 (dolist (location new-locs)
215 (elmo-map-folder-set-number-max-internal folder i)
216 (setq new-alist (cons (setq pair (cons i location)) new-alist))
217 (setq new-alist (nreverse new-alist))
218 (elmo-set-hash-val (concat "#" (int-to-string i))
220 (elmo-map-folder-location-hash-internal
222 (elmo-set-hash-val location
224 (elmo-map-folder-location-hash-internal
227 (sort (nconc location-alist new-alist)
228 (lambda (x y) (< (car x) (car y)))))
229 (elmo-map-folder-set-location-alist-internal folder location-alist)))
231 (luna-define-method elmo-folder-open-internal ((folder elmo-map-folder))
232 (elmo-map-folder-location-setup
234 (elmo-msgdb-location-load (elmo-folder-msgdb-path folder)))
235 (if (elmo-folder-plugged-p folder)
236 (elmo-map-folder-update-locations
238 (elmo-map-folder-list-message-locations folder))))
240 (luna-define-method elmo-folder-commit :after ((folder elmo-map-folder))
241 (when (elmo-folder-persistent-p folder)
242 (elmo-msgdb-location-save (elmo-folder-msgdb-path folder)
243 (elmo-map-folder-location-alist-internal
246 (luna-define-method elmo-folder-close-internal ((folder elmo-map-folder))
247 (elmo-map-folder-set-location-alist-internal folder nil)
248 (elmo-map-folder-set-location-hash-internal folder nil))
250 (luna-define-method elmo-folder-check ((folder elmo-map-folder))
251 (elmo-map-folder-update-locations
253 (elmo-map-folder-list-message-locations folder)))
255 (luna-define-method elmo-folder-clear :around ((folder elmo-map-folder)
256 &optional keep-killed)
258 (elmo-map-folder-set-number-max-internal folder 0)
259 (elmo-map-folder-set-location-alist-internal folder nil)
261 (elmo-map-folder-set-location-hash-internal folder (elmo-make-hash)))
262 (luna-call-next-method))
264 (luna-define-method elmo-folder-list-messages-internal
265 ((folder elmo-map-folder) &optional nohide)
266 (mapcar 'car (elmo-map-folder-location-alist-internal folder)))
268 (luna-define-method elmo-folder-unflag-important :before ((folder
274 (elmo-map-folder-unflag-important
276 (elmo-map-folder-numbers-to-locations folder numbers))))
278 (luna-define-method elmo-folder-flag-as-important :before ((folder
284 (elmo-map-folder-flag-as-important
286 (elmo-map-folder-numbers-to-locations folder numbers))))
288 (luna-define-method elmo-folder-unflag-read :before ((folder elmo-map-folder)
292 (elmo-map-folder-unflag-read
294 (elmo-map-folder-numbers-to-locations folder numbers))))
296 (luna-define-method elmo-folder-flag-as-read :before ((folder
301 (elmo-map-folder-flag-as-read
303 (elmo-map-folder-numbers-to-locations folder numbers))))
305 (luna-define-method elmo-folder-unflag-answered :before ((folder
309 (elmo-map-folder-unflag-answered
311 (elmo-map-folder-numbers-to-locations folder numbers)))
313 (luna-define-method elmo-folder-flag-as-answered :before ((folder
317 (elmo-map-folder-flag-as-answered
319 (elmo-map-folder-numbers-to-locations folder numbers)))
321 (luna-define-method elmo-message-fetch-internal ((folder elmo-map-folder)
323 &optional section unread)
324 (elmo-map-message-fetch
326 (elmo-map-message-location folder number)
327 strategy section unread))
329 (luna-define-method elmo-folder-list-unreads :around ((folder elmo-map-folder))
330 (let ((locations (elmo-map-folder-list-unreads folder)))
331 (if (listp locations)
332 (elmo-map-folder-locations-to-numbers folder locations)
333 (luna-call-next-method))))
335 (luna-define-method elmo-folder-list-importants :around ((folder
337 (let ((locations (elmo-map-folder-list-importants folder)))
338 (if (listp locations)
339 (elmo-map-folder-locations-to-numbers folder locations)
340 (luna-call-next-method))))
342 (luna-define-method elmo-folder-list-answereds :around ((folder
344 (let ((locations (elmo-map-folder-list-answereds folder)))
345 (if (listp locations)
346 (elmo-map-folder-locations-to-numbers folder locations)
347 (luna-call-next-method))))
349 (luna-define-method elmo-folder-delete-messages ((folder elmo-map-folder)
351 (elmo-map-folder-delete-messages
353 (elmo-map-folder-numbers-to-locations folder numbers))
354 (dolist (number numbers)
355 (elmo-map-folder-set-location-alist-internal
357 (delq (elmo-get-hash-val
358 (concat "#" (int-to-string number))
359 (elmo-map-folder-location-hash-internal
361 (elmo-map-folder-location-alist-internal folder))))
365 (product-provide (provide 'elmo-map) (require 'elmo-version))
367 ;;; elmo-map.el ends here