* modb.el (elmo-msgdb-flag-available-p): New generic function.
[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 (require 'modb-entity)
37
38 (eval-and-compile
39   (luna-define-class modb-generic () (location         ; location for save.
40                                       message-modified ; message is modified.
41                                       flag-modified    ; flag is modified.
42                                       ))
43   (luna-define-internal-accessors 'modb-generic))
44
45 (luna-define-generic elmo-msgdb-load (msgdb)
46   "Load the MSGDB.")
47
48 (luna-define-generic elmo-msgdb-save (msgdb)
49   "Save the MSGDB.")
50
51 (luna-define-generic elmo-msgdb-location (msgdb)
52   "Return the location of MSGDB.")
53
54 (luna-define-generic elmo-msgdb-message-modified-p (msgdb)
55   "Return non-nil if message is modified.")
56
57 (luna-define-generic elmo-msgdb-flag-modified-p (msgdb)
58   "Return non-nil if flag is modified.")
59
60 (luna-define-generic elmo-msgdb-append (msgdb msgdb-append)
61   "Append the MSGDB-APPEND to the MSGDB.
62 Return a list of messages which have duplicated message-id.")
63
64 (luna-define-generic elmo-msgdb-clear (msgdb)
65   "Clear the MSGDB structure.")
66
67 (luna-define-generic elmo-msgdb-length (msgdb)
68   "Return number of messages in the MSGDB")
69
70 (luna-define-generic elmo-msgdb-flag-available-p (msgdb flag)
71   "Return non-nil when FLAG is available.")
72
73 (luna-define-generic elmo-msgdb-flags (msgdb number)
74   "Return a list of flag which corresponds to the message with NUMBER.")
75
76 (luna-define-generic elmo-msgdb-set-flag (msgdb number flag)
77   "Set message flag.
78 MSGDB is the ELMO msgdb.
79 NUMBER is a message number to set flag.
80 FLAG is a symbol which is one of the following:
81   `new'       ... Message which is new.
82   `unread'    ... Message which is not read.
83   `important' ... Message which is important.
84   `answered'  ... Message which is answered.
85   `cached'    ... Message which is cached.
86 'sugar' flag:
87   `read'      ... Message which is already read.
88   `uncached'  ... Message which is not cached.")
89
90 (luna-define-generic elmo-msgdb-unset-flag (msgdb number flag)
91   "Unset message flag.
92 MSGDB is the ELMO msgdb.
93 NUMBER is a message number to set flag.
94 FLAG is a symbol which is one of the following:
95   `new'       ... Message which is new.
96   `unread'    ... Message which is not read.
97   `important' ... Message which is important.
98   `answered'  ... Message which is answered.
99   `cached'    ... Message which is cached.
100 'sugar' flag:
101   `read'      ... Message which is already read.
102   `uncached'  ... Message which is not cached.
103   `all'       ... Remove all flags.")
104
105 (luna-define-generic elmo-msgdb-flag-count (msgdb)
106   "Return a list of cons cell as (flag . count).
107 The count is number of message which is set flag in the MSGDB.")
108
109 (luna-define-generic elmo-msgdb-list-messages (msgdb)
110   "Return a list of message numbers in the MSGDB.")
111
112 (luna-define-generic elmo-msgdb-list-flagged (msgdb flag)
113   "Return a list of message numbers which is set FLAG in the MSGDB.")
114
115 (luna-define-generic elmo-msgdb-search (msgdb condition &optional numbers)
116   "Search and return list of message numbers.
117 MSGDB is the ELMO msgdb structure.
118 CONDITION is a condition structure for searching.
119 If optional argument NUMBERS is specified and is a list of message numbers,
120 messages are searched from the list.
121 Return t if the condition is unsupported.")
122
123 (luna-define-generic elmo-msgdb-match-condition (msgdb condition number
124                                                        &optional numbers)
125   "Check whether the condition of the message is satisfied or not.
126 MSGDB is the msgdb to search from.
127 CONDITION is the search condition.
128 NUMBER is the message number to check.
129 If optional argument NUMBERS is specified and is a list of message numbers,
130 messages are searched from the list.
131 Return CONDITION itself if no entity exists in msgdb.")
132
133 (luna-define-generic elmo-msgdb-append-entity (msgdb entity &optional flags)
134   "Append a ENTITY with FLAGS into the MSGDB.
135 Return non-nil if message-id of entity is duplicated.")
136
137 (luna-define-generic elmo-msgdb-delete-messages (msgdb numbers)
138   "Delete messages which are contained NUMBERS from MSGDB.
139 Return non-nil if messages is deleted successfully.")
140
141 (luna-define-generic elmo-msgdb-sort-entities (msgdb predicate
142                                                      &optional app-data)
143   "Sort entities of MSGDB, comparing with PREDICATE.
144 PREDICATE is called with two entities and APP-DATA.
145 Should return non-nil if the first entity is \"less\" than the second.")
146
147 (luna-define-generic elmo-msgdb-message-number (msgdb message-id)
148   "Get message number from MSGDB which corresponds to MESSAGE-ID.")
149
150 (luna-define-method elmo-msgdb-message-number ((msgdb modb-generic)
151                                                message-id)
152   (elmo-message-entity-number
153    (elmo-msgdb-message-entity msgdb message-id)))
154
155 (luna-define-generic elmo-msgdb-message-entity (msgdb key)
156   "Return the message-entity structure which matches to the KEY.
157 KEY is a number or a string.
158 A number is for message number in the MSGDB.
159 A string is for message-id of the message.")
160
161 (luna-define-generic elmo-msgdb-message-field (msgdb number field)
162   "Get message field value in the MSGDB.
163 NUMBER is a number of the message.
164 FIELD is a symbol of the field.")
165
166 (luna-define-method elmo-msgdb-message-field ((msgdb modb-generic)
167                                               number field)
168   (elmo-message-entity-field (elmo-msgdb-message-entity msgdb number)
169                              field))
170
171 (luna-define-generic elmo-msgdb-message-entity-handler (msgdb)
172   "Get modb entity handler instance which corresponds to the MSGDB.")
173
174 ;;; generic implement
175 ;;
176 (luna-define-method elmo-msgdb-load ((msgdb modb-generic))
177   t)
178
179 (luna-define-method elmo-msgdb-location ((msgdb modb-generic))
180   (modb-generic-location-internal msgdb))
181
182 (luna-define-method elmo-msgdb-message-modified-p ((msgdb modb-generic))
183   (modb-generic-message-modified-internal msgdb))
184
185 (luna-define-method elmo-msgdb-flag-modified-p ((msgdb modb-generic))
186   (modb-generic-flag-modified-internal msgdb))
187
188 (luna-define-method elmo-msgdb-append ((msgdb modb-generic) msgdb-append)
189   (let (duplicates)
190     (dolist (number (elmo-msgdb-list-messages msgdb-append))
191       (when (elmo-msgdb-append-entity
192              msgdb
193              (elmo-msgdb-message-entity msgdb-append number)
194              (elmo-msgdb-flags msgdb-append number))
195         (setq duplicates (cons number duplicates))))
196     duplicates))
197
198 (luna-define-method elmo-msgdb-clear ((msgdb modb-generic))
199   (modb-generic-set-message-modified-internal msgdb nil)
200   (modb-generic-set-flag-modified-internal msgdb nil))
201
202 (luna-define-method elmo-msgdb-length ((msgdb modb-generic))
203   0)
204
205 (luna-define-method elmo-msgdb-search ((msgdb modb-generic)
206                                        condition &optional numbers)
207   t)
208
209 (luna-define-method elmo-msgdb-match-condition ((msgdb modb-generic)
210                                                 condition
211                                                 number
212                                                 &optional numbers)
213   (let ((entity (elmo-msgdb-message-entity msgdb number)))
214     (if entity
215         (elmo-msgdb-message-match-condition
216          (elmo-msgdb-message-entity-handler msgdb)
217          condition
218          entity
219          (elmo-msgdb-flags msgdb number)
220          (or numbers (elmo-msgdb-list-messages msgdb)))
221       condition)))
222
223 (luna-define-method elmo-msgdb-message-entity-handler ((msgdb modb-generic))
224   (or modb-entity-default-cache-internal
225       (setq modb-entity-default-cache-internal
226             (luna-make-entity modb-entity-default-handler))))
227
228 ;; for on demand loading
229 (provide 'modb-generic)
230
231 (require 'product)
232 (product-provide (provide 'modb) (require 'elmo-version))
233
234 ;;; modb.el ends here