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:
017: package org.columba.mail.folder.temp;
018:
019: import java.io.File;
020: import java.io.InputStream;
021: import java.util.Hashtable;
022: import java.util.logging.Logger;
023:
024: import org.columba.api.command.IWorkerStatusController;
025: import org.columba.core.filter.Filter;
026: import org.columba.core.io.DiskIO;
027: import org.columba.mail.config.FolderItem;
028: import org.columba.mail.folder.AbstractMessageFolder;
029: import org.columba.mail.folder.IMailbox;
030: import org.columba.mail.folder.headercache.MemoryHeaderList;
031: import org.columba.mail.folder.imap.IMAPFolder;
032: import org.columba.mail.folder.search.DefaultSearchEngine;
033: import org.columba.mail.message.ColumbaHeader;
034: import org.columba.mail.message.ColumbaMessage;
035: import org.columba.mail.message.IColumbaHeader;
036: import org.columba.mail.message.IColumbaMessage;
037: import org.columba.mail.message.IHeaderList;
038: import org.columba.ristretto.io.SourceInputStream;
039: import org.columba.ristretto.io.TempSourceFactory;
040: import org.columba.ristretto.message.Attributes;
041: import org.columba.ristretto.message.Flags;
042: import org.columba.ristretto.message.Header;
043: import org.columba.ristretto.message.LocalMimePart;
044: import org.columba.ristretto.message.Message;
045: import org.columba.ristretto.message.MimeTree;
046: import org.columba.ristretto.parser.MessageParser;
047:
048: /**
049: * @author freddy
050: */
051: public class TempFolder extends AbstractMessageFolder {
052:
053: /** JDK 1.4+ logging framework logger, used for logging. */
054: private static final Logger LOG = Logger
055: .getLogger("org.columba.mail.folder.temp");
056:
057: protected IHeaderList headerList;
058: protected Hashtable messageList;
059: protected int nextUid = 0;
060: protected IColumbaMessage aktMessage;
061:
062: /**
063: * Constructor for TempFolder.
064: *
065: * @param path example: /home/donald/mail/
066: */
067: public TempFolder(String path) {
068: super ("temp-folder-name", "temp", path);
069:
070: String dir = path + "temp";
071:
072: if (DiskIO.ensureDirectory(dir)) {
073: directoryFile = new File(dir);
074: }
075:
076: headerList = new MemoryHeaderList();
077: messageList = new Hashtable();
078:
079: setSearchEngine(new DefaultSearchEngine(this ));
080: }
081:
082: public void clear() {
083: headerList.clear();
084: messageList.clear();
085: }
086:
087: public Object[] getUids() throws Exception {
088: return messageList.keySet().toArray();
089: }
090:
091: protected Object generateNextUid() {
092: return new Integer(nextUid++);
093: }
094:
095: public void setNextUid(int next) {
096: nextUid = next;
097: }
098:
099: /**
100: * @see org.columba.modules.mail.folder.Folder#exists(Object)
101: */
102: public boolean exists(Object uid) throws Exception {
103: return messageList.containsKey(uid);
104: }
105:
106: /**
107: * @see org.columba.modules.mail.folder.Folder#getHeaderList(IWorkerStatusController)
108: */
109: public IHeaderList getHeaderList() throws Exception {
110: return headerList;
111: }
112:
113: /**
114: * @see org.columba.modules.mail.folder.Folder#remove(Object)
115: */
116: public void removeMessage(Object uid) throws Exception {
117:
118: Flags flags = getFlags(uid);
119:
120: fireMessageRemoved(uid, flags);
121:
122: headerList.remove(uid);
123: messageList.remove(uid);
124:
125: }
126:
127: /**
128: * @see org.columba.modules.mail.folder.Folder#getMimeTree(Object,
129: * IMAPFolder)
130: */
131: public MimeTree getMimePartTree(Object uid) throws Exception {
132: return ((IColumbaMessage) messageList.get(uid))
133: .getMimePartTree();
134: }
135:
136: /**
137: * @see org.columba.modules.mail.folder.Folder#getMessageHeader(Object,
138: * IWorkerStatusController)
139: * @TODO dont use deprecated method
140: */
141: public IColumbaHeader getMessageHeader(Object uid) throws Exception {
142: ColumbaHeader header = (ColumbaHeader) headerList.get(uid);
143:
144: return header;
145: }
146:
147: /**
148: * @see org.columba.modules.mail.folder.Folder#getMessage(Object,
149: * IWorkerStatusController)
150: */
151: public IColumbaMessage getMessage(Object uid) throws Exception {
152: IColumbaMessage message = (IColumbaMessage) messageList
153: .get(uid);
154:
155: return message;
156: }
157:
158: /**
159: * @see org.columba.modules.mail.folder.Folder#searchMessages(Filter,
160: * Object[], IWorkerStatusController)
161: */
162: public Object[] searchMessages(Filter filter, Object[] uids)
163: throws Exception {
164: return getSearchEngine().searchMessages(filter, uids);
165: }
166:
167: /**
168: * @see org.columba.modules.mail.folder.Folder#searchMessages(Filter,
169: * IWorkerStatusController)
170: */
171: public Object[] searchMessages(Filter filter) throws Exception {
172: return getSearchEngine().searchMessages(filter);
173: }
174:
175: /**
176: * @see org.columba.modules.mail.folder.FolderTreeNode#instanceNewChildNode(AdapterNode,
177: * FolderItem)
178: */
179: public String getDefaultChild() {
180: return null;
181: }
182:
183: public String getName() {
184: return toString();
185: }
186:
187: public String toString() {
188: return (String) getUserObject();
189: }
190:
191: // FIXME (@author fdietz): Do we need this implementation in a TempFolder?
192: // If not, just put an empty method here, just like in VirtualFolder.
193: public void innerCopy(IMailbox destFolder, Object[] uids)
194: throws Exception {
195: for (int i = 0; i < uids.length; i++) {
196:
197: InputStream messageSourceStream = getMessageSourceStream(uids[i]);
198: destFolder.addMessage(messageSourceStream);
199: messageSourceStream.close();
200: }
201: }
202:
203: public Object addMessage(InputStream in, Attributes attributes,
204: Flags flags) throws Exception {
205:
206: Message message = MessageParser.parse(TempSourceFactory
207: .createTempSource(in, -1));
208:
209: Object newUid = generateNextUid();
210:
211: LOG.info("new UID=" + newUid);
212:
213: ColumbaHeader h = new ColumbaHeader(message.getHeader());
214:
215: if (attributes != null) {
216: h.setAttributes((Attributes) attributes.clone());
217: }
218:
219: if (flags != null) {
220: h.setFlags((Flags) flags.clone());
221: }
222:
223: h.getAttributes().put("columba.uid", newUid);
224:
225: headerList.add(h, newUid);
226:
227: messageList.put(newUid, new ColumbaMessage(h, message));
228:
229: fireMessageAdded(newUid, getFlags(newUid));
230: return newUid;
231: }
232:
233: /*
234: * (non-Javadoc)
235: *
236: * @see org.columba.mail.folder.IMailbox#getAttribute(java.lang.Object,
237: * java.lang.String)
238: */
239: public Object getAttribute(Object uid, String key) throws Exception {
240: return ((ColumbaHeader) headerList.get(uid)).getAttributes()
241: .get(key);
242: }
243:
244: /*
245: * (non-Javadoc)
246: *
247: * @see org.columba.mail.folder.IMailbox#getFlags(java.lang.Object)
248: */
249: public Flags getFlags(Object uid) throws Exception {
250: return ((ColumbaHeader) headerList.get(uid)).getFlags();
251: }
252:
253: public Attributes getAttributes(Object uid) throws Exception {
254: if (getHeaderList().exists(uid)) {
255: return getHeaderList().get(uid).getAttributes();
256: } else {
257: return null;
258: }
259: }
260:
261: /*
262: * (non-Javadoc)
263: *
264: * @see org.columba.mail.folder.IMailbox#getHeaderFields(java.lang.Object,
265: * java.lang.String[])
266: */
267: public Header getHeaderFields(Object uid, String[] keys)
268: throws Exception {
269: IColumbaHeader header = ((IColumbaMessage) messageList.get(uid))
270: .getHeader();
271:
272: Header subHeader = new Header();
273: String value;
274:
275: for (int i = 0; i < keys.length; i++) {
276: value = (String) header.get(keys[i]);
277:
278: if (value != null) {
279: subHeader.set(keys[i], value);
280: }
281: }
282:
283: return subHeader;
284: }
285:
286: /*
287: * (non-Javadoc)
288: *
289: * @see org.columba.mail.folder.IMailbox#getMessageSourceStream(java.lang.Object)
290: */
291: public InputStream getMessageSourceStream(Object uid)
292: throws Exception {
293: return new SourceInputStream(((IColumbaMessage) messageList
294: .get(uid)).getSource());
295: }
296:
297: /*
298: * (non-Javadoc)
299: *
300: * @see org.columba.mail.folder.IMailbox#getMimePartBodyStream(java.lang.Object,
301: * java.lang.Integer[])
302: */
303: public InputStream getMimePartBodyStream(Object uid,
304: Integer[] address) throws Exception {
305: IColumbaMessage message = (IColumbaMessage) messageList
306: .get(uid);
307:
308: LocalMimePart mimepart = (LocalMimePart) message
309: .getMimePartTree().getFromAddress(address);
310:
311: return mimepart.getInputStream();
312: }
313:
314: /*
315: * (non-Javadoc)
316: *
317: * @see org.columba.mail.folder.IMailbox#getMimePartSourceStream(java.lang.Object,
318: * java.lang.Integer[])
319: */
320: public InputStream getMimePartSourceStream(Object uid,
321: Integer[] address) throws Exception {
322: IColumbaMessage message = (IColumbaMessage) messageList
323: .get(uid);
324:
325: LocalMimePart mimepart = (LocalMimePart) message
326: .getMimePartTree().getFromAddress(address);
327:
328: return new SourceInputStream(mimepart.getSource());
329: }
330:
331: public void setAttribute(Object uid, String key, Object value)
332: throws Exception {
333: // get header with UID
334: ColumbaHeader header = (ColumbaHeader) getHeaderList().get(uid);
335:
336: header.getAttributes().put(key, value);
337: }
338:
339: /* (non-Javadoc)
340: * @see org.columba.mail.folder.IMailbox#addMessage(java.io.InputStream, org.columba.ristretto.message.Attributes)
341: */
342: public Object addMessage(InputStream in) throws Exception {
343: return addMessage(in, null, null);
344: }
345:
346: /**
347: * @see org.columba.modules.mail.folder.Folder#addMessage(AbstractMessage,
348: * IWorkerStatusController)
349: */
350: public Object addMessage(IColumbaMessage message) throws Exception {
351: Object newUid = generateNextUid();
352:
353: ColumbaHeader h = (ColumbaHeader) ((ColumbaHeader) message
354: .getHeader());
355:
356: h.getAttributes().put("columba.uid", newUid);
357:
358: headerList.add(h, newUid);
359:
360: messageList.put(newUid, message);
361:
362: return newUid;
363: }
364:
365: /**
366: * @see org.columba.mail.folder.AbstractFolder#removeFolder()
367: */
368: public void removeFolder() throws Exception {
369: //super.removeFolder();
370: // do nothing
371: }
372:
373: /**
374: * @see org.columba.mail.folder.IMailbox#getAllHeaderFields(java.lang.Object)
375: */
376: public Header getAllHeaderFields(Object uid) throws Exception {
377: IColumbaHeader header = ((IColumbaMessage) messageList.get(uid))
378: .getHeader();
379:
380: return header.getHeader();
381: }
382: }
|