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-folder-unmark-answered (folder locations)
80 (luna-define-generic elmo-map-folder-mark-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-number ((folder elmo-map-folder))
140 (let* ((msgdb (elmo-folder-msgdb folder))
141 (old-number-alist (elmo-msgdb-get-number-alist msgdb))
142 (old-overview (elmo-msgdb-get-overview msgdb))
143 (old-mark-alist (elmo-msgdb-get-mark-alist msgdb))
144 (old-location (elmo-map-folder-location-alist-internal folder))
145 old-number overview number-alist mark-alist location
147 (setq overview old-overview)
150 (elmo-msgdb-overview-entity-get-number (car old-overview)))
151 (elmo-msgdb-overview-entity-set-number (car old-overview) number)
153 (cons (cons number (cdr (assq old-number old-number-alist)))
155 (when (setq mark (cadr (assq old-number old-mark-alist)))
157 (elmo-msgdb-mark-append
158 mark-alist number mark)))
161 (elmo-map-message-location folder old-number))
163 (setq number (1+ number))
164 (setq old-overview (cdr old-overview)))
165 (elmo-map-folder-location-setup folder (nreverse location))
166 (elmo-folder-set-msgdb-internal
168 (elmo-make-msgdb overview
169 (nreverse number-alist)
170 (nreverse mark-alist)))))
172 (defun elmo-map-folder-location-setup (folder locations)
173 (elmo-map-folder-set-location-alist-internal
176 (elmo-map-folder-set-location-hash-internal
177 folder (elmo-make-hash
178 (* 2 (length locations))))
179 (elmo-map-folder-set-number-max-internal folder 0)
180 ;; Set number-max and hashtables.
181 (dolist (location-cons locations)
182 (if (< (elmo-map-folder-number-max-internal folder)
184 (elmo-map-folder-set-number-max-internal folder (car location-cons)))
185 (elmo-set-hash-val (cdr location-cons)
187 (elmo-map-folder-location-hash-internal folder))
188 (elmo-set-hash-val (concat "#" (int-to-string (car location-cons)))
190 (elmo-map-folder-location-hash-internal folder))))
192 (defun elmo-map-folder-update-locations (folder locations)
193 ;; A subroutine to make location-alist.
194 ;; location-alist is existing location-alist.
195 ;; locations is the newest locations.
196 (let* ((location-alist (elmo-map-folder-location-alist-internal folder))
197 (locations-in-db (mapcar 'cdr location-alist))
198 new-locs new-alist deleted-locs pair i)
200 (elmo-delete-if (function
201 (lambda (x) (member x locations-in-db)))
204 (elmo-delete-if (function
205 (lambda (x) (member x locations)))
207 (dolist (location deleted-locs)
212 (elmo-map-folder-location-hash-internal
216 (elmo-clear-hash-val (concat "#" (int-to-string (car pair)))
217 (elmo-map-folder-location-hash-internal
219 (elmo-clear-hash-val location
220 (elmo-map-folder-location-hash-internal
222 (setq i (elmo-map-folder-number-max-internal folder))
223 (dolist (location new-locs)
225 (elmo-map-folder-set-number-max-internal folder i)
226 (setq new-alist (cons (setq pair (cons i location)) new-alist))
227 (setq new-alist (nreverse new-alist))
228 (elmo-set-hash-val (concat "#" (int-to-string i))
230 (elmo-map-folder-location-hash-internal
232 (elmo-set-hash-val location
234 (elmo-map-folder-location-hash-internal
237 (sort (nconc location-alist new-alist)
238 (lambda (x y) (< (car x) (car y)))))
239 (elmo-map-folder-set-location-alist-internal folder location-alist)))
241 (luna-define-method elmo-folder-open-internal ((folder elmo-map-folder))
242 (elmo-map-folder-location-setup
244 (elmo-msgdb-location-load (elmo-folder-msgdb-path folder)))
245 (if (elmo-folder-plugged-p folder)
246 (elmo-map-folder-update-locations
248 (elmo-map-folder-list-message-locations folder))))
250 (luna-define-method elmo-folder-commit :after ((folder elmo-map-folder))
251 (when (elmo-folder-persistent-p folder)
252 (elmo-msgdb-location-save (elmo-folder-msgdb-path folder)
253 (elmo-map-folder-location-alist-internal
256 (luna-define-method elmo-folder-close-internal ((folder elmo-map-folder))
257 (elmo-map-folder-set-location-alist-internal folder nil)
258 (elmo-map-folder-set-location-hash-internal folder nil))
260 (luna-define-method elmo-folder-check ((folder elmo-map-folder))
261 (elmo-map-folder-update-locations
263 (elmo-map-folder-list-message-locations folder)))
265 (luna-define-method elmo-folder-clear :around ((folder elmo-map-folder)
266 &optional keep-killed)
268 (elmo-map-folder-set-number-max-internal folder 0)
269 (elmo-map-folder-set-location-alist-internal folder nil)
271 (elmo-map-folder-set-location-hash-internal folder (elmo-make-hash)))
272 (luna-call-next-method))
274 (luna-define-method elmo-folder-list-messages-internal
275 ((folder elmo-map-folder) &optional nohide)
276 (mapcar 'car (elmo-map-folder-location-alist-internal folder)))
278 (luna-define-method elmo-folder-unmark-important :before ((folder
281 (elmo-map-folder-unmark-important
283 (elmo-map-folder-numbers-to-locations folder numbers)))
285 (luna-define-method elmo-folder-mark-as-important :before ((folder
288 (elmo-map-folder-mark-as-important
290 (elmo-map-folder-numbers-to-locations folder numbers)))
292 (luna-define-method elmo-folder-unmark-read :before ((folder elmo-map-folder)
294 &optional ignore-flags)
296 (elmo-map-folder-unmark-read
298 (elmo-map-folder-numbers-to-locations folder numbers))))
300 (luna-define-method elmo-folder-mark-as-read :before ((folder
303 &optional ignore-flags)
305 (elmo-map-folder-mark-as-read
307 (elmo-map-folder-numbers-to-locations folder numbers))))
309 (luna-define-method elmo-folder-unmark-answered :before ((folder
312 (elmo-map-folder-unmark-answered
314 (elmo-map-folder-numbers-to-locations folder numbers)))
316 (luna-define-method elmo-folder-mark-as-answered :before ((folder
319 (elmo-map-folder-mark-as-answered
321 (elmo-map-folder-numbers-to-locations folder numbers)))
323 (luna-define-method elmo-message-fetch-internal ((folder elmo-map-folder)
325 &optional section unread)
326 (elmo-map-message-fetch
328 (elmo-map-message-location folder number)
329 strategy section unread))
331 (luna-define-method elmo-folder-list-unreads :around ((folder elmo-map-folder))
332 (let ((locations (elmo-map-folder-list-unreads folder)))
333 (if (listp locations)
334 (elmo-map-folder-locations-to-numbers folder locations)
335 (luna-call-next-method))))
337 (luna-define-method elmo-folder-list-importants :around ((folder
339 (let ((locations (elmo-map-folder-list-importants folder)))
340 (if (listp locations)
341 (elmo-map-folder-locations-to-numbers folder locations)
342 (luna-call-next-method))))
344 (luna-define-method elmo-folder-list-answereds :around ((folder
346 (let ((locations (elmo-map-folder-list-answereds folder)))
347 (if (listp locations)
348 (elmo-map-folder-locations-to-numbers folder locations)
349 (luna-call-next-method))))
351 (luna-define-method elmo-folder-delete-messages ((folder elmo-map-folder)
353 (elmo-map-folder-delete-messages
355 (elmo-map-folder-numbers-to-locations folder numbers))
356 (dolist (number numbers)
357 (elmo-map-folder-set-location-alist-internal
359 (delq (elmo-get-hash-val
360 (concat "#" (int-to-string number))
361 (elmo-map-folder-location-hash-internal
363 (elmo-map-folder-location-alist-internal folder))))
367 (product-provide (provide 'elmo-map) (require 'elmo-version))
369 ;;; elmo-map.el ends here