* wl-summary.el (wl-summary-detect-mark-position): Use generic msgdb
[elisp/wanderlust.git] / elmo / modb.el
1 ;;; modb.el --- Message Orchestration DataBase.
2
3 ;; Copyright (C) 2003 Yuuichi Teranishi <teranisi@gohome.org>
4
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;;      Hiroya Murata <lapis-lazuli@pop06.odn.ne.jp>
7 ;; Keywords: mail, net news
8
9 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15 ;;
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20 ;;
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25 ;;
26
27 ;;; Commentary:
28 ;;
29
30 ;;; Code:
31 ;;
32
33 (eval-when-compile (require 'cl))
34
35 (require 'luna)
36
37 (eval-and-compile
38   (luna-define-class modb-generic () (location         ; location for save.
39                                       message-modified ; message is modified.
40                                       flag-modified    ; flag is modified.
41                                       ))
42   (luna-define-internal-accessors 'modb-generic))
43
44 (luna-define-generic elmo-msgdb-load (msgdb)
45   "Load the MSGDB.")
46
47 (luna-define-generic elmo-msgdb-save (msgdb)
48   "Save the MSGDB.")
49
50 (luna-define-generic elmo-msgdb-location (msgdb)
51   "Return the location of MSGDB.")
52
53 (luna-define-generic elmo-msgdb-message-modified-p (msgdb)
54   "Return non-nil if message is modified.")
55
56 (luna-define-generic elmo-msgdb-flag-modified-p (msgdb)
57   "Return non-nil if flag is modified.")
58
59 (luna-define-generic elmo-msgdb-append (msgdb msgdb-append)
60   "Append the MSGDB-APPEND to the MSGDB.
61 Return a list of messages which have duplicated message-id.")
62
63 (luna-define-generic elmo-msgdb-clear (msgdb)
64   "Clear the MSGDB structure.")
65
66 (luna-define-generic elmo-msgdb-length (msgdb)
67   "Return number of messages in the MSGDB")
68
69 (luna-define-generic elmo-msgdb-flags (msgdb number)
70   "Return a list of flag which corresponds to the message with NUMBER.")
71
72 (luna-define-generic elmo-msgdb-set-flag (msgdb number flag)
73   "Set message flag.
74 MSGDB is the ELMO msgdb.
75 NUMBER is a message number to set flag.
76 FLAG is a symbol which is one of the following:
77 `new'       ... Message which is new.
78 `read'      ... Message which is already read.
79 `important' ... Message which is important.
80 `answered'  ... Message which is answered.
81 `cached'    ... Message which is cached.")
82
83 (luna-define-generic elmo-msgdb-unset-flag (msgdb number flag)
84   "Unset message flag.
85 MSGDB is the ELMO msgdb.
86 NUMBER is a message number to set flag.
87 FLAG is a symbol which is one of the following:
88 `new'       ... Message which is new.
89 `read'      ... Message which is already read.
90 `important' ... Message which is important.
91 `answered'  ... Message which is answered.
92 `cached'    ... Message which is cached.")
93
94 (luna-define-generic elmo-msgdb-list-messages (msgdb)
95   "Return a list of message numbers in the MSGDB.")
96
97 (luna-define-generic elmo-msgdb-list-flagged (msgdb flag)
98   "Return a list of message numbers which is set FLAG in the MSGDB.")
99
100 ;;; (luna-define-generic elmo-msgdb-search (msgdb condition &optional numbers)
101 ;;;   "Search and return list of message numbers.
102 ;;; MSGDB is the ELMO msgdb structure.
103 ;;; CONDITION is a condition structure for searching.
104 ;;; If optional argument NUMBERS is specified and is a list of message numbers,
105 ;;; messages are searched from the list.")
106
107 (luna-define-generic elmo-msgdb-append-entity (msgdb entity &optional flags)
108   "Append a ENTITY with FLAGS into the MSGDB.
109 Return non-nil if message-id of entity is duplicated.")
110
111 (luna-define-generic elmo-msgdb-delete-messages (msgdb numbers)
112   "Delete messages which are contained NUMBERS from MSGDB.")
113
114 (luna-define-generic elmo-msgdb-sort-entities (msgdb predicate
115                                                      &optional app-data)
116   "Sort entities of MSGDB, comparing with PREDICATE.
117 PREDICATE is called with two entities and APP-DATA.
118 Should return non-nil if the first entity is \"less\" than the second.")
119
120 (luna-define-generic elmo-msgdb-message-entity (msgdb key)
121   "Return the message-entity structure which matches to the KEY.
122 KEY is a number or a string.
123 A number is for message number in the MSGDB.
124 A string is for message-id of the message.")
125
126 ;; Message entity handling.
127 (defvar modb-cache-internal nil)
128 (defun elmo-message-entity-db (entity)
129   "Get modb instance which corresponds to the ENTITY."
130   (if (or (null (car entity))
131           (stringp (car entity)))
132       ;; Transitional implementation for modb-legacy.
133       (or modb-cache-internal
134           (progn
135             (require 'modb-legacy)
136             (setq modb-cache-internal (luna-make-entity 'modb-legacy))))
137     ;; XXX Next generation entity structure...not decided yet.
138     (car entity)))
139
140 (luna-define-generic elmo-msgdb-make-message-entity (msgdb
141                                                      &rest args)
142   "Make a message entity for MSGDB.")
143
144 (luna-define-generic elmo-msgdb-message-entity-number (msgdb entity)
145   "Number of the ENTITY.")
146
147 (luna-define-generic elmo-msgdb-message-entity-set-number (msgdb entity number)
148   "Set number of the ENTITY.")
149
150 (luna-define-generic elmo-msgdb-message-entity-field (msgdb
151                                                       entity field
152                                                       &optional decode)
153   "Retrieve field value of the message entity.
154 MSGDB is the msgdb structure.
155 ENTITY is the message entity structure.
156 FIELD is a symbol of the field.
157 If optional DECODE is no-nil, the field value is decoded.")
158
159 (luna-define-generic elmo-msgdb-message-entity-set-field (msgdb
160                                                           entity field value)
161   "Set the field value of the message entity.
162 MSGDB is the msgdb structure.
163 ENTITY is the message entity structure.
164 FIELD is a symbol of the field.
165 VALUE is the field value to set.")
166
167 (luna-define-generic elmo-msgdb-copy-message-entity (msgdb entity)
168   "Copy message entity.
169 MSGDB is the msgdb structure.
170 ENTITY is the message entity structure.")
171
172 (luna-define-generic elmo-msgdb-create-message-entity-from-file (msgdb number
173                                                                        file)
174   "Create message entity from file.
175 MSGDB is the msgdb structure.
176 NUMBER is the number of the newly created message entity.
177 FILE is the message file.")
178
179 (luna-define-generic elmo-msgdb-create-message-entity-from-buffer (msgdb
180                                                                    number
181                                                                    &rest args)
182   "Create message entity from current buffer.
183 NUMBER is the number of the newly created message entity.
184 Rest of the ARGS is a plist of message entity field for initial value.
185 Header region is supposed to be narrowed.")
186
187 ;; Transitional interface.
188 (luna-define-generic elmo-msgdb-match-condition-internal (msgdb
189                                                           condition
190                                                           entity
191                                                           flags
192                                                           numbers)
193   "Return non-nil when the entity matches the condition.")
194
195 ;;; generic implement
196 ;;
197 (luna-define-method elmo-msgdb-load ((msgdb modb-generic))
198   t)
199
200 (luna-define-method elmo-msgdb-location ((msgdb modb-generic))
201   (modb-generic-location-internal msgdb))
202
203 (luna-define-method elmo-msgdb-message-modified-p ((msgdb modb-generic))
204   (modb-generic-message-modified-internal msgdb))
205
206 (luna-define-method elmo-msgdb-flag-modified-p ((msgdb modb-generic))
207   (modb-generic-flag-modified-internal msgdb))
208
209 (luna-define-method elmo-msgdb-append ((msgdb modb-generic) msgdb-append)
210   (let (duplicates)
211     (dolist (number (elmo-msgdb-list-messages msgdb-append))
212       (when (elmo-msgdb-append-entity
213              msgdb
214              (elmo-msgdb-message-entity msgdb-append number)
215              (elmo-msgdb-flags msgdb-append number))
216         (setq duplicates (cons number duplicates))))
217     duplicates))
218
219 (luna-define-method elmo-msgdb-clear ((msgdb modb-generic))
220   (modb-generic-set-message-modified-internal msgdb nil)
221   (modb-generic-set-flag-modified-internal msgdb nil))
222
223 (luna-define-method elmo-msgdb-length ((msgdb modb-generic))
224   0)
225
226 (luna-define-method elmo-msgdb-make-message-entity ((msgdb modb-generic)
227                                                     args)
228   (cons msgdb args))
229
230 (luna-define-method elmo-msgdb-message-entity-field ((msgdb modb-generic)
231                                                      entity field
232                                                      &optional decode)
233   (plist-get (cdr entity) (intern (concat ":" (symbol-name field)))))
234
235 (luna-define-method elmo-msgdb-message-entity-number ((msgdb modb-generic)
236                                                       entity)
237   (plist-get (cdr entity) :number))
238
239 ;; for on demand loading
240 (provide 'modb-generic)
241
242 (require 'product)
243 (product-provide (provide 'modb) (require 'elmo-version))
244
245 ;;; modb.el ends here