001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.message;
017:
018: import org.columba.core.gui.base.ColorFactory;
019: import org.columba.mail.folder.headercache.CachedHeaderfields;
020: import org.columba.ristretto.message.Address;
021: import org.columba.ristretto.message.Attributes;
022: import org.columba.ristretto.message.BasicHeader;
023: import org.columba.ristretto.message.Flags;
024: import org.columba.ristretto.message.Header;
025: import org.columba.ristretto.message.MimeHeader;
026: import org.columba.ristretto.message.MimeType;
027: import org.columba.ristretto.parser.HeaderParser;
028:
029: /**
030: * Represents a RFC822-compliant header
031: * <p>
032: * Every headerfield is saved in {@HeaderList}.
033: * <p>
034: * Generally every headerfield is a string, but for optimization reasons some
035: * items are going to change to for example a Date class
036: * <p>
037: * These items are saved in {@link Attributes} to separate them clearly from
038: * general RFC822 headerfields.
039: *
040: * <p>
041: *
042: * @see CachedHeaderfields
043: *
044: * @author tstich, fdietz
045: */
046: public class ColumbaHeader implements IColumbaHeader {
047:
048: protected Header header;
049:
050: protected Attributes attributes;
051:
052: protected Flags flags;
053:
054: public ColumbaHeader(Header header, Attributes attributes,
055: Flags flags) {
056: this .attributes = attributes;
057: this .flags = flags;
058: this .header = header;
059: }
060:
061: public ColumbaHeader(IColumbaHeader header) {
062: this .header = header.getHeader();
063: this .attributes = header.getAttributes();
064: this .flags = header.getFlags();
065: }
066:
067: public ColumbaHeader() {
068: this (new Header());
069: }
070:
071: public ColumbaHeader(Header header) {
072: this .header = header;
073: flags = new Flags();
074: attributes = new Attributes();
075:
076: BasicHeader basicHeader = new BasicHeader(header);
077:
078: attributes.put("columba.alreadyfetched", Boolean.FALSE);
079: attributes.put("columba.spam", Boolean.FALSE);
080:
081: attributes.put("columba.priority", new Integer(basicHeader
082: .getPriority()));
083:
084: Address from = basicHeader.getFrom();
085:
086: if (from != null) {
087: attributes.put("columba.from", from);
088: } else {
089: attributes.put("columba.from", "");
090: }
091:
092: Address[] to = basicHeader.getTo();
093:
094: if (to.length > 0) {
095: // We save only the first item in the to-list
096: attributes.put("columba.to", to[0]);
097: } else {
098: attributes.put("columba.to", "");
099: }
100:
101: Address[] cc = basicHeader.getCc();
102:
103: if (cc.length > 0) {
104: // We save only the first item in the cc-list
105: attributes.put("columba.cc", cc[0]);
106: } else {
107: attributes.put("columba.cc", "");
108: }
109:
110: attributes.put("columba.host", "");
111: attributes.put("columba.date", basicHeader.getDate());
112:
113: String subject = basicHeader.getSubject();
114:
115: if (subject != null) {
116: attributes.put("columba.subject", subject);
117: } else {
118: attributes.put("columba.subject", "");
119: }
120:
121: attributes.put("columba.attachment", hasAttachments());
122: attributes.put("columba.size", new Integer(0));
123:
124: // message colour should be black as default
125: attributes.put("columba.color", ColorFactory.getColor(0));
126:
127: // use default account
128: attributes.put("columba.accountuid", new Integer(0));
129: }
130:
131: /**
132: * Pay attention when using this method. Only the Attributes and the flags
133: * get copied. The Header stays the same!
134: *
135: */
136: public Object clone() {
137: ColumbaHeader clone = new ColumbaHeader();
138: clone.header = this .header;
139: clone.attributes = (Attributes) this .attributes.clone();
140: clone.flags = (Flags) this .flags.clone();
141: return clone;
142: }
143:
144: public void copyColumbaKeys(IColumbaHeader header) {
145: header.setFlags((Flags) flags.clone());
146: header.setAttributes((Attributes) attributes.clone());
147: }
148:
149: /*
150: * (non-Javadoc)
151: *
152: * @see org.columba.mail.message.HeaderInterface#count()
153: */
154: public int count() {
155: return attributes.count() + header.count() + 5;
156: }
157:
158: /*
159: * (non-Javadoc)
160: *
161: * @see org.columba.mail.message.HeaderInterface#getFlags()
162: */
163: public Flags getFlags() {
164: return flags;
165: }
166:
167: /**
168: * Note: Don't use this method anymore when accessing attributes like
169: * "columba.size", use getAttribute() instead
170: *
171: */
172: public Object get(String s) {
173: if (s.startsWith("columba.flags.")) {
174: String flag = s.substring("columba.flags.".length());
175:
176: if (flag.equals("seen")) {
177: return Boolean.valueOf(flags.get(Flags.SEEN));
178: } else if (flag.equals("recent")) {
179: return Boolean.valueOf(flags.get(Flags.RECENT));
180: } else if (flag.equals("answered")) {
181: return Boolean.valueOf(flags.get(Flags.ANSWERED));
182: } else if (flag.equals("draft")) {
183: return Boolean.valueOf(flags.get(Flags.DRAFT));
184: } else if (flag.equals("flagged")) {
185: return Boolean.valueOf(flags.get(Flags.FLAGGED));
186: } else if (flag.equals("expunged")) {
187: return Boolean.valueOf(flags.get(Flags.DELETED));
188: }
189: }
190:
191: if (s.startsWith("columba.")) {
192: return attributes.get(s);
193: }
194:
195: return header.get(HeaderParser.normalizeKey(s));
196: }
197:
198: /*
199: * (non-Javadoc)
200: *
201: * @see org.columba.mail.message.HeaderInterface#set(java.lang.String,
202: * java.lang.Object)
203: */
204: public void set(String s, Object o) {
205: if (o == null) {
206: return;
207: }
208: if (s.startsWith("columba.flags")) {
209: String flag = s.substring("columba.flags.".length());
210: boolean value = ((Boolean) o).booleanValue();
211:
212: if (flag.equals("seen")) {
213: flags.set(Flags.SEEN, value);
214:
215: return;
216: }
217:
218: if (flag.equals("recent")) {
219: flags.set(Flags.RECENT, value);
220:
221: return;
222: }
223:
224: if (flag.equals("answered")) {
225: flags.set(Flags.ANSWERED, value);
226:
227: return;
228: }
229:
230: if (flag.equals("expunged")) {
231: flags.set(Flags.DELETED, value);
232:
233: return;
234: }
235:
236: if (flag.equals("draft")) {
237: flags.set(Flags.DRAFT, value);
238:
239: return;
240: }
241:
242: if (flag.equals("flagged")) {
243: flags.set(Flags.FLAGGED, value);
244: }
245: }
246:
247: if (s.startsWith("columba.")) {
248: attributes.put(s, o);
249: } else {
250: header.set(HeaderParser.normalizeKey(s), (String) o);
251: }
252: }
253:
254: /**
255: * @return
256: */
257: public Header getHeader() {
258: return header;
259: }
260:
261: /**
262: * @return
263: */
264: public Attributes getAttributes() {
265: return attributes;
266: }
267:
268: /**
269: * @param attributes
270: */
271: public void setAttributes(Attributes attributes) {
272: this .attributes = attributes;
273: }
274:
275: /**
276: * @param flags
277: */
278: public void setFlags(Flags flags) {
279: this .flags = flags;
280: }
281:
282: /**
283: * @param header
284: */
285: public void setHeader(Header header) {
286: this .header = header;
287: }
288:
289: public Boolean hasAttachments() {
290: boolean hasAttachments = false;
291: MimeType mimeType = new MimeHeader(header).getMimeType();
292: hasAttachments = !mimeType.getType().equals("text")
293: && !mimeType.getSubtype().equals("alternative");
294:
295: return Boolean.valueOf(hasAttachments);
296: }
297: }
|