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-unmark-important (folder locations)
68 (luna-define-generic elmo-map-folder-mark-as-important (folder locations)
71 (luna-define-generic elmo-map-folder-unmark-read (folder locations)
74 (luna-define-generic elmo-map-folder-mark-as-read (folder locations)
77 (luna-define-generic elmo-map-message-fetch (folder location
84 (luna-define-generic elmo-map-folder-list-unreads (folder)
87 (luna-define-generic elmo-map-folder-list-importants (folder)
90 (luna-define-method elmo-map-folder-list-importants ((folder elmo-map-folder))
93 (luna-define-generic elmo-map-folder-delete-messages (folder locations)
96 (luna-define-method elmo-folder-status ((folder elmo-map-folder))
97 (elmo-folder-open-internal folder)
98 (elmo-folder-set-killed-list-internal
100 (elmo-msgdb-killed-list-load (elmo-folder-msgdb-path folder)))
101 (let ((numbers (mapcar
103 (elmo-map-folder-location-alist-internal folder))))
104 (setq numbers (elmo-living-messages numbers (elmo-folder-killed-list-internal folder)))
106 (cons (elmo-max-of-list numbers)
108 ;; Don't close after status.
109 (unless (elmo-folder-reserve-status-p folder)
110 (elmo-folder-close-internal folder)))))
112 (defun elmo-map-message-number (folder location)
113 "Return number of the message in the FOLDER with LOCATION."
114 (car (elmo-get-hash-val
116 (elmo-map-folder-location-hash-internal folder))))
118 (defun elmo-map-message-location (folder number)
119 "Return location of the message in the FOLDER with NUMBER."
120 (cdr (elmo-get-hash-val
121 (concat "#" (int-to-string number))
122 (elmo-map-folder-location-hash-internal folder))))
124 (luna-define-method elmo-folder-pack-number ((folder elmo-map-folder))
125 (let* ((msgdb (elmo-folder-msgdb folder))
126 (old-number-alist (elmo-msgdb-get-number-alist msgdb))
127 (old-overview (elmo-msgdb-get-overview msgdb))
128 (old-mark-alist (elmo-msgdb-get-mark-alist msgdb))
129 (old-location (elmo-map-folder-location-alist-internal folder))
130 old-number overview number-alist mark-alist location
132 (setq overview old-overview)
135 (elmo-msgdb-overview-entity-get-number (car old-overview)))
136 (elmo-msgdb-overview-entity-set-number (car old-overview) number)
138 (cons (cons number (cdr (assq old-number old-number-alist)))
140 (when (setq mark (cadr (assq old-number old-mark-alist)))
142 (elmo-msgdb-mark-append
143 mark-alist number mark)))
146 (elmo-map-message-location folder old-number))
148 (setq number (1+ number))
149 (setq old-overview (cdr old-overview)))
150 (elmo-map-folder-location-setup folder (nreverse location))
151 (elmo-folder-set-msgdb-internal
153 (elmo-make-msgdb overview
154 (nreverse number-alist)
155 (nreverse mark-alist)))))
157 (defun elmo-map-folder-location-setup (folder locations)
158 (elmo-map-folder-set-location-alist-internal
161 (elmo-map-folder-set-location-hash-internal
162 folder (elmo-make-hash
163 (* 2 (length locations))))
164 (elmo-map-folder-set-number-max-internal folder 0)
165 ;; Set number-max and hashtables.
166 (dolist (location-cons locations)
167 (if (< (elmo-map-folder-number-max-internal folder)
169 (elmo-map-folder-set-number-max-internal folder (car location-cons)))
170 (elmo-set-hash-val (cdr location-cons)
172 (elmo-map-folder-location-hash-internal folder))
173 (elmo-set-hash-val (concat "#" (int-to-string (car location-cons)))
175 (elmo-map-folder-location-hash-internal folder))))
177 (defun elmo-map-folder-update-locations (folder locations)
178 ;; A subroutine to make location-alist.
179 ;; location-alist is existing location-alist.
180 ;; locations is the newest locations.
181 (let* ((location-alist (elmo-map-folder-location-alist-internal folder))
182 (locations-in-db (mapcar 'cdr location-alist))
183 new-locs new-alist deleted-locs pair i)
185 (elmo-delete-if (function
186 (lambda (x) (member x locations-in-db)))
189 (elmo-delete-if (function
190 (lambda (x) (member x locations)))
192 (dolist (location deleted-locs)
197 (elmo-map-folder-location-hash-internal
201 (elmo-clear-hash-val (concat "#" (int-to-string (car pair)))
202 (elmo-map-folder-location-hash-internal
204 (elmo-clear-hash-val location
205 (elmo-map-folder-location-hash-internal
207 (setq i (elmo-map-folder-number-max-internal folder))
208 (dolist (location new-locs)
210 (elmo-map-folder-set-number-max-internal folder i)
211 (setq new-alist (cons (setq pair (cons i location)) new-alist))
212 (setq new-alist (nreverse new-alist))
213 (elmo-set-hash-val (concat "#" (int-to-string i))
215 (elmo-map-folder-location-hash-internal
217 (elmo-set-hash-val location
219 (elmo-map-folder-location-hash-internal
222 (sort (nconc location-alist new-alist)
223 (lambda (x y) (< (car x) (car y)))))
224 (elmo-map-folder-set-location-alist-internal folder location-alist)))
226 (luna-define-method elmo-folder-open-internal ((folder elmo-map-folder))
227 (elmo-map-folder-location-setup
229 (elmo-msgdb-location-load (elmo-folder-msgdb-path folder)))
230 (if (elmo-folder-plugged-p folder)
231 (elmo-map-folder-update-locations
233 (elmo-map-folder-list-message-locations folder))))
235 (luna-define-method elmo-folder-commit :after ((folder elmo-map-folder))
236 (when (elmo-folder-persistent-p folder)
237 (elmo-msgdb-location-save (elmo-folder-msgdb-path folder)
238 (elmo-map-folder-location-alist-internal
241 (luna-define-method elmo-folder-close-internal ((folder elmo-map-folder))
242 (elmo-map-folder-set-location-alist-internal folder nil)
243 (elmo-map-folder-set-location-hash-internal folder nil))
245 (luna-define-method elmo-folder-check ((folder elmo-map-folder))
246 (elmo-map-folder-update-locations
248 (elmo-map-folder-list-message-locations folder)))
250 (luna-define-method elmo-folder-clear :around ((folder elmo-map-folder)
251 &optional keep-killed)
253 (elmo-map-folder-set-number-max-internal folder 0)
254 (elmo-map-folder-set-location-alist-internal folder nil)
256 (elmo-map-folder-set-location-hash-internal folder (elmo-make-hash)))
257 (luna-call-next-method))
259 (luna-define-method elmo-folder-list-messages-internal
260 ((folder elmo-map-folder) &optional nohide)
261 (mapcar 'car (elmo-map-folder-location-alist-internal folder)))
263 (luna-define-method elmo-folder-unmark-important ((folder elmo-map-folder)
265 (elmo-map-folder-unmark-important
267 (elmo-map-folder-numbers-to-locations folder numbers)))
269 (luna-define-method elmo-folder-mark-as-important ((folder elmo-map-folder)
271 (elmo-map-folder-mark-as-important
273 (elmo-map-folder-numbers-to-locations folder numbers)))
275 (luna-define-method elmo-folder-unmark-read ((folder elmo-map-folder)
277 (elmo-map-folder-unmark-read
279 (elmo-map-folder-numbers-to-locations folder numbers)))
281 (luna-define-method elmo-folder-mark-as-read ((folder elmo-map-folder) numbers)
282 (elmo-map-folder-mark-as-read
284 (elmo-map-folder-numbers-to-locations folder numbers)))
286 (luna-define-method elmo-message-fetch-internal ((folder elmo-map-folder)
288 &optional section unread)
289 (elmo-map-message-fetch
291 (elmo-map-message-location folder number)
292 strategy section unread))
294 (luna-define-method elmo-folder-list-unreads-internal
295 ((folder elmo-map-folder) unread-marks &optional mark-alist)
296 (elmo-map-folder-locations-to-numbers
298 (elmo-map-folder-list-unreads folder)))
300 (luna-define-method elmo-folder-list-importants-internal
301 ((folder elmo-map-folder) important-mark)
302 (let ((locations (elmo-map-folder-list-importants folder)))
303 (if (listp locations)
304 (elmo-map-folder-locations-to-numbers folder locations)
307 (luna-define-method elmo-folder-delete-messages ((folder elmo-map-folder)
309 (elmo-map-folder-delete-messages
311 (elmo-map-folder-numbers-to-locations folder numbers))
312 (dolist (number numbers)
313 (elmo-map-folder-set-location-alist-internal
315 (delq (elmo-get-hash-val
316 (concat "#" (int-to-string number))
317 (elmo-map-folder-location-hash-internal
319 (elmo-map-folder-location-alist-internal folder))))
324 (product-provide (provide 'elmo-map) (require 'elmo-version))
326 ;;; elmo-map.el ends here