KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > message > ColumbaHeader


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.message;
17
18 import org.columba.core.gui.base.ColorFactory;
19 import org.columba.mail.folder.headercache.CachedHeaderfields;
20 import org.columba.ristretto.message.Address;
21 import org.columba.ristretto.message.Attributes;
22 import org.columba.ristretto.message.BasicHeader;
23 import org.columba.ristretto.message.Flags;
24 import org.columba.ristretto.message.Header;
25 import org.columba.ristretto.message.MimeHeader;
26 import org.columba.ristretto.message.MimeType;
27 import org.columba.ristretto.parser.HeaderParser;
28
29 /**
30  * Represents a RFC822-compliant header
31  * <p>
32  * Every headerfield is saved in {@HeaderList}.
33  * <p>
34  * Generally every headerfield is a string, but for optimization reasons some
35  * items are going to change to for example a Date class
36  * <p>
37  * These items are saved in {@link Attributes} to separate them clearly from
38  * general RFC822 headerfields.
39  *
40  * <p>
41  *
42  * @see CachedHeaderfields
43  *
44  * @author tstich, fdietz
45  */

46 public class ColumbaHeader implements IColumbaHeader {
47
48     protected Header header;
49
50     protected Attributes attributes;
51
52     protected Flags flags;
53
54     public ColumbaHeader(Header header, Attributes attributes, Flags flags) {
55         this.attributes = attributes;
56         this.flags = flags;
57         this.header = header;
58     }
59
60     public ColumbaHeader(IColumbaHeader header) {
61         this.header = header.getHeader();
62         this.attributes = header.getAttributes();
63         this.flags = header.getFlags();
64     }
65
66     public ColumbaHeader() {
67         this(new Header());
68     }
69
70     public ColumbaHeader(Header header) {
71         this.header = header;
72         flags = new Flags();
73         attributes = new Attributes();
74
75         BasicHeader basicHeader = new BasicHeader(header);
76
77         attributes.put("columba.alreadyfetched", Boolean.FALSE);
78         attributes.put("columba.spam", Boolean.FALSE);
79
80         attributes.put("columba.priority", new Integer JavaDoc(basicHeader
81                 .getPriority()));
82
83         Address from = basicHeader.getFrom();
84
85         if (from != null) {
86             attributes.put("columba.from", from);
87         } else {
88             attributes.put("columba.from", "");
89         }
90
91         Address[] to = basicHeader.getTo();
92
93         if (to.length > 0) {
94             // We save only the first item in the to-list
95
attributes.put("columba.to", to[0]);
96         } else {
97             attributes.put("columba.to", "");
98         }
99
100         Address[] cc = basicHeader.getCc();
101
102         if (cc.length > 0) {
103             // We save only the first item in the cc-list
104
attributes.put("columba.cc", cc[0]);
105         } else {
106             attributes.put("columba.cc", "");
107         }
108
109         attributes.put("columba.host", "");
110         attributes.put("columba.date", basicHeader.getDate());
111
112         String JavaDoc subject = basicHeader.getSubject();
113
114         if (subject != null) {
115             attributes.put("columba.subject", subject);
116         } else {
117             attributes.put("columba.subject", "");
118         }
119
120         attributes.put("columba.attachment", hasAttachments());
121         attributes.put("columba.size", new Integer JavaDoc(0));
122
123         // message colour should be black as default
124
attributes.put("columba.color", ColorFactory.getColor(0));
125
126         // use default account
127
attributes.put("columba.accountuid", new Integer JavaDoc(0));
128     }
129
130     /**
131      * Pay attention when using this method. Only the Attributes and the flags
132      * get copied. The Header stays the same!
133      *
134      */

135     public Object JavaDoc clone() {
136         ColumbaHeader clone = new ColumbaHeader();
137         clone.header = this.header;
138         clone.attributes = (Attributes) this.attributes.clone();
139         clone.flags = (Flags) this.flags.clone();
140         return clone;
141     }
142
143     public void copyColumbaKeys(IColumbaHeader header) {
144         header.setFlags((Flags) flags.clone());
145         header.setAttributes((Attributes) attributes.clone());
146     }
147
148     /*
149      * (non-Javadoc)
150      *
151      * @see org.columba.mail.message.HeaderInterface#count()
152      */

153     public int count() {
154         return attributes.count() + header.count() + 5;
155     }
156
157     /*
158      * (non-Javadoc)
159      *
160      * @see org.columba.mail.message.HeaderInterface#getFlags()
161      */

162     public Flags getFlags() {
163         return flags;
164     }
165
166     /**
167      * Note: Don't use this method anymore when accessing attributes like
168      * "columba.size", use getAttribute() instead
169      *
170      */

171     public Object JavaDoc get(String JavaDoc s) {
172         if (s.startsWith("columba.flags.")) {
173             String JavaDoc flag = s.substring("columba.flags.".length());
174
175             if (flag.equals("seen")) {
176                 return Boolean.valueOf(flags.get(Flags.SEEN));
177             } else if (flag.equals("recent")) {
178                 return Boolean.valueOf(flags.get(Flags.RECENT));
179             } else if (flag.equals("answered")) {
180                 return Boolean.valueOf(flags.get(Flags.ANSWERED));
181             } else if (flag.equals("draft")) {
182                 return Boolean.valueOf(flags.get(Flags.DRAFT));
183             } else if (flag.equals("flagged")) {
184                 return Boolean.valueOf(flags.get(Flags.FLAGGED));
185             } else if (flag.equals("expunged")) {
186                 return Boolean.valueOf(flags.get(Flags.DELETED));
187             }
188         }
189
190         if (s.startsWith("columba.")) {
191             return attributes.get(s);
192         }
193
194         return header.get(HeaderParser.normalizeKey(s));
195     }
196
197     /*
198      * (non-Javadoc)
199      *
200      * @see org.columba.mail.message.HeaderInterface#set(java.lang.String,
201      * java.lang.Object)
202      */

203     public void set(String JavaDoc s, Object JavaDoc o) {
204         if (o == null) {
205             return;
206         }
207         if (s.startsWith("columba.flags")) {
208             String JavaDoc flag = s.substring("columba.flags.".length());
209             boolean value = ((Boolean JavaDoc) o).booleanValue();
210
211             if (flag.equals("seen")) {
212                 flags.set(Flags.SEEN, value);
213
214                 return;
215             }
216
217             if (flag.equals("recent")) {
218                 flags.set(Flags.RECENT, value);
219
220                 return;
221             }
222
223             if (flag.equals("answered")) {
224                 flags.set(Flags.ANSWERED, value);
225
226                 return;
227             }
228
229             if (flag.equals("expunged")) {
230                 flags.set(Flags.DELETED, value);
231
232                 return;
233             }
234
235             if (flag.equals("draft")) {
236                 flags.set(Flags.DRAFT, value);
237
238                 return;
239             }
240
241             if (flag.equals("flagged")) {
242                 flags.set(Flags.FLAGGED, value);
243             }
244         }
245
246         if (s.startsWith("columba.")) {
247             attributes.put(s, o);
248         } else {
249             header.set(HeaderParser.normalizeKey(s), (String JavaDoc) o);
250         }
251     }
252
253     /**
254      * @return
255      */

256     public Header getHeader() {
257         return header;
258     }
259
260     /**
261      * @return
262      */

263     public Attributes getAttributes() {
264         return attributes;
265     }
266
267     /**
268      * @param attributes
269      */

270     public void setAttributes(Attributes attributes) {
271         this.attributes = attributes;
272     }
273
274     /**
275      * @param flags
276      */

277     public void setFlags(Flags flags) {
278         this.flags = flags;
279     }
280
281     /**
282      * @param header
283      */

284     public void setHeader(Header header) {
285         this.header = header;
286     }
287
288     public Boolean JavaDoc hasAttachments() {
289         boolean hasAttachments = false;
290         MimeType mimeType = new MimeHeader(header).getMimeType();
291         hasAttachments = !mimeType.getType().equals("text")
292                 && !mimeType.getSubtype().equals("alternative");
293
294         return Boolean.valueOf(hasAttachments);
295     }
296 }
297
Popular Tags