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:
023: import org.columba.api.command.ICommandReference;
024: import org.columba.api.command.IWorkerStatusController;
025: import org.columba.api.gui.frame.IFrameMediator;
026: import org.columba.core.command.CommandCancelledException;
027: import org.columba.core.io.StreamUtils;
028: import org.columba.core.xml.XmlElement;
029: import org.columba.mail.command.MailFolderCommandReference;
030: import org.columba.mail.config.MailConfig;
031: import org.columba.mail.folder.IMailbox;
032: import org.columba.mail.gui.composer.ComposerModel;
033: import org.columba.mail.gui.config.template.ChooseTemplateDialog;
034: import org.columba.mail.gui.tree.FolderTreeModel;
035: import org.columba.mail.message.IHeaderList;
036: import org.columba.ristretto.message.MimePart;
037: import org.columba.ristretto.message.MimeTree;
038:
039: /**
040: * Opens a dialog to ask the user which template to use
041: *
042: * @author fdietz
043: */
044: public class ReplyWithTemplateCommand extends ReplyCommand {
045:
046: private IFrameMediator mediator;
047:
048: public ReplyWithTemplateCommand(IFrameMediator mediator,
049: ICommandReference reference) {
050: super (reference);
051:
052: this .mediator = mediator;
053: }
054:
055: public void execute(IWorkerStatusController worker)
056: throws Exception {
057: // create composer model
058: model = new ComposerModel();
059:
060: // get selected folder
061: IMailbox folder = (IMailbox) ((MailFolderCommandReference) getReference())
062: .getSourceFolder();
063:
064: // get first selected message
065: Object[] uids = ((MailFolderCommandReference) getReference())
066: .getUids();
067:
068: // ->set source reference in composermodel
069: // when replying this is the original sender's message
070: // you selected and replied to
071: MailFolderCommandReference ref = new MailFolderCommandReference(
072: folder, uids);
073: model.setSourceReference(ref);
074:
075: // setup to, references and account
076: initHeader(folder, uids);
077:
078: // get mimeparts
079: MimeTree mimePartTree = folder.getMimePartTree(uids[0]);
080:
081: XmlElement html = MailConfig.getInstance()
082: .getMainFrameOptionsConfig().getRoot().getElement(
083: "/options/html");
084:
085: // Which Bodypart shall be shown? (html/plain)
086: MimePart bodyPart = null;
087:
088: if (Boolean.valueOf(html.getAttribute("prefer")).booleanValue()) {
089: bodyPart = mimePartTree.getFirstTextPart("html");
090: } else {
091: bodyPart = mimePartTree.getFirstTextPart("plain");
092: }
093:
094: if (bodyPart != null) {
095: // setup charset and html
096: initMimeHeader(bodyPart);
097:
098: Integer[] address = bodyPart.getAddress();
099:
100: String quotedBodyText = createQuotedBody(bodyPart
101: .getHeader(), folder, uids, address);
102:
103: // get answer from template
104: String templateBody = getTemplateBody();
105:
106: model.setBodyText(quotedBodyText + templateBody);
107: } else {
108: model.setBodyText(getTemplateBody());
109: }
110: }
111:
112: private String getTemplateBody() throws Exception,
113: CommandCancelledException, IOException {
114: // template folder has uid=107
115: IMailbox templateFolder = (IMailbox) FolderTreeModel
116: .getInstance().getFolder("107");
117:
118: // retrieve headerlist of tempate folder
119: IHeaderList list = templateFolder.getHeaderList();
120:
121: // choose template
122: ChooseTemplateDialog d = new ChooseTemplateDialog(mediator
123: .getView().getFrame(), list);
124:
125: Object uid = null;
126:
127: if (d.isResult()) {
128: // user pressed OK
129: uid = d.getUid();
130: } else {
131: throw new CommandCancelledException();
132: }
133:
134: // get bodytext of template message
135: MimeTree tree = templateFolder.getMimePartTree(uid);
136:
137: // *20030926, karlpeder* Added html support
138: // MimePart mp = tree.getFirstTextPart("plain");
139: MimePart mp;
140:
141: if (model.isHtml()) {
142: mp = tree.getFirstTextPart("html");
143: } else {
144: mp = tree.getFirstTextPart("text");
145: }
146:
147: InputStream bodyStream = templateFolder.getMimePartBodyStream(
148: uid, mp.getAddress());
149:
150: String body = StreamUtils.readCharacterStream(bodyStream)
151: .toString();
152:
153: bodyStream.close();
154: return body;
155: }
156: }
|