KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > composer > command > ReplyCommand


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

17 package org.columba.mail.gui.composer.command;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.nio.charset.Charset JavaDoc;
22 import java.nio.charset.UnsupportedCharsetException JavaDoc;
23 import java.text.DateFormat JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25
26 import org.columba.api.command.ICommandReference;
27 import org.columba.api.command.IWorkerStatusController;
28 import org.columba.core.command.Command;
29 import org.columba.core.gui.frame.DefaultContainer;
30 import org.columba.core.io.StreamUtils;
31 import org.columba.core.xml.XmlElement;
32 import org.columba.mail.command.MailFolderCommandReference;
33 import org.columba.mail.composer.MessageBuilderHelper;
34 import org.columba.mail.config.AccountItem;
35 import org.columba.mail.config.MailConfig;
36 import org.columba.mail.folder.IMailbox;
37 import org.columba.mail.gui.composer.ComposerController;
38 import org.columba.mail.gui.composer.ComposerModel;
39 import org.columba.mail.gui.composer.util.QuoteFilterInputStream;
40 import org.columba.mail.gui.util.AddressListRenderer;
41 import org.columba.mail.parser.text.HtmlParser;
42 import org.columba.mail.util.MailResourceLoader;
43 import org.columba.ristretto.coder.Base64DecoderInputStream;
44 import org.columba.ristretto.coder.CharsetDecoderInputStream;
45 import org.columba.ristretto.coder.QuotedPrintableDecoderInputStream;
46 import org.columba.ristretto.message.Address;
47 import org.columba.ristretto.message.BasicHeader;
48 import org.columba.ristretto.message.Header;
49 import org.columba.ristretto.message.MimeHeader;
50 import org.columba.ristretto.message.MimePart;
51 import org.columba.ristretto.message.MimeTree;
52
53 /**
54  * Reply to message.
55  * <p>
56  * Bodytext is quoted.
57  *
58  * @author fdietz
59  */

60 public class ReplyCommand extends Command {
61
62     /** JDK 1.4+ logging framework logger, used for logging. */
63     private static final Logger JavaDoc LOG = Logger
64             .getLogger("org.columba.mail.gui.composer.command");
65
66     protected final String JavaDoc[] headerfields = new String JavaDoc[] { "Subject", "Date",
67             "From", "To", "Reply-To", "Message-ID", "In-Reply-To", "References"};
68
69     protected ComposerController controller;
70
71     protected ComposerModel model;
72
73     /**
74      * Constructor for ReplyCommand.
75      *
76      * @param frameMediator
77      * @param references
78      */

79     public ReplyCommand(ICommandReference reference) {
80         super(reference);
81     }
82
83     public void updateGUI() throws Exception JavaDoc {
84         // open composer frame
85
controller = new ComposerController();
86         new DefaultContainer(controller);
87
88         // apply model
89
controller.setComposerModel(model);
90
91         // model->view update
92
controller.updateComponents(true);
93         
94         // Set the focus to the editor pane and set cursor to the top
95
controller.getCurrentEditor().getViewUIComponent().requestFocus();
96         controller.getCurrentEditor().getViewUIComponent().moveCaretPosition(0);
97         controller.getCurrentEditor().getViewUIComponent().select(0,0);
98     }
99
100     public void execute(IWorkerStatusController worker) throws Exception JavaDoc {
101         // create composer model
102
model = new ComposerModel();
103
104         // get selected folder
105
IMailbox folder = (IMailbox) ((MailFolderCommandReference) getReference())
106                 .getSourceFolder();
107
108         // get first selected message
109
Object JavaDoc[] uids = ((MailFolderCommandReference) getReference()).getUids();
110         
111         // ->set source reference in composermodel
112
// when replying this is the original sender's message
113
// you selected and replied to
114
MailFolderCommandReference ref = new MailFolderCommandReference(folder, uids);
115         model.setSourceReference(ref);
116         
117         // setup to, references and account
118
initHeader(folder, uids);
119
120         // get mimeparts
121
MimeTree mimePartTree = folder.getMimePartTree(uids[0]);
122
123         XmlElement html = MailConfig.getInstance().getMainFrameOptionsConfig()
124                 .getRoot().getElement("/options/html");
125
126         // Which Bodypart shall be shown? (html/plain)
127
MimePart bodyPart = null;
128
129         if (Boolean.valueOf(html.getAttribute("prefer")).booleanValue()) {
130             bodyPart = mimePartTree.getFirstTextPart("html");
131         } else {
132             bodyPart = mimePartTree.getFirstTextPart("plain");
133         }
134
135         if (bodyPart != null) {
136             // setup charset and html
137
initMimeHeader(bodyPart);
138
139             Integer JavaDoc[] address = bodyPart.getAddress();
140
141             String JavaDoc quotedBodyText = createQuotedBody(bodyPart.getHeader(), folder, uids, address);
142
143             // debug output
144
LOG.fine("Quoted body text:\n" + quotedBodyText);
145
146             model.setBodyText(quotedBodyText);
147         }
148     }
149
150     protected void initMimeHeader(MimePart bodyPart) {
151         MimeHeader bodyHeader = bodyPart.getHeader();
152
153         if (bodyHeader.getMimeType().getSubtype().equals("html")) {
154             model.setHtml(true);
155         } else {
156             model.setHtml(false);
157         }
158
159         // Select the charset of the original message
160
String JavaDoc charset = bodyHeader.getContentParameter("charset");
161
162         if (charset != null) {
163             try {
164                 model.setCharset(Charset.forName(charset));
165             } catch( UnsupportedCharsetException JavaDoc e ) {
166                 // Stick with the default charset
167
}
168         }
169     }
170
171     protected void initHeader(IMailbox folder, Object JavaDoc[] uids) throws Exception JavaDoc {
172         // get headerfields
173
Header header = folder.getHeaderFields(uids[0], headerfields);
174
175         BasicHeader rfcHeader = new BasicHeader(header);
176
177         // set subject
178
model.setSubject(MessageBuilderHelper.createReplySubject(rfcHeader
179                 .getSubject()));
180
181         // Use reply-to field if given, else use from
182
Address[] to = rfcHeader.getReplyTo();
183
184         if (to.length == 0) {
185             to = new Address[] { rfcHeader.getFrom()};
186         }
187
188         // Add addresses to the addressbook
189
MessageBuilderHelper.addAddressesToAddressbook(to);
190         model.setTo(to);
191
192         // create In-Reply-To:, References: headerfields
193
MessageBuilderHelper.createMailingListHeaderItems(header, model);
194
195         // select the account this mail was received from
196
Integer JavaDoc accountUid = (Integer JavaDoc) folder.getAttribute(uids[0],
197                 "columba.accountuid");
198         AccountItem accountItem = MessageBuilderHelper
199                 .getAccountItem(accountUid);
200         model.setAccountItem(accountItem);
201     }
202
203     protected String JavaDoc createQuotedBody(MimeHeader header, IMailbox folder, Object JavaDoc[] uids,
204             Integer JavaDoc[] address) throws IOException JavaDoc, Exception JavaDoc {
205         InputStream JavaDoc bodyStream = folder.getMimePartBodyStream(uids[0], address);
206         
207         // Do decoding stuff
208
switch( header.getContentTransferEncoding() ) {
209             case MimeHeader.QUOTED_PRINTABLE : {
210                 bodyStream = new QuotedPrintableDecoderInputStream(bodyStream);
211                 break;
212             }
213             
214             case MimeHeader.BASE64 : {
215                 bodyStream = new Base64DecoderInputStream(bodyStream);
216             }
217         }
218         String JavaDoc charset = header.getContentParameter("charset");
219         if( charset != null ) {
220             try {
221                 bodyStream = new CharsetDecoderInputStream(bodyStream, Charset.forName(charset));
222             } catch( UnsupportedCharsetException JavaDoc e ) {
223                 // Stick with the default charset
224
}
225         }
226         
227         String JavaDoc quotedBody;
228         // Quote original message - different methods for text and html
229
if (model.isHtml()) {
230             // Html: Insertion of text before and after original message
231
// get necessary headerfields
232
BasicHeader rfcHeader = new BasicHeader(folder.getHeaderFields(
233                     uids[0], headerfields));
234             String JavaDoc subject = rfcHeader.getSubject();
235             String JavaDoc date = DateFormat.getDateTimeInstance(DateFormat.LONG,
236                     DateFormat.MEDIUM).format(rfcHeader.getDate());
237             String JavaDoc from = AddressListRenderer.renderToHTMLWithLinks(
238                     new Address[] { rfcHeader.getFrom()}).toString();
239             String JavaDoc to = AddressListRenderer.renderToHTMLWithLinks(
240                     rfcHeader.getTo()).toString();
241
242             // build "quoted" message
243
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
244             buf.append("<html><body><p>");
245             buf.append(MailResourceLoader.getString("dialog", "composer",
246                     "original_message_start"));
247             buf.append("<br>"
248                     + MailResourceLoader.getString("header", "header",
249                             "subject") + ": " + subject);
250             buf.append("<br>"
251                     + MailResourceLoader.getString("header", "header", "date")
252                     + ": " + date);
253             buf.append("<br>"
254                     + MailResourceLoader.getString("header", "header", "from")
255                     + ": " + from);
256             buf.append("<br>"
257                     + MailResourceLoader.getString("header", "header", "to")
258                     + ": " + to);
259             buf.append("</p>");
260             buf.append(HtmlParser.removeComments(// comments are not displayed
261
// correctly in composer
262
HtmlParser.getHtmlBody(StreamUtils.readCharacterStream(bodyStream)
263                             .toString())));
264             buf.append("<p>");
265             buf.append(MailResourceLoader.getString("dialog", "composer",
266                     "original_message_end"));
267             buf.append("</p></body></html>");
268
269             quotedBody = buf.toString();
270         } else {
271             // Text: Addition of > before each line
272
quotedBody = StreamUtils.readCharacterStream(new QuoteFilterInputStream(bodyStream)).toString();
273         }
274
275         bodyStream.close();
276         return quotedBody;
277     }
278
279     /**
280      * Get composer model.
281      * <p>
282      * Needed for testcases.
283      *
284      * @return Returns the model.
285      */

286     public ComposerModel getModel() {
287         return model;
288     }
289 }
290
Popular Tags