001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.gui.composer.command;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.nio.charset.Charset;
023: import java.util.Arrays;
024: import java.util.Iterator;
025:
026: import org.columba.api.command.ICommandReference;
027: import org.columba.api.command.IWorkerStatusController;
028: import org.columba.core.io.StreamUtils;
029: import org.columba.core.xml.XmlElement;
030: import org.columba.mail.command.MailFolderCommandReference;
031: import org.columba.mail.config.MailConfig;
032: import org.columba.mail.folder.IMailbox;
033: import org.columba.mail.gui.composer.ComposerModel;
034: import org.columba.mail.parser.text.HtmlParser;
035: import org.columba.ristretto.message.BasicHeader;
036: import org.columba.ristretto.message.Header;
037: import org.columba.ristretto.message.InputStreamMimePart;
038: import org.columba.ristretto.message.MimeHeader;
039: import org.columba.ristretto.message.MimePart;
040: import org.columba.ristretto.message.MimeTree;
041:
042: /**
043: * Redirect message, which is the same as forwarding without Quotationsmarks and
044: * the orginal sender is the new sender.
045: *
046: * This is modified ForwardInlineCommand.
047: *
048: * @author fdietz modified by switt
049: */
050: public class RedirectCommand extends ForwardCommand {
051:
052: protected final String[] headerfields = new String[] { "Subject",
053: "Date", "From", "To" };
054:
055: /**
056: * Constructor for RedirectCommand.
057: *
058: * @param frameMediator
059: * @param references
060: */
061: public RedirectCommand(ICommandReference reference) {
062: super (reference);
063: }
064:
065: public void execute(IWorkerStatusController worker)
066: throws Exception {
067: // create composer model
068: model = new ComposerModel();
069:
070: // get selected folder
071: IMailbox folder = (IMailbox) ((MailFolderCommandReference) getReference())
072: .getSourceFolder();
073:
074: // get first selected message
075: Object[] uids = ((MailFolderCommandReference) getReference())
076: .getUids();
077:
078: // ->set source reference in composermodel
079: // when replying this is the original sender's message
080: // you selected and replied to
081: MailFolderCommandReference ref = new MailFolderCommandReference(
082: folder, uids);
083: model.setSourceReference(ref);
084:
085: // setup to, references and account
086: initHeader(folder, uids);
087:
088: // get mimeparts
089: MimeTree mimePartTree = folder.getMimePartTree(uids[0]);
090:
091: XmlElement html = MailConfig.getInstance()
092: .getMainFrameOptionsConfig().getRoot().getElement(
093: "/options/html");
094:
095: // Which Bodypart shall be shown? (html/plain)
096: MimePart bodyPart = null;
097: Integer[] bodyPartAddress = null;
098: if (Boolean.valueOf(html.getAttribute("prefer")).booleanValue()) {
099: bodyPart = mimePartTree.getFirstTextPart("html");
100: } else {
101: bodyPart = mimePartTree.getFirstTextPart("plain");
102: }
103:
104: if (bodyPart != null) {
105: // setup charset and html
106: initMimeHeader(bodyPart);
107:
108: bodyPartAddress = bodyPart.getAddress();
109:
110: String quotedBodyText = createQuotedBody(folder, uids,
111: bodyPartAddress);
112:
113: /*
114: * *20040210, karlpeder* Remove html comments - they are not
115: * displayed properly in the composer
116: */
117: if (bodyPart.getHeader().getMimeType().getSubtype().equals(
118: "html")) {
119: quotedBodyText = HtmlParser
120: .removeComments(quotedBodyText);
121: }
122:
123: model.setBodyText(quotedBodyText);
124: }
125:
126: // add all attachments
127: MimeTree mt = folder.getMimePartTree(uids[0]);
128: Iterator it = mt.getAllLeafs().iterator();
129: while (it.hasNext()) {
130: MimePart mp = (MimePart) it.next();
131: Integer[] address = mp.getAddress();
132: // skip if bodypart (already added as quoted text)
133: if (Arrays.equals(address, bodyPartAddress))
134: continue;
135:
136: // add attachment
137: InputStream stream = folder.getMimePartBodyStream(uids[0],
138: address);
139: model.addMimePart(new InputStreamMimePart(mp.getHeader(),
140: stream));
141: }
142:
143: }
144:
145: private void initMimeHeader(MimePart bodyPart) {
146: MimeHeader bodyHeader = bodyPart.getHeader();
147:
148: if (bodyHeader.getMimeType().getSubtype().equals("html")) {
149: model.setHtml(true);
150: } else {
151: model.setHtml(false);
152: }
153:
154: // Select the charset of the original message
155: String charset = bodyHeader.getContentParameter("charset");
156:
157: if (charset != null) {
158: model.setCharset(Charset.forName(charset));
159: }
160: }
161:
162: private void initHeader(IMailbox folder, Object[] uids)
163: throws Exception {
164: // get headerfields
165: Header header = folder.getHeaderFields(uids[0], new String[] {
166: "Subject", "To", "From" });
167:
168: BasicHeader rfcHeader = new BasicHeader(header);
169:
170: // set subject ; mod:20040629 SWITT
171: model.setSubject(rfcHeader.getSubject());
172:
173: // set From for redirecting; new: 20040629 SWITT
174: model.setHeaderField("From", rfcHeader.getFrom().toString()
175: + " (by way of " + rfcHeader.get("To") + ")");
176: }
177:
178: protected String createQuotedBody(IMailbox folder, Object[] uids,
179: Integer[] address) throws IOException, Exception {
180: InputStream bodyStream = folder.getMimePartBodyStream(uids[0],
181: address);
182:
183: String quotedBody;
184: // Quote original message - different methods for text and html
185: if (model.isHtml()) {
186: // Html: Insertion of text before and after original message
187:
188: // build message orginal ; mod:2004629 SWITT
189: StringBuffer buf = new StringBuffer();
190: buf.append("<html><body>");
191: buf.append(HtmlParser.removeComments( // comments are not
192: // displayed
193: // correctly in composer
194: HtmlParser
195: .getHtmlBody(StreamUtils
196: .readCharacterStream(bodyStream)
197: .toString())));
198: buf.append("</body></html>");
199:
200: quotedBody = buf.toString();
201: } else {
202: // Text: take org. message; mod:20040629 SWITT
203: quotedBody = StreamUtils.readCharacterStream(bodyStream)
204: .toString();
205:
206: }
207:
208: bodyStream.close();
209: return quotedBody;
210: }
211: }
|